Skip to content

Complete span AST lowering. #144557

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_ast_lowering/src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {

fn lower_local(&mut self, l: &Local) -> &'hir hir::LetStmt<'hir> {
// Let statements are allowed to have impl trait in bindings.
let super_ = l.super_;
let super_ = l.super_.map(|span| self.lower_span(span));
let ty = l.ty.as_ref().map(|t| {
self.lower_ty(t, self.impl_trait_in_bindings_ctxt(ImplTraitPosition::Variable))
});
Expand Down
37 changes: 26 additions & 11 deletions compiler/rustc_ast_lowering/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
ExprKind::Field(el, ident) => {
hir::ExprKind::Field(self.lower_expr(el), self.lower_ident(*ident))
}
ExprKind::Index(el, er, brackets_span) => {
hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er), *brackets_span)
}
ExprKind::Index(el, er, brackets_span) => hir::ExprKind::Index(
self.lower_expr(el),
self.lower_expr(er),
self.lower_span(*brackets_span),
),
ExprKind::Range(e1, e2, lims) => {
self.lower_expr_range(e.span, e1.as_deref(), e2.as_deref(), *lims)
}
Expand Down Expand Up @@ -334,7 +336,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
ExprKind::Struct(se) => {
let rest = match &se.rest {
StructRest::Base(e) => hir::StructTailExpr::Base(self.lower_expr(e)),
StructRest::Rest(sp) => hir::StructTailExpr::DefaultFields(*sp),
StructRest::Rest(sp) => {
hir::StructTailExpr::DefaultFields(self.lower_span(*sp))
}
StructRest::None => hir::StructTailExpr::None,
};
hir::ExprKind::Struct(
Expand Down Expand Up @@ -678,6 +682,14 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::Arm { hir_id, pat, guard, body, span }
}

fn lower_capture_clause(&mut self, capture_clause: CaptureBy) -> CaptureBy {
match capture_clause {
CaptureBy::Ref => CaptureBy::Ref,
CaptureBy::Use { use_kw } => CaptureBy::Use { use_kw: self.lower_span(use_kw) },
CaptureBy::Value { move_kw } => CaptureBy::Value { move_kw: self.lower_span(move_kw) },
}
}

/// Lower/desugar a coroutine construct.
///
/// In particular, this creates the correct async resume argument and `_task_context`.
Expand Down Expand Up @@ -769,7 +781,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
hir::ExprKind::Closure(self.arena.alloc(hir::Closure {
def_id: closure_def_id,
binder: hir::ClosureBinder::Default,
capture_clause,
capture_clause: self.lower_capture_clause(capture_clause),
bound_generic_params: &[],
fn_decl,
body,
Expand Down Expand Up @@ -1035,7 +1047,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}

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

fn lower_expr_closure(
Expand Down Expand Up @@ -1083,7 +1095,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let c = self.arena.alloc(hir::Closure {
def_id: closure_def_id,
binder: binder_clause,
capture_clause,
capture_clause: self.lower_capture_clause(capture_clause),
bound_generic_params,
fn_decl,
body: body_id,
Expand Down Expand Up @@ -1197,7 +1209,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let c = self.arena.alloc(hir::Closure {
def_id: closure_def_id,
binder: binder_clause,
capture_clause,
capture_clause: self.lower_capture_clause(capture_clause),
bound_generic_params,
fn_decl,
body,
Expand Down Expand Up @@ -2101,7 +2113,7 @@ impl<'hir> LoweringContext<'_, 'hir> {

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

pub(super) fn expr_str(&mut self, sp: Span, value: Symbol) -> hir::Expr<'hir> {
let lit = hir::Lit { span: sp, node: ast::LitKind::Str(value, ast::StrStyle::Cooked) };
let lit = hir::Lit {
span: self.lower_span(sp),
node: ast::LitKind::Str(value, ast::StrStyle::Cooked),
};
self.expr(sp, hir::ExprKind::Lit(lit))
}

Expand Down Expand Up @@ -2206,7 +2221,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.arena.alloc(hir::Path {
span: self.lower_span(span),
res,
segments: arena_vec![self; hir::PathSegment::new(ident, hir_id, res)],
segments: arena_vec![self; hir::PathSegment::new(self.lower_ident(ident), hir_id, res)],
}),
));

Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_ast_lowering/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,8 @@ fn expand_format_args<'hir>(
fmt: &FormatArgs,
allow_const: bool,
) -> hir::ExprKind<'hir> {
let macsp = ctx.lower_span(macsp);

let mut incomplete_lit = String::new();
let lit_pieces =
ctx.arena.alloc_from_iter(fmt.template.iter().enumerate().filter_map(|(i, piece)| {
Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
} else {
// For non-empty lists we can just drop all the data, the prefix is already
// present in HIR as a part of nested imports.
let span = self.lower_span(span);
self.arena.alloc(hir::UsePath { res: PerNS::default(), segments: &[], span })
};
hir::ItemKind::Use(path, hir::UseKind::ListStem)
Expand Down Expand Up @@ -1567,7 +1568,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
attrs: &[hir::Attribute],
) -> hir::FnHeader {
let asyncness = if let Some(CoroutineKind::Async { span, .. }) = h.coroutine_kind {
hir::IsAsync::Async(span)
hir::IsAsync::Async(self.lower_span(span))
} else {
hir::IsAsync::NotAsync
};
Expand Down Expand Up @@ -1804,7 +1805,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let res = Res::Def(DefKind::TyParam, def_id);
let ident = self.lower_ident(ident);
let ty_path = self.arena.alloc(hir::Path {
span: param_span,
span: self.lower_span(param_span),
res,
segments: self
.arena
Expand Down
15 changes: 13 additions & 2 deletions compiler/rustc_ast_lowering/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2368,7 +2368,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
&mut self,
modifiers: TraitBoundModifiers,
) -> hir::TraitBoundModifiers {
hir::TraitBoundModifiers { constness: modifiers.constness, polarity: modifiers.polarity }
let constness = match modifiers.constness {
BoundConstness::Never => BoundConstness::Never,
BoundConstness::Always(span) => BoundConstness::Always(self.lower_span(span)),
BoundConstness::Maybe(span) => BoundConstness::Maybe(self.lower_span(span)),
};
let polarity = match modifiers.polarity {
BoundPolarity::Positive => BoundPolarity::Positive,
BoundPolarity::Negative(span) => BoundPolarity::Negative(self.lower_span(span)),
BoundPolarity::Maybe(span) => BoundPolarity::Maybe(self.lower_span(span)),
};
hir::TraitBoundModifiers { constness, polarity }
}

// Helper methods for building HIR.
Expand Down Expand Up @@ -2414,14 +2424,15 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
init: Option<&'hir hir::Expr<'hir>>,
) -> hir::Stmt<'hir> {
let hir_id = self.next_id();
let span = self.lower_span(span);
let local = hir::LetStmt {
super_: Some(span),
hir_id,
init,
pat,
els: None,
source: hir::LocalSource::Normal,
span: self.lower_span(span),
span,
ty: None,
};
self.stmt(span, hir::StmtKind::Let(self.arena.alloc(local)))
Expand Down
Loading