88
99use std:: borrow:: Cow ;
1010use std:: collections:: hash_map:: Entry ;
11- use std:: debug_assert_matches;
1211use std:: mem:: { replace, swap, take} ;
1312use std:: ops:: { ControlFlow , Range } ;
13+ use std:: { debug_assert_matches, slice} ;
1414
1515use rustc_ast:: visit:: {
1616 AssocCtxt , BoundKind , FnCtxt , FnKind , Visitor , try_visit, visit_opt, walk_list,
1717} ;
1818use rustc_ast:: * ;
19+ use rustc_attr_parsing:: { AttrResolutionRequest , AttributeParser } ;
1920use rustc_data_structures:: either:: Either ;
2021use rustc_data_structures:: fx:: { FxHashMap , FxHashSet , FxIndexMap } ;
2122use rustc_data_structures:: unord:: { UnordMap , UnordSet } ;
@@ -24,7 +25,7 @@ use rustc_errors::{
2425 Applicability , Diag , DiagArgValue , Diagnostic , ErrorGuaranteed , IntoDiagArg , MultiSpan ,
2526 StashKey , Suggestions , elided_lifetime_in_path_suggestion, pluralize,
2627} ;
27- use rustc_hir:: attrs:: AttrConstResolved ;
28+ use rustc_hir:: attrs:: AttrResolved ;
2829use rustc_hir:: def:: Namespace :: { self , * } ;
2930use rustc_hir:: def:: { self , CtorKind , DefKind , LifetimeRes , NonMacroAttrKind , PartialRes , PerNS } ;
3031use rustc_hir:: def_id:: { CRATE_DEF_ID , DefId , LOCAL_CRATE , LocalDefId } ;
@@ -826,7 +827,7 @@ struct LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
826827/// Walks the whole crate in DFS order, visiting each item, resolving names as it goes.
827828impl < ' ast , ' ra , ' tcx > Visitor < ' ast > for LateResolutionVisitor < ' _ , ' ast , ' ra , ' tcx > {
828829 fn visit_attribute ( & mut self , attr : & ' ast Attribute ) {
829- self . resolve_attr_const_paths ( attr) ;
830+ self . resolve_attr_paths ( attr) ;
830831 }
831832 fn visit_item ( & mut self , item : & ' ast Item ) {
832833 let prev = replace ( & mut self . diag_metadata . current_item , Some ( item) ) ;
@@ -1471,65 +1472,37 @@ impl<'ast, 'ra, 'tcx> Visitor<'ast> for LateResolutionVisitor<'_, 'ast, 'ra, 'tc
14711472}
14721473
14731474impl < ' a , ' ast , ' ra , ' tcx > LateResolutionVisitor < ' a , ' ast , ' ra , ' tcx > {
1474- fn resolve_attr_const_paths ( & mut self , attr : & ' ast Attribute ) {
1475- match attr. name ( ) {
1476- Some ( sym:: repr) => {
1477- let Some ( items) = attr. meta_item_list ( ) else {
1478- return ;
1479- } ;
1480- for item in & items {
1481- let Some ( meta) = item. meta_item ( ) else {
1482- continue ;
1483- } ;
1484- let Some ( name) = meta. name ( ) else {
1485- continue ;
1486- } ;
1487- if !matches ! ( name, sym:: align | sym:: packed) {
1488- continue ;
1489- }
1490- let Some ( [ arg] ) = meta. meta_item_list ( ) else {
1491- continue ;
1492- } ;
1493- let Some ( path_meta) = arg. meta_item ( ) else {
1494- continue ;
1495- } ;
1496- if !path_meta. is_word ( ) {
1497- continue ;
1498- }
1499- if !self . should_resolve_attr_const_path ( path_meta. path . span ) {
1500- continue ;
1501- }
1502- self . resolve_attr_const_path ( attr. id , & path_meta. path ) ;
1503- }
1504- }
1505- Some ( sym:: rustc_align | sym:: rustc_align_static) => {
1506- let Some ( items) = attr. meta_item_list ( ) else {
1507- return ;
1508- } ;
1509- let [ arg] = items. as_slice ( ) else {
1510- return ;
1511- } ;
1512- let Some ( path_meta) = arg. meta_item ( ) else {
1513- return ;
1514- } ;
1515- if !path_meta. is_word ( ) {
1516- return ;
1517- }
1518- if !self . should_resolve_attr_const_path ( path_meta. path . span ) {
1519- return ;
1520- }
1521- self . resolve_attr_const_path ( attr. id , & path_meta. path ) ;
1522- }
1523- _ => { }
1475+ fn resolve_attr_paths ( & mut self , attr : & ' ast Attribute ) {
1476+ let Some ( name) = attr. name ( ) else {
1477+ return ;
1478+ } ;
1479+ if !matches ! ( name, sym:: repr | sym:: rustc_align | sym:: rustc_align_static) {
1480+ return ;
15241481 }
1525- }
15261482
1527- fn should_resolve_attr_const_path ( & self , span : Span ) -> bool {
1528- self . r . tcx . features ( ) . const_attr_paths ( ) || span. allows_unstable ( sym:: const_attr_paths)
1483+ let parse_only = match name {
1484+ sym:: repr => & [ sym:: repr] [ ..] ,
1485+ sym:: rustc_align => & [ sym:: rustc_align] [ ..] ,
1486+ sym:: rustc_align_static => & [ sym:: rustc_align_static] [ ..] ,
1487+ _ => unreachable ! ( ) ,
1488+ } ;
1489+
1490+ let mut requests = AttributeParser :: parse_limited_attr_resolution_requests (
1491+ self . r . tcx . sess ,
1492+ slice:: from_ref ( attr) ,
1493+ parse_only,
1494+ attr. span ,
1495+ DUMMY_NODE_ID ,
1496+ Some ( self . r . tcx . features ( ) ) ,
1497+ ) ;
1498+ for request in requests. shift_remove ( & attr. id ) . into_iter ( ) . flatten ( ) {
1499+ self . resolve_attr_path ( attr. id , request) ;
1500+ }
15291501 }
15301502
1531- fn resolve_attr_const_path ( & mut self , attr_id : AttrId , path : & Path ) {
1532- let path_segments = path
1503+ fn resolve_attr_path ( & mut self , attr_id : AttrId , request : AttrResolutionRequest ) {
1504+ let path_segments = request
1505+ . path
15331506 . segments
15341507 . iter ( )
15351508 . map ( |segment| Segment :: from_ident ( segment. ident ) )
@@ -1538,19 +1511,21 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15381511 & None ,
15391512 & path_segments,
15401513 PathSource :: Expr ( None ) ,
1541- Finalize :: new ( DUMMY_NODE_ID , path. span ) ,
1514+ Finalize :: new ( DUMMY_NODE_ID , request . path . span ) ,
15421515 RecordPartialRes :: No ,
15431516 None ,
15441517 ) ;
15451518 let resolved = match partial_res. full_res ( ) {
1546- Some ( Res :: Err ) | None => AttrConstResolved :: Error ,
1547- Some ( res) => AttrConstResolved :: Resolved ( res) ,
1519+ Some ( Res :: Err ) | None => AttrResolved :: Error ,
1520+ Some ( res) => AttrResolved :: Resolved ( res) ,
15481521 } ;
1549- self . r
1550- . attr_const_resolutions
1551- . entry ( attr_id)
1552- . or_default ( )
1553- . push ( rustc_hir:: attrs:: AttrConstResolution { path_span : path. span , resolved } ) ;
1522+ self . r . attr_resolutions . entry ( attr_id) . or_default ( ) . push (
1523+ rustc_hir:: attrs:: AttrResolution {
1524+ kind : request. kind ,
1525+ path_span : request. path . span ,
1526+ resolved,
1527+ } ,
1528+ ) ;
15541529 }
15551530}
15561531
0 commit comments