Skip to content

Commit 872ff96

Browse files
committed
Mark span parent in def_collector.
1 parent 516b8e6 commit 872ff96

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use rustc_middle::span_bug;
2020
use rustc_session::errors::report_lit_error;
2121
use rustc_span::source_map::{respan, Spanned};
2222
use rustc_span::symbol::{kw, sym, Ident, Symbol};
23-
use rustc_span::DUMMY_SP;
2423
use rustc_span::{DesugaringKind, Span};
2524
use thin_vec::{thin_vec, ThinVec};
2625

@@ -190,7 +189,9 @@ impl<'hir> LoweringContext<'_, 'hir> {
190189
MatchKind::Postfix => hir::MatchSource::Postfix,
191190
},
192191
),
193-
ExprKind::Await(expr, await_kw_span) => self.lower_expr_await(*await_kw_span, expr),
192+
ExprKind::Await(expr, await_kw_span) => {
193+
self.lower_expr_await(*await_kw_span, e.span, expr)
194+
}
194195
ExprKind::Closure(box Closure {
195196
binder,
196197
capture_clause,
@@ -256,9 +257,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
256257
ExprKind::Field(el, ident) => {
257258
hir::ExprKind::Field(self.lower_expr(el), self.lower_ident(*ident))
258259
}
259-
ExprKind::Index(el, er, brackets_span) => {
260-
hir::ExprKind::Index(self.lower_expr(el), self.lower_expr(er), *brackets_span)
261-
}
260+
ExprKind::Index(el, er, brackets_span) => hir::ExprKind::Index(
261+
self.lower_expr(el),
262+
self.lower_expr(er),
263+
self.lower_span(*brackets_span),
264+
),
262265
ExprKind::Range(Some(e1), Some(e2), RangeLimits::Closed) => {
263266
self.lower_expr_range_closed(e.span, e1, e2)
264267
}
@@ -400,7 +403,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
400403
let last_segment = path.segments.last_mut().unwrap();
401404
assert!(last_segment.args.is_none());
402405
last_segment.args = Some(AstP(GenericArgs::AngleBracketed(AngleBracketedArgs {
403-
span: DUMMY_SP,
406+
span: last_segment.span().shrink_to_hi(),
404407
args: generic_args,
405408
})));
406409

@@ -747,20 +750,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
747750
/// }
748751
/// }
749752
/// ```
750-
fn lower_expr_await(&mut self, await_kw_span: Span, expr: &Expr) -> hir::ExprKind<'hir> {
753+
fn lower_expr_await(
754+
&mut self,
755+
await_kw_span: Span,
756+
full_span: Span,
757+
expr: &Expr,
758+
) -> hir::ExprKind<'hir> {
751759
let expr = self.arena.alloc(self.lower_expr_mut(expr));
752-
self.make_lowered_await(await_kw_span, expr, FutureKind::Future)
760+
self.make_lowered_await(await_kw_span, full_span, expr, FutureKind::Future)
753761
}
754762

755763
/// Takes an expr that has already been lowered and generates a desugared await loop around it
756764
fn make_lowered_await(
757765
&mut self,
758766
await_kw_span: Span,
767+
full_span: Span,
759768
expr: &'hir hir::Expr<'hir>,
760769
await_kind: FutureKind,
761770
) -> hir::ExprKind<'hir> {
762-
let full_span = expr.span.to(await_kw_span);
763-
764771
let is_async_gen = match self.coroutine_kind {
765772
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Async, _)) => false,
766773
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::AsyncGen, _)) => true,
@@ -1692,7 +1699,12 @@ impl<'hir> LoweringContext<'_, 'hir> {
16921699
));
16931700
// `unsafe { ... }`
16941701
let iter = self.arena.alloc(self.expr_unsafe(iter));
1695-
let kind = self.make_lowered_await(head_span, iter, FutureKind::AsyncIterator);
1702+
let kind = self.make_lowered_await(
1703+
head_span,
1704+
head_span,
1705+
iter,
1706+
FutureKind::AsyncIterator,
1707+
);
16961708
self.arena.alloc(hir::Expr { hir_id: self.next_id(), kind, span: head_span })
16971709
}
16981710
};
@@ -1998,6 +2010,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
19982010
}
19992011

20002012
pub(super) fn expr_str(&mut self, sp: Span, value: Symbol) -> hir::Expr<'hir> {
2013+
let sp = self.lower_span(sp);
20012014
let lit = self
20022015
.arena
20032016
.alloc(hir::Lit { span: sp, node: ast::LitKind::Str(value, ast::StrStyle::Cooked) });
@@ -2052,7 +2065,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
20522065
lang_item: hir::LangItem,
20532066
name: Symbol,
20542067
) -> hir::Expr<'hir> {
2055-
let qpath = self.make_lang_item_qpath(lang_item, self.lower_span(span));
2068+
let span = self.lower_span(span);
2069+
let qpath = self.make_lang_item_qpath(lang_item, span);
20562070
let path = hir::ExprKind::Path(hir::QPath::TypeRelative(
20572071
self.arena.alloc(self.ty(span, hir::TyKind::Path(qpath))),
20582072
self.arena.alloc(hir::PathSegment::new(

compiler/rustc_ast_lowering/src/item.rs

+2
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
179179
// Start with an empty prefix.
180180
let prefix = Path { segments: ThinVec::new(), span: use_tree.span, tokens: None };
181181

182+
let vis_span = self.lower_span(vis_span);
182183
self.lower_use_tree(use_tree, &prefix, id, vis_span, ident, attrs)
183184
}
184185
ItemKind::Static(box ast::StaticItem { ty: t, safety: _, mutability: m, expr: e }) => {
@@ -591,6 +592,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
591592
} else {
592593
// For non-empty lists we can just drop all the data, the prefix is already
593594
// present in HIR as a part of nested imports.
595+
let span = self.lower_span(span);
594596
self.arena.alloc(hir::UsePath { res: smallvec![], segments: &[], span })
595597
};
596598
hir::ItemKind::Use(path, hir::UseKind::ListStem)

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -772,9 +772,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
772772
expected.only_has_type(self),
773773
);
774774
}
775-
if let Some(span) =
776-
tcx.resolutions(()).confused_type_with_std_module.get(&span.with_parent(None))
777-
{
775+
if let Some(span) = tcx.resolutions(()).confused_type_with_std_module.get(&span) {
778776
err.span_suggestion(
779777
span.shrink_to_lo(),
780778
"you are looking for the module in `std`, not the primitive type",

compiler/rustc_resolve/src/def_collector.rs

+4
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ impl<'a, 'b, 'tcx> DefCollector<'a, 'b, 'tcx> {
130130
}
131131

132132
impl<'a, 'b, 'tcx> mut_visit::MutVisitor for DefCollector<'a, 'b, 'tcx> {
133+
fn visit_span(&mut self, span: &mut Span) {
134+
*span = span.with_parent(Some(self.parent_def));
135+
}
136+
133137
#[tracing::instrument(level = "trace", skip(self))]
134138
fn flat_map_item(&mut self, mut i: P<Item>) -> SmallVec<[P<Item>; 1]> {
135139
// Pick the def data. This need not be unique, but the more

tests/ui/macros/issue-69396-const-no-type-in-macro.stderr

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ error[E0428]: the name `A` is defined multiple times
22
--> $DIR/issue-69396-const-no-type-in-macro.rs:4:13
33
|
44
LL | const A = "A".$fn();
5-
| ^^^^^^^^^^^^^^^^^^^^ `A` redefined here
5+
| ^^^^^^^^^^^^^^^^^^^^
6+
| |
7+
| `A` redefined here
8+
| previous definition of the value `A` here
69
...
710
LL | / suite! {
811
LL | | len;

0 commit comments

Comments
 (0)