Skip to content

internal: init: implement automated field usage scanning#106

Open
Jkhall81 wants to merge 1 commit intoRust-for-Linux:mainfrom
Jkhall81:feat/auto-accessor-scanning
Open

internal: init: implement automated field usage scanning#106
Jkhall81 wants to merge 1 commit intoRust-for-Linux:mainfrom
Jkhall81:feat/auto-accessor-scanning

Conversation

@Jkhall81
Copy link

@Jkhall81 Jkhall81 commented Feb 1, 2026

Summary

Automatically scans the initializer to see which fields are actually used, preventing errors on packed structs.

Closes #98

Signed-off-by: jkhall81 <jason.kei.hall@gmail.com>
@Jkhall81 Jkhall81 force-pushed the feat/auto-accessor-scanning branch from 6ed15b4 to bc2e3f0 Compare February 1, 2026 02:08
@Jkhall81
Copy link
Author

Jkhall81 commented Feb 1, 2026

Fixed the cargo fmt --check --all stuff, manually. I need to figure out how or make something to do that automatically. There was a ui test failure tests/ui/compile-fail/init/packed_struct.rs. That compiles now. So I deleted the .rs and .stderr for that. Now there is just this MSRV (1.78) issue. What I tried just now didn't fix it. I'll look at it in the morning.

@BennoLossin
Copy link
Member

Hey! Sorry for the wait, thanks for the PR!

The issue with 1.78 should be easily resolvible, since we merged #102, you might just need to enable the correct feature in the test.

As for the implementation itself, I'm not sure that we want to do it this way, because it will have both false positives and false negatives:

  • false positive: init!(Struct { field: field::new(), other: 42 }) where field also is a module.
  • false negative: init!(Struct { field: Field::new(), other: dma_read!(field) }) since macro token streams are skipped by visitors by default.

I'm not so sure if it's a good idea to create a very clever parser here to also avoid closure variables and other item definitions. (we wouldn't want a helper function declared inside of a code block that reuses a field name as a parameter to trigger it) I'll have to think a bit more about this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Only create accessors for initialized fields if it would be used

2 participants