refactor: shrink check_attr by moving attribute checks to parsers#153845
refactor: shrink check_attr by moving attribute checks to parsers#153845herbenderbler wants to merge 2 commits intorust-lang:mainfrom
Conversation
7d4cf51 to
b5ad742
Compare
This comment has been minimized.
This comment has been minimized.
b5ad742 to
1717ac5
Compare
This comment has been minimized.
This comment has been minimized.
1717ac5 to
da1fcbc
Compare
|
Some changes occurred in compiler/rustc_passes/src/check_attr.rs cc @jdonszelmann, @JonathanBrouwer Some changes occurred in compiler/rustc_attr_parsing |
|
r? @TaKO8Ki rustbot has assigned @TaKO8Ki. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
|
Reminder, once the PR becomes ready for a review, use |
|
In the future, it would be nice if you could coordinate your ideas in the issue thread or on zulip before making a PR :) |
My apologies, this is my first attempt at landing a PR here 😬 |
|
Some changes occurred in compiler/rustc_hir/src/attrs |
ca6510b to
dc254ef
Compare
This comment has been minimized.
This comment has been minimized.
dc254ef to
eae00f6
Compare
Add late_validation with LateValidationContext and validators for attributes that need HIR/target context: deprecated, loop_match, const_continue, link, macro_export, and diagnostic::{do_not_recommend, on_unimplemented, on_const, on_move}, plus shared Directive format-parameter checking.
Move custom_mir dialect/phase validation into CustomMirParser (check_custom_mir); emit via session_diagnostics and remove duplicate rustc_passes errors.
Includes crate-root late-context fix (opt_local_parent), directive/impl targeting, and on_move handling aligned with upstream.
Part of the rustc check_attr cleanup.
eae00f6 to
24ee2e7
Compare
This comment has been minimized.
This comment has been minimized.
…/const_continue checks
refactor: shrink
check_attrby moving attribute checks intorustc_attr_parsingPart of #153101.
Problem
compiler/rustc_passes/src/check_attr.rsaccumulates a large amount of attribute validation logic. Much of that logic is really about what an attribute means on a particular syntactic target, but it historically lived next to diagnostic emission in the pass, which makes the attribute pipeline harder to follow and duplicates concepts that already live inrustc_attr_parsing.Issue #153101 tracks shrinking
check_attrand moving checks as far toward parsing as information allows. Many rules cannot run during raw attribute parsing because they need HIR / target context (e.g. “is this a trait impl?”, “is this expression a loop?”). This PR takes a concrete step in that direction without pullingrustc_middleinto the attribute parser crate.What we implemented
1.
late_validation(compiler/rustc_attr_parsing/src/late_validation.rs)New module with:
LateValidationContext— Plain data built incheck_attrfromTyCtxt/ HIR:Target,parent_is_trait_impl, optional expression context (loop vs break), trait-impl constness /impl_of_trait, foreign mod ABI, decl-macro flag formacro_export, etc.Option<…>(or small enums) describing what is wrong, without emitting diagnostics:#[deprecated]where it has no effect (trait impl members)#[loop_match],#[const_continue]on the wrong expression kind#[link](foreign mod + notextern "Rust")#[macro_export]on decl macrosdo_not_recommend,on_unimplemented(placement),on_const,on_move(placement)for_each_unknown_diagnostic_format_param— shared walk overDirectiveformat parameters vs non-lifetime type parameters (used foron_unimplementedandon_move)The pass
check_attrremains responsible for emitting lints/errors viarustc_passes::errorsand existing lint machinery; it only calls intolate_validationfor the predicate.2.
#[custom_mir](compiler/rustc_attr_parsing/src/attributes/prototype.rs)Dialect/phase compatibility and “phase without dialect” are validated in
CustomMirParservia a dedicatedcheck_custom_mirhelper (same shape as the standalone parser work in #154126:failedflag +session_diagnostics).AttributeKind::CustomMirkeeps optional dialect/phase with spans.Corresponding diagnostics were removed from
rustc_passes::errorsand live inrustc_attr_parsing::session_diagnosticsinstead.3.
check_attrcleanup (compiler/rustc_passes/src/check_attr.rs)LateValidationContextonce per attribute walk and dispatches to the validators above.generics_has_type_param_named— small local helper for diagnostic format-parameter checking (shared byon_unimplemented/on_movearms).CustomMirarm is a no-op beyond a comment (validation is in the parser).4. Small HIR doc tweak
Directive::visit_paramsincompiler/rustc_hir/src/attrs/diagnostic.rsnow points callers atlate_validation::for_each_unknown_diagnostic_format_paramfor the post-HIR check.Why this design
rustc_attr_parsing;rustc_passeskeeps tcx/HIR wiring and user-facing diagnostics.late_validationavoidsrustc_middle/ typeck; onlyrustc_hir+rustc_span(andDirectivefor format-parameter validation).This matches reviewer feedback that we should not pretend
check_attris “done” when logic merely moves crates—it documents the staged approach and keeps validation code in the attribute stack.Tests
No new
tests/files were added in this PR. Behavior is covered by existing UI / compiler tests (e.g. diagnostic namespace,custom_mir, attribute lints).Local verification (recommended before merge):
Documentation
late_validation(purpose,LateValidationContext, link to Reduce the size of check_attrs.rs and move as much as possible into the attribute parsers. #153101, note on future target-aware parsing).CustomMirhandling incheck_attr.Directive::visit_paramsrustdoc cross-reference as above.Rebase note
This branch was rebased onto
rust-lang/rustmainat86c839ffb36. Ifmainadvances (e.g. after #154126 lands), run:git fetch https://github.com/rust-lang/rust.git main git rebase FETCH_HEAD # or: git rebase rust-lang/mainIf #154126 merges first, expect at most a small conflict or redundancy check in
prototype.rsaroundcheck_custom_mir—the intent is a single shared implementation pattern.