Skip to content

Commit 83e5bfa

Browse files
committed
[WIP] Benchmark span combining changes
1 parent 99f77a2 commit 83e5bfa

File tree

14 files changed

+82
-43
lines changed

14 files changed

+82
-43
lines changed

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
743743
// or
744744
// `fn foo(&x: &i32)` -> `fn foo(&(mut x): &i32)`
745745
let def_id = self.body.source.def_id();
746-
if let Some(local_def_id) = def_id.as_local()
747-
&& let Some(body) = self.infcx.tcx.hir().maybe_body_owned_by(local_def_id)
748-
&& let Some(hir_id) = (BindingFinder { span: pat_span }).visit_body(&body).break_value()
746+
let hir_id = def_id
747+
.as_local()
748+
.and_then(|def_id| self.infcx.tcx.hir().maybe_body_owned_by(def_id))
749+
.and_then(|body| (BindingFinder { span: pat_span }).visit_body(&body).break_value());
750+
if let Some(hir_id) = hir_id
749751
&& let node = self.infcx.tcx.hir_node(hir_id)
750752
&& let hir::Node::LetStmt(hir::LetStmt {
751753
pat: hir::Pat { kind: hir::PatKind::Ref(_, _), .. },
@@ -767,8 +769,11 @@ impl<'tcx> MirBorrowckCtxt<'_, '_, '_, 'tcx> {
767769
return;
768770
}
769771

772+
let span = local_decl.source_info.span;
773+
let span =
774+
hir_id.map_or(span, |hir_id| span.with_neighbor(self.infcx.tcx.hir().span(hir_id)));
770775
err.span_suggestion_verbose(
771-
local_decl.source_info.span.shrink_to_lo(),
776+
span.shrink_to_lo(),
772777
"consider changing this to be mutable",
773778
"mut ",
774779
Applicability::MachineApplicable,

compiler/rustc_expand/src/mbe/transcribe.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,22 @@ fn maybe_use_metavar_location(
395395
return orig_tt.clone();
396396
}
397397

398-
let insert = |mspans: &mut FxHashMap<_, _>, s, ms| match mspans.try_insert(s, ms) {
398+
let insert = |mspans: &mut FxHashMap<_, _>, s, ms: Span| match mspans.try_insert(s, ms) {
399399
Ok(_) => true,
400-
Err(err) => *err.entry.get() == ms, // Tried to insert the same span, still success
400+
Err(mut err) => {
401+
let old_ms = *err.entry.get();
402+
if ms == old_ms {
403+
// Tried to insert the same span, still success.
404+
return true;
405+
}
406+
if !ms.eq_ctxt(old_ms) && ms.ctxt().outer_expn_data().call_site.eq_ctxt(old_ms) {
407+
// This looks like a variable passed to an inner (possibly recursive) macro call.
408+
// The innermost metavar span is the most useful, so override.
409+
err.entry.insert(ms);
410+
return true;
411+
}
412+
false
413+
}
401414
};
402415
marker.visit_span(&mut metavar_span);
403416
let no_collision = match orig_tt {
@@ -411,16 +424,19 @@ fn maybe_use_metavar_location(
411424
}),
412425
};
413426
if no_collision || psess.source_map().is_imported(metavar_span) {
427+
if let TokenTree::Token(token, _) = orig_tt {
428+
return TokenTree::Token(token.clone(), Spacing::Alone);
429+
}
414430
return orig_tt.clone();
415431
}
416432

417433
// Setting metavar spans for the heuristic spans gives better opportunities for combining them
418434
// with neighboring spans even despite their different syntactic contexts.
419435
match orig_tt {
420-
TokenTree::Token(Token { kind, span }, spacing) => {
436+
TokenTree::Token(Token { kind, span }, _spacing) => {
421437
let span = metavar_span.with_ctxt(span.ctxt());
422438
with_metavar_spans(|mspans| insert(mspans, span, metavar_span));
423-
TokenTree::Token(Token { kind: kind.clone(), span }, *spacing)
439+
TokenTree::Token(Token { kind: kind.clone(), span }, Spacing::Alone)
424440
}
425441
TokenTree::Delimited(dspan, dspacing, delimiter, tts) => {
426442
let open = metavar_span.with_ctxt(dspan.open.ctxt());

compiler/rustc_hir_analysis/src/hir_ty_lowering/lint.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
5656
));
5757
}
5858

59-
if self_ty.span.edition().at_least_rust_2021() {
59+
if tcx.hir().span_in_context(self_ty.hir_id).edition().at_least_rust_2021() {
6060
let msg = "trait objects must include the `dyn` keyword";
6161
let label = "add `dyn` keyword before this trait";
6262
let mut diag =

compiler/rustc_hir_typeck/src/method/suggest.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2454,7 +2454,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
24542454
ExprKind::Path(QPath::Resolved(_, path)) => {
24552455
// local binding
24562456
if let hir::def::Res::Local(hir_id) = path.res {
2457-
let span = tcx.hir().span(hir_id);
2457+
let span = tcx.hir().span_in_context(hir_id);
24582458
let filename = tcx.sess.source_map().span_to_filename(span);
24592459

24602460
let parent_node = self.tcx.parent_hir_node(hir_id);

compiler/rustc_lint/src/types.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -718,8 +718,9 @@ fn lint_wide_pointer<'tcx>(
718718
return;
719719
};
720720

721+
let (l_span, r_span) = (l.span.with_neighbor(e.span), r.span.with_neighbor(e.span));
721722
let (Some(l_span), Some(r_span)) =
722-
(l.span.find_ancestor_inside(e.span), r.span.find_ancestor_inside(e.span))
723+
(l_span.find_ancestor_inside(e.span), r_span.find_ancestor_inside(e.span))
723724
else {
724725
return cx.emit_span_lint(
725726
AMBIGUOUS_WIDE_POINTER_COMPARISONS,

compiler/rustc_middle/src/hir/map/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,11 @@ impl<'hir> Map<'hir> {
935935
}
936936
}
937937

938+
pub fn span_in_context(self, hir_id: HirId) -> Span {
939+
let parent_span = self.span(self.tcx.parent_hir_id(hir_id));
940+
self.span(hir_id).with_neighbor(parent_span)
941+
}
942+
938943
/// Get a representation of this `id` for debugging purposes.
939944
/// NOTE: Do NOT use this in diagnostics!
940945
pub fn node_to_string(self, id: HirId) -> String {

compiler/rustc_parse/src/parser/expr.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,11 @@ impl<'a> Parser<'a> {
249249
continue;
250250
}
251251

252+
let op_span = op.span;
252253
let op = op.node;
253254
// Special cases:
254255
if op == AssocOp::As {
255-
lhs = self.parse_assoc_op_cast(lhs, lhs_span, ExprKind::Cast)?;
256+
lhs = self.parse_assoc_op_cast(lhs, lhs_span, op_span, ExprKind::Cast)?;
256257
continue;
257258
} else if op == AssocOp::DotDot || op == AssocOp::DotDotEq {
258259
// If we didn't have to handle `x..`/`x..=`, it would be pretty easy to
@@ -274,7 +275,7 @@ impl<'a> Parser<'a> {
274275
this.parse_expr_assoc_with(prec + prec_adjustment, LhsExpr::Unparsed { attrs })
275276
})?;
276277

277-
let span = self.mk_expr_sp(&lhs, lhs_span, rhs.span);
278+
let span = self.mk_expr_sp(&lhs, lhs_span, op_span, rhs.span);
278279
lhs = match op {
279280
AssocOp::Add
280281
| AssocOp::Subtract
@@ -453,7 +454,7 @@ impl<'a> Parser<'a> {
453454
None
454455
};
455456
let rhs_span = rhs.as_ref().map_or(cur_op_span, |x| x.span);
456-
let span = self.mk_expr_sp(&lhs, lhs.span, rhs_span);
457+
let span = self.mk_expr_sp(&lhs, lhs.span, cur_op_span, rhs_span);
457458
let limits =
458459
if op == AssocOp::DotDot { RangeLimits::HalfOpen } else { RangeLimits::Closed };
459460
let range = self.mk_range(Some(lhs), rhs, limits);
@@ -674,10 +675,11 @@ impl<'a> Parser<'a> {
674675
&mut self,
675676
lhs: P<Expr>,
676677
lhs_span: Span,
678+
op_span: Span,
677679
expr_kind: fn(P<Expr>, P<Ty>) -> ExprKind,
678680
) -> PResult<'a, P<Expr>> {
679681
let mk_expr = |this: &mut Self, lhs: P<Expr>, rhs: P<Ty>| {
680-
this.mk_expr(this.mk_expr_sp(&lhs, lhs_span, rhs.span), expr_kind(lhs, rhs))
682+
this.mk_expr(this.mk_expr_sp(&lhs, lhs_span, op_span, rhs.span), expr_kind(lhs, rhs))
681683
};
682684

683685
// Save the state of the parser before parsing type normally, in case there is a
@@ -3852,11 +3854,13 @@ impl<'a> Parser<'a> {
38523854

38533855
/// Create expression span ensuring the span of the parent node
38543856
/// is larger than the span of lhs and rhs, including the attributes.
3855-
fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, rhs_span: Span) -> Span {
3857+
fn mk_expr_sp(&self, lhs: &P<Expr>, lhs_span: Span, op_span: Span, rhs_span: Span) -> Span {
38563858
lhs.attrs
38573859
.iter()
38583860
.find(|a| a.style == AttrStyle::Outer)
38593861
.map_or(lhs_span, |a| a.span)
3862+
// An approximation to #126763.
3863+
.to(op_span)
38603864
.to(rhs_span)
38613865
}
38623866

compiler/rustc_parse/src/parser/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -687,9 +687,9 @@ impl<'a> Parser<'a> {
687687
self.is_keyword_ahead(0, &[kw::Const])
688688
&& self.look_ahead(1, |t| match &t.kind {
689689
// async closures do not work with const closures, so we do not parse that here.
690-
token::Ident(kw::Move | kw::Static, _) | token::OrOr | token::BinOp(token::Or) => {
691-
true
692-
}
690+
token::Ident(kw::Move | kw::Static, IdentIsRaw::No)
691+
| token::OrOr
692+
| token::BinOp(token::Or) => true,
693693
_ => false,
694694
})
695695
}

compiler/rustc_parse/src/parser/path.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ impl<'a> Parser<'a> {
211211
if self.eat(&token::PathSep) {
212212
segments.push(PathSegment::path_root(lo.shrink_to_lo().with_ctxt(mod_sep_ctxt)));
213213
}
214-
self.parse_path_segments(&mut segments, style, ty_generics)?;
214+
let sep_span = self.parse_path_segments(&mut segments, style, ty_generics)?;
215+
let lo = sep_span.map_or(lo, |sep_span| lo.with_neighbor(sep_span));
215216
Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None })
216217
}
217218

@@ -220,7 +221,9 @@ impl<'a> Parser<'a> {
220221
segments: &mut ThinVec<PathSegment>,
221222
style: PathStyle,
222223
ty_generics: Option<&Generics>,
223-
) -> PResult<'a, ()> {
224+
) -> PResult<'a, Option<Span>> {
225+
// An approximation to #126763.
226+
let mut sep_span = None;
224227
loop {
225228
let segment = self.parse_path_segment(style, ty_generics)?;
226229
if style.has_generic_ambiguity() {
@@ -268,7 +271,9 @@ impl<'a> Parser<'a> {
268271
continue;
269272
}
270273

271-
return Ok(());
274+
return Ok(sep_span);
275+
} else {
276+
sep_span = Some(self.prev_token.span);
272277
}
273278
}
274279
}

compiler/rustc_span/src/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -876,6 +876,7 @@ impl Span {
876876

877877
/// Check if you can select metavar spans for the given spans to get matching contexts.
878878
fn try_metavars(a: SpanData, b: SpanData, a_orig: Span, b_orig: Span) -> (SpanData, SpanData) {
879+
let (a_orig, b_orig) = (a_orig.with_parent(None), b_orig.with_parent(None));
879880
let get = |mspans: &FxHashMap<_, _>, s| mspans.get(&s).copied();
880881
match with_metavar_spans(|mspans| (get(mspans, a_orig), get(mspans, b_orig))) {
881882
(None, None) => {}
@@ -938,7 +939,7 @@ impl Span {
938939
pub fn with_neighbor(self, neighbor: Span) -> Span {
939940
match Span::prepare_to_combine(self, neighbor) {
940941
Ok((this, ..)) => this.span(),
941-
Err(_) => self,
942+
Err(fallback) => self.with_ctxt(fallback.ctxt()),
942943
}
943944
}
944945

tests/ui/consts/const-float-classify.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ error[E0284]: type annotations needed
2929
--> $DIR/const-float-classify.rs:21:35
3030
|
3131
LL | const _: () = assert!($a == $b);
32-
| ^^ cannot infer the value of the constant `_`
32+
| ^^^^^ cannot infer the value of the constant `_`
3333
...
3434
LL | / suite! {
3535
LL | | [is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]
@@ -53,7 +53,7 @@ error[E0284]: type annotations needed
5353
--> $DIR/const-float-classify.rs:21:35
5454
|
5555
LL | const _: () = assert!($a == $b);
56-
| ^^ cannot infer the value of the constant `_`
56+
| ^^^^^ cannot infer the value of the constant `_`
5757
...
5858
LL | / suite! {
5959
LL | | [is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]
@@ -77,7 +77,7 @@ error[E0284]: type annotations needed
7777
--> $DIR/const-float-classify.rs:21:35
7878
|
7979
LL | const _: () = assert!($a == $b);
80-
| ^^ cannot infer the value of the constant `_`
80+
| ^^^^^ cannot infer the value of the constant `_`
8181
...
8282
LL | / suite! {
8383
LL | | [is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]
@@ -101,7 +101,7 @@ error[E0284]: type annotations needed
101101
--> $DIR/const-float-classify.rs:21:35
102102
|
103103
LL | const _: () = assert!($a == $b);
104-
| ^^ cannot infer the value of the constant `_`
104+
| ^^^^^ cannot infer the value of the constant `_`
105105
...
106106
LL | / suite! {
107107
LL | | [is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]
@@ -125,7 +125,7 @@ error[E0284]: type annotations needed
125125
--> $DIR/const-float-classify.rs:21:35
126126
|
127127
LL | const _: () = assert!($a == $b);
128-
| ^^ cannot infer the value of the constant `_`
128+
| ^^^^^ cannot infer the value of the constant `_`
129129
...
130130
LL | / suite! {
131131
LL | | [is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]
@@ -149,7 +149,7 @@ error[E0284]: type annotations needed
149149
--> $DIR/const-float-classify.rs:21:35
150150
|
151151
LL | const _: () = assert!($a == $b);
152-
| ^^ cannot infer the value of the constant `_`
152+
| ^^^^^ cannot infer the value of the constant `_`
153153
...
154154
LL | / suite! {
155155
LL | | [is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]
@@ -173,7 +173,7 @@ error[E0284]: type annotations needed
173173
--> $DIR/const-float-classify.rs:21:35
174174
|
175175
LL | const _: () = assert!($a == $b);
176-
| ^^ cannot infer the value of the constant `_`
176+
| ^^^^^ cannot infer the value of the constant `_`
177177
...
178178
LL | / suite! {
179179
LL | | [is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]
@@ -197,7 +197,7 @@ error[E0284]: type annotations needed
197197
--> $DIR/const-float-classify.rs:21:35
198198
|
199199
LL | const _: () = assert!($a == $b);
200-
| ^^ cannot infer the value of the constant `_`
200+
| ^^^^^ cannot infer the value of the constant `_`
201201
...
202202
LL | / suite! {
203203
LL | | [is_nan, is_infinite, is_finite, is_normal, is_sign_positive, is_sign_negative]

tests/ui/lint/wide_pointer_comparisons.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,10 @@ fn main() {
146146
{
147147
macro_rules! cmp {
148148
($a:tt, $b:tt) => { $a == $b }
149+
//~^ WARN ambiguous wide pointer comparison
149150
}
150151

151-
// FIXME: This lint uses some custom span combination logic.
152-
// Rewrite it to adapt to the new metavariable span rules.
153152
cmp!(a, b);
154-
//~^ WARN ambiguous wide pointer comparison
155153
}
156154

157155
{

tests/ui/lint/wide_pointer_comparisons.stderr

+11-7
Original file line numberDiff line numberDiff line change
@@ -586,18 +586,22 @@ LL | std::ptr::eq(*a, *b)
586586
| ~~~~~~~~~~~~~ ~ +
587587

588588
warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
589-
--> $DIR/wide_pointer_comparisons.rs:153:14
589+
--> $DIR/wide_pointer_comparisons.rs:148:33
590590
|
591+
LL | ($a:tt, $b:tt) => { $a == $b }
592+
| ^^^^^^^^
593+
...
591594
LL | cmp!(a, b);
592-
| ^^^^
595+
| ---------- in this macro invocation
593596
|
597+
= note: this warning originates in the macro `cmp` (in Nightly builds, run with -Z macro-backtrace for more info)
594598
help: use `std::ptr::addr_eq` or untyped pointers to only compare their addresses
595599
|
596-
LL | cmp!(std::ptr::addr_eq(a, b));
597-
| ++++++++++++++++++ ~ +
600+
LL | ($a:tt, $b:tt) => { std::ptr::addr_eq($a, $b) }
601+
| ++++++++++++++++++ ~ +
598602

599603
warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
600-
--> $DIR/wide_pointer_comparisons.rs:159:39
604+
--> $DIR/wide_pointer_comparisons.rs:157:39
601605
|
602606
LL | ($a:ident, $b:ident) => { $a == $b }
603607
| ^^^^^^^^
@@ -612,10 +616,10 @@ LL | ($a:ident, $b:ident) => { std::ptr::addr_eq($a, $b) }
612616
| ++++++++++++++++++ ~ +
613617

614618
warning: ambiguous wide pointer comparison, the comparison includes metadata which may not be expected
615-
--> $DIR/wide_pointer_comparisons.rs:169:37
619+
--> $DIR/wide_pointer_comparisons.rs:167:37
616620
|
617621
LL | ($a:expr, $b:expr) => { $a == $b }
618-
| ^^
622+
| ^^^^^
619623
...
620624
LL | cmp!(&a, &b);
621625
| ------------ in this macro invocation

tests/ui/methods/method-on-ambiguous-numeric-type.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ LL | local_bar_tt.pow(2);
4747
|
4848
help: you must specify a type for this binding, like `i32`
4949
|
50-
LL | local_mac_tt!(local_bar_tt: i32);
51-
| +++++
50+
LL | ($tt:tt) => { let $tt: i32 = 42; }
51+
| +++++
5252

5353
error[E0689]: can't call method `pow` on ambiguous numeric type `{integer}`
5454
--> $DIR/method-on-ambiguous-numeric-type.rs:37:9

0 commit comments

Comments
 (0)