From dc0db358abe19481e475e10c32149b53370f1a1c Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Thu, 30 May 2024 05:57:31 +0200 Subject: Merging upstream version 1.72.1+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_ast_lowering/src/asm.rs | 1 + compiler/rustc_ast_lowering/src/expr.rs | 23 +++--- compiler/rustc_ast_lowering/src/index.rs | 8 ++ compiler/rustc_ast_lowering/src/item.rs | 117 ++++++++++++++++++++++++--- compiler/rustc_ast_lowering/src/lib.rs | 133 +++++++++++++++++++------------ 5 files changed, 212 insertions(+), 70 deletions(-) (limited to 'compiler/rustc_ast_lowering/src') diff --git a/compiler/rustc_ast_lowering/src/asm.rs b/compiler/rustc_ast_lowering/src/asm.rs index 941d31795..d350498bc 100644 --- a/compiler/rustc_ast_lowering/src/asm.rs +++ b/compiler/rustc_ast_lowering/src/asm.rs @@ -44,6 +44,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { | asm::InlineAsmArch::AArch64 | asm::InlineAsmArch::RiscV32 | asm::InlineAsmArch::RiscV64 + | asm::InlineAsmArch::LoongArch64 ); if !is_stable && !self.tcx.features().asm_experimental_arch { feature_err( diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 5e0ab80c6..dcaaaafed 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -71,9 +71,13 @@ impl<'hir> LoweringContext<'_, 'hir> { let kind = match &e.kind { ExprKind::Array(exprs) => hir::ExprKind::Array(self.lower_exprs(exprs)), - ExprKind::ConstBlock(anon_const) => { - let anon_const = self.lower_anon_const(anon_const); - hir::ExprKind::ConstBlock(anon_const) + ExprKind::ConstBlock(c) => { + let c = self.with_new_scopes(|this| hir::ConstBlock { + def_id: this.local_def_id(c.id), + hir_id: this.lower_node_id(c.id), + body: this.lower_const_body(c.value.span, Some(&c.value)), + }); + hir::ExprKind::ConstBlock(c) } ExprKind::Repeat(expr, count) => { let expr = self.lower_expr(expr); @@ -271,6 +275,10 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::ExprKind::Ret(e) } ExprKind::Yeet(sub_expr) => self.lower_expr_yeet(e.span, sub_expr.as_deref()), + ExprKind::Become(sub_expr) => { + let sub_expr = self.lower_expr(sub_expr); + hir::ExprKind::Become(sub_expr) + } ExprKind::InlineAsm(asm) => { hir::ExprKind::InlineAsm(self.lower_inline_asm(e.span, asm)) } @@ -665,14 +673,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_attrs( inner_hir_id, &[Attribute { - kind: AttrKind::Normal(ptr::P(NormalAttr { - item: AttrItem { - path: Path::from_ident(Ident::new(sym::track_caller, span)), - args: AttrArgs::Empty, - tokens: None, - }, - tokens: None, - })), + kind: AttrKind::Normal(ptr::P(NormalAttr::from_ident(Ident::new(sym::track_caller, span)))), id: self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(), style: AttrStyle::Outer, span: unstable_span, diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs index 2e66c81eb..ce847906f 100644 --- a/compiler/rustc_ast_lowering/src/index.rs +++ b/compiler/rustc_ast_lowering/src/index.rs @@ -223,6 +223,14 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { }); } + fn visit_inline_const(&mut self, constant: &'hir ConstBlock) { + self.insert(DUMMY_SP, constant.hir_id, Node::ConstBlock(constant)); + + self.with_parent(constant.hir_id, |this| { + intravisit::walk_inline_const(this, constant); + }); + } + fn visit_expr(&mut self, expr: &'hir Expr<'hir>) { self.insert(expr.span, expr.hir_id, Node::Expr(expr)); diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 08ee3761b..ab68436c0 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -3,6 +3,7 @@ use super::ResolverAstLoweringExt; use super::{AstOwner, ImplTraitContext, ImplTraitPosition}; use super::{FnDeclKind, LoweringContext, ParamMode}; +use hir::definitions::DefPathData; use rustc_ast::ptr::P; use rustc_ast::visit::AssocCtxt; use rustc_ast::*; @@ -257,10 +258,11 @@ impl<'hir> LoweringContext<'_, 'hir> { ); let itctx = ImplTraitContext::Universal; - let (generics, decl) = this.lower_generics(generics, id, &itctx, |this| { - let ret_id = asyncness.opt_return_id(); - this.lower_fn_decl(&decl, id, *fn_sig_span, FnDeclKind::Fn, ret_id) - }); + let (generics, decl) = + this.lower_generics(generics, header.constness, id, &itctx, |this| { + let ret_id = asyncness.opt_return_id(); + this.lower_fn_decl(&decl, id, *fn_sig_span, FnDeclKind::Fn, ret_id) + }); let sig = hir::FnSig { decl, header: this.lower_fn_header(*header), @@ -295,6 +297,7 @@ impl<'hir> LoweringContext<'_, 'hir> { add_ty_alias_where_clause(&mut generics, *where_clauses, true); let (generics, ty) = self.lower_generics( &generics, + Const::No, id, &ImplTraitContext::Disallowed(ImplTraitPosition::Generic), |this| match ty { @@ -316,6 +319,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ItemKind::Enum(enum_definition, generics) => { let (generics, variants) = self.lower_generics( generics, + Const::No, id, &ImplTraitContext::Disallowed(ImplTraitPosition::Generic), |this| { @@ -329,6 +333,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ItemKind::Struct(struct_def, generics) => { let (generics, struct_def) = self.lower_generics( generics, + Const::No, id, &ImplTraitContext::Disallowed(ImplTraitPosition::Generic), |this| this.lower_variant_data(hir_id, struct_def), @@ -338,6 +343,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ItemKind::Union(vdata, generics) => { let (generics, vdata) = self.lower_generics( generics, + Const::No, id, &ImplTraitContext::Disallowed(ImplTraitPosition::Generic), |this| this.lower_variant_data(hir_id, vdata), @@ -369,7 +375,7 @@ impl<'hir> LoweringContext<'_, 'hir> { // parent lifetime. let itctx = ImplTraitContext::Universal; let (generics, (trait_ref, lowered_ty)) = - self.lower_generics(ast_generics, id, &itctx, |this| { + self.lower_generics(ast_generics, *constness, id, &itctx, |this| { let trait_ref = trait_ref.as_ref().map(|trait_ref| { this.lower_trait_ref( trait_ref, @@ -410,8 +416,15 @@ impl<'hir> LoweringContext<'_, 'hir> { })) } ItemKind::Trait(box Trait { is_auto, unsafety, generics, bounds, items }) => { + // FIXME(const_trait_impl, effects, fee1-dead) this should be simplified if possible + let constness = attrs + .unwrap_or(&[]) + .iter() + .find(|x| x.has_name(sym::const_trait)) + .map_or(Const::No, |x| Const::Yes(x.span)); let (generics, (unsafety, items, bounds)) = self.lower_generics( generics, + constness, id, &ImplTraitContext::Disallowed(ImplTraitPosition::Generic), |this| { @@ -431,6 +444,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ItemKind::TraitAlias(generics, bounds) => { let (generics, bounds) = self.lower_generics( generics, + Const::No, id, &ImplTraitContext::Disallowed(ImplTraitPosition::Generic), |this| { @@ -593,7 +607,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let fdec = &sig.decl; let itctx = ImplTraitContext::Universal; let (generics, (fn_dec, fn_args)) = - self.lower_generics(generics, i.id, &itctx, |this| { + self.lower_generics(generics, Const::No, i.id, &itctx, |this| { ( // Disallow `impl Trait` in foreign items. this.lower_fn_decl( @@ -745,6 +759,7 @@ impl<'hir> LoweringContext<'_, 'hir> { add_ty_alias_where_clause(&mut generics, *where_clauses, false); let (generics, kind) = self.lower_generics( &generics, + Const::No, i.id, &ImplTraitContext::Disallowed(ImplTraitPosition::Generic), |this| { @@ -843,6 +858,7 @@ impl<'hir> LoweringContext<'_, 'hir> { add_ty_alias_where_clause(&mut generics, *where_clauses, false); self.lower_generics( &generics, + Const::No, i.id, &ImplTraitContext::Disallowed(ImplTraitPosition::Generic), |this| match ty { @@ -1201,9 +1217,10 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> (&'hir hir::Generics<'hir>, hir::FnSig<'hir>) { let header = self.lower_fn_header(sig.header); let itctx = ImplTraitContext::Universal; - let (generics, decl) = self.lower_generics(generics, id, &itctx, |this| { - this.lower_fn_decl(&sig.decl, id, sig.span, kind, is_async) - }); + let (generics, decl) = + self.lower_generics(generics, sig.header.constness, id, &itctx, |this| { + this.lower_fn_decl(&sig.decl, id, sig.span, kind, is_async) + }); (generics, hir::FnSig { header, decl, span: self.lower_span(sig.span) }) } @@ -1275,6 +1292,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_generics( &mut self, generics: &Generics, + constness: Const, parent_node_id: NodeId, itctx: &ImplTraitContext, f: impl FnOnce(&mut Self) -> T, @@ -1372,6 +1390,87 @@ impl<'hir> LoweringContext<'_, 'hir> { let impl_trait_bounds = std::mem::take(&mut self.impl_trait_bounds); predicates.extend(impl_trait_bounds.into_iter()); + // Desugar `~const` bound in generics into an additional `const host: bool` param + // if the effects feature is enabled. + if let Const::Yes(span) = constness && self.tcx.features().effects + // Do not add host param if it already has it (manually specified) + && !params.iter().any(|x| { + self.attrs.get(&x.hir_id.local_id).map_or(false, |attrs| { + attrs.iter().any(|x| x.has_name(sym::rustc_host)) + }) + }) + { + let param_node_id = self.next_node_id(); + let const_node_id = self.next_node_id(); + let def_id = self.create_def(self.local_def_id(parent_node_id), param_node_id, DefPathData::TypeNs(sym::host), span); + let anon_const: LocalDefId = self.create_def(def_id, const_node_id, DefPathData::AnonConst, span); + + let hir_id = self.next_id(); + let const_id = self.next_id(); + let const_expr_id = self.next_id(); + let bool_id = self.next_id(); + + self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id))); + self.children.push((anon_const, hir::MaybeOwner::NonOwner(const_id))); + + let attr_id = self.tcx.sess.parse_sess.attr_id_generator.mk_attr_id(); + + let attrs = self.arena.alloc_from_iter([ + Attribute { + kind: AttrKind::Normal(P(NormalAttr::from_ident(Ident::new(sym::rustc_host, span)))), + span, + id: attr_id, + style: AttrStyle::Outer, + }, + ]); + self.attrs.insert(hir_id.local_id, attrs); + + let const_body = self.lower_body(|this| { + ( + &[], + hir::Expr { + hir_id: const_expr_id, + kind: hir::ExprKind::Lit( + this.arena.alloc(hir::Lit { node: LitKind::Bool(true), span }), + ), + span, + }, + ) + }); + + let param = hir::GenericParam { + def_id, + hir_id, + name: hir::ParamName::Plain(Ident { name: sym::host, span }), + span, + kind: hir::GenericParamKind::Const { + ty: self.arena.alloc(self.ty( + span, + hir::TyKind::Path(hir::QPath::Resolved( + None, + self.arena.alloc(hir::Path { + res: Res::PrimTy(hir::PrimTy::Bool), + span, + segments: self.arena.alloc_from_iter([hir::PathSegment { + ident: Ident { name: sym::bool, span }, + hir_id: bool_id, + res: Res::PrimTy(hir::PrimTy::Bool), + args: None, + infer_args: false, + }]), + }), + )), + )), + default: Some(hir::AnonConst { def_id: anon_const, hir_id: const_id, body: const_body }), + }, + colon_span: None, + pure_wrt_drop: false, + source: hir::GenericParamSource::Generics, + }; + + params.push(param); + } + let lowered_generics = self.arena.alloc(hir::Generics { params: self.arena.alloc_from_iter(params), predicates: self.arena.alloc_from_iter(predicates), diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 8d4f96639..429e62c4a 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -1539,9 +1539,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ); debug!(?opaque_ty_def_id); - // Contains the new lifetime definitions created for the TAIT (if any). - let mut collected_lifetimes = Vec::new(); - // If this came from a TAIT (as opposed to a function that returns an RPIT), we only want // to capture the lifetimes that appear in the bounds. So visit the bounds to find out // exactly which ones those are. @@ -1558,20 +1555,31 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }; debug!(?lifetimes_to_remap); - self.with_hir_id_owner(opaque_ty_node_id, |lctx| { - let mut new_remapping = FxHashMap::default(); + let mut new_remapping = FxHashMap::default(); - // If this opaque type is only capturing a subset of the lifetimes (those that appear - // in bounds), then create the new lifetime parameters required and create a mapping - // from the old `'a` (on the function) to the new `'a` (on the opaque type). - collected_lifetimes = lctx.create_lifetime_defs( - opaque_ty_def_id, - &lifetimes_to_remap, - &mut new_remapping, - ); - debug!(?collected_lifetimes); - debug!(?new_remapping); + // Contains the new lifetime definitions created for the TAIT (if any). + // If this opaque type is only capturing a subset of the lifetimes (those that appear in + // bounds), then create the new lifetime parameters required and create a mapping from the + // old `'a` (on the function) to the new `'a` (on the opaque type). + let collected_lifetimes = + self.create_lifetime_defs(opaque_ty_def_id, &lifetimes_to_remap, &mut new_remapping); + debug!(?collected_lifetimes); + debug!(?new_remapping); + + // This creates HIR lifetime arguments as `hir::GenericArg`, in the given example `type + // TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing `&['x]`. + let collected_lifetime_mapping: Vec<_> = collected_lifetimes + .iter() + .map(|(node_id, lifetime)| { + let id = self.next_node_id(); + let lifetime = self.new_named_lifetime(lifetime.id, id, lifetime.ident); + let def_id = self.local_def_id(*node_id); + (lifetime, def_id) + }) + .collect(); + debug!(?collected_lifetime_mapping); + self.with_hir_id_owner(opaque_ty_node_id, |lctx| { // Install the remapping from old to new (if any): lctx.with_remapping(new_remapping, |lctx| { // This creates HIR lifetime definitions as `hir::GenericParam`, in the given @@ -1610,6 +1618,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let hir_bounds = lctx.lower_param_bounds(bounds, itctx); debug!(?hir_bounds); + let lifetime_mapping = if in_trait { + self.arena.alloc_from_iter( + collected_lifetime_mapping + .iter() + .map(|(lifetime, def_id)| (**lifetime, *def_id)), + ) + } else { + &mut [] + }; + let opaque_ty_item = hir::OpaqueTy { generics: self.arena.alloc(hir::Generics { params: lifetime_defs, @@ -1620,6 +1638,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }), bounds: hir_bounds, origin, + lifetime_mapping, in_trait, }; debug!(?opaque_ty_item); @@ -1628,20 +1647,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }) }); - // This creates HIR lifetime arguments as `hir::GenericArg`, in the given example `type - // TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing `&['x]`. - let lifetimes = - self.arena.alloc_from_iter(collected_lifetimes.into_iter().map(|(_, lifetime)| { - let id = self.next_node_id(); - let l = self.new_named_lifetime(lifetime.id, id, lifetime.ident); - hir::GenericArg::Lifetime(l) - })); - debug!(?lifetimes); - // `impl Trait` now just becomes `Foo<'a, 'b, ..>`. hir::TyKind::OpaqueDef( hir::ItemId { owner_id: hir::OwnerId { def_id: opaque_ty_def_id } }, - lifetimes, + self.arena.alloc_from_iter( + collected_lifetime_mapping + .iter() + .map(|(lifetime, _)| hir::GenericArg::Lifetime(*lifetime)), + ), in_trait, ) } @@ -1655,7 +1668,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { span: Span, opaque_ty_span: Span, ) -> hir::OwnerNode<'hir> { - let opaque_ty_item_kind = hir::ItemKind::OpaqueTy(opaque_ty_item); + let opaque_ty_item_kind = hir::ItemKind::OpaqueTy(self.arena.alloc(opaque_ty_item)); // Generate an `type Foo = impl Trait;` declaration. trace!("registering opaque type with id {:#?}", opaque_ty_id); let opaque_ty_item = hir::Item { @@ -1983,7 +1996,6 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let lifetime = Lifetime { id: outer_node_id, ident }; collected_lifetimes.push((inner_node_id, lifetime, Some(inner_res))); } - debug!(?collected_lifetimes); // We only want to capture the lifetimes that appear in the bounds. So visit the bounds to @@ -1993,22 +2005,36 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let lifetimes_to_remap = lifetime_collector::lifetimes_in_ret_ty(&self.resolver, output); debug!(?lifetimes_to_remap); - self.with_hir_id_owner(opaque_ty_node_id, |this| { - // If this opaque type is only capturing a subset of the lifetimes (those that appear - // in bounds), then create the new lifetime parameters required and create a mapping - // from the old `'a` (on the function) to the new `'a` (on the opaque type). - collected_lifetimes.extend( - this.create_lifetime_defs( - opaque_ty_def_id, - &lifetimes_to_remap, - &mut new_remapping, - ) + // If this opaque type is only capturing a subset of the lifetimes (those that appear in + // bounds), then create the new lifetime parameters required and create a mapping from the + // old `'a` (on the function) to the new `'a` (on the opaque type). + collected_lifetimes.extend( + self.create_lifetime_defs(opaque_ty_def_id, &lifetimes_to_remap, &mut new_remapping) .into_iter() .map(|(new_node_id, lifetime)| (new_node_id, lifetime, None)), - ); - debug!(?collected_lifetimes); - debug!(?new_remapping); + ); + debug!(?collected_lifetimes); + debug!(?new_remapping); + + // This creates pairs of HIR lifetimes and def_ids. In the given example `type + // TestReturn<'a, T, 'x> = impl Debug + 'x`, it creates a collection containing the + // new lifetime of the RPIT 'x and the def_id of the lifetime 'x corresponding to + // `TestReturn`. + let collected_lifetime_mapping: Vec<_> = collected_lifetimes + .iter() + .map(|(node_id, lifetime, res)| { + let id = self.next_node_id(); + let res = res.unwrap_or( + self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error), + ); + let lifetime = self.new_named_lifetime_with_res(id, lifetime.ident, res); + let def_id = self.local_def_id(*node_id); + (lifetime, def_id) + }) + .collect(); + debug!(?collected_lifetime_mapping); + self.with_hir_id_owner(opaque_ty_node_id, |this| { // Install the remapping from old to new (if any): this.with_remapping(new_remapping, |this| { // We have to be careful to get elision right here. The @@ -2063,6 +2089,16 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { )); debug!("lower_async_fn_ret_ty: generic_params={:#?}", generic_params); + let lifetime_mapping = if in_trait { + self.arena.alloc_from_iter( + collected_lifetime_mapping + .iter() + .map(|(lifetime, def_id)| (**lifetime, *def_id)), + ) + } else { + &mut [] + }; + let opaque_ty_item = hir::OpaqueTy { generics: this.arena.alloc(hir::Generics { params: generic_params, @@ -2073,6 +2109,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { }), bounds: arena_vec![this; future_bound], origin: hir::OpaqueTyOrigin::AsyncFn(fn_def_id), + lifetime_mapping, in_trait, }; @@ -2096,15 +2133,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // // For the "output" lifetime parameters, we just want to // generate `'_`. - let generic_args = self.arena.alloc_from_iter(collected_lifetimes.into_iter().map( - |(_, lifetime, res)| { - let id = self.next_node_id(); - let res = res.unwrap_or( - self.resolver.get_lifetime_res(lifetime.id).unwrap_or(LifetimeRes::Error), - ); - hir::GenericArg::Lifetime(self.new_named_lifetime_with_res(id, lifetime.ident, res)) - }, - )); + let generic_args = self.arena.alloc_from_iter( + collected_lifetime_mapping + .iter() + .map(|(lifetime, _)| hir::GenericArg::Lifetime(*lifetime)), + ); // Create the `Foo<...>` reference itself. Note that the `type // Foo = impl Trait` is, internally, created as a child of the -- cgit v1.2.3