Skip to content
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

Enforce that all spans are lowered in ast lowering #135874

Merged
merged 1 commit into from
Jan 22, 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/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let path = hir::ExprKind::Path(hir::QPath::TypeRelative(
self.arena.alloc(self.ty(span, hir::TyKind::Path(qpath))),
self.arena.alloc(hir::PathSegment::new(
Ident::new(name, span),
Ident::new(name, self.lower_span(span)),
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used a lot in the format_args! lowering

self.next_id(),
Res::Err,
)),
Expand Down
41 changes: 24 additions & 17 deletions compiler/rustc_ast_lowering/src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,31 @@ impl<'a, 'hir> NodeCollector<'a, 'hir> {

// Make sure that the DepNode of some node coincides with the HirId
// owner of that node.
if cfg!(debug_assertions) && hir_id.owner != self.owner {
span_bug!(
span,
"inconsistent HirId at `{:?}` for `{:?}`: \
if cfg!(debug_assertions) {
if hir_id.owner != self.owner {
span_bug!(
span,
"inconsistent HirId at `{:?}` for `{node:?}`: \
current_dep_node_owner={} ({:?}), hir_id.owner={} ({:?})",
self.tcx.sess.source_map().span_to_diagnostic_string(span),
node,
self.tcx
.definitions_untracked()
.def_path(self.owner.def_id)
.to_string_no_crate_verbose(),
self.owner,
self.tcx
.definitions_untracked()
.def_path(hir_id.owner.def_id)
.to_string_no_crate_verbose(),
hir_id.owner,
)
self.tcx.sess.source_map().span_to_diagnostic_string(span),
self.tcx
.definitions_untracked()
.def_path(self.owner.def_id)
.to_string_no_crate_verbose(),
self.owner,
self.tcx
.definitions_untracked()
.def_path(hir_id.owner.def_id)
.to_string_no_crate_verbose(),
hir_id.owner,
)
}
if self.tcx.sess.opts.incremental.is_some()
&& span.parent().is_none()
&& !span.is_dummy()
{
span_bug!(span, "span without a parent: {:#?}, {node:?}", span.data())
}
}

self.nodes[hir_id.local_id] = ParentedNode { parent: self.parent_node, node };
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1092,6 +1092,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
// this as a special case.
return self.lower_fn_body(decl, |this| {
if attrs.iter().any(|a| a.name_or_empty() == sym::rustc_intrinsic) {
let span = this.lower_span(span);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unlikely to have any effect anywhere, as this is only for intrinsics, but to pacify the new debug assert

let empty_block = hir::Block {
hir_id: this.next_id(),
stmts: &[],
Expand Down
24 changes: 10 additions & 14 deletions compiler/rustc_ast_lowering/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -375,24 +375,24 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
expr: &Expr,
allow_paths: bool,
) -> &'hir hir::PatExpr<'hir> {
let span = self.lower_span(expr.span);
let err = |guar| hir::PatExprKind::Lit {
lit: self.arena.alloc(respan(self.lower_span(expr.span), LitKind::Err(guar))),
lit: self.arena.alloc(respan(span, LitKind::Err(guar))),
negated: false,
};
let kind = match &expr.kind {
ExprKind::Lit(lit) => {
hir::PatExprKind::Lit { lit: self.lower_lit(lit, expr.span), negated: false }
hir::PatExprKind::Lit { lit: self.lower_lit(lit, span), negated: false }
}
ExprKind::ConstBlock(c) => hir::PatExprKind::ConstBlock(self.lower_const_block(c)),
ExprKind::IncludedBytes(bytes) => hir::PatExprKind::Lit {
lit: self.arena.alloc(respan(
self.lower_span(expr.span),
LitKind::ByteStr(Arc::clone(bytes), StrStyle::Cooked),
)),
lit: self
.arena
.alloc(respan(span, LitKind::ByteStr(Arc::clone(bytes), StrStyle::Cooked))),
negated: false,
},
ExprKind::Err(guar) => err(*guar),
ExprKind::Dummy => span_bug!(expr.span, "lowered ExprKind::Dummy"),
ExprKind::Dummy => span_bug!(span, "lowered ExprKind::Dummy"),
ExprKind::Path(qself, path) if allow_paths => hir::PatExprKind::Path(self.lower_qpath(
expr.id,
qself,
Expand All @@ -403,21 +403,17 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
None,
)),
ExprKind::Unary(UnOp::Neg, inner) if let ExprKind::Lit(lit) = &inner.kind => {
hir::PatExprKind::Lit { lit: self.lower_lit(lit, expr.span), negated: true }
hir::PatExprKind::Lit { lit: self.lower_lit(lit, span), negated: true }
}
_ => {
let pattern_from_macro = expr.is_approximately_pattern();
let guar = self.dcx().emit_err(ArbitraryExpressionInPattern {
span: expr.span,
span,
pattern_from_macro_note: pattern_from_macro,
});
err(guar)
}
};
self.arena.alloc(hir::PatExpr {
hir_id: self.lower_node_id(expr.id),
span: expr.span,
kind,
})
self.arena.alloc(hir::PatExpr { hir_id: self.lower_node_id(expr.id), span, kind })
}
}
Loading