From ef24de24a82fe681581cc130f342363c47c0969a Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 7 Jun 2024 07:48:48 +0200 Subject: Merging upstream version 1.75.0+dfsg1. Signed-off-by: Daniel Baumann --- compiler/rustc_ast_lowering/Cargo.toml | 6 +- compiler/rustc_ast_lowering/messages.ftl | 18 +-- compiler/rustc_ast_lowering/src/errors.rs | 19 +-- compiler/rustc_ast_lowering/src/expr.rs | 159 ++++++++++++++++----- compiler/rustc_ast_lowering/src/format.rs | 54 +++---- compiler/rustc_ast_lowering/src/item.rs | 42 +++--- compiler/rustc_ast_lowering/src/lib.rs | 111 +++++--------- .../rustc_ast_lowering/src/lifetime_collector.rs | 3 +- 8 files changed, 215 insertions(+), 197 deletions(-) (limited to 'compiler/rustc_ast_lowering') diff --git a/compiler/rustc_ast_lowering/Cargo.toml b/compiler/rustc_ast_lowering/Cargo.toml index 6b0da2565..8cc4521e0 100644 --- a/compiler/rustc_ast_lowering/Cargo.toml +++ b/compiler/rustc_ast_lowering/Cargo.toml @@ -7,18 +7,20 @@ edition = "2021" doctest = false [dependencies] +# tidy-alphabetical-start rustc_ast = { path = "../rustc_ast" } rustc_ast_pretty = { path = "../rustc_ast_pretty" } rustc_data_structures = { path = "../rustc_data_structures" } rustc_errors = { path = "../rustc_errors" } -rustc_hir = { path = "../rustc_hir" } rustc_fluent_macro = { path = "../rustc_fluent_macro" } +rustc_hir = { path = "../rustc_hir" } rustc_index = { path = "../rustc_index" } -rustc_middle = { path = "../rustc_middle" } rustc_macros = { path = "../rustc_macros" } +rustc_middle = { path = "../rustc_middle" } rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } rustc_target = { path = "../rustc_target" } smallvec = { version = "1.8.1", features = ["union", "may_dangle"] } thin-vec = "0.2.12" tracing = "0.1" +# tidy-alphabetical-end diff --git a/compiler/rustc_ast_lowering/messages.ftl b/compiler/rustc_ast_lowering/messages.ftl index 8115c4b55..91591a716 100644 --- a/compiler/rustc_ast_lowering/messages.ftl +++ b/compiler/rustc_ast_lowering/messages.ftl @@ -11,8 +11,8 @@ ast_lowering_argument = argument ast_lowering_assoc_ty_parentheses = parenthesized generic arguments cannot be used in associated type constraints -ast_lowering_async_generators_not_supported = - `async` generators are not yet supported +ast_lowering_async_coroutines_not_supported = + `async` coroutines are not yet supported ast_lowering_async_non_move_closure_not_supported = `async` non-`move` closures with parameters are not currently supported @@ -42,6 +42,9 @@ ast_lowering_clobber_abi_not_supported = ast_lowering_closure_cannot_be_static = closures cannot be static +ast_lowering_coroutine_too_many_parameters = + too many parameters for a coroutine (expected 0 or 1 parameters) + ast_lowering_does_not_support_modifiers = the `{$class_name}` register class does not support template modifiers @@ -53,9 +56,6 @@ ast_lowering_functional_record_update_destructuring_assignment = functional record updates are not allowed in destructuring assignments .suggestion = consider removing the trailing pattern -ast_lowering_generator_too_many_parameters = - too many parameters for a generator (expected 0 or 1 parameters) - ast_lowering_generic_type_with_parentheses = parenthesized type parameters may only be used with a `Fn` trait .label = only `Fn` traits may use parentheses @@ -99,7 +99,7 @@ ast_lowering_misplaced_double_dot = .note = only allowed in tuple, tuple struct, and slice patterns ast_lowering_misplaced_impl_trait = - `impl Trait` only allowed in function and inherent method return types, not in {$position} + `impl Trait` only allowed in function and inherent method argument and return types, not in {$position} ast_lowering_misplaced_relax_trait_bound = `?Trait` bounds are only permitted at the point where a type parameter is declared @@ -136,12 +136,6 @@ ast_lowering_template_modifier = template modifier ast_lowering_this_not_async = this is not `async` -ast_lowering_trait_fn_async = - functions in traits cannot be declared `async` - .label = `async` because of this - .note = `async` trait functions are not currently supported - .note2 = consider using the `async-trait` crate: https://crates.io/crates/async-trait - ast_lowering_underscore_expr_lhs_assign = in expressions, `_` can only be used on the left-hand side of an assignment .label = `_` not allowed here diff --git a/compiler/rustc_ast_lowering/src/errors.rs b/compiler/rustc_ast_lowering/src/errors.rs index a63bd4f8a..6e1a9eff5 100644 --- a/compiler/rustc_ast_lowering/src/errors.rs +++ b/compiler/rustc_ast_lowering/src/errors.rs @@ -131,8 +131,8 @@ pub struct AwaitOnlyInAsyncFnAndBlocks { } #[derive(Diagnostic, Clone, Copy)] -#[diag(ast_lowering_generator_too_many_parameters, code = "E0628")] -pub struct GeneratorTooManyParameters { +#[diag(ast_lowering_coroutine_too_many_parameters, code = "E0628")] +pub struct CoroutineTooManyParameters { #[primary_span] pub fn_decl_span: Span, } @@ -161,8 +161,8 @@ pub struct FunctionalRecordUpdateDestructuringAssignment { } #[derive(Diagnostic, Clone, Copy)] -#[diag(ast_lowering_async_generators_not_supported, code = "E0727")] -pub struct AsyncGeneratorsNotSupported { +#[diag(ast_lowering_async_coroutines_not_supported, code = "E0727")] +pub struct AsyncCoroutinesNotSupported { #[primary_span] pub span: Span, } @@ -354,17 +354,6 @@ pub struct InclusiveRangeWithNoEnd { pub span: Span, } -#[derive(Diagnostic, Clone, Copy)] -#[diag(ast_lowering_trait_fn_async, code = "E0706")] -#[note] -#[note(ast_lowering_note2)] -pub struct TraitFnAsync { - #[primary_span] - pub fn_span: Span, - #[label] - pub span: Span, -} - #[derive(Diagnostic)] pub enum BadReturnTypeNotation { #[diag(ast_lowering_bad_return_type_notation_inputs)] diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 57c54f854..0fff9a6be 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -1,8 +1,8 @@ use super::errors::{ - AsyncGeneratorsNotSupported, AsyncNonMoveClosureNotSupported, AwaitOnlyInAsyncFnAndBlocks, - BaseExpressionDoubleDot, ClosureCannotBeStatic, FunctionalRecordUpdateDestructuringAssignment, - GeneratorTooManyParameters, InclusiveRangeWithNoEnd, NotSupportedForLifetimeBinderAsyncClosure, - UnderscoreExprLhsAssign, + AsyncCoroutinesNotSupported, AsyncNonMoveClosureNotSupported, AwaitOnlyInAsyncFnAndBlocks, + BaseExpressionDoubleDot, ClosureCannotBeStatic, CoroutineTooManyParameters, + FunctionalRecordUpdateDestructuringAssignment, InclusiveRangeWithNoEnd, + NotSupportedForLifetimeBinderAsyncClosure, UnderscoreExprLhsAssign, }; use super::ResolverAstLoweringExt; use super::{ImplTraitContext, LoweringContext, ParamMode, ParenthesizedGenericArgs}; @@ -15,9 +15,10 @@ use rustc_hir as hir; use rustc_hir::def::Res; use rustc_hir::definitions::DefPathData; use rustc_session::errors::report_lit_error; -use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned}; +use rustc_span::source_map::{respan, Spanned}; use rustc_span::symbol::{sym, Ident, Symbol}; use rustc_span::DUMMY_SP; +use rustc_span::{DesugaringKind, Span}; use thin_vec::{thin_vec, ThinVec}; impl<'hir> LoweringContext<'_, 'hir> { @@ -183,12 +184,12 @@ impl<'hir> LoweringContext<'_, 'hir> { self.arena.alloc_from_iter(arms.iter().map(|x| self.lower_arm(x))), hir::MatchSource::Normal, ), - ExprKind::Async(capture_clause, block) => self.make_async_expr( + ExprKind::Gen(capture_clause, block, GenBlockKind::Async) => self.make_async_expr( *capture_clause, e.id, None, e.span, - hir::AsyncGeneratorKind::Block, + hir::CoroutineSource::Block, |this| this.with_new_scopes(|this| this.lower_block_expr(block)), ), ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr), @@ -317,6 +318,14 @@ impl<'hir> LoweringContext<'_, 'hir> { rest, ) } + ExprKind::Gen(capture_clause, block, GenBlockKind::Gen) => self.make_gen_expr( + *capture_clause, + e.id, + None, + e.span, + hir::CoroutineSource::Block, + |this| this.with_new_scopes(|this| this.lower_block_expr(block)), + ), ExprKind::Yield(opt_expr) => self.lower_expr_yield(e.span, opt_expr.as_deref()), ExprKind::Err => hir::ExprKind::Err( self.tcx.sess.delay_span_bug(e.span, "lowered ExprKind::Err"), @@ -583,7 +592,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } } - /// Lower an `async` construct to a generator that implements `Future`. + /// Lower an `async` construct to a coroutine that implements `Future`. /// /// This results in: /// @@ -598,7 +607,7 @@ impl<'hir> LoweringContext<'_, 'hir> { closure_node_id: NodeId, ret_ty: Option>, span: Span, - async_gen_kind: hir::AsyncGeneratorKind, + async_coroutine_source: hir::CoroutineSource, body: impl FnOnce(&mut Self) -> hir::Expr<'hir>, ) -> hir::ExprKind<'hir> { let output = ret_ty.unwrap_or_else(|| hir::FnRetTy::DefaultReturn(self.lower_span(span))); @@ -613,7 +622,7 @@ impl<'hir> LoweringContext<'_, 'hir> { span: unstable_span, }; - // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`. + // The closure/coroutine `FnDecl` takes a single (resume) argument of type `input_ty`. let fn_decl = self.arena.alloc(hir::FnDecl { inputs: arena_vec![self; input_ty], output, @@ -637,7 +646,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let params = arena_vec![self; param]; let body = self.lower_body(move |this| { - this.generator_kind = Some(hir::GeneratorKind::Async(async_gen_kind)); + this.coroutine_kind = Some(hir::CoroutineKind::Async(async_coroutine_source)); let old_ctx = this.task_context; this.task_context = Some(task_context_hid); @@ -661,6 +670,57 @@ impl<'hir> LoweringContext<'_, 'hir> { })) } + /// Lower a `gen` construct to a generator that implements `Iterator`. + /// + /// This results in: + /// + /// ```text + /// static move? |()| -> () { + /// + /// } + /// ``` + pub(super) fn make_gen_expr( + &mut self, + capture_clause: CaptureBy, + closure_node_id: NodeId, + _yield_ty: Option>, + span: Span, + coroutine_source: hir::CoroutineSource, + body: impl FnOnce(&mut Self) -> hir::Expr<'hir>, + ) -> hir::ExprKind<'hir> { + let output = hir::FnRetTy::DefaultReturn(self.lower_span(span)); + + // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`. + let fn_decl = self.arena.alloc(hir::FnDecl { + inputs: &[], + output, + c_variadic: false, + implicit_self: hir::ImplicitSelfKind::None, + lifetime_elision_allowed: false, + }); + + let body = self.lower_body(move |this| { + this.coroutine_kind = Some(hir::CoroutineKind::Gen(coroutine_source)); + + let res = body(this); + (&[], res) + }); + + // `static |()| -> () { body }`: + hir::ExprKind::Closure(self.arena.alloc(hir::Closure { + def_id: self.local_def_id(closure_node_id), + binder: hir::ClosureBinder::Default, + capture_clause, + bound_generic_params: &[], + fn_decl, + body, + fn_decl_span: self.lower_span(span), + fn_arg_span: None, + movability: Some(Movability::Movable), + constness: hir::Constness::NotConst, + })) + } + /// Forwards a possible `#[track_caller]` annotation from `outer_hir_id` to /// `inner_hir_id` in case the `async_fn_track_caller` feature is enabled. pub(super) fn maybe_forward_track_caller( @@ -673,12 +733,18 @@ impl<'hir> LoweringContext<'_, 'hir> { && let Some(attrs) = self.attrs.get(&outer_hir_id.local_id) && attrs.into_iter().any(|attr| attr.has_name(sym::track_caller)) { - let unstable_span = - self.mark_span_with_reason(DesugaringKind::Async, span, self.allow_gen_future.clone()); + let unstable_span = self.mark_span_with_reason( + DesugaringKind::Async, + span, + self.allow_gen_future.clone(), + ); self.lower_attrs( inner_hir_id, &[Attribute { - kind: AttrKind::Normal(ptr::P(NormalAttr::from_ident(Ident::new(sym::track_caller, span)))), + 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, @@ -704,9 +770,9 @@ impl<'hir> LoweringContext<'_, 'hir> { /// ``` fn lower_expr_await(&mut self, await_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> { let full_span = expr.span.to(await_kw_span); - match self.generator_kind { - Some(hir::GeneratorKind::Async(_)) => {} - Some(hir::GeneratorKind::Gen) | None => { + match self.coroutine_kind { + Some(hir::CoroutineKind::Async(_)) => {} + Some(hir::CoroutineKind::Coroutine) | Some(hir::CoroutineKind::Gen(_)) | None => { self.tcx.sess.emit_err(AwaitOnlyInAsyncFnAndBlocks { await_kw_span, item_span: self.current_item, @@ -881,19 +947,19 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> hir::ExprKind<'hir> { let (binder_clause, generic_params) = self.lower_closure_binder(binder); - let (body_id, generator_option) = self.with_new_scopes(move |this| { + let (body_id, coroutine_option) = self.with_new_scopes(move |this| { let prev = this.current_item; this.current_item = Some(fn_decl_span); - let mut generator_kind = None; + let mut coroutine_kind = None; let body_id = this.lower_fn_body(decl, |this| { let e = this.lower_expr_mut(body); - generator_kind = this.generator_kind; + coroutine_kind = this.coroutine_kind; e }); - let generator_option = - this.generator_movability_for_fn(&decl, fn_decl_span, generator_kind, movability); + let coroutine_option = + this.coroutine_movability_for_fn(&decl, fn_decl_span, coroutine_kind, movability); this.current_item = prev; - (body_id, generator_option) + (body_id, coroutine_option) }); let bound_generic_params = self.lower_lifetime_binder(closure_id, generic_params); @@ -909,29 +975,29 @@ impl<'hir> LoweringContext<'_, 'hir> { body: body_id, fn_decl_span: self.lower_span(fn_decl_span), fn_arg_span: Some(self.lower_span(fn_arg_span)), - movability: generator_option, + movability: coroutine_option, constness: self.lower_constness(constness), }); hir::ExprKind::Closure(c) } - fn generator_movability_for_fn( + fn coroutine_movability_for_fn( &mut self, decl: &FnDecl, fn_decl_span: Span, - generator_kind: Option, + coroutine_kind: Option, movability: Movability, ) -> Option { - match generator_kind { - Some(hir::GeneratorKind::Gen) => { + match coroutine_kind { + Some(hir::CoroutineKind::Coroutine) => { if decl.inputs.len() > 1 { - self.tcx.sess.emit_err(GeneratorTooManyParameters { fn_decl_span }); + self.tcx.sess.emit_err(CoroutineTooManyParameters { fn_decl_span }); } Some(movability) } - Some(hir::GeneratorKind::Async(_)) => { - panic!("non-`async` closure body turned `async` during lowering"); + Some(hir::CoroutineKind::Gen(_)) | Some(hir::CoroutineKind::Async(_)) => { + panic!("non-`async`/`gen` closure body turned `async`/`gen` during lowering"); } None => { if movability == Movability::Static { @@ -999,7 +1065,7 @@ impl<'hir> LoweringContext<'_, 'hir> { inner_closure_id, async_ret_ty, body.span, - hir::AsyncGeneratorKind::Closure, + hir::CoroutineSource::Closure, |this| this.with_new_scopes(|this| this.lower_expr_mut(body)), ); let hir_id = this.lower_node_id(inner_closure_id); @@ -1102,7 +1168,9 @@ impl<'hir> LoweringContext<'_, 'hir> { if let ExprKind::Path(qself, path) = &expr.kind { // Does the path resolve to something disallowed in a tuple struct/variant pattern? if let Some(partial_res) = self.resolver.get_partial_res(expr.id) { - if let Some(res) = partial_res.full_res() && !res.expected_in_tuple_struct_pat() { + if let Some(res) = partial_res.full_res() + && !res.expected_in_tuple_struct_pat() + { return None; } } @@ -1122,7 +1190,9 @@ impl<'hir> LoweringContext<'_, 'hir> { if let ExprKind::Path(qself, path) = &expr.kind { // Does the path resolve to something disallowed in a unit struct/variant pattern? if let Some(partial_res) = self.resolver.get_partial_res(expr.id) { - if let Some(res) = partial_res.full_res() && !res.expected_in_unit_struct_pat() { + if let Some(res) = partial_res.full_res() + && !res.expected_in_unit_struct_pat() + { return None; } } @@ -1434,12 +1504,23 @@ impl<'hir> LoweringContext<'_, 'hir> { } fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> { - match self.generator_kind { - Some(hir::GeneratorKind::Gen) => {} - Some(hir::GeneratorKind::Async(_)) => { - self.tcx.sess.emit_err(AsyncGeneratorsNotSupported { span }); + match self.coroutine_kind { + Some(hir::CoroutineKind::Gen(_)) => {} + Some(hir::CoroutineKind::Async(_)) => { + self.tcx.sess.emit_err(AsyncCoroutinesNotSupported { span }); + } + Some(hir::CoroutineKind::Coroutine) | None => { + if !self.tcx.features().coroutines { + rustc_session::parse::feature_err( + &self.tcx.sess.parse_sess, + sym::coroutines, + span, + "yield syntax is experimental", + ) + .emit(); + } + self.coroutine_kind = Some(hir::CoroutineKind::Coroutine) } - None => self.generator_kind = Some(hir::GeneratorKind::Gen), } let expr = diff --git a/compiler/rustc_ast_lowering/src/format.rs b/compiler/rustc_ast_lowering/src/format.rs index afcf8b15c..c7d0719e7 100644 --- a/compiler/rustc_ast_lowering/src/format.rs +++ b/compiler/rustc_ast_lowering/src/format.rs @@ -61,9 +61,12 @@ fn flatten_format_args(mut fmt: Cow<'_, FormatArgs>) -> Cow<'_, FormatArgs> { let remaining_args = args.split_off(arg_index + 1); let old_arg_offset = args.len(); let mut fmt2 = &mut args.pop().unwrap().expr; // The inner FormatArgs. - let fmt2 = loop { // Unwrap the Expr to get to the FormatArgs. + let fmt2 = loop { + // Unwrap the Expr to get to the FormatArgs. match &mut fmt2.kind { - ExprKind::Paren(inner) | ExprKind::AddrOf(BorrowKind::Ref, _, inner) => fmt2 = inner, + ExprKind::Paren(inner) | ExprKind::AddrOf(BorrowKind::Ref, _, inner) => { + fmt2 = inner + } ExprKind::FormatArgs(fmt2) => break fmt2, _ => unreachable!(), } @@ -410,15 +413,11 @@ fn expand_format_args<'hir>( let format_options = use_format_options.then(|| { // Generate: // &[format_spec_0, format_spec_1, format_spec_2] - let elements: Vec<_> = fmt - .template - .iter() - .filter_map(|piece| { - let FormatArgsPiece::Placeholder(placeholder) = piece else { return None }; - Some(make_format_spec(ctx, macsp, placeholder, &mut argmap)) - }) - .collect(); - ctx.expr_array_ref(macsp, ctx.arena.alloc_from_iter(elements)) + let elements = ctx.arena.alloc_from_iter(fmt.template.iter().filter_map(|piece| { + let FormatArgsPiece::Placeholder(placeholder) = piece else { return None }; + Some(make_format_spec(ctx, macsp, placeholder, &mut argmap)) + })); + ctx.expr_array_ref(macsp, elements) }); let arguments = fmt.arguments.all_args(); @@ -477,10 +476,8 @@ fn expand_format_args<'hir>( // ::new_debug(&arg2), // … // ] - let elements: Vec<_> = arguments - .iter() - .zip(argmap) - .map(|(arg, ((_, ty), placeholder_span))| { + let elements = ctx.arena.alloc_from_iter(arguments.iter().zip(argmap).map( + |(arg, ((_, ty), placeholder_span))| { let placeholder_span = placeholder_span.unwrap_or(arg.expr.span).with_ctxt(macsp.ctxt()); let arg_span = match arg.kind { @@ -493,9 +490,9 @@ fn expand_format_args<'hir>( hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg), )); make_argument(ctx, placeholder_span, ref_arg, ty) - }) - .collect(); - ctx.expr_array_ref(macsp, ctx.arena.alloc_from_iter(elements)) + }, + )); + ctx.expr_array_ref(macsp, elements) } else { // Generate: // &match (&arg0, &arg1, &…) { @@ -528,19 +525,14 @@ fn expand_format_args<'hir>( make_argument(ctx, placeholder_span, arg, ty) }, )); - let elements: Vec<_> = arguments - .iter() - .map(|arg| { - let arg_expr = ctx.lower_expr(&arg.expr); - ctx.expr( - arg.expr.span.with_ctxt(macsp.ctxt()), - hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg_expr), - ) - }) - .collect(); - let args_tuple = ctx - .arena - .alloc(ctx.expr(macsp, hir::ExprKind::Tup(ctx.arena.alloc_from_iter(elements)))); + let elements = ctx.arena.alloc_from_iter(arguments.iter().map(|arg| { + let arg_expr = ctx.lower_expr(&arg.expr); + ctx.expr( + arg.expr.span.with_ctxt(macsp.ctxt()), + hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg_expr), + ) + })); + let args_tuple = ctx.arena.alloc(ctx.expr(macsp, hir::ExprKind::Tup(elements))); let array = ctx.arena.alloc(ctx.expr(macsp, hir::ExprKind::Array(args))); let match_arms = ctx.arena.alloc_from_iter([ctx.arm(args_pat, array)]); let match_expr = ctx.arena.alloc(ctx.expr_match( diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index edc1e2f0b..9a70e6d7c 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -16,9 +16,8 @@ use rustc_hir::PredicateOrigin; use rustc_index::{Idx, IndexSlice, IndexVec}; use rustc_middle::ty::{ResolverAstLowering, TyCtxt}; use rustc_span::edit_distance::find_best_match_for_name; -use rustc_span::source_map::DesugaringKind; use rustc_span::symbol::{kw, sym, Ident}; -use rustc_span::{Span, Symbol}; +use rustc_span::{DesugaringKind, Span, Symbol}; use rustc_target::spec::abi; use smallvec::{smallvec, SmallVec}; use thin_vec::ThinVec; @@ -82,7 +81,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> { is_in_loop_condition: false, is_in_trait_impl: false, is_in_dyn_type: false, - generator_kind: None, + coroutine_kind: None, task_context: None, current_item: None, impl_trait_defs: Vec::new(), @@ -974,7 +973,7 @@ impl<'hir> LoweringContext<'_, 'hir> { value: hir::Expr<'hir>, ) -> hir::BodyId { let body = hir::Body { - generator_kind: self.generator_kind, + coroutine_kind: self.coroutine_kind, params, value: self.arena.alloc(value), }; @@ -988,12 +987,12 @@ impl<'hir> LoweringContext<'_, 'hir> { &mut self, f: impl FnOnce(&mut Self) -> (&'hir [hir::Param<'hir>], hir::Expr<'hir>), ) -> hir::BodyId { - let prev_gen_kind = self.generator_kind.take(); + let prev_coroutine_kind = self.coroutine_kind.take(); let task_context = self.task_context.take(); let (parameters, result) = f(self); let body_id = self.record_body(parameters, result); self.task_context = task_context; - self.generator_kind = prev_gen_kind; + self.coroutine_kind = prev_coroutine_kind; body_id } @@ -1202,11 +1201,11 @@ impl<'hir> LoweringContext<'_, 'hir> { } let async_expr = this.make_async_expr( - CaptureBy::Value, + CaptureBy::Value { move_kw: rustc_span::DUMMY_SP }, closure_id, None, body.span, - hir::AsyncGeneratorKind::Fn, + hir::CoroutineSource::Fn, |this| { // Create a block from the user's function body: let user_body = this.lower_block_expr(body); @@ -1387,10 +1386,12 @@ impl<'hir> LoweringContext<'_, 'hir> { // Desugar `~const` bound in generics into an additional `const host: bool` param // if the effects feature is enabled. This needs to be done before we lower where // clauses since where clauses need to bind to the DefId of the host param - let host_param_parts = if let Const::Yes(span) = constness && self.tcx.features().effects { - if let Some(param) = generics.params.iter().find(|x| { - x.attrs.iter().any(|x| x.has_name(sym::rustc_host)) - }) { + let host_param_parts = if let Const::Yes(span) = constness + && self.tcx.features().effects + { + if let Some(param) = + generics.params.iter().find(|x| x.attrs.iter().any(|x| x.has_name(sym::rustc_host))) + { // user has manually specified a `rustc_host` param, in this case, we set // the param id so that lowering logic can use that. But we don't create // another host param, so this gives `None`. @@ -1399,7 +1400,12 @@ impl<'hir> LoweringContext<'_, 'hir> { } else { let param_node_id = self.next_node_id(); let hir_id = self.next_id(); - let def_id = self.create_def(self.local_def_id(parent_node_id), param_node_id, DefPathData::TypeNs(sym::host), span); + let def_id = self.create_def( + self.local_def_id(parent_node_id), + param_node_id, + DefPathData::TypeNs(sym::host), + span, + ); self.host_param_id = Some(def_id); Some((span, hir_id, def_id)) } @@ -1623,12 +1629,10 @@ impl<'hir> LoweringContext<'_, 'hir> { .lower_generic_params(bound_generic_params, hir::GenericParamSource::Binder), bounded_ty: self .lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Bound)), - bounds: self.arena.alloc_from_iter(bounds.iter().map(|bound| { - self.lower_param_bound( - bound, - &ImplTraitContext::Disallowed(ImplTraitPosition::Bound), - ) - })), + bounds: self.lower_param_bounds( + bounds, + &ImplTraitContext::Disallowed(ImplTraitPosition::Bound), + ), span: self.lower_span(*span), origin: PredicateOrigin::WhereClause, }), diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 85ab5e722..a88493acf 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -30,6 +30,9 @@ //! get confused if the spans from leaf AST nodes occur in multiple places //! in the HIR, especially for multiple identifiers. +#![cfg_attr(not(bootstrap), allow(internal_features))] +#![cfg_attr(not(bootstrap), feature(rustdoc_internals))] +#![cfg_attr(not(bootstrap), doc(rust_logo))] #![feature(box_patterns)] #![feature(let_chains)] #![feature(never_type)] @@ -40,7 +43,7 @@ #[macro_use] extern crate tracing; -use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait, TraitFnAsync}; +use crate::errors::{AssocTyParentheses, AssocTyParenthesesSub, MisplacedImplTrait}; use rustc_ast::ptr::P; use rustc_ast::visit; @@ -68,9 +71,8 @@ use rustc_middle::{ }; use rustc_session::parse::{add_feature_diagnostics, feature_err}; use rustc_span::hygiene::MacroKind; -use rustc_span::source_map::DesugaringKind; use rustc_span::symbol::{kw, sym, Ident, Symbol}; -use rustc_span::{Span, DUMMY_SP}; +use rustc_span::{DesugaringKind, Span, DUMMY_SP}; use smallvec::SmallVec; use std::collections::hash_map::Entry; use thin_vec::ThinVec; @@ -108,10 +110,10 @@ struct LoweringContext<'a, 'hir> { /// Collect items that were created by lowering the current owner. children: Vec<(LocalDefId, hir::MaybeOwner<&'hir hir::OwnerInfo<'hir>>)>, - generator_kind: Option, + coroutine_kind: Option, /// When inside an `async` context, this is the `HirId` of the - /// `task_context` local bound to the resume argument of the generator. + /// `task_context` local bound to the resume argument of the coroutine. task_context: Option, /// Used to get the current `fn`'s def span to point to when using `await` @@ -271,8 +273,6 @@ enum ImplTraitPosition { ClosureReturn, PointerReturn, FnTraitReturn, - TraitReturn, - ImplReturn, GenericDefault, ConstTy, StaticTy, @@ -302,8 +302,6 @@ impl std::fmt::Display for ImplTraitPosition { ImplTraitPosition::ClosureReturn => "closure return types", ImplTraitPosition::PointerReturn => "`fn` pointer return types", ImplTraitPosition::FnTraitReturn => "`Fn` trait return types", - ImplTraitPosition::TraitReturn => "trait method return types", - ImplTraitPosition::ImplReturn => "`impl` method return types", ImplTraitPosition::GenericDefault => "generic parameter defaults", ImplTraitPosition::ConstTy => "const types", ImplTraitPosition::StaticTy => "static types", @@ -334,20 +332,9 @@ impl FnDeclKind { matches!(self, FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait) } - fn return_impl_trait_allowed(&self, tcx: TyCtxt<'_>) -> bool { + fn return_impl_trait_allowed(&self) -> bool { match self { - FnDeclKind::Fn | FnDeclKind::Inherent => true, - FnDeclKind::Impl if tcx.features().return_position_impl_trait_in_trait => true, - FnDeclKind::Trait if tcx.features().return_position_impl_trait_in_trait => true, - _ => false, - } - } - - fn async_fn_allowed(&self, tcx: TyCtxt<'_>) -> bool { - match self { - FnDeclKind::Fn | FnDeclKind::Inherent => true, - FnDeclKind::Impl if tcx.features().async_fn_in_trait => true, - FnDeclKind::Trait if tcx.features().async_fn_in_trait => true, + FnDeclKind::Fn | FnDeclKind::Inherent | FnDeclKind::Impl | FnDeclKind::Trait => true, _ => false, } } @@ -1229,7 +1216,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { hir_id: this.lower_node_id(node_id), body: this.lower_const_body(path_expr.span, Some(&path_expr)), }); - return GenericArg::Const(ConstArg { value: ct, span }); + return GenericArg::Const(ConstArg { + value: ct, + span, + is_desugared_from_effects: false, + }); } } } @@ -1240,6 +1231,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ast::GenericArg::Const(ct) => GenericArg::Const(ConstArg { value: self.lower_anon_const(&ct), span: self.lower_span(ct.value.span), + is_desugared_from_effects: false, }), } } @@ -1271,7 +1263,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { &PolyTraitRef { bound_generic_params: ThinVec::new(), trait_ref: TraitRef { path: path.clone(), ref_id: t.id }, - span: t.span + span: t.span, }, itctx, ast::Const::No, @@ -1749,14 +1741,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } fn lower_fn_params_to_names(&mut self, decl: &FnDecl) -> &'hir [Ident] { - // Skip the `...` (`CVarArgs`) trailing arguments from the AST, - // as they are not explicit in HIR/Ty function signatures. - // (instead, the `c_variadic` flag is set to `true`) - let mut inputs = &decl.inputs[..]; - if decl.c_variadic() { - inputs = &inputs[..inputs.len() - 1]; - } - self.arena.alloc_from_iter(inputs.iter().map(|param| match param.pat.kind { + self.arena.alloc_from_iter(decl.inputs.iter().map(|param| match param.pat.kind { PatKind::Ident(_, ident, _) => self.lower_ident(ident), _ => Ident::new(kw::Empty, self.lower_span(param.pat.span)), })) @@ -1805,53 +1790,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { self.lower_ty_direct(¶m.ty, &itctx) })); - let output = if let Some((ret_id, span)) = make_ret_async { - if !kind.async_fn_allowed(self.tcx) { - match kind { - FnDeclKind::Trait | FnDeclKind::Impl => { - self.tcx - .sess - .create_feature_err( - TraitFnAsync { fn_span, span }, - sym::async_fn_in_trait, - ) - .emit(); - } - _ => { - self.tcx.sess.emit_err(TraitFnAsync { fn_span, span }); - } - } - } - + let output = if let Some((ret_id, _span)) = make_ret_async { let fn_def_id = self.local_def_id(fn_node_id); - self.lower_async_fn_ret_ty(&decl.output, fn_def_id, ret_id, kind) + self.lower_async_fn_ret_ty(&decl.output, fn_def_id, ret_id, kind, fn_span) } else { match &decl.output { FnRetTy::Ty(ty) => { - let context = if kind.return_impl_trait_allowed(self.tcx) { + let context = if kind.return_impl_trait_allowed() { let fn_def_id = self.local_def_id(fn_node_id); ImplTraitContext::ReturnPositionOpaqueTy { origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), fn_kind: kind, } } else { - let position = match kind { - FnDeclKind::Fn | FnDeclKind::Inherent => { - unreachable!("fn should allow in-band lifetimes") + ImplTraitContext::Disallowed(match kind { + FnDeclKind::Fn + | FnDeclKind::Inherent + | FnDeclKind::Trait + | FnDeclKind::Impl => { + unreachable!("fn should allow return-position impl trait in traits") } FnDeclKind::ExternFn => ImplTraitPosition::ExternFnReturn, FnDeclKind::Closure => ImplTraitPosition::ClosureReturn, FnDeclKind::Pointer => ImplTraitPosition::PointerReturn, - FnDeclKind::Trait => ImplTraitPosition::TraitReturn, - FnDeclKind::Impl => ImplTraitPosition::ImplReturn, - }; - match kind { - FnDeclKind::Trait | FnDeclKind::Impl => ImplTraitContext::FeatureGated( - position, - sym::return_position_impl_trait_in_trait, - ), - _ => ImplTraitContext::Disallowed(position), - } + }) }; hir::FnRetTy::Return(self.lower_ty(ty, &context)) } @@ -1901,8 +1863,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { fn_def_id: LocalDefId, opaque_ty_node_id: NodeId, fn_kind: FnDeclKind, + fn_span: Span, ) -> hir::FnRetTy<'hir> { - let span = self.lower_span(output.span()); + let span = self.lower_span(fn_span); let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::Async, span, None); let captured_lifetimes: Vec<_> = self @@ -1923,18 +1886,9 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let future_bound = this.lower_async_fn_output_type_to_future_bound( output, span, - if let FnDeclKind::Trait = fn_kind - && !this.tcx.features().return_position_impl_trait_in_trait - { - ImplTraitContext::FeatureGated( - ImplTraitPosition::TraitReturn, - sym::return_position_impl_trait_in_trait, - ) - } else { - ImplTraitContext::ReturnPositionOpaqueTy { - origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), - fn_kind, - } + ImplTraitContext::ReturnPositionOpaqueTy { + origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), + fn_kind, }, ); arena_vec![this; future_bound] @@ -2568,6 +2522,7 @@ impl<'hir> GenericArgsCtor<'hir> { self.args.push(hir::GenericArg::Const(hir::ConstArg { value: hir::AnonConst { def_id, hir_id, body }, span, + is_desugared_from_effects: true, })) } diff --git a/compiler/rustc_ast_lowering/src/lifetime_collector.rs b/compiler/rustc_ast_lowering/src/lifetime_collector.rs index 6f75419c3..d66bba517 100644 --- a/compiler/rustc_ast_lowering/src/lifetime_collector.rs +++ b/compiler/rustc_ast_lowering/src/lifetime_collector.rs @@ -82,7 +82,8 @@ impl<'ast> Visitor<'ast> for LifetimeCollectVisitor<'ast> { // We can sometimes encounter bare trait objects // which are represented in AST as paths. if let Some(partial_res) = self.resolver.get_partial_res(t.id) - && let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res() + && let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = + partial_res.full_res() { self.current_binders.push(t.id); visit::walk_ty(self, t); -- cgit v1.2.3