diff --git a/compiler/rustc_ast/src/token.rs b/compiler/rustc_ast/src/token.rs index efa2a008e65ae..0144aabb24413 100644 --- a/compiler/rustc_ast/src/token.rs +++ b/compiler/rustc_ast/src/token.rs @@ -308,8 +308,13 @@ impl LitKind { } pub fn ident_can_begin_expr(name: Symbol, span: Span, is_raw: IdentIsRaw) -> bool { + // WARNING: Take care when modifying this function! It will change the stable(!) set of + // tokens that are allowed to match an `expr` nonterminal which is user observable. + let ident_token = Token::new(Ident(name, is_raw), span); + // FIXME: Remove `box` from this list given we officially no longer support box expressions + // (#108471) (needs lang FCP as it affects stable macro matching behavior). !ident_token.is_reserved_ident() || ident_token.is_path_segment_keyword() || [ @@ -340,6 +345,9 @@ pub fn ident_can_begin_expr(name: Symbol, span: Span, is_raw: IdentIsRaw) -> boo } fn ident_can_begin_type(name: Symbol, span: Span, is_raw: IdentIsRaw) -> bool { + // WARNING: Take care when modifying this function! It will change the stable(!) set of + // tokens that are allowed to match an `ty` nonterminal which is user observable. + let ident_token = Token::new(Ident(name, is_raw), span); !ident_token.is_reserved_ident() @@ -661,10 +669,10 @@ impl Token { } /// Returns `true` if the token can appear at the start of an expression. - /// - /// **NB**: Take care when modifying this function, since it will change - /// the stable set of tokens that are allowed to match an expr nonterminal. pub fn can_begin_expr(&self) -> bool { + // WARNING: Take care when modifying this function! It will change the stable(!) set of + // tokens that are allowed to match an `expr` nonterminal which is user observable. + match self.uninterpolate().kind { Ident(name, is_raw) => ident_can_begin_expr(name, self.span, is_raw), // value name or keyword @@ -695,9 +703,10 @@ impl Token { } /// Returns `true` if the token can appear at the start of a pattern. - /// - /// Shamelessly borrowed from `can_begin_expr`. pub fn can_begin_pattern(&self, pat_kind: NtPatKind) -> bool { + // WARNING: Take care when modifying this function! It will change the stable(!) set of + // tokens that are allowed to match an `pat` nonterminal which is user observable. + match &self.uninterpolate().kind { // box, ref, mut, and other identifiers (can stricten) Ident(..) | NtIdent(..) | @@ -727,6 +736,12 @@ impl Token { /// Returns `true` if the token can appear at the start of a type. pub fn can_begin_type(&self) -> bool { + // WARNING: Take care when modifying this function! It will change the stable(!) set of + // tokens that are allowed to match an `ty` nonterminal which is user observable. + + // FIXME: Arguably, `use` should be included in this list since it can begin bare trait + // object types (consider `use<>+` and `use + Trait` for example). + match self.uninterpolate().kind { Ident(name, is_raw) => ident_can_begin_type(name, self.span, is_raw), // type name or keyword diff --git a/compiler/rustc_ast_lowering/src/delegation/attributes.rs b/compiler/rustc_ast_lowering/src/delegation/attributes.rs index 885ee0d51c730..834f85450a2cd 100644 --- a/compiler/rustc_ast_lowering/src/delegation/attributes.rs +++ b/compiler/rustc_ast_lowering/src/delegation/attributes.rs @@ -43,17 +43,17 @@ impl<'hir> LoweringContext<'_, 'hir> { let &DelegationResolution { span, sig_id, .. } = resolution; const PARENT_ID: hir::ItemLocalId = hir::ItemLocalId::ZERO; - let new_attrs = self.create_new_attrs(span, sig_id, self.attrs.get(&PARENT_ID)); + let new_attrs = self.create_new_attrs(span, sig_id, self.curr_owner.attrs.get(&PARENT_ID)); if !new_attrs.is_empty() { - let new_attrs = match self.attrs.get(&PARENT_ID) { + let new_attrs = match self.curr_owner.attrs.get(&PARENT_ID) { Some(existing_attrs) => self.arena.alloc_from_iter( existing_attrs.iter().map(|a| a.clone()).chain(new_attrs.into_iter()), ), None => self.arena.alloc_from_iter(new_attrs.into_iter()), }; - self.attrs.insert(PARENT_ID, new_attrs); + self.curr_owner.attrs.insert(PARENT_ID, new_attrs); } } diff --git a/compiler/rustc_ast_lowering/src/delegation/generics.rs b/compiler/rustc_ast_lowering/src/delegation/generics.rs index 911ec5956006d..867ed364433e7 100644 --- a/compiler/rustc_ast_lowering/src/delegation/generics.rs +++ b/compiler/rustc_ast_lowering/src/delegation/generics.rs @@ -587,7 +587,7 @@ impl<'hir> LoweringContext<'_, 'hir> { }; // Important: we don't use `self.next_id()` as we want to execute - // `lower_node_id` routine so param's id is added to `self.children`. + // `lower_node_id` routine so param's id is added to `self.curr_owner.children`. let hir_id = self.lower_node_id(node_id); Some(hir::GenericParam { diff --git a/compiler/rustc_ast_lowering/src/delegation/mod.rs b/compiler/rustc_ast_lowering/src/delegation/mod.rs index 3b9074e67bdd2..a92b62517e61d 100644 --- a/compiler/rustc_ast_lowering/src/delegation/mod.rs +++ b/compiler/rustc_ast_lowering/src/delegation/mod.rs @@ -140,9 +140,11 @@ impl<'hir> LoweringContext<'_, 'hir> { let id = match source { DelegationSource::Single => None, DelegationSource::List(expn_id) => Some(expn_id), - DelegationSource::Glob => { - Some(self.tcx.expn_that_defined(self.owner.def_id).expect_local()) - } + DelegationSource::Glob => Some( + self.tcx + .expn_that_defined(self.curr_owner.owner.def_id) + .expect_local(), + ), }; id.map(|id| (id, unused_target_expr)) @@ -335,7 +337,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let overwrites = self_resolver.overwrites; // Target expr needs to lower `self` path. - self.ident_and_label_to_local_id.insert(pat_node_id, param_local_id); + self.curr_owner.ident_and_label_to_local_id.insert(pat_node_id, param_local_id); let block = cfg_select! { debug_assertions => { diff --git a/compiler/rustc_ast_lowering/src/delegation/resolution.rs b/compiler/rustc_ast_lowering/src/delegation/resolution.rs index dd1b9518e6d7f..85604223c8509 100644 --- a/compiler/rustc_ast_lowering/src/delegation/resolution.rs +++ b/compiler/rustc_ast_lowering/src/delegation/resolution.rs @@ -73,7 +73,7 @@ pub(super) mod resolver { #[inline] pub(crate) fn owner_id(&self) -> LocalDefId { - self.0.owner.def_id + self.0.curr_owner.owner.def_id } /// (from `tests\ui\delegation\target-expr-removal-defs-inside.rs`): @@ -91,7 +91,7 @@ pub(super) mod resolver { #[inline] pub(crate) fn is_definition(&self, id: NodeId) -> bool { self.0.resolver.owners.contains_key(&id) - || self.0.owner.node_id_to_def_id.contains_key(&id) + || self.0.curr_owner.owner.node_id_to_def_id.contains_key(&id) } #[inline] diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 4d5b98fd1ac00..0a4a2ae7145e3 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -172,7 +172,8 @@ impl<'hir> LoweringContext<'_, 'hir> { } // Merge attributes into the inner expression. if !e.attrs.is_empty() { - let old_attrs = self.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]); + let old_attrs = + self.curr_owner.attrs.get(&ex.hir_id.local_id).copied().unwrap_or(&[]); let new_attrs = self .lower_attrs_vec(&e.attrs, e.span, ex.hir_id, Target::from_expr(e)) .into_iter() @@ -181,7 +182,7 @@ impl<'hir> LoweringContext<'_, 'hir> { if new_attrs.is_empty() { return ex; } - self.attrs.insert(ex.hir_id.local_id, new_attrs); + self.curr_owner.attrs.insert(ex.hir_id.local_id, new_attrs); } return ex; } @@ -884,7 +885,7 @@ impl<'hir> LoweringContext<'_, 'hir> { /// `inner_hir_id` in case the `async_fn_track_caller` feature is enabled. pub(super) fn maybe_forward_track_caller(&mut self, outer_hir_id: HirId, inner_hir_id: HirId) { if self.tcx.features().async_fn_track_caller() - && let Some(attrs) = self.attrs.get(&outer_hir_id.local_id) + && let Some(attrs) = self.curr_owner.attrs.get(&outer_hir_id.local_id) && let Some(t) = attrs.iter().find(|a| { matches!( a, @@ -892,7 +893,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ) }) { - self.attrs.insert(inner_hir_id.local_id, std::slice::from_ref(t)); + self.curr_owner.attrs.insert(inner_hir_id.local_id, std::slice::from_ref(t)); } } @@ -1505,16 +1506,16 @@ impl<'hir> LoweringContext<'_, 'hir> { dest_hir_id: hir::HirId, ) -> Option