Skip to content

Comments

fix #20473 - Struct with both bitfields and a slice or class doesn't compile#21651

Merged
thewilsonator merged 1 commit intodlang:masterfrom
ibuclaw:issue20473
Aug 5, 2025
Merged

fix #20473 - Struct with both bitfields and a slice or class doesn't compile#21651
thewilsonator merged 1 commit intodlang:masterfrom
ibuclaw:issue20473

Conversation

@ibuclaw
Copy link
Member

@ibuclaw ibuclaw commented Aug 5, 2025

This was caused by the compiler-generated xtoHash function which inserted code that took the address of the bit-field. This patch adds a new conditional code path to the generated code that stores the value to a temporary before calling getHash().

Tested by adding injected code into test file.

struct S
{
    char[] slice;
    bool flag : 1;
    size_t toHash() const { ... }
}

Which generates.

/*unrolled*/ {
    {
        enum ulong i = 0LU;
        alias T = const(char[]);
        h = h * 33LU + typeid(const(char[])).getHash(cast(const(void*))&p.slice);
    }
    {
	enum ulong i = 1LU;
	alias T = const(bool);
	const const(bool) t = p.flag;
	h = h * 33LU + typeid(const(bool)).getHash(& t);
    }
}

…sn't compile

This was caused by the compiler-generated xtoHash function which
inserted code that took the address of the bit-field. This patch adds a
new conditional code path to the generated code that stores the value to
a temporary before calling getHash().

Tested by adding injected code into test file.
```d
struct S
{
    char[] slice;
    bool flag : 1;
    size_t toHash() const { ... }
}
```
Which generates.
```d
/*unrolled*/ {
    {
        enum ulong i = 0LU;
        alias T = const(char[]);
        h = h * 33LU + typeid(const(char[])).getHash(cast(const(void*))&p.slice);
    }
    {
	enum ulong i = 1LU;
	alias T = const(bool);
	const const(bool) t = p.flag;
	h = h * 33LU + typeid(const(bool)).getHash(& t);
    }
}
```
@dlang-bot
Copy link
Contributor

Thanks for your pull request, @ibuclaw!

Bugzilla references

Your PR doesn't reference any Bugzilla issue.

If your PR contains non-trivial changes, please reference a Bugzilla issue or create a manual changelog.

⚠️⚠️⚠️ Warnings ⚠️⚠️⚠️

  • In preparation for migrating from Bugzilla to GitHub Issues, the issue reference syntax has changed. Please add the word "Bugzilla" to issue references. For example, Fix Bugzilla Issue 12345 or Fix Bugzilla 12345.(Reminder: the edit needs to be done in the Git commit message, not the GitHub pull request.)

Testing this PR locally

If you don't have a local development environment setup, you can use Digger to test this PR:

dub run digger -- build "master + dmd#21651"

@thewilsonator thewilsonator merged commit 94e1c33 into dlang:master Aug 5, 2025
42 of 44 checks passed
@ibuclaw ibuclaw deleted the issue20473 branch August 5, 2025 23:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Struct with both bitfields and a slice or class instance ref doesn't compile.

3 participants