D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 15305 - std.manip.bitfields generate bogous code
Summary: std.manip.bitfields generate bogous code
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86 Mac OS X
: P1 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-11-09 02:28 UTC by deadalnix
Modified: 2016-01-03 14:15 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description deadalnix 2015-11-09 02:28:12 UTC
import std.bitmanip;
struct S {
	mixin(bitfields!(
		bool, "alice", 1,
		ulong, "bob", 63,
	));
}
	
S s;
s.bob = long.max - 1;
s.alice = false;
assert(s.bob == long.max - 1);

Setting alice to false is clearing bits in bob, while it really shouldn't.
Comment 1 Lionello Lunesu 2015-11-16 11:10:14 UTC
The generated setter for alice is wrong:

@property void alice(bool v) @safe pure nothrow @nogc { if (v) _alice_bob |= 1U;else _alice_bob &= ~1U;}

and it's wrong because the &=~1U would clear not just the first bit, but bits 32-64 as well. This in turn because of the code in myToString, which picks the smallest type suffix, "U" in this case.
Comment 3 github-bugzilla 2015-11-16 12:45:25 UTC
Commits pushed to master at https://github.com/D-Programming-Language/phobos

https://github.com/D-Programming-Language/phobos/commit/8e452d8dd484866a6be2c3c180a664ee57b16905
[Issue 15305] bitfields! setter erroneously clears subsequent fields

https://github.com/D-Programming-Language/phobos/commit/4e53643652b8e927965c9888c35859c2a81bd56a
Merge pull request #3815 from lionello/issue15305

[Issue 15305] bitfields! setter erroneously clears subsequent fields