diff options
| author | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2021-08-21 00:29:08 +0300 |
|---|---|---|
| committer | Vadim Petrochenkov <vadim.petrochenkov@gmail.com> | 2021-08-29 10:49:40 +0300 |
| commit | 59013cdebe665377e0d733ea3906c84216bd33db (patch) | |
| tree | 2fd27cb9ff11bb3b3bae0d43ac5b9e60073318fb /compiler/rustc_ast_lowering/src/expr.rs | |
| parent | 757a65bfdf504f6c9177514abb385618e784f0a5 (diff) | |
| download | rust-59013cdebe665377e0d733ea3906c84216bd33db.tar.gz rust-59013cdebe665377e0d733ea3906c84216bd33db.zip | |
ast_lowering: Introduce `lower_span` for catching all spans entering HIR
Diffstat (limited to 'compiler/rustc_ast_lowering/src/expr.rs')
| -rw-r--r-- | compiler/rustc_ast_lowering/src/expr.rs | 184 |
1 files changed, 134 insertions, 50 deletions
diff --git a/compiler/rustc_ast_lowering/src/expr.rs b/compiler/rustc_ast_lowering/src/expr.rs index bf7589e84ad..80633e141fc 100644 --- a/compiler/rustc_ast_lowering/src/expr.rs +++ b/compiler/rustc_ast_lowering/src/expr.rs @@ -58,7 +58,12 @@ impl<'hir> LoweringContext<'_, 'hir> { None, )); let args = self.lower_exprs(args); - hir::ExprKind::MethodCall(hir_seg, seg.ident.span, args, span) + hir::ExprKind::MethodCall( + hir_seg, + self.lower_span(seg.ident.span), + args, + self.lower_span(span), + ) } ExprKind::Binary(binop, ref lhs, ref rhs) => { let binop = self.lower_binop(binop); @@ -71,7 +76,9 @@ impl<'hir> LoweringContext<'_, 'hir> { let ohs = self.lower_expr(ohs); hir::ExprKind::Unary(op, ohs) } - ExprKind::Lit(ref l) => hir::ExprKind::Lit(respan(l.span, l.kind.clone())), + ExprKind::Lit(ref l) => { + hir::ExprKind::Lit(respan(self.lower_span(l.span), l.kind.clone())) + } ExprKind::Cast(ref expr, ref ty) => { let expr = self.lower_expr(expr); let ty = self.lower_ty(ty, ImplTraitContext::disallowed()); @@ -86,9 +93,11 @@ impl<'hir> LoweringContext<'_, 'hir> { let ohs = self.lower_expr(ohs); hir::ExprKind::AddrOf(k, m, ohs) } - ExprKind::Let(ref pat, ref scrutinee, span) => { - hir::ExprKind::Let(self.lower_pat(pat), self.lower_expr(scrutinee), span) - } + ExprKind::Let(ref pat, ref scrutinee, span) => hir::ExprKind::Let( + self.lower_pat(pat), + self.lower_expr(scrutinee), + self.lower_span(span), + ), ExprKind::If(ref cond, ref then, ref else_opt) => { self.lower_expr_if(cond, then, else_opt.as_deref()) } @@ -99,7 +108,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ExprKind::Loop(ref body, opt_label) => self.with_loop_scope(e.id, |this| { hir::ExprKind::Loop( this.lower_block(body, false), - opt_label, + this.lower_label(opt_label), hir::LoopSource::Loop, DUMMY_SP, ) @@ -147,6 +156,7 @@ impl<'hir> LoweringContext<'_, 'hir> { } } ExprKind::Block(ref blk, opt_label) => { + let opt_label = self.lower_label(opt_label); hir::ExprKind::Block(self.lower_block(blk, opt_label.is_some()), opt_label) } ExprKind::Assign(ref el, ref er, span) => { @@ -157,7 +167,9 @@ impl<'hir> LoweringContext<'_, 'hir> { self.lower_expr(el), self.lower_expr(er), ), - ExprKind::Field(ref el, ident) => hir::ExprKind::Field(self.lower_expr(el), ident), + ExprKind::Field(ref el, ident) => { + hir::ExprKind::Field(self.lower_expr(el), self.lower_ident(ident)) + } ExprKind::Index(ref el, ref er) => { hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er)) } @@ -234,7 +246,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let mut ex = self.lower_expr_mut(ex); // Include parens in span, but only if it is a super-span. if e.span.contains(ex.span) { - ex.span = e.span; + ex.span = self.lower_span(e.span); } // Merge attributes into the inner expression. if !e.attrs.is_empty() { @@ -262,7 +274,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let hir_id = self.lower_node_id(e.id); self.lower_attrs(hir_id, &e.attrs); - hir::Expr { hir_id, kind, span: e.span } + hir::Expr { hir_id, kind, span: self.lower_span(e.span) } }) } @@ -296,7 +308,7 @@ impl<'hir> LoweringContext<'_, 'hir> { BinOpKind::Ge => hir::BinOpKind::Ge, BinOpKind::Gt => hir::BinOpKind::Gt, }, - span: b.span, + span: self.lower_span(b.span), } } @@ -478,7 +490,13 @@ impl<'hir> LoweringContext<'_, 'hir> { }); let hir_id = self.next_id(); self.lower_attrs(hir_id, &arm.attrs); - hir::Arm { hir_id, pat, guard, body: self.lower_expr(&arm.body), span: arm.span } + hir::Arm { + hir_id, + pat, + guard, + body: self.lower_expr(&arm.body), + span: self.lower_span(arm.span), + } } /// Lower an `async` construct to a generator that is then wrapped so it implements `Future`. @@ -501,12 +519,16 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> hir::ExprKind<'hir> { let output = match ret_ty { Some(ty) => hir::FnRetTy::Return(self.lower_ty(&ty, ImplTraitContext::disallowed())), - None => hir::FnRetTy::DefaultReturn(span), + None => hir::FnRetTy::DefaultReturn(self.lower_span(span)), }; // Resume argument type. We let the compiler infer this to simplify the lowering. It is // fully constrained by `future::from_generator`. - let input_ty = hir::Ty { hir_id: self.next_id(), kind: hir::TyKind::Infer, span }; + let input_ty = hir::Ty { + hir_id: self.next_id(), + kind: hir::TyKind::Infer, + span: self.lower_span(span), + }; // The closure/generator `FnDecl` takes a single (resume) argument of type `input_ty`. let decl = self.arena.alloc(hir::FnDecl { @@ -522,7 +544,12 @@ impl<'hir> LoweringContext<'_, 'hir> { Ident::with_dummy_span(sym::_task_context), hir::BindingAnnotation::Mutable, ); - let param = hir::Param { hir_id: self.next_id(), pat, ty_span: span, span }; + let param = hir::Param { + hir_id: self.next_id(), + pat, + ty_span: self.lower_span(span), + span: self.lower_span(span), + }; let params = arena_vec![self; param]; let body_id = self.lower_body(move |this| { @@ -540,11 +567,14 @@ impl<'hir> LoweringContext<'_, 'hir> { capture_clause, decl, body_id, - span, + self.lower_span(span), Some(hir::Movability::Static), ); - let generator = - hir::Expr { hir_id: self.lower_node_id(closure_node_id), kind: generator_kind, span }; + let generator = hir::Expr { + hir_id: self.lower_node_id(closure_node_id), + kind: generator_kind, + span: self.lower_span(span), + }; // `future::from_generator`: let unstable_span = @@ -681,8 +711,11 @@ impl<'hir> LoweringContext<'_, 'hir> { if let Some(task_context_hid) = self.task_context { let lhs = self.expr_ident(span, task_context_ident, task_context_hid); - let assign = - self.expr(span, hir::ExprKind::Assign(lhs, yield_expr, span), AttrVec::new()); + let assign = self.expr( + span, + hir::ExprKind::Assign(lhs, yield_expr, self.lower_span(span)), + AttrVec::new(), + ); self.stmt_expr(span, assign) } else { // Use of `await` outside of an async context. Return `yield_expr` so that we can @@ -696,8 +729,13 @@ impl<'hir> LoweringContext<'_, 'hir> { // loop { .. } let loop_expr = self.arena.alloc(hir::Expr { hir_id: loop_hir_id, - kind: hir::ExprKind::Loop(loop_block, None, hir::LoopSource::Loop, span), - span, + kind: hir::ExprKind::Loop( + loop_block, + None, + hir::LoopSource::Loop, + self.lower_span(span), + ), + span: self.lower_span(span), }); // mut pinned => loop { ... } @@ -735,7 +773,13 @@ impl<'hir> LoweringContext<'_, 'hir> { // Lower outside new scope to preserve `is_in_loop_condition`. let fn_decl = self.lower_fn_decl(decl, None, false, None); - hir::ExprKind::Closure(capture_clause, fn_decl, body_id, fn_decl_span, generator_option) + hir::ExprKind::Closure( + capture_clause, + fn_decl, + body_id, + self.lower_span(fn_decl_span), + generator_option, + ) } fn generator_movability_for_fn( @@ -821,7 +865,13 @@ impl<'hir> LoweringContext<'_, 'hir> { // closure argument types. let fn_decl = self.lower_fn_decl(&outer_decl, None, false, None); - hir::ExprKind::Closure(capture_clause, fn_decl, body_id, fn_decl_span, None) + hir::ExprKind::Closure( + capture_clause, + fn_decl, + body_id, + self.lower_span(fn_decl_span), + None, + ) } /// Destructure the LHS of complex assignments. @@ -853,7 +903,11 @@ impl<'hir> LoweringContext<'_, 'hir> { } } if is_ordinary(self, lhs) { - return hir::ExprKind::Assign(self.lower_expr(lhs), self.lower_expr(rhs), eq_sign_span); + return hir::ExprKind::Assign( + self.lower_expr(lhs), + self.lower_expr(rhs), + self.lower_span(eq_sign_span), + ); } if !self.sess.features_untracked().destructuring_assignment { feature_err( @@ -878,7 +932,7 @@ impl<'hir> LoweringContext<'_, 'hir> { whole_span, Some(rhs), pat, - hir::LocalSource::AssignDesugar(eq_sign_span), + hir::LocalSource::AssignDesugar(self.lower_span(eq_sign_span)), ); // `a = lhs1; b = lhs2;`. @@ -978,10 +1032,10 @@ impl<'hir> LoweringContext<'_, 'hir> { let pat = self.destructure_assign(&f.expr, eq_sign_span, assignments); hir::PatField { hir_id: self.next_id(), - ident: f.ident, + ident: self.lower_ident(f.ident), pat, is_shorthand: f.is_shorthand, - span: f.span, + span: self.lower_span(f.span), } })); let qpath = self.lower_qpath( @@ -1033,10 +1087,11 @@ impl<'hir> LoweringContext<'_, 'hir> { _ => {} } // Treat all other cases as normal lvalue. - let ident = Ident::new(sym::lhs, lhs.span); + let ident = Ident::new(sym::lhs, self.lower_span(lhs.span)); let (pat, binding) = self.pat_ident_mut(lhs.span, ident); let ident = self.expr_ident(lhs.span, ident, binding); - let assign = hir::ExprKind::Assign(self.lower_expr(lhs), ident, eq_sign_span); + let assign = + hir::ExprKind::Assign(self.lower_expr(lhs), ident, self.lower_span(eq_sign_span)); let expr = self.expr(lhs.span, assign, ThinVec::new()); assignments.push(self.stmt_expr(lhs.span, expr)); pat @@ -1076,7 +1131,7 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_expr_range_closed(&mut self, span: Span, e1: &Expr, e2: &Expr) -> hir::ExprKind<'hir> { let e1 = self.lower_expr_mut(e1); let e2 = self.lower_expr_mut(e2); - let fn_path = hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, span); + let fn_path = hir::QPath::LangItem(hir::LangItem::RangeInclusiveNew, self.lower_span(span)); let fn_expr = self.arena.alloc(self.expr(span, hir::ExprKind::Path(fn_path), ThinVec::new())); hir::ExprKind::Call(fn_expr, arena_vec![self; e1, e2]) @@ -1104,12 +1159,21 @@ impl<'hir> LoweringContext<'_, 'hir> { let fields = self.arena.alloc_from_iter( e1.iter().map(|e| ("start", e)).chain(e2.iter().map(|e| ("end", e))).map(|(s, e)| { let expr = self.lower_expr(&e); - let ident = Ident::new(Symbol::intern(s), e.span); + let ident = Ident::new(Symbol::intern(s), self.lower_span(e.span)); self.expr_field(ident, expr, e.span) }), ); - hir::ExprKind::Struct(self.arena.alloc(hir::QPath::LangItem(lang_item, span)), fields, None) + hir::ExprKind::Struct( + self.arena.alloc(hir::QPath::LangItem(lang_item, self.lower_span(span))), + fields, + None, + ) + } + + fn lower_label(&self, opt_label: Option<Label>) -> Option<Label> { + let label = opt_label?; + Some(Label { ident: self.lower_ident(label.ident) }) } fn lower_loop_destination(&mut self, destination: Option<(NodeId, Label)>) -> hir::Destination { @@ -1128,7 +1192,8 @@ impl<'hir> LoweringContext<'_, 'hir> { .map(|id| Ok(self.lower_node_id(id))) .unwrap_or(Err(hir::LoopIdError::OutsideLoopScope)), }; - hir::Destination { label: destination.map(|(_, label)| label), target_id } + let label = self.lower_label(destination.map(|(_, label)| label)); + hir::Destination { label, target_id } } fn lower_jump_destination(&mut self, id: NodeId, opt_label: Option<Label>) -> hir::Destination { @@ -1201,7 +1266,7 @@ impl<'hir> LoweringContext<'_, 'hir> { constraint: out.constraint, is_rw: out.is_rw, is_indirect: out.is_indirect, - span: out.expr.span, + span: self.lower_span(out.expr.span), }) .collect(), asm: asm.asm, @@ -1226,9 +1291,9 @@ impl<'hir> LoweringContext<'_, 'hir> { fn lower_expr_field(&mut self, f: &ExprField) -> hir::ExprField<'hir> { hir::ExprField { hir_id: self.next_id(), - ident: f.ident, + ident: self.lower_ident(f.ident), expr: self.lower_expr(&f.expr), - span: f.span, + span: self.lower_span(f.span), is_shorthand: f.is_shorthand, } } @@ -1289,7 +1354,7 @@ impl<'hir> LoweringContext<'_, 'hir> { orig_head_span, None, ); - head.span = desugared_span; + head.span = self.lower_span(desugared_span); let iter = Ident::with_dummy_span(sym::iter); @@ -1308,7 +1373,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let next_expr = self.expr_ident(pat.span, next_ident, next_pat_hid); let assign = self.arena.alloc(self.expr( pat.span, - hir::ExprKind::Assign(next_expr, val_expr, pat.span), + hir::ExprKind::Assign(next_expr, val_expr, self.lower_span(pat.span)), ThinVec::new(), )); let some_pat = self.pat_some(pat.span, val_pat); @@ -1376,12 +1441,15 @@ impl<'hir> LoweringContext<'_, 'hir> { // `[opt_ident]: loop { ... }` let kind = hir::ExprKind::Loop( loop_block, - opt_label, + self.lower_label(opt_label), hir::LoopSource::ForLoop, - e.span.with_hi(orig_head_span.hi()), + self.lower_span(e.span.with_hi(orig_head_span.hi())), ); - let loop_expr = - self.arena.alloc(hir::Expr { hir_id: self.lower_node_id(e.id), kind, span: e.span }); + let loop_expr = self.arena.alloc(hir::Expr { + hir_id: self.lower_node_id(e.id), + kind, + span: self.lower_span(e.span), + }); // `mut iter => { ... }` let iter_arm = self.arm(iter_pat, loop_expr); @@ -1460,8 +1528,8 @@ impl<'hir> LoweringContext<'_, 'hir> { let attr = { // `allow(unreachable_code)` let allow = { - let allow_ident = Ident::new(sym::allow, span); - let uc_ident = Ident::new(sym::unreachable_code, span); + let allow_ident = Ident::new(sym::allow, self.lower_span(span)); + let uc_ident = Ident::new(sym::unreachable_code, self.lower_span(span)); let uc_nested = attr::mk_nested_word_item(uc_ident); attr::mk_list_item(allow_ident, vec![uc_nested]) }; @@ -1630,7 +1698,11 @@ impl<'hir> LoweringContext<'_, 'hir> { lang_item: hir::LangItem, attrs: AttrVec, ) -> hir::Expr<'hir> { - self.expr(span, hir::ExprKind::Path(hir::QPath::LangItem(lang_item, span)), attrs) + self.expr( + span, + hir::ExprKind::Path(hir::QPath::LangItem(lang_item, self.lower_span(span))), + attrs, + ) } pub(super) fn expr_ident( @@ -1661,7 +1733,7 @@ impl<'hir> LoweringContext<'_, 'hir> { let expr_path = hir::ExprKind::Path(hir::QPath::Resolved( None, self.arena.alloc(hir::Path { - span, + span: self.lower_span(span), res: Res::Local(binding), segments: arena_vec![self; hir::PathSegment::from_ident(ident)], }), @@ -1681,7 +1753,7 @@ impl<'hir> LoweringContext<'_, 'hir> { expr: Some(expr), hir_id, rules: hir::BlockCheckMode::UnsafeBlock(hir::UnsafeSource::CompilerGenerated), - span, + span: self.lower_span(span), targeted_by_break: false, }), None, @@ -1712,7 +1784,7 @@ impl<'hir> LoweringContext<'_, 'hir> { ) -> hir::Expr<'hir> { let hir_id = self.next_id(); self.lower_attrs(hir_id, &attrs); - hir::Expr { hir_id, kind, span } + hir::Expr { hir_id, kind, span: self.lower_span(span) } } fn expr_field( @@ -1721,10 +1793,22 @@ impl<'hir> LoweringContext<'_, 'hir> { expr: &'hir hir::Expr<'hir>, span: Span, ) -> hir::ExprField<'hir> { - hir::ExprField { hir_id: self.next_id(), ident, span, expr, is_shorthand: false } + hir::ExprField { + hir_id: self.next_id(), + ident, + span: self.lower_span(span), + expr, + is_shorthand: false, + } } fn arm(&mut self, pat: &'hir hir::Pat<'hir>, expr: &'hir hir::Expr<'hir>) -> hir::Arm<'hir> { - hir::Arm { hir_id: self.next_id(), pat, guard: None, span: expr.span, body: expr } + hir::Arm { + hir_id: self.next_id(), + pat, + guard: None, + span: self.lower_span(expr.span), + body: expr, + } } } |
