diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:11:38 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-17 12:12:43 +0000 |
commit | cf94bdc0742c13e2a0cac864c478b8626b266e1b (patch) | |
tree | 044670aa50cc5e2b4229aa0b6b3df6676730c0a6 /compiler/rustc_ast_lowering/src | |
parent | Adding debian version 1.65.0+dfsg1-2. (diff) | |
download | rustc-cf94bdc0742c13e2a0cac864c478b8626b266e1b.tar.xz rustc-cf94bdc0742c13e2a0cac864c478b8626b266e1b.zip |
Merging upstream version 1.66.0+dfsg1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compiler/rustc_ast_lowering/src')
-rw-r--r-- | compiler/rustc_ast_lowering/src/asm.rs | 23 | ||||
-rw-r--r-- | compiler/rustc_ast_lowering/src/errors.rs | 236 | ||||
-rw-r--r-- | compiler/rustc_ast_lowering/src/expr.rs | 87 | ||||
-rw-r--r-- | compiler/rustc_ast_lowering/src/index.rs | 22 | ||||
-rw-r--r-- | compiler/rustc_ast_lowering/src/item.rs | 99 | ||||
-rw-r--r-- | compiler/rustc_ast_lowering/src/lib.rs | 158 | ||||
-rw-r--r-- | compiler/rustc_ast_lowering/src/pat.rs | 2 | ||||
-rw-r--r-- | compiler/rustc_ast_lowering/src/path.rs | 10 |
8 files changed, 340 insertions, 297 deletions
diff --git a/compiler/rustc_ast_lowering/src/asm.rs b/compiler/rustc_ast_lowering/src/asm.rs index 24672efc6..450cdf246 100644 --- a/compiler/rustc_ast_lowering/src/asm.rs +++ b/compiler/rustc_ast_lowering/src/asm.rs @@ -192,26 +192,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } } InlineAsmOperand::Sym { ref sym } => { - if !self.tcx.features().asm_sym { - feature_err( - &sess.parse_sess, - sym::asm_sym, - *op_sp, - "sym operands for inline assembly are unstable", - ) - .emit(); - } - let static_def_id = self .resolver .get_partial_res(sym.id) - .filter(|res| res.unresolved_segments() == 0) - .and_then(|res| { - if let Res::Def(DefKind::Static(_), def_id) = res.base_res() { - Some(def_id) - } else { - None - } + .and_then(|res| res.full_res()) + .and_then(|res| match res { + Res::Def(DefKind::Static(_), def_id) => Some(def_id), + _ => None, }); if let Some(def_id) = static_def_id { @@ -237,7 +224,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Wrap the expression in an AnonConst. let parent_def_id = self.current_hir_id_owner; let node_id = self.next_node_id(); - self.create_def(parent_def_id, node_id, DefPathData::AnonConst); + self.create_def(parent_def_id.def_id, node_id, DefPathData::AnonConst); let anon_const = AnonConst { id: node_id, value: P(expr) }; hir::InlineAsmOperand::SymFn { anon_const: self.lower_anon_const(&anon_const), diff --git a/compiler/rustc_ast_lowering/src/errors.rs b/compiler/rustc_ast_lowering/src/errors.rs index c87d0ca96..157f46501 100644 --- a/compiler/rustc_ast_lowering/src/errors.rs +++ b/compiler/rustc_ast_lowering/src/errors.rs @@ -1,9 +1,9 @@ -use rustc_errors::{fluent, AddSubdiagnostic, Applicability, Diagnostic, DiagnosticArgFromDisplay}; -use rustc_macros::{SessionDiagnostic, SessionSubdiagnostic}; +use rustc_errors::DiagnosticArgFromDisplay; +use rustc_macros::{Diagnostic, Subdiagnostic}; use rustc_span::{symbol::Ident, Span, Symbol}; -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::generic_type_with_parentheses, code = "E0214")] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_generic_type_with_parentheses, code = "E0214")] pub struct GenericTypeWithParentheses { #[primary_span] #[label] @@ -12,35 +12,42 @@ pub struct GenericTypeWithParentheses { pub sub: Option<UseAngleBrackets>, } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Subdiagnostic)] +#[multipart_suggestion(ast_lowering_use_angle_brackets, applicability = "maybe-incorrect")] pub struct UseAngleBrackets { + #[suggestion_part(code = "<")] pub open_param: Span, + #[suggestion_part(code = ">")] pub close_param: Span, } -impl AddSubdiagnostic for UseAngleBrackets { - fn add_to_diagnostic(self, diag: &mut Diagnostic) { - diag.multipart_suggestion( - fluent::ast_lowering::use_angle_brackets, - vec![(self.open_param, String::from("<")), (self.close_param, String::from(">"))], - Applicability::MaybeIncorrect, - ); - } -} - -#[derive(SessionDiagnostic)] -#[help] -#[diag(ast_lowering::invalid_abi, code = "E0703")] +#[derive(Diagnostic)] +#[diag(ast_lowering_invalid_abi, code = "E0703")] +#[note] pub struct InvalidAbi { #[primary_span] #[label] pub span: Span, pub abi: Symbol, - pub valid_abis: String, + pub command: String, + #[subdiagnostic] + pub suggestion: Option<InvalidAbiSuggestion>, +} + +#[derive(Subdiagnostic)] +#[suggestion( + ast_lowering_invalid_abi_suggestion, + code = "{suggestion}", + applicability = "maybe-incorrect" +)] +pub struct InvalidAbiSuggestion { + #[primary_span] + pub span: Span, + pub suggestion: String, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::assoc_ty_parentheses)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_assoc_ty_parentheses)] pub struct AssocTyParentheses { #[primary_span] pub span: Span, @@ -48,123 +55,116 @@ pub struct AssocTyParentheses { pub sub: AssocTyParenthesesSub, } -#[derive(Clone, Copy)] +#[derive(Clone, Copy, Subdiagnostic)] pub enum AssocTyParenthesesSub { - Empty { parentheses_span: Span }, - NotEmpty { open_param: Span, close_param: Span }, -} - -impl AddSubdiagnostic for AssocTyParenthesesSub { - fn add_to_diagnostic(self, diag: &mut Diagnostic) { - match self { - Self::Empty { parentheses_span } => diag.multipart_suggestion( - fluent::ast_lowering::remove_parentheses, - vec![(parentheses_span, String::new())], - Applicability::MaybeIncorrect, - ), - Self::NotEmpty { open_param, close_param } => diag.multipart_suggestion( - fluent::ast_lowering::use_angle_brackets, - vec![(open_param, String::from("<")), (close_param, String::from(">"))], - Applicability::MaybeIncorrect, - ), - }; - } -} - -#[derive(SessionDiagnostic)] -#[diag(ast_lowering::misplaced_impl_trait, code = "E0562")] + #[multipart_suggestion(ast_lowering_remove_parentheses)] + Empty { + #[suggestion_part(code = "")] + parentheses_span: Span, + }, + #[multipart_suggestion(ast_lowering_use_angle_brackets)] + NotEmpty { + #[suggestion_part(code = "<")] + open_param: Span, + #[suggestion_part(code = ">")] + close_param: Span, + }, +} + +#[derive(Diagnostic)] +#[diag(ast_lowering_misplaced_impl_trait, code = "E0562")] pub struct MisplacedImplTrait<'a> { #[primary_span] pub span: Span, pub position: DiagnosticArgFromDisplay<'a>, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::rustc_box_attribute_error)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_rustc_box_attribute_error)] pub struct RustcBoxAttributeError { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::underscore_expr_lhs_assign)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_underscore_expr_lhs_assign)] pub struct UnderscoreExprLhsAssign { #[primary_span] #[label] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::base_expression_double_dot)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_base_expression_double_dot)] pub struct BaseExpressionDoubleDot { #[primary_span] #[label] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::await_only_in_async_fn_and_blocks, code = "E0728")] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_await_only_in_async_fn_and_blocks, code = "E0728")] pub struct AwaitOnlyInAsyncFnAndBlocks { #[primary_span] #[label] pub dot_await_span: Span, - #[label(ast_lowering::this_not_async)] + #[label(ast_lowering_this_not_async)] pub item_span: Option<Span>, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::generator_too_many_parameters, code = "E0628")] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_generator_too_many_parameters, code = "E0628")] pub struct GeneratorTooManyParameters { #[primary_span] pub fn_decl_span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::closure_cannot_be_static, code = "E0697")] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_closure_cannot_be_static, code = "E0697")] pub struct ClosureCannotBeStatic { #[primary_span] pub fn_decl_span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] +#[derive(Diagnostic, Clone, Copy)] #[help] -#[diag(ast_lowering::async_non_move_closure_not_supported, code = "E0708")] +#[diag(ast_lowering_async_non_move_closure_not_supported, code = "E0708")] pub struct AsyncNonMoveClosureNotSupported { #[primary_span] pub fn_decl_span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::functional_record_update_destructuring_assignment)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_functional_record_update_destructuring_assignment)] pub struct FunctionalRecordUpdateDestructuringAssignemnt { #[primary_span] #[suggestion(code = "", applicability = "machine-applicable")] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::async_generators_not_supported, code = "E0727")] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_async_generators_not_supported, code = "E0727")] pub struct AsyncGeneratorsNotSupported { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::inline_asm_unsupported_target, code = "E0472")] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_inline_asm_unsupported_target, code = "E0472")] pub struct InlineAsmUnsupportedTarget { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::att_syntax_only_x86)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_att_syntax_only_x86)] pub struct AttSyntaxOnlyX86 { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::abi_specified_multiple_times)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_abi_specified_multiple_times)] pub struct AbiSpecifiedMultipleTimes { #[primary_span] pub abi_span: Span, @@ -175,24 +175,24 @@ pub struct AbiSpecifiedMultipleTimes { pub equivalent: Option<()>, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::clobber_abi_not_supported)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_clobber_abi_not_supported)] pub struct ClobberAbiNotSupported { #[primary_span] pub abi_span: Span, } -#[derive(SessionDiagnostic)] +#[derive(Diagnostic)] #[note] -#[diag(ast_lowering::invalid_abi_clobber_abi)] +#[diag(ast_lowering_invalid_abi_clobber_abi)] pub struct InvalidAbiClobberAbi { #[primary_span] pub abi_span: Span, pub supported_abis: String, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::invalid_register)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_invalid_register)] pub struct InvalidRegister<'a> { #[primary_span] pub op_span: Span, @@ -200,8 +200,8 @@ pub struct InvalidRegister<'a> { pub error: &'a str, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::invalid_register_class)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_invalid_register_class)] pub struct InvalidRegisterClass<'a> { #[primary_span] pub op_span: Span, @@ -209,61 +209,61 @@ pub struct InvalidRegisterClass<'a> { pub error: &'a str, } -#[derive(SessionDiagnostic)] -#[diag(ast_lowering::invalid_asm_template_modifier_reg_class)] +#[derive(Diagnostic)] +#[diag(ast_lowering_invalid_asm_template_modifier_reg_class)] pub struct InvalidAsmTemplateModifierRegClass { #[primary_span] - #[label(ast_lowering::template_modifier)] + #[label(ast_lowering_template_modifier)] pub placeholder_span: Span, - #[label(ast_lowering::argument)] + #[label(ast_lowering_argument)] pub op_span: Span, #[subdiagnostic] pub sub: InvalidAsmTemplateModifierRegClassSub, } -#[derive(SessionSubdiagnostic)] +#[derive(Subdiagnostic)] pub enum InvalidAsmTemplateModifierRegClassSub { - #[note(ast_lowering::support_modifiers)] + #[note(ast_lowering_support_modifiers)] SupportModifier { class_name: Symbol, modifiers: String }, - #[note(ast_lowering::does_not_support_modifiers)] + #[note(ast_lowering_does_not_support_modifiers)] DoesNotSupportModifier { class_name: Symbol }, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::invalid_asm_template_modifier_const)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_invalid_asm_template_modifier_const)] pub struct InvalidAsmTemplateModifierConst { #[primary_span] - #[label(ast_lowering::template_modifier)] + #[label(ast_lowering_template_modifier)] pub placeholder_span: Span, - #[label(ast_lowering::argument)] + #[label(ast_lowering_argument)] pub op_span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::invalid_asm_template_modifier_sym)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_invalid_asm_template_modifier_sym)] pub struct InvalidAsmTemplateModifierSym { #[primary_span] - #[label(ast_lowering::template_modifier)] + #[label(ast_lowering_template_modifier)] pub placeholder_span: Span, - #[label(ast_lowering::argument)] + #[label(ast_lowering_argument)] pub op_span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::register_class_only_clobber)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_register_class_only_clobber)] pub struct RegisterClassOnlyClobber { #[primary_span] pub op_span: Span, pub reg_class_name: Symbol, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::register_conflict)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_register_conflict)] pub struct RegisterConflict<'a> { #[primary_span] - #[label(ast_lowering::register1)] + #[label(ast_lowering_register1)] pub op_span1: Span, - #[label(ast_lowering::register2)] + #[label(ast_lowering_register2)] pub op_span2: Span, pub reg1_name: &'a str, pub reg2_name: &'a str, @@ -271,14 +271,14 @@ pub struct RegisterConflict<'a> { pub in_out: Option<Span>, } -#[derive(SessionDiagnostic, Clone, Copy)] +#[derive(Diagnostic, Clone, Copy)] #[help] -#[diag(ast_lowering::sub_tuple_binding)] +#[diag(ast_lowering_sub_tuple_binding)] pub struct SubTupleBinding<'a> { #[primary_span] #[label] #[suggestion_verbose( - ast_lowering::sub_tuple_binding_suggestion, + ast_lowering_sub_tuple_binding_suggestion, code = "..", applicability = "maybe-incorrect" )] @@ -288,57 +288,57 @@ pub struct SubTupleBinding<'a> { pub ctx: &'a str, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::extra_double_dot)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_extra_double_dot)] pub struct ExtraDoubleDot<'a> { #[primary_span] #[label] pub span: Span, - #[label(ast_lowering::previously_used_here)] + #[label(ast_lowering_previously_used_here)] pub prev_span: Span, pub ctx: &'a str, } -#[derive(SessionDiagnostic, Clone, Copy)] +#[derive(Diagnostic, Clone, Copy)] #[note] -#[diag(ast_lowering::misplaced_double_dot)] +#[diag(ast_lowering_misplaced_double_dot)] pub struct MisplacedDoubleDot { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::misplaced_relax_trait_bound)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_misplaced_relax_trait_bound)] pub struct MisplacedRelaxTraitBound { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::not_supported_for_lifetime_binder_async_closure)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_not_supported_for_lifetime_binder_async_closure)] pub struct NotSupportedForLifetimeBinderAsyncClosure { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::arbitrary_expression_in_pattern)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_arbitrary_expression_in_pattern)] pub struct ArbitraryExpressionInPattern { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::inclusive_range_with_no_end)] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_inclusive_range_with_no_end)] pub struct InclusiveRangeWithNoEnd { #[primary_span] pub span: Span, } -#[derive(SessionDiagnostic, Clone, Copy)] -#[diag(ast_lowering::trait_fn_async, code = "E0706")] +#[derive(Diagnostic, Clone, Copy)] +#[diag(ast_lowering_trait_fn_async, code = "E0706")] #[note] -#[note(ast_lowering::note2)] +#[note(note2)] pub struct TraitFnAsync { #[primary_span] pub fn_span: Span, diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index 7b8070d3c..ec9c39350 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -60,7 +60,7 @@ impl<'hir> LoweringContext<'_, 'hir> { hir::ExprKind::Call(f, self.lower_exprs(args)) } } - ExprKind::MethodCall(ref seg, ref args, span) => { + ExprKind::MethodCall(ref seg, ref receiver, ref args, span) => { let hir_seg = self.arena.alloc(self.lower_path_segment( e.span, seg, @@ -68,9 +68,9 @@ impl<'hir> LoweringContext<'_, 'hir> { ParenthesizedGenericArgs::Err, &ImplTraitContext::Disallowed(ImplTraitPosition::Path), )); - let receiver = self.lower_expr(&args[0]); + let receiver = self.lower_expr(receiver); let args = - self.arena.alloc_from_iter(args[1..].iter().map(|x| self.lower_expr_mut(x))); + self.arena.alloc_from_iter(args.iter().map(|x| self.lower_expr_mut(x))); hir::ExprKind::MethodCall(hir_seg, receiver, args, self.lower_span(span)) } ExprKind::Binary(binop, ref lhs, ref rhs) => { @@ -359,7 +359,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let node_id = self.next_node_id(); // Add a definition for the in-band const def. - self.create_def(parent_def_id, node_id, DefPathData::AnonConst); + self.create_def(parent_def_id.def_id, node_id, DefPathData::AnonConst); let anon_const = AnonConst { id: node_id, value: arg }; generic_args.push(AngleBracketedArg::Arg(GenericArg::Const(anon_const))); @@ -387,32 +387,58 @@ impl<'hir> LoweringContext<'_, 'hir> { then: &Block, else_opt: Option<&Expr>, ) -> hir::ExprKind<'hir> { - let lowered_cond = self.lower_expr(cond); - let new_cond = self.manage_let_cond(lowered_cond); + let lowered_cond = self.lower_cond(cond); let then_expr = self.lower_block_expr(then); if let Some(rslt) = else_opt { - hir::ExprKind::If(new_cond, self.arena.alloc(then_expr), Some(self.lower_expr(rslt))) + hir::ExprKind::If( + lowered_cond, + self.arena.alloc(then_expr), + Some(self.lower_expr(rslt)), + ) } else { - hir::ExprKind::If(new_cond, self.arena.alloc(then_expr), None) + hir::ExprKind::If(lowered_cond, self.arena.alloc(then_expr), None) } } - // If `cond` kind is `let`, returns `let`. Otherwise, wraps and returns `cond` - // in a temporary block. - fn manage_let_cond(&mut self, cond: &'hir hir::Expr<'hir>) -> &'hir hir::Expr<'hir> { - fn has_let_expr<'hir>(expr: &'hir hir::Expr<'hir>) -> bool { - match expr.kind { - hir::ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs), - hir::ExprKind::Let(..) => true, + // Lowers a condition (i.e. `cond` in `if cond` or `while cond`), wrapping it in a terminating scope + // so that temporaries created in the condition don't live beyond it. + fn lower_cond(&mut self, cond: &Expr) -> &'hir hir::Expr<'hir> { + fn has_let_expr(expr: &Expr) -> bool { + match &expr.kind { + ExprKind::Binary(_, lhs, rhs) => has_let_expr(lhs) || has_let_expr(rhs), + ExprKind::Let(..) => true, _ => false, } } - if has_let_expr(cond) { - cond - } else { - let reason = DesugaringKind::CondTemporary; - let span_block = self.mark_span_with_reason(reason, cond.span, None); - self.expr_drop_temps(span_block, cond, AttrVec::new()) + + // We have to take special care for `let` exprs in the condition, e.g. in + // `if let pat = val` or `if foo && let pat = val`, as we _do_ want `val` to live beyond the + // condition in this case. + // + // In order to mantain the drop behavior for the non `let` parts of the condition, + // we still wrap them in terminating scopes, e.g. `if foo && let pat = val` essentially + // gets transformed into `if { let _t = foo; _t } && let pat = val` + match &cond.kind { + ExprKind::Binary(op @ Spanned { node: ast::BinOpKind::And, .. }, lhs, rhs) + if has_let_expr(cond) => + { + let op = self.lower_binop(*op); + let lhs = self.lower_cond(lhs); + let rhs = self.lower_cond(rhs); + + self.arena.alloc(self.expr( + cond.span, + hir::ExprKind::Binary(op, lhs, rhs), + AttrVec::new(), + )) + } + ExprKind::Let(..) => self.lower_expr(cond), + _ => { + let cond = self.lower_expr(cond); + let reason = DesugaringKind::CondTemporary; + let span_block = self.mark_span_with_reason(reason, cond.span, None); + self.expr_drop_temps(span_block, cond, AttrVec::new()) + } } } @@ -439,14 +465,13 @@ impl<'hir> LoweringContext<'_, 'hir> { body: &Block, opt_label: Option<Label>, ) -> hir::ExprKind<'hir> { - let lowered_cond = self.with_loop_condition_scope(|t| t.lower_expr(cond)); - let new_cond = self.manage_let_cond(lowered_cond); + let lowered_cond = self.with_loop_condition_scope(|t| t.lower_cond(cond)); let then = self.lower_block_expr(body); let expr_break = self.expr_break(span, AttrVec::new()); let stmt_break = self.stmt_expr(span, expr_break); let else_blk = self.block_all(span, arena_vec![self; stmt_break], None); let else_expr = self.arena.alloc(self.expr_block(else_blk, AttrVec::new())); - let if_kind = hir::ExprKind::If(new_cond, self.arena.alloc(then), Some(else_expr)); + let if_kind = hir::ExprKind::If(lowered_cond, self.arena.alloc(then), Some(else_expr)); let if_expr = self.expr(span, if_kind, AttrVec::new()); let block = self.block_expr(self.arena.alloc(if_expr)); let span = self.lower_span(span.with_hi(cond.span.hi())); @@ -1044,9 +1069,7 @@ 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 partial_res.unresolved_segments() == 0 - && !partial_res.base_res().expected_in_tuple_struct_pat() - { + if let Some(res) = partial_res.full_res() && !res.expected_in_tuple_struct_pat() { return None; } } @@ -1066,9 +1089,7 @@ 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 partial_res.unresolved_segments() == 0 - && !partial_res.base_res().expected_in_unit_struct_pat() - { + if let Some(res) = partial_res.full_res() && !res.expected_in_unit_struct_pat() { return None; } } @@ -1609,11 +1630,11 @@ impl<'hir> LoweringContext<'_, 'hir> { } /// Desugar `ExprKind::Yeet` from: `do yeet <expr>` into: - /// ```rust + /// ```ignore(illustrative) /// // If there is an enclosing `try {...}`: - /// break 'catch_target FromResidual::from_residual(Yeet(residual)), + /// break 'catch_target FromResidual::from_residual(Yeet(residual)); /// // Otherwise: - /// return FromResidual::from_residual(Yeet(residual)), + /// return FromResidual::from_residual(Yeet(residual)); /// ``` /// But to simplify this, there's a `from_yeet` lang item function which /// handles the combined `FromResidual::from_residual(Yeet(residual))`. diff --git a/compiler/rustc_ast_lowering/src/index.rs b/compiler/rustc_ast_lowering/src/index.rs index 85846b567..f1851d7b4 100644 --- a/compiler/rustc_ast_lowering/src/index.rs +++ b/compiler/rustc_ast_lowering/src/index.rs @@ -24,7 +24,7 @@ pub(super) struct NodeCollector<'a, 'hir> { /// The parent of this node parent_node: hir::ItemLocalId, - owner: LocalDefId, + owner: OwnerId, definitions: &'a definitions::Definitions, } @@ -81,9 +81,9 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> { current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})", self.source_map.span_to_diagnostic_string(span), node, - self.definitions.def_path(self.owner).to_string_no_crate_verbose(), + self.definitions.def_path(self.owner.def_id).to_string_no_crate_verbose(), self.owner, - self.definitions.def_path(hir_id.owner).to_string_no_crate_verbose(), + self.definitions.def_path(hir_id.owner.def_id).to_string_no_crate_verbose(), hir_id.owner, ) } @@ -112,19 +112,19 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { fn visit_nested_item(&mut self, item: ItemId) { debug!("visit_nested_item: {:?}", item); - self.insert_nested(item.def_id); + self.insert_nested(item.owner_id.def_id); } fn visit_nested_trait_item(&mut self, item_id: TraitItemId) { - self.insert_nested(item_id.def_id); + self.insert_nested(item_id.owner_id.def_id); } fn visit_nested_impl_item(&mut self, item_id: ImplItemId) { - self.insert_nested(item_id.def_id); + self.insert_nested(item_id.owner_id.def_id); } fn visit_nested_foreign_item(&mut self, foreign_id: ForeignItemId) { - self.insert_nested(foreign_id.def_id); + self.insert_nested(foreign_id.owner_id.def_id); } fn visit_nested_body(&mut self, id: BodyId) { @@ -143,7 +143,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { #[instrument(level = "debug", skip(self))] fn visit_item(&mut self, i: &'hir Item<'hir>) { - debug_assert_eq!(i.def_id, self.owner); + debug_assert_eq!(i.owner_id, self.owner); self.with_parent(i.hir_id(), |this| { if let ItemKind::Struct(ref struct_def, _) = i.kind { // If this is a tuple or unit-like struct, register the constructor. @@ -157,7 +157,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { #[instrument(level = "debug", skip(self))] fn visit_foreign_item(&mut self, fi: &'hir ForeignItem<'hir>) { - debug_assert_eq!(fi.def_id, self.owner); + debug_assert_eq!(fi.owner_id, self.owner); self.with_parent(fi.hir_id(), |this| { intravisit::walk_foreign_item(this, fi); }); @@ -176,7 +176,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { #[instrument(level = "debug", skip(self))] fn visit_trait_item(&mut self, ti: &'hir TraitItem<'hir>) { - debug_assert_eq!(ti.def_id, self.owner); + debug_assert_eq!(ti.owner_id, self.owner); self.with_parent(ti.hir_id(), |this| { intravisit::walk_trait_item(this, ti); }); @@ -184,7 +184,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> { #[instrument(level = "debug", skip(self))] fn visit_impl_item(&mut self, ii: &'hir ImplItem<'hir>) { - debug_assert_eq!(ii.def_id, self.owner); + debug_assert_eq!(ii.owner_id, self.owner); self.with_parent(ii.hir_id(), |this| { intravisit::walk_impl_item(this, ii); }); diff --git a/compiler/rustc_ast_lowering/src/item.rs b/compiler/rustc_ast_lowering/src/item.rs index 550833275..76316a574 100644 --- a/compiler/rustc_ast_lowering/src/item.rs +++ b/compiler/rustc_ast_lowering/src/item.rs @@ -1,4 +1,4 @@ -use super::errors::{InvalidAbi, MisplacedRelaxTraitBound}; +use super::errors::{InvalidAbi, InvalidAbiSuggestion, MisplacedRelaxTraitBound}; use super::ResolverAstLoweringExt; use super::{Arena, AstOwner, ImplTraitContext, ImplTraitPosition}; use super::{FnDeclKind, LoweringContext, ParamMode}; @@ -14,9 +14,10 @@ use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; use rustc_hir::PredicateOrigin; use rustc_index::vec::{Idx, IndexVec}; use rustc_middle::ty::{DefIdTree, ResolverAstLowering, TyCtxt}; +use rustc_span::lev_distance::find_best_match_for_name; use rustc_span::source_map::DesugaringKind; use rustc_span::symbol::{kw, sym, Ident}; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; use rustc_target::spec::abi; use smallvec::{smallvec, SmallVec}; @@ -67,7 +68,7 @@ impl<'a, 'hir> ItemLowerer<'a, 'hir> { bodies: Vec::new(), attrs: SortedMap::default(), children: FxHashMap::default(), - current_hir_id_owner: CRATE_DEF_ID, + current_hir_id_owner: hir::CRATE_OWNER_ID, item_local_id_counter: hir::ItemLocalId::new(0), node_id_to_local_id: Default::default(), local_id_to_def_id: SortedMap::new(), @@ -176,7 +177,8 @@ impl<'hir> LoweringContext<'_, 'hir> { } pub(super) fn lower_item_ref(&mut self, i: &Item) -> SmallVec<[hir::ItemId; 1]> { - let mut node_ids = smallvec![hir::ItemId { def_id: self.local_def_id(i.id) }]; + let mut node_ids = + smallvec![hir::ItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } }]; if let ItemKind::Use(ref use_tree) = &i.kind { self.lower_item_id_use_tree(use_tree, i.id, &mut node_ids); } @@ -192,7 +194,9 @@ impl<'hir> LoweringContext<'_, 'hir> { match tree.kind { UseTreeKind::Nested(ref nested_vec) => { for &(ref nested, id) in nested_vec { - vec.push(hir::ItemId { def_id: self.local_def_id(id) }); + vec.push(hir::ItemId { + owner_id: hir::OwnerId { def_id: self.local_def_id(id) }, + }); self.lower_item_id_use_tree(nested, id, vec); } } @@ -201,7 +205,9 @@ impl<'hir> LoweringContext<'_, 'hir> { for (_, &id) in iter::zip(self.expect_full_res_from_use(base_id).skip(1), &[id1, id2]) { - vec.push(hir::ItemId { def_id: self.local_def_id(id) }); + vec.push(hir::ItemId { + owner_id: hir::OwnerId { def_id: self.local_def_id(id) }, + }); } } } @@ -214,7 +220,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let attrs = self.lower_attrs(hir_id, &i.attrs); let kind = self.lower_item_kind(i.span, i.id, hir_id, &mut ident, attrs, vis_span, &i.kind); let item = hir::Item { - def_id: hir_id.expect_owner(), + owner_id: hir_id.expect_owner(), ident: self.lower_ident(ident), kind, vis_span, @@ -539,7 +545,11 @@ impl<'hir> LoweringContext<'_, 'hir> { let ident = *ident; let mut path = path.clone(); for seg in &mut path.segments { - seg.id = self.next_node_id(); + // Give the cloned segment the same resolution information + // as the old one (this is needed for stability checking). + let new_id = self.next_node_id(); + self.resolver.clone_res(seg.id, new_id); + seg.id = new_id; } let span = path.span; @@ -552,7 +562,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } let item = hir::Item { - def_id: new_id, + owner_id: hir::OwnerId { def_id: new_id }, ident: this.lower_ident(ident), kind, vis_span, @@ -608,7 +618,11 @@ impl<'hir> LoweringContext<'_, 'hir> { // Give the segments new node-ids since they are being cloned. for seg in &mut prefix.segments { - seg.id = self.next_node_id(); + // Give the cloned segment the same resolution information + // as the old one (this is needed for stability checking). + let new_id = self.next_node_id(); + self.resolver.clone_res(seg.id, new_id); + seg.id = new_id; } // Each `use` import is an item and thus are owners of the @@ -626,7 +640,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } let item = hir::Item { - def_id: new_hir_id, + owner_id: hir::OwnerId { def_id: new_hir_id }, ident: this.lower_ident(ident), kind, vis_span, @@ -646,10 +660,10 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_foreign_item(&mut self, i: &ForeignItem) -> &'hir hir::ForeignItem<'hir> { let hir_id = self.lower_node_id(i.id); - let def_id = hir_id.expect_owner(); + let owner_id = hir_id.expect_owner(); self.lower_attrs(hir_id, &i.attrs); let item = hir::ForeignItem { - def_id, + owner_id, ident: self.lower_ident(i.ident), kind: match i.kind { ForeignItemKind::Fn(box Fn { ref sig, ref generics, .. }) => { @@ -688,7 +702,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_foreign_item_ref(&mut self, i: &ForeignItem) -> hir::ForeignItemRef { hir::ForeignItemRef { - id: hir::ForeignItemId { def_id: self.local_def_id(i.id) }, + id: hir::ForeignItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } }, ident: self.lower_ident(i.ident), span: self.lower_span(i.span), } @@ -798,7 +812,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ); (generics, hir::TraitItemKind::Fn(sig, hir::TraitFn::Provided(body_id)), true) } - AssocItemKind::TyAlias(box TyAlias { + AssocItemKind::Type(box TyAlias { ref generics, where_clauses, ref bounds, @@ -831,7 +845,7 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_attrs(hir_id, &i.attrs); let item = hir::TraitItem { - def_id: trait_item_def_id, + owner_id: trait_item_def_id, ident: self.lower_ident(i.ident), generics, kind, @@ -844,13 +858,13 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_trait_item_ref(&mut self, i: &AssocItem) -> hir::TraitItemRef { let kind = match &i.kind { AssocItemKind::Const(..) => hir::AssocItemKind::Const, - AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type, + AssocItemKind::Type(..) => hir::AssocItemKind::Type, AssocItemKind::Fn(box Fn { sig, .. }) => { hir::AssocItemKind::Fn { has_self: sig.decl.has_self() } } AssocItemKind::MacCall(..) => unimplemented!(), }; - let id = hir::TraitItemId { def_id: self.local_def_id(i.id) }; + let id = hir::TraitItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } }; hir::TraitItemRef { id, ident: self.lower_ident(i.ident), @@ -892,7 +906,7 @@ impl<'hir> LoweringContext<'_, 'hir> { (generics, hir::ImplItemKind::Fn(sig, body_id)) } - AssocItemKind::TyAlias(box TyAlias { generics, where_clauses, ty, .. }) => { + AssocItemKind::Type(box TyAlias { generics, where_clauses, ty, .. }) => { let mut generics = generics.clone(); add_ty_alias_where_clause(&mut generics, *where_clauses, false); self.lower_generics( @@ -902,11 +916,11 @@ impl<'hir> LoweringContext<'_, 'hir> { |this| match ty { None => { let ty = this.arena.alloc(this.ty(i.span, hir::TyKind::Err)); - hir::ImplItemKind::TyAlias(ty) + hir::ImplItemKind::Type(ty) } Some(ty) => { let ty = this.lower_ty(ty, &ImplTraitContext::TypeAliasesOpaqueTy); - hir::ImplItemKind::TyAlias(ty) + hir::ImplItemKind::Type(ty) } }, ) @@ -917,7 +931,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let hir_id = self.lower_node_id(i.id); self.lower_attrs(hir_id, &i.attrs); let item = hir::ImplItem { - def_id: hir_id.expect_owner(), + owner_id: hir_id.expect_owner(), ident: self.lower_ident(i.ident), generics, kind, @@ -930,18 +944,21 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_impl_item_ref(&mut self, i: &AssocItem) -> hir::ImplItemRef { hir::ImplItemRef { - id: hir::ImplItemId { def_id: self.local_def_id(i.id) }, + id: hir::ImplItemId { owner_id: hir::OwnerId { def_id: self.local_def_id(i.id) } }, ident: self.lower_ident(i.ident), span: self.lower_span(i.span), kind: match &i.kind { AssocItemKind::Const(..) => hir::AssocItemKind::Const, - AssocItemKind::TyAlias(..) => hir::AssocItemKind::Type, + AssocItemKind::Type(..) => hir::AssocItemKind::Type, AssocItemKind::Fn(box Fn { sig, .. }) => { hir::AssocItemKind::Fn { has_self: sig.decl.has_self() } } AssocItemKind::MacCall(..) => unimplemented!(), }, - trait_item_def_id: self.resolver.get_partial_res(i.id).map(|r| r.base_res().def_id()), + trait_item_def_id: self + .resolver + .get_partial_res(i.id) + .map(|r| r.expect_full_res().def_id()), } } @@ -1049,9 +1066,9 @@ impl<'hir> LoweringContext<'_, 'hir> { asyncness: Async, body: Option<&Block>, ) -> hir::BodyId { - let closure_id = match asyncness { - Async::Yes { closure_id, .. } => closure_id, - Async::No => return self.lower_fn_body_block(span, decl, body), + let (closure_id, body) = match (asyncness, body) { + (Async::Yes { closure_id, .. }, Some(body)) => (closure_id, body), + _ => return self.lower_fn_body_block(span, decl, body), }; self.lower_body(|this| { @@ -1193,16 +1210,15 @@ impl<'hir> LoweringContext<'_, 'hir> { parameters.push(new_parameter); } - let body_span = body.map_or(span, |b| b.span); let async_expr = this.make_async_expr( CaptureBy::Value, closure_id, None, - body_span, + body.span, hir::AsyncGeneratorKind::Fn, |this| { // Create a block from the user's function body: - let user_body = this.lower_block_expr_opt(body_span, body); + let user_body = this.lower_block_expr(body); // Transform into `drop-temps { <user-body> }`, an expression: let desugared_span = @@ -1234,7 +1250,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ( this.arena.alloc_from_iter(parameters), - this.expr(body_span, async_expr, AttrVec::new()), + this.expr(body.span, async_expr, AttrVec::new()), ) }) } @@ -1280,10 +1296,19 @@ impl<'hir> LoweringContext<'_, 'hir> { } fn error_on_invalid_abi(&self, abi: StrLit) { + let abi_names = abi::enabled_names(self.tcx.features(), abi.span) + .iter() + .map(|s| Symbol::intern(s)) + .collect::<Vec<_>>(); + let suggested_name = find_best_match_for_name(&abi_names, abi.symbol_unescaped, None); self.tcx.sess.emit_err(InvalidAbi { + abi: abi.symbol_unescaped, span: abi.span, - abi: abi.symbol, - valid_abis: abi::all_names().join(", "), + suggestion: suggested_name.map(|suggested_name| InvalidAbiSuggestion { + span: abi.span, + suggestion: format!("\"{suggested_name}\""), + }), + command: "rustc --print=calling-conventions".to_string(), }); } @@ -1335,9 +1360,9 @@ impl<'hir> LoweringContext<'_, 'hir> { match self .resolver .get_partial_res(bound_pred.bounded_ty.id) - .map(|d| (d.base_res(), d.unresolved_segments())) + .and_then(|r| r.full_res()) { - Some((Res::Def(DefKind::TyParam, def_id), 0)) + Some(Res::Def(DefKind::TyParam, def_id)) if bound_pred.bound_generic_params.is_empty() => { generics @@ -1464,6 +1489,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let bounded_ty = self.ty_path(ty_id, param_span, hir::QPath::Resolved(None, ty_path)); Some(hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate { + hir_id: self.next_id(), bounded_ty: self.arena.alloc(bounded_ty), bounds, span, @@ -1494,6 +1520,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ref bounds, span, }) => hir::WherePredicate::BoundPredicate(hir::WhereBoundPredicate { + hir_id: self.next_id(), bound_generic_params: self.lower_generic_params(bound_generic_params), bounded_ty: self .lower_ty(bounded_ty, &ImplTraitContext::Disallowed(ImplTraitPosition::Type)), diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 9012aa704..ff29d15f1 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -32,7 +32,6 @@ #![feature(box_patterns)] #![feature(let_chains)] -#![cfg_attr(bootstrap, feature(let_else))] #![feature(never_type)] #![recursion_limit = "256"] #![allow(rustc::potential_query_instability)] @@ -62,8 +61,8 @@ use rustc_hir::def_id::{LocalDefId, CRATE_DEF_ID}; use rustc_hir::definitions::DefPathData; use rustc_hir::{ConstArg, GenericArg, ItemLocalId, ParamName, TraitCandidate}; use rustc_index::vec::{Idx, IndexVec}; -use rustc_middle::span_bug; use rustc_middle::ty::{ResolverAstLowering, TyCtxt}; +use rustc_middle::{bug, span_bug}; use rustc_session::parse::feature_err; use rustc_span::hygiene::MacroKind; use rustc_span::source_map::DesugaringKind; @@ -126,7 +125,7 @@ struct LoweringContext<'a, 'hir> { is_in_trait_impl: bool, is_in_dyn_type: bool, - current_hir_id_owner: LocalDefId, + current_hir_id_owner: hir::OwnerId, item_local_id_counter: hir::ItemLocalId, local_id_to_def_id: SortedMap<ItemLocalId, LocalDefId>, trait_map: FxHashMap<ItemLocalId, Box<[TraitCandidate]>>, @@ -161,6 +160,10 @@ trait ResolverAstLoweringExt { fn legacy_const_generic_args(&self, expr: &Expr) -> Option<Vec<usize>>; fn get_partial_res(&self, id: NodeId) -> Option<PartialRes>; fn get_import_res(&self, id: NodeId) -> PerNS<Option<Res<NodeId>>>; + // Clones the resolution (if any) on 'source' and applies it + // to 'target'. Used when desugaring a `UseTreeKind::Nested` to + // multiple `UseTreeKind::Simple`s + fn clone_res(&mut self, source: NodeId, target: NodeId); fn get_label_res(&self, id: NodeId) -> Option<NodeId>; fn get_lifetime_res(&self, id: NodeId) -> Option<LifetimeRes>; fn take_extra_lifetime_params(&mut self, id: NodeId) -> Vec<(Ident, NodeId, LifetimeRes)>; @@ -176,12 +179,7 @@ impl ResolverAstLoweringExt for ResolverAstLowering { return None; } - let partial_res = self.partial_res_map.get(&expr.id)?; - if partial_res.unresolved_segments() != 0 { - return None; - } - - if let Res::Def(DefKind::Fn, def_id) = partial_res.base_res() { + if let Res::Def(DefKind::Fn, def_id) = self.partial_res_map.get(&expr.id)?.full_res()? { // We only support cross-crate argument rewriting. Uses // within the same crate should be updated to use the new // const generics style. @@ -198,6 +196,12 @@ impl ResolverAstLoweringExt for ResolverAstLowering { None } + fn clone_res(&mut self, source: NodeId, target: NodeId) { + if let Some(res) = self.partial_res_map.get(&source) { + self.partial_res_map.insert(target, *res); + } + } + /// Obtains resolution for a `NodeId` with a single resolution. fn get_partial_res(&self, id: NodeId) -> Option<PartialRes> { self.partial_res_map.get(&id).copied() @@ -324,17 +328,20 @@ enum FnDeclKind { } impl FnDeclKind { - fn impl_trait_return_allowed(&self, tcx: TyCtxt<'_>) -> bool { + fn impl_trait_allowed(&self, tcx: TyCtxt<'_>) -> 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 impl_trait_in_trait_allowed(&self, tcx: TyCtxt<'_>) -> bool { + fn async_fn_allowed(&self, tcx: TyCtxt<'_>) -> bool { match self { - FnDeclKind::Trait if tcx.features().return_position_impl_trait_in_trait => true, + 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, _ => false, } } @@ -572,7 +579,8 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let current_node_ids = std::mem::take(&mut self.node_id_to_local_id); let current_id_to_def_id = std::mem::take(&mut self.local_id_to_def_id); let current_trait_map = std::mem::take(&mut self.trait_map); - let current_owner = std::mem::replace(&mut self.current_hir_id_owner, def_id); + let current_owner = + std::mem::replace(&mut self.current_hir_id_owner, hir::OwnerId { def_id }); let current_local_counter = std::mem::replace(&mut self.item_local_id_counter, hir::ItemLocalId::new(1)); let current_impl_trait_defs = std::mem::take(&mut self.impl_trait_defs); @@ -587,7 +595,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { debug_assert_eq!(_old, None); let item = f(self); - debug_assert_eq!(def_id, item.def_id()); + debug_assert_eq!(def_id, item.def_id().def_id); // `f` should have consumed all the elements in these vectors when constructing `item`. debug_assert!(self.impl_trait_defs.is_empty()); debug_assert!(self.impl_trait_bounds.is_empty()); @@ -753,12 +761,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { } fn expect_full_res(&mut self, id: NodeId) -> Res<NodeId> { - self.resolver.get_partial_res(id).map_or(Res::Err, |pr| { - if pr.unresolved_segments() != 0 { - panic!("path not fully resolved: {:?}", pr); - } - pr.base_res() - }) + self.resolver.get_partial_res(id).map_or(Res::Err, |pr| pr.expect_full_res()) } fn expect_full_res_from_use(&mut self, id: NodeId) -> impl Iterator<Item = Res<NodeId>> { @@ -786,7 +789,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { /// Mark a span as relative to the current owning item. fn lower_span(&self, span: Span) -> Span { if self.tcx.sess.opts.unstable_opts.incremental_relative_spans { - span.with_parent(Some(self.current_hir_id_owner)) + span.with_parent(Some(self.current_hir_id_owner.def_id)) } else { // Do not make spans relative when not using incremental compilation. span @@ -812,7 +815,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { LifetimeRes::Fresh { param, .. } => { // Late resolution delegates to us the creation of the `LocalDefId`. let _def_id = self.create_def( - self.current_hir_id_owner, + self.current_hir_id_owner.def_id, param, DefPathData::LifetimeNs(kw::UnderscoreLifetime), ); @@ -1060,9 +1063,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by // constructing the HIR for `impl bounds...` and then lowering that. - let parent_def_id = self.current_hir_id_owner; let impl_trait_node_id = self.next_node_id(); - self.create_def(parent_def_id, impl_trait_node_id, DefPathData::ImplTrait); self.with_dyn_type_scope(false, |this| { let node_id = this.next_node_id(); @@ -1140,8 +1141,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // type and value namespaces. If we resolved the path in the value namespace, we // transform it into a generic const argument. TyKind::Path(ref qself, ref path) => { - if let Some(partial_res) = self.resolver.get_partial_res(ty.id) { - let res = partial_res.base_res(); + if let Some(res) = self + .resolver + .get_partial_res(ty.id) + .and_then(|partial_res| partial_res.full_res()) + { if !res.matches_ns(Namespace::TypeNS) { debug!( "lower_generic_arg: Lowering type argument as const argument: {:?}", @@ -1154,7 +1158,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let node_id = self.next_node_id(); // Add a definition for the in-band const def. - self.create_def(parent_def_id, node_id, DefPathData::AnonConst); + self.create_def( + parent_def_id.def_id, + node_id, + DefPathData::AnonConst, + ); let span = self.lower_span(ty.span); let path_expr = Expr { @@ -1204,8 +1212,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // by `ty_path`. if qself.is_none() && let Some(partial_res) = self.resolver.get_partial_res(t.id) - && partial_res.unresolved_segments() == 0 - && let Res::Def(DefKind::Trait | DefKind::TraitAlias, _) = partial_res.base_res() + && let Some(Res::Def(DefKind::Trait | DefKind::TraitAlias, _)) = partial_res.full_res() { let (bounds, lifetime_bound) = self.with_dyn_type_scope(true, |this| { let poly_trait_ref = this.ast_arena.ptr.alloc(PolyTraitRef { @@ -1349,9 +1356,14 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { def_node_id, bounds, false, - &ImplTraitContext::TypeAliasesOpaqueTy, + itctx, ), ImplTraitContext::Universal => { + self.create_def( + self.current_hir_id_owner.def_id, + def_node_id, + DefPathData::ImplTrait, + ); let span = t.span; let ident = Ident::from_str_and_span(&pprust::ty_to_string(t), span); let (param, bounds, path) = @@ -1445,7 +1457,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // frequently opened issues show. let opaque_ty_span = self.mark_span_with_reason(DesugaringKind::OpaqueTy, span, None); - let opaque_ty_def_id = self.local_def_id(opaque_ty_node_id); + let opaque_ty_def_id = match origin { + hir::OpaqueTyOrigin::TyAlias => self.create_def( + self.current_hir_id_owner.def_id, + opaque_ty_node_id, + DefPathData::ImplTrait, + ), + hir::OpaqueTyOrigin::FnReturn(fn_def_id) => { + self.create_def(fn_def_id, opaque_ty_node_id, DefPathData::ImplTrait) + } + hir::OpaqueTyOrigin::AsyncFn(..) => bug!("unreachable"), + }; debug!(?opaque_ty_def_id); // Contains the new lifetime definitions created for the TAIT (if any). @@ -1551,7 +1573,11 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { debug!(?lifetimes); // `impl Trait` now just becomes `Foo<'a, 'b, ..>`. - hir::TyKind::OpaqueDef(hir::ItemId { def_id: opaque_ty_def_id }, lifetimes, in_trait) + hir::TyKind::OpaqueDef( + hir::ItemId { owner_id: hir::OwnerId { def_id: opaque_ty_def_id } }, + lifetimes, + in_trait, + ) } /// Registers a new opaque type with the proper `NodeId`s and @@ -1567,7 +1593,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Generate an `type Foo = impl Trait;` declaration. trace!("registering opaque type with id {:#?}", opaque_ty_id); let opaque_ty_item = hir::Item { - def_id: opaque_ty_id, + owner_id: hir::OwnerId { def_id: opaque_ty_id }, ident: Ident::empty(), kind: opaque_ty_item_kind, vis_span: self.lower_span(span.shrink_to_lo()), @@ -1701,62 +1727,38 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { })); let output = if let Some((ret_id, span)) = make_ret_async { - match kind { - FnDeclKind::Trait => { - if !kind.impl_trait_in_trait_allowed(self.tcx) { + if !kind.async_fn_allowed(self.tcx) { + match kind { + FnDeclKind::Trait | FnDeclKind::Impl => { self.tcx .sess .create_feature_err( TraitFnAsync { fn_span, span }, - sym::return_position_impl_trait_in_trait, + sym::async_fn_in_trait, ) .emit(); } - self.lower_async_fn_ret_ty( - &decl.output, - fn_node_id.expect("`make_ret_async` but no `fn_def_id`"), - ret_id, - true, - ) - } - _ => { - if !kind.impl_trait_return_allowed(self.tcx) { - if kind == FnDeclKind::Impl { - self.tcx - .sess - .create_feature_err( - TraitFnAsync { fn_span, span }, - sym::return_position_impl_trait_in_trait, - ) - .emit(); - } else { - self.tcx.sess.emit_err(TraitFnAsync { fn_span, span }); - } + _ => { + self.tcx.sess.emit_err(TraitFnAsync { fn_span, span }); } - self.lower_async_fn_ret_ty( - &decl.output, - fn_node_id.expect("`make_ret_async` but no `fn_def_id`"), - ret_id, - false, - ) } } + + self.lower_async_fn_ret_ty( + &decl.output, + fn_node_id.expect("`make_ret_async` but no `fn_def_id`"), + ret_id, + matches!(kind, FnDeclKind::Trait), + ) } else { match decl.output { FnRetTy::Ty(ref ty) => { let mut context = match fn_node_id { - Some(fn_node_id) if kind.impl_trait_return_allowed(self.tcx) => { - let fn_def_id = self.local_def_id(fn_node_id); - ImplTraitContext::ReturnPositionOpaqueTy { - origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), - in_trait: false, - } - } - Some(fn_node_id) if kind.impl_trait_in_trait_allowed(self.tcx) => { + Some(fn_node_id) if kind.impl_trait_allowed(self.tcx) => { let fn_def_id = self.local_def_id(fn_node_id); ImplTraitContext::ReturnPositionOpaqueTy { origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), - in_trait: true, + in_trait: matches!(kind, FnDeclKind::Trait), } } _ => ImplTraitContext::Disallowed(match kind { @@ -1950,9 +1952,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let future_bound = this.lower_async_fn_output_type_to_future_bound( output, span, - ImplTraitContext::ReturnPositionOpaqueTy { - origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), - in_trait, + if in_trait && !this.tcx.features().return_position_impl_trait_in_trait { + ImplTraitContext::Disallowed(ImplTraitPosition::TraitReturn) + } else { + ImplTraitContext::ReturnPositionOpaqueTy { + origin: hir::OpaqueTyOrigin::FnReturn(fn_def_id), + in_trait, + } }, ); @@ -2038,7 +2044,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // async fn, so the *type parameters* are inherited. It's // only the lifetime parameters that we must supply. let opaque_ty_ref = hir::TyKind::OpaqueDef( - hir::ItemId { def_id: opaque_ty_def_id }, + hir::ItemId { owner_id: hir::OwnerId { def_id: opaque_ty_def_id } }, generic_args, in_trait, ); diff --git a/compiler/rustc_ast_lowering/src/pat.rs b/compiler/rustc_ast_lowering/src/pat.rs index 1ea76fdbf..1af1633b5 100644 --- a/compiler/rustc_ast_lowering/src/pat.rs +++ b/compiler/rustc_ast_lowering/src/pat.rs @@ -239,7 +239,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { ident: Ident, lower_sub: impl FnOnce(&mut Self) -> Option<&'hir hir::Pat<'hir>>, ) -> hir::PatKind<'hir> { - match self.resolver.get_partial_res(p.id).map(|d| d.base_res()) { + match self.resolver.get_partial_res(p.id).map(|d| d.expect_full_res()) { // `None` can occur in body-less function signatures res @ (None | Some(Res::Local(_))) => { let canonical_id = match res { diff --git a/compiler/rustc_ast_lowering/src/path.rs b/compiler/rustc_ast_lowering/src/path.rs index 6bb1bb9ea..888776ccc 100644 --- a/compiler/rustc_ast_lowering/src/path.rs +++ b/compiler/rustc_ast_lowering/src/path.rs @@ -29,11 +29,13 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { let partial_res = self.resolver.get_partial_res(id).unwrap_or_else(|| PartialRes::new(Res::Err)); + let base_res = partial_res.base_res(); + let unresolved_segments = partial_res.unresolved_segments(); let path_span_lo = p.span.shrink_to_lo(); - let proj_start = p.segments.len() - partial_res.unresolved_segments(); + let proj_start = p.segments.len() - unresolved_segments; let path = self.arena.alloc(hir::Path { - res: self.lower_res(partial_res.base_res()), + res: self.lower_res(base_res), segments: self.arena.alloc_from_iter(p.segments[..proj_start].iter().enumerate().map( |(i, segment)| { let param_mode = match (qself_position, param_mode) { @@ -46,7 +48,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { _ => param_mode, }; - let parenthesized_generic_args = match partial_res.base_res() { + let parenthesized_generic_args = match base_res { // `a::b::Trait(Args)` Res::Def(DefKind::Trait, _) if i + 1 == proj_start => { ParenthesizedGenericArgs::Ok @@ -83,7 +85,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> { // Simple case, either no projections, or only fully-qualified. // E.g., `std::mem::size_of` or `<I as Iterator>::Item`. - if partial_res.unresolved_segments() == 0 { + if unresolved_segments == 0 { return hir::QPath::Resolved(qself, path); } |