Skip to content

Apply #[diagnostic::opaque] to macros expanding to built-in syntax#159522

Merged
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
fmease:opaquify-builtin-syntax-macros
Jul 19, 2026
Merged

Apply #[diagnostic::opaque] to macros expanding to built-in syntax#159522
rust-bors[bot] merged 1 commit into
rust-lang:mainfrom
fmease:opaquify-builtin-syntax-macros

Conversation

@fmease

@fmease fmease commented Jul 18, 2026

Copy link
Copy Markdown
Member

For context, thin wrapper macros expanding to built-in syntax builtin # SYNTAX(…) (internal feature builtin_syntax) is an alternative to built-in macros (#[rustc_builtin_macro]) for introducing new syntax constructs that takes slightly less code to implement in the compiler (since one doesn't need to write boiler-plate expanders, see RUST-122806 for example).

However, one disadvantage of that approach is the fact that the thin wrapper macro is a normal macro and is thus considered "interesting" wrt. macro backtraces. The fact that it expands to builtin # SYNTAX(…) should be considered an implementation detail and thus these macros should be considered opaque.

I've applied #[diagnostic::opaque] (#158608) to all of these macros which successfully suppresses diagnostic notes of the form this error originates in the macro `SYNTAX` (…). Well, it doesn't actually omit the expansion from the macro backtrace when -Zmacro-backtrace is passed which was surprising but seems intentional looking at the linked PR. Still, this is better than nothing.

r? @mejrs

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue. labels Jul 18, 2026
#[allow_internal_unstable(builtin_syntax, core_intrinsics)]
#[rustc_diagnostic_opaque]
pub macro offset_of($Container:ty, $($fields:expr)+ $(,)?) {
// The `{}` is for better error messages

@fmease fmease Jul 18, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It used to be a normal block but since RUST-148151 it is (and has to be) a const block since it gets lowered to calls to core::intrinsics::offset_of which is effectively #[rustc_comptime]1.

View changes since the review

Footnotes

  1. Currently, it lacks that attribute but that's just an oversight I'm certain. I'm gonna rectify that in a separate PR.

@rust-log-analyzer

This comment has been minimized.

@fmease
fmease force-pushed the opaquify-builtin-syntax-macros branch from b90d221 to d73a269 Compare July 18, 2026 15:51

@mejrs mejrs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can you use #[diagnostic::opaque] instead? The rustc variant exists to work around #98291, but that shouldn't be necessary for decl macros

View changes since this review

@fmease
fmease force-pushed the opaquify-builtin-syntax-macros branch from d73a269 to a4142b7 Compare July 18, 2026 16:41
@fmease fmease changed the title Apply #[rustc_diagnostic_opaque] to macros expanding to built-in syntax Apply #[diagnostic::opaque] to macros expanding to built-in syntax Jul 18, 2026
@fmease

fmease commented Jul 18, 2026

Copy link
Copy Markdown
Member Author

Applied.

@mejrs mejrs left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Nice to see it used :)

@bors r+ rollup

View changes since this review

@rust-bors

rust-bors Bot commented Jul 18, 2026

Copy link
Copy Markdown
Contributor

📌 Commit a4142b7 has been approved by mejrs

It is now in the queue for this repository.

@rust-bors rust-bors Bot added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2026
rust-bors Bot pushed a commit that referenced this pull request Jul 18, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - #159517 (stdarch subtree update)
 - #159010 (Simplify the unwind crate)
 - #156650 (Stop using prefix_tys)
 - #159152 (triagebot: cc miri on any special-casing of miri in the standard library)
 - #159522 (Apply `#[diagnostic::opaque]` to macros expanding to built-in syntax)
 - #159528 (implement `const_binary_search`)
@rust-bors
rust-bors Bot merged commit 15ceac8 into rust-lang:main Jul 19, 2026
13 checks passed
@rustbot rustbot added this to the 1.99.0 milestone Jul 19, 2026
rust-timer added a commit that referenced this pull request Jul 19, 2026
Rollup merge of #159522 - fmease:opaquify-builtin-syntax-macros, r=mejrs

Apply `#[diagnostic::opaque]` to macros expanding to built-in syntax

For context, thin wrapper macros expanding to built-in syntax `builtin # SYNTAX(…)` (internal feature `builtin_syntax`) is an alternative to built-in macros (`#[rustc_builtin_macro]`) for introducing new syntax constructs that takes slightly less code to implement in the compiler (since one doesn't need to write boiler-plate expanders, see RUST-122806 for example).

However, one disadvantage of that approach is the fact that the thin wrapper macro is a normal macro and is thus considered "interesting" wrt. macro backtraces. The fact that it expands to `builtin # SYNTAX(…)` should be considered an implementation detail and thus these macros should be considered opaque.

I've applied `#[diagnostic::opaque]` (#158608) to all of these macros which successfully suppresses diagnostic notes of the form `` this error originates in the macro `SYNTAX` (…) ``. Well, it doesn't actually omit the expansion from the macro backtrace when `-Zmacro-backtrace` is passed which was surprising but seems intentional looking at the linked PR. Still, this is better than nothing.

r? @mejrs
pull Bot pushed a commit to LeeeeeeM/miri that referenced this pull request Jul 19, 2026
…uwer

Rollup of 6 pull requests

Successful merges:

 - rust-lang/rust#159517 (stdarch subtree update)
 - rust-lang/rust#159010 (Simplify the unwind crate)
 - rust-lang/rust#156650 (Stop using prefix_tys)
 - rust-lang/rust#159152 (triagebot: cc miri on any special-casing of miri in the standard library)
 - rust-lang/rust#159522 (Apply `#[diagnostic::opaque]` to macros expanding to built-in syntax)
 - rust-lang/rust#159528 (implement `const_binary_search`)
@fmease
fmease deleted the opaquify-builtin-syntax-macros branch July 19, 2026 07:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-libs Relevant to the library team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants