Skip to content

macros: add support for exhaustive attribute - #5390

Open
dybucc wants to merge 1 commit into
rust-lang:mainfrom
dybucc:non_exhaustive-macro-extension
Open

macros: add support for exhaustive attribute#5390
dybucc wants to merge 1 commit into
rust-lang:mainfrom
dybucc:non_exhaustive-macro-extension

Conversation

@dybucc

@dybucc dybucc commented Aug 8, 2026

Copy link
Copy Markdown
Contributor

Description

Attempts to take the first steps in closing #4080. This should
allow us to add a private field for each of the records declared within
s_with_default or s_no_extra_traits_with_default.

This should in turn enforce those records to be initialized field-by-field in
downstream crates, while avoiding the lint that currently pops up when using a
struct marked non_exhaustive in FFI contexts.

Checklist

  • Relevant tests in libc-test/semver have been updated
  • Commit messages permalink to headers for added or changed API
  • Placeholder or unstable values like *LAST or *MAX have the standard
    doc comment
  • Tested locally (cargo test -p libc-test --target mytarget); especially
    relevant for platforms that may not be checked in CI

@rustbot label +stable-nominated

@dybucc dybucc changed the title macros: add support for exhaustive attr macros: add support for exhaustive attribute Aug 8, 2026
@dybucc
dybucc force-pushed the non_exhaustive-macro-extension branch from 8c61548 to 5a738cb Compare August 8, 2026 08:06
@rustbot rustbot added the stable-nominated This PR should be considered for cherry-pick to libc's stable release branch label Aug 8, 2026
Add support for `exhaustive` custom attribute to opt out of having the
record have a `__non_exhaustive` field added to it.

Modify `struct_with_default` to automatically add a private field to all
records such that they are always built field-by-field in downstream
crates.
@dybucc
dybucc force-pushed the non_exhaustive-macro-extension branch from 5a738cb to f11b352 Compare August 8, 2026 08:13
@tgross35

Copy link
Copy Markdown
Member

Could you base this on top of #5372? It looks like there may be some overlap. Feel free to leave a review there as well, if you have any suggestions.

Comment thread src/macros.rs
@attr
attrs: { #[exhaustive] $($attrs:tt)* }
processed_attrs: { $($prev_attrs:tt)* }
found: { $_:tt }

@tgross35 tgross35 Aug 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

found_exhaustive_attr or so, to make it more clear in other blocks without the comment

View changes since the review

Comment thread src/macros.rs
Comment on lines +517 to 533
macro_rules! fin {
(true decl: $(#[$attr:meta])* $vis:vis $name:ident { $($field:tt)* }) => {
$(#[$attr])*
$vis struct $name { $($field)* }
};
(true default: { $($field_default:tt)* }) => {
Self { $($field_default)* }
};

(false decl: $(#[$attr:meta])* $vis:vis $name:ident { $($field:tt)* }) => {
$(#[$attr])*
$vis struct $name { $($field)* __non_exhaustive: () }
};
(false default: { $($field_default:tt)* }) => {
Self { $($field_default)* __non_exhaustive: () }
};
}

@tgross35 tgross35 Aug 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Give this a more descriptive name, and prefer the field: $value pattern for matchers like the other macros have. That's easier to follow than needing to figure out what different token sequences indicate.

View changes since the review

Comment thread src/lib.rs
Comment on lines +155 to +157
// FIXME(rust-lang/rust#132699): get rid of this once we stop using the private
// field.
#![allow(clippy::manual_non_exhaustive)]

@tgross35 tgross35 Aug 25, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this be scoped to only where needed?

View changes since the review

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

Labels

S-waiting-on-review stable-nominated This PR should be considered for cherry-pick to libc's stable release branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants