Skip to content

Commit 686bc1c

Browse files
committed
Auto merge of #144557 - cjgillot:lower-more-span, r=compiler-errors
Complete span AST lowering. r? `@ghost`
2 parents ac0cb05 + 7db72f8 commit 686bc1c

File tree

5 files changed

+45
-16
lines changed

5 files changed

+45
-16
lines changed

compiler/rustc_ast_lowering/src/block.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
9595

9696
fn lower_local(&mut self, l: &Local) -> &'hir hir::LetStmt<'hir> {
9797
// Let statements are allowed to have impl trait in bindings.
98-
let super_ = l.super_;
98+
let super_ = l.super_.map(|span| self.lower_span(span));
9999
let ty = l.ty.as_ref().map(|t| {
100100
self.lower_ty(t, self.impl_trait_in_bindings_ctxt(ImplTraitPosition::Variable))
101101
});

compiler/rustc_ast_lowering/src/expr.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
282282
ExprKind::Field(el, ident) => {
283283
hir::ExprKind::Field(self.lower_expr(el), self.lower_ident(*ident))
284284
}
285-
ExprKind::Index(el, er, brackets_span) => {
286-
hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er), *brackets_span)
287-
}
285+
ExprKind::Index(el, er, brackets_span) => hir::ExprKind::Index(
286+
self.lower_expr(el),
287+
self.lower_expr(er),
288+
self.lower_span(*brackets_span),
289+
),
288290
ExprKind::Range(e1, e2, lims) => {
289291
self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), *lims)
290292
}
@@ -334,7 +336,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
334336
ExprKind::Struct(se) => {
335337
let rest = match &se.rest {
336338
StructRest::Base(e) => hir::StructTailExpr::Base(self.lower_expr(e)),
337-
StructRest::Rest(sp) => hir::StructTailExpr::DefaultFields(*sp),
339+
StructRest::Rest(sp) => {
340+
hir::StructTailExpr::DefaultFields(self.lower_span(*sp))
341+
}
338342
StructRest::None => hir::StructTailExpr::None,
339343
};
340344
hir::ExprKind::Struct(
@@ -678,6 +682,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
678682
hir::Arm { hir_id, pat, guard, body, span }
679683
}
680684

685+
fn lower_capture_clause(&mut self, capture_clause: CaptureBy) -> CaptureBy {
686+
match capture_clause {
687+
CaptureBy::Ref => CaptureBy::Ref,
688+
CaptureBy::Use { use_kw } => CaptureBy::Use { use_kw: self.lower_span(use_kw) },
689+
CaptureBy::Value { move_kw } => CaptureBy::Value { move_kw: self.lower_span(move_kw) },
690+
}
691+
}
692+
681693
/// Lower/desugar a coroutine construct.
682694
///
683695
/// In particular, this creates the correct async resume argument and `_task_context`.
@@ -769,7 +781,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
769781
hir::ExprKind::Closure(self.arena.alloc(hir::Closure {
770782
def_id: closure_def_id,
771783
binder: hir::ClosureBinder::Default,
772-
capture_clause,
784+
capture_clause: self.lower_capture_clause(capture_clause),
773785
bound_generic_params: &[],
774786
fn_decl,
775787
body,
@@ -1035,7 +1047,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10351047
}
10361048

10371049
fn lower_expr_use(&mut self, use_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
1038-
hir::ExprKind::Use(self.lower_expr(expr), use_kw_span)
1050+
hir::ExprKind::Use(self.lower_expr(expr), self.lower_span(use_kw_span))
10391051
}
10401052

10411053
fn lower_expr_closure(
@@ -1083,7 +1095,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
10831095
let c = self.arena.alloc(hir::Closure {
10841096
def_id: closure_def_id,
10851097
binder: binder_clause,
1086-
capture_clause,
1098+
capture_clause: self.lower_capture_clause(capture_clause),
10871099
bound_generic_params,
10881100
fn_decl,
10891101
body: body_id,
@@ -1197,7 +1209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
11971209
let c = self.arena.alloc(hir::Closure {
11981210
def_id: closure_def_id,
11991211
binder: binder_clause,
1200-
capture_clause,
1212+
capture_clause: self.lower_capture_clause(capture_clause),
12011213
bound_generic_params,
12021214
fn_decl,
12031215
body,
@@ -2101,7 +2113,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
21012113

21022114
fn expr_uint(&mut self, sp: Span, ty: ast::UintTy, value: u128) -> hir::Expr<'hir> {
21032115
let lit = hir::Lit {
2104-
span: sp,
2116+
span: self.lower_span(sp),
21052117
node: ast::LitKind::Int(value.into(), ast::LitIntType::Unsigned(ty)),
21062118
};
21072119
self.expr(sp, hir::ExprKind::Lit(lit))
@@ -2120,7 +2132,10 @@ impl<'hir> LoweringContext<'_, 'hir> {
21202132
}
21212133

21222134
pub(super) fn expr_str(&mut self, sp: Span, value: Symbol) -> hir::Expr<'hir> {
2123-
let lit = hir::Lit { span: sp, node: ast::LitKind::Str(value, ast::StrStyle::Cooked) };
2135+
let lit = hir::Lit {
2136+
span: self.lower_span(sp),
2137+
node: ast::LitKind::Str(value, ast::StrStyle::Cooked),
2138+
};
21242139
self.expr(sp, hir::ExprKind::Lit(lit))
21252140
}
21262141

@@ -2206,7 +2221,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
22062221
self.arena.alloc(hir::Path {
22072222
span: self.lower_span(span),
22082223
res,
2209-
segments: arena_vec![self; hir::PathSegment::new(ident, hir_id, res)],
2224+
segments: arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
22102225
}),
22112226
));
22122227

compiler/rustc_ast_lowering/src/format.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,8 @@ fn expand_format_args<'hir>(
402402
fmt: &FormatArgs,
403403
allow_const: bool,
404404
) -> hir::ExprKind<'hir> {
405+
let macsp = ctx.lower_span(macsp);
406+
405407
let mut incomplete_lit = String::new();
406408
let lit_pieces =
407409
ctx.arena.alloc_from_iter(fmt.template.iter().enumerate().filter_map(|(i, piece)| {

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,6 +627,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
627627
} else {
628628
// For non-empty lists we can just drop all the data, the prefix is already
629629
// present in HIR as a part of nested imports.
630+
let span = self.lower_span(span);
630631
self.arena.alloc(hir::UsePath { res: PerNS::default(), segments: &[], span })
631632
};
632633
hir::ItemKind::Use(path, hir::UseKind::ListStem)
@@ -1567,7 +1568,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15671568
attrs: &[hir::Attribute],
15681569
) -> hir::FnHeader {
15691570
let asyncness = if let Some(CoroutineKind::Async { span, .. }) = h.coroutine_kind {
1570-
hir::IsAsync::Async(span)
1571+
hir::IsAsync::Async(self.lower_span(span))
15711572
} else {
15721573
hir::IsAsync::NotAsync
15731574
};
@@ -1804,7 +1805,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
18041805
let res = Res::Def(DefKind::TyParam, def_id);
18051806
let ident = self.lower_ident(ident);
18061807
let ty_path = self.arena.alloc(hir::Path {
1807-
span: param_span,
1808+
span: self.lower_span(param_span),
18081809
res,
18091810
segments: self
18101811
.arena

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,7 +2368,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23682368
&mut self,
23692369
modifiers: TraitBoundModifiers,
23702370
) -> hir::TraitBoundModifiers {
2371-
hir::TraitBoundModifiers { constness: modifiers.constness, polarity: modifiers.polarity }
2371+
let constness = match modifiers.constness {
2372+
BoundConstness::Never => BoundConstness::Never,
2373+
BoundConstness::Always(span) => BoundConstness::Always(self.lower_span(span)),
2374+
BoundConstness::Maybe(span) => BoundConstness::Maybe(self.lower_span(span)),
2375+
};
2376+
let polarity = match modifiers.polarity {
2377+
BoundPolarity::Positive => BoundPolarity::Positive,
2378+
BoundPolarity::Negative(span) => BoundPolarity::Negative(self.lower_span(span)),
2379+
BoundPolarity::Maybe(span) => BoundPolarity::Maybe(self.lower_span(span)),
2380+
};
2381+
hir::TraitBoundModifiers { constness, polarity }
23722382
}
23732383

23742384
// Helper methods for building HIR.
@@ -2414,14 +2424,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
24142424
init: Option<&'hir hir::Expr<'hir>>,
24152425
) -> hir::Stmt<'hir> {
24162426
let hir_id = self.next_id();
2427+
let span = self.lower_span(span);
24172428
let local = hir::LetStmt {
24182429
super_: Some(span),
24192430
hir_id,
24202431
init,
24212432
pat,
24222433
els: None,
24232434
source: hir::LocalSource::Normal,
2424-
span: self.lower_span(span),
2435+
span,
24252436
ty: None,
24262437
};
24272438
self.stmt(span, hir::StmtKind::Let(self.arena.alloc(local)))

0 commit comments

Comments
 (0)