Skip to content

Conversation

@MadhurKumar004
Copy link
Contributor

This implements parsing, semantic analysis, and backend support for default initializers in bitfields.

  • Modified 'declaration.d' & 'parse.d' to parse assignment expressions after bitfield width.
  • Updated 'dsymbolsem.d' to validate initializers and check for overflows.
  • Updated 'todt.d' to pack default values into the binary representation.
  • Added regression tests.

@dlang-bot
Copy link
Contributor

Thanks for your pull request and interest in making D better, @MadhurKumar004! We are looking forward to reviewing it, and you should be hearing from a maintainer soon.
Please verify that your PR follows this checklist:

  • My PR is fully covered with tests (you can see the coverage diff by visiting the details link of the codecov check)
  • My PR is as minimal as possible (smaller, focused PRs are easier to review than big ones)
  • I have provided a detailed rationale explaining my changes
  • New or modified functions have Ddoc comments (with Params: and Returns:)

Please see CONTRIBUTING.md for more information.


If you have addressed all reviews or aren't sure how to proceed, don't hesitate to ping us with a simple comment.

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#22396"

@ibuclaw
Copy link
Member

ibuclaw commented Jan 14, 2026

@MadhurKumar004 thanks for picking this up! 👍

@MadhurKumar004
Copy link
Contributor Author

@ibuclaw Made the changes as you suggested

@MadhurKumar004
Copy link
Contributor Author

@ibuclaw review please.

Copy link
Member

@ibuclaw ibuclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't comment on the backend part. Frontend looks ok, maybe just need more combinations of tests to ensure we don't start accepting invalid code.

If there's bitfield documentation on dlang.org, that requires updating too.

As this is a feature fix, would be best to include a changelog entry too.

Thanks!

@MadhurKumar004
Copy link
Contributor Author

@ibuclaw Thanks for the advice on test combination. Because of that i was able to fix couple of edge cases that were giving ICE. Also added the changelog and currently working on the dlang.org documentation. Will send a pr for that after this get merged. Please check and review

if (storage_class)
error("storage class not allowed for bitfield declaration");
s = new AST.BitFieldDeclaration(loc, t, ident, width);
s = new AST.BitFieldDeclaration(width.loc, t, ident, width, _init);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please rebase against current origin/master.

Suggested change
s = new AST.BitFieldDeclaration(width.loc, t, ident, width, _init);
s = new AST.BitFieldDeclaration(loc, t, ident, width, _init);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please expand on what you mean by Location is still wrong?

@ibuclaw
Copy link
Member

ibuclaw commented Jan 16, 2026

Front-end part LGTM. 👍

Copy link
Member

@ibuclaw ibuclaw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mind there are testsuite failures.

==============================
Test 'fail_compilation\biterrors.d' failed: 
diff:
----
-fail_compilation\biterrors.d(103): Error: initializer not allowed for bitfield declaration
 fail_compilation\biterrors.d(104): Error: storage class not allowed for bitfield declaration
----
==============================
Test 'fail_compilation\biterrors2.d' failed: 
diff:
----
 fail_compilation\biterrors2.d(100): Error: variable `biterrors2.a` - bitfield must be member of struct, union, or class
 int a : 2;
-    ^
+        ^
 fail_compilation\biterrors2.d(104): Error: bitfield `b` has zero width
     int b:0;
           ^
 fail_compilation\biterrors2.d(105): Error: bitfield type `float` is not an integer type
     float c:3;
-          ^
+            ^
----
==============================
Test 'fail_compilation\fail22384.d' failed: 
diff:
----
-fail_compilation\fail22384.d(21): Error: bitfield initializer `4294967295u` does not fit in 4 bits
-fail_compilation\fail22384.d(26): Error: bitfield initializer `E.B` does not fit in 2 bits
-fail_compilation\fail22384.d(30): Error: bitfield initializer `4` does not fit in 3 bits
-fail_compilation\fail22384.d(31): Error: bitfield initializer `65` does not fit in 7 bits
 fail_compilation\fail22384.d(32): Error: bitfield `z` has zero width
 fail_compilation\fail22384.d(36): Error: bitfield type `float` is not an integer type
 fail_compilation\fail22384.d(36): Error: bitfield `f` has zero width
 fail_compilation\fail22384.d(37): Error: bitfield type `float` is not an integer type
 fail_compilation\fail22384.d(38): Error: bitfield type `float` is not an integer type
 fail_compilation\fail22384.d(39): Error: bitfield type `float` is not an integer type
-fail_compilation\fail22384.d(43): Error: cannot implicitly convert expression `4.2F` of type `float` to `int`
 fail_compilation\fail22384.d(45): Error: anonymous bitfield cannot be initialized
+fail_compilation\fail22384.d(21): Error: bitfield initializer `4294967295u` does not fit in 4 bits
+fail_compilation\fail22384.d(26): Error: bitfield initializer `E.B` does not fit in 2 bits
+fail_compilation\fail22384.d(30): Error: bitfield initializer `4` does not fit in 3 bits
+fail_compilation\fail22384.d(31): Error: bitfield initializer `65` does not fit in 7 bits
+fail_compilation\fail22384.d(43): Error: cannot implicitly convert expression `4.2F` of type `float` to `int`
 fail_compilation\fail22384.d(46): Error: bitfield initializer `65` does not fit in 7 bits
 fail_compilation\fail22384.d(47): Error: cannot implicitly convert expression `42` of type `int` to `bool`
----

This implements parsing, semantic analysis, and backend support for default initializers in bitfields.

- Modified 'declaration.d' & 'parse.d' to parse assignment expressions after bitfield width.
- Implemented semantic cheks in 'semantic2.d' using IntRange for bound
  validation.
- Updated 'todt.d' to pack default values into the binary representation.
- Added regression tests.
- Added the changlog of the feature
@MadhurKumar004
Copy link
Contributor Author

@ibuclaw fixed the testsuite failure , whitespace and the correct error message. Also can you please explain what you mean by location is still wrong?

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.

Error: initializer not allowed for bitfield declaration

3 participants