Skip to content

Commit 86ce958

Browse files
committed
improve annotation spans in thir/mir
The diagnostic changes says it all :)
1 parent 427dcbc commit 86ce958

File tree

15 files changed

+52
-49
lines changed

15 files changed

+52
-49
lines changed

compiler/rustc_hir/src/hir.rs

+7
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,13 @@ impl<'hir> PathSegment<'hir> {
242242
DUMMY
243243
}
244244
}
245+
246+
pub fn span(&self) -> Span {
247+
match self.args {
248+
Some(ref args) => self.ident.span.to(args.span_ext),
249+
None => self.ident.span,
250+
}
251+
}
245252
}
246253

247254
#[derive(Encodable, Debug, HashStable_Generic)]

compiler/rustc_middle/src/thir.rs

+2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ pub struct Adt<'tcx> {
132132
/// Optional user-given substs: for something like `let x =
133133
/// Bar::<T> { ... }`.
134134
pub user_ty: Option<Canonical<'tcx, UserType<'tcx>>>,
135+
/// Constructor span, e.g. `Foo::<u8>` in `Foo::<u8>(1)`.
136+
pub ctor_span: Span,
135137

136138
pub fields: Box<[FieldExpr]>,
137139
/// The base, e.g. `Foo {x: 1, .. base}`.

compiler/rustc_middle/src/thir/visit.rs

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ pub fn walk_expr<'a, 'tcx: 'a, V: Visitor<'a, 'tcx>>(visitor: &mut V, expr: &Exp
115115
variant_index: _,
116116
substs: _,
117117
user_ty: _,
118+
ctor_span: _,
118119
}) => {
119120
for field in &**fields {
120121
visitor.visit_expr(&visitor.thir()[field.expr]);

compiler/rustc_mir_build/src/build/expr/into.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
319319
variant_index,
320320
substs,
321321
user_ty,
322+
ctor_span,
322323
ref fields,
323324
ref base,
324325
}) => {
@@ -380,7 +381,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
380381
let inferred_ty = expr.ty;
381382
let user_ty = user_ty.map(|ty| {
382383
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
383-
span: source_info.span,
384+
span: ctor_span,
384385
user_ty: ty,
385386
inferred_ty,
386387
})

compiler/rustc_mir_build/src/check_unsafety.rs

+1
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ impl<'a, 'tcx> Visitor<'a, 'tcx> for UnsafetyVisitor<'a, 'tcx> {
395395
variant_index: _,
396396
substs: _,
397397
user_ty: _,
398+
ctor_span: _,
398399
fields: _,
399400
base: _,
400401
}) => match self.tcx.layout_scalar_valid_range(adt_def.did()) {

compiler/rustc_mir_build/src/thir/cx/expr.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ impl<'tcx> Cx<'tcx> {
263263
// Here comes the interesting stuff:
264264
hir::ExprKind::MethodCall(segment, ref args, fn_span) => {
265265
// Rewrite a.b(c) into UFCS form like Trait::b(a, c)
266-
let expr = self.method_callee(expr, segment.ident.span, None);
266+
let expr = self.method_callee(expr, segment.span(), None);
267267
// When we apply adjustments to the receiver, use the span of
268268
// the overall method call for better diagnostics. args[0]
269269
// is guaranteed to exist, since a method call always has a receiver.
@@ -347,6 +347,7 @@ impl<'tcx> Cx<'tcx> {
347347
variant_index: index,
348348
fields: field_refs,
349349
user_ty,
350+
ctor_span: fun.span,
350351
base: None,
351352
}))
352353
} else {
@@ -471,6 +472,7 @@ impl<'tcx> Cx<'tcx> {
471472
variant_index: VariantIdx::new(0),
472473
substs,
473474
user_ty,
475+
ctor_span: qpath.span(),
474476
fields: self.field_refs(fields),
475477
base: base.as_ref().map(|base| FruInfo {
476478
base: self.mirror_expr(base),
@@ -497,6 +499,7 @@ impl<'tcx> Cx<'tcx> {
497499
variant_index: index,
498500
substs,
499501
user_ty,
502+
ctor_span: qpath.span(),
500503
fields: self.field_refs(fields),
501504
base: None,
502505
}))
@@ -862,6 +865,7 @@ impl<'tcx> Cx<'tcx> {
862865
variant_index: adt_def.variant_index_with_ctor_id(def_id),
863866
substs,
864867
user_ty: user_provided_type,
868+
ctor_span: expr.span,
865869
fields: Box::new([]),
866870
base: None,
867871
})),

src/test/ui/closure-expected-type/expect-fn-supply-fn.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ error[E0308]: mismatched types
3434
LL | with_closure_expecting_fn_with_bound_region(|x: fn(&'x u32), y| {});
3535
| ^ one type is more general than the other
3636
|
37-
= note: expected fn pointer `fn(&'x u32)`
37+
= note: expected fn pointer `fn(&u32)`
3838
found fn pointer `for<'r> fn(&'r u32)`
3939

4040
error[E0308]: mismatched types

src/test/ui/nll/issue-98170.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | impl MyStruct<'_> {
66
LL | pub fn new<'a>(field: &'a [u32]) -> MyStruct<'a> {
77
| -- lifetime `'a` defined here
88
LL | Self { field }
9-
| ^^^^^^^^^^^^^^ associated function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`
9+
| ^^^^ type annotation requires that `'a` must outlive `'1`
1010

1111
error: lifetime may not live long enough
1212
--> $DIR/issue-98170.rs:7:9
@@ -16,7 +16,7 @@ LL | impl MyStruct<'_> {
1616
LL | pub fn new<'a>(field: &'a [u32]) -> MyStruct<'a> {
1717
| -- lifetime `'a` defined here
1818
LL | Self { field }
19-
| ^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'1`
19+
| ^^^^^^^^^^^^^^ associated function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`
2020

2121
error: lifetime may not live long enough
2222
--> $DIR/issue-98170.rs:19:9
@@ -27,7 +27,7 @@ LL | impl<'a> Trait<'a> for MyStruct<'_> {
2727
| lifetime `'a` defined here
2828
LL | fn new(field: &'a [u32]) -> MyStruct<'a> {
2929
LL | Self { field }
30-
| ^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'1`
30+
| ^^^^ type annotation requires that `'a` must outlive `'1`
3131

3232
error: lifetime may not live long enough
3333
--> $DIR/issue-98170.rs:19:9

src/test/ui/nll/user-annotations/adt-brace-enums.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error[E0597]: `c` does not live long enough
22
--> $DIR/adt-brace-enums.rs:25:48
33
|
44
LL | SomeEnum::SomeVariant::<&'static u32> { t: &c };
5-
| -------------------------------------------^^--
6-
| | |
7-
| | borrowed value does not live long enough
5+
| ------------------------------------- ^^ borrowed value does not live long enough
6+
| |
87
| type annotation requires that `c` is borrowed for `'static`
98
LL | }
109
| - `c` dropped here while still borrowed
@@ -16,9 +15,8 @@ LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
1615
| -- lifetime `'a` defined here
1716
LL | let c = 66;
1817
LL | SomeEnum::SomeVariant::<&'a u32> { t: &c };
19-
| --------------------------------------^^--
20-
| | |
21-
| | borrowed value does not live long enough
18+
| -------------------------------- ^^ borrowed value does not live long enough
19+
| |
2220
| type annotation requires that `c` is borrowed for `'a`
2321
LL | }
2422
| - `c` dropped here while still borrowed
@@ -30,9 +28,8 @@ LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
3028
| -- lifetime `'a` defined here
3129
...
3230
LL | SomeEnum::SomeVariant::<&'a u32> { t: &c };
33-
| --------------------------------------^^--
34-
| | |
35-
| | borrowed value does not live long enough
31+
| -------------------------------- ^^ borrowed value does not live long enough
32+
| |
3633
| type annotation requires that `c` is borrowed for `'a`
3734
LL | };
3835
| - `c` dropped here while still borrowed

src/test/ui/nll/user-annotations/adt-brace-structs.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error[E0597]: `c` does not live long enough
22
--> $DIR/adt-brace-structs.rs:23:37
33
|
44
LL | SomeStruct::<&'static u32> { t: &c };
5-
| --------------------------------^^--
6-
| | |
7-
| | borrowed value does not live long enough
5+
| -------------------------- ^^ borrowed value does not live long enough
6+
| |
87
| type annotation requires that `c` is borrowed for `'static`
98
LL | }
109
| - `c` dropped here while still borrowed
@@ -16,9 +15,8 @@ LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
1615
| -- lifetime `'a` defined here
1716
LL | let c = 66;
1817
LL | SomeStruct::<&'a u32> { t: &c };
19-
| ---------------------------^^--
20-
| | |
21-
| | borrowed value does not live long enough
18+
| --------------------- ^^ borrowed value does not live long enough
19+
| |
2220
| type annotation requires that `c` is borrowed for `'a`
2321
LL | }
2422
| - `c` dropped here while still borrowed
@@ -30,9 +28,8 @@ LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
3028
| -- lifetime `'a` defined here
3129
...
3230
LL | SomeStruct::<&'a u32> { t: &c };
33-
| ---------------------------^^--
34-
| | |
35-
| | borrowed value does not live long enough
31+
| --------------------- ^^ borrowed value does not live long enough
32+
| |
3633
| type annotation requires that `c` is borrowed for `'a`
3734
LL | };
3835
| - `c` dropped here while still borrowed

src/test/ui/nll/user-annotations/adt-tuple-enums.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error[E0597]: `c` does not live long enough
22
--> $DIR/adt-tuple-enums.rs:28:43
33
|
44
LL | SomeEnum::SomeVariant::<&'static u32>(&c);
5-
| --------------------------------------^^-
6-
| | |
7-
| | borrowed value does not live long enough
5+
| ------------------------------------- ^^ borrowed value does not live long enough
6+
| |
87
| type annotation requires that `c` is borrowed for `'static`
98
LL | }
109
| - `c` dropped here while still borrowed
@@ -16,9 +15,8 @@ LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
1615
| -- lifetime `'a` defined here
1716
LL | let c = 66;
1817
LL | SomeEnum::SomeVariant::<&'a u32>(&c);
19-
| ---------------------------------^^-
20-
| | |
21-
| | borrowed value does not live long enough
18+
| -------------------------------- ^^ borrowed value does not live long enough
19+
| |
2220
| type annotation requires that `c` is borrowed for `'a`
2321
LL | }
2422
| - `c` dropped here while still borrowed
@@ -30,9 +28,8 @@ LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
3028
| -- lifetime `'a` defined here
3129
...
3230
LL | SomeEnum::SomeVariant::<&'a u32>(&c);
33-
| ---------------------------------^^-
34-
| | |
35-
| | borrowed value does not live long enough
31+
| -------------------------------- ^^ borrowed value does not live long enough
32+
| |
3633
| type annotation requires that `c` is borrowed for `'a`
3734
LL | };
3835
| - `c` dropped here while still borrowed

src/test/ui/nll/user-annotations/adt-tuple-struct.stderr

+6-9
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error[E0597]: `c` does not live long enough
22
--> $DIR/adt-tuple-struct.rs:23:32
33
|
44
LL | SomeStruct::<&'static u32>(&c);
5-
| ---------------------------^^-
6-
| | |
7-
| | borrowed value does not live long enough
5+
| -------------------------- ^^ borrowed value does not live long enough
6+
| |
87
| type annotation requires that `c` is borrowed for `'static`
98
LL | }
109
| - `c` dropped here while still borrowed
@@ -16,9 +15,8 @@ LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
1615
| -- lifetime `'a` defined here
1716
LL | let c = 66;
1817
LL | SomeStruct::<&'a u32>(&c);
19-
| ----------------------^^-
20-
| | |
21-
| | borrowed value does not live long enough
18+
| --------------------- ^^ borrowed value does not live long enough
19+
| |
2220
| type annotation requires that `c` is borrowed for `'a`
2321
LL | }
2422
| - `c` dropped here while still borrowed
@@ -30,9 +28,8 @@ LL | fn annot_reference_named_lifetime_in_closure<'a>(_: &'a u32) {
3028
| -- lifetime `'a` defined here
3129
...
3230
LL | SomeStruct::<&'a u32>(&c);
33-
| ----------------------^^-
34-
| | |
35-
| | borrowed value does not live long enough
31+
| --------------------- ^^ borrowed value does not live long enough
32+
| |
3633
| type annotation requires that `c` is borrowed for `'a`
3734
LL | };
3835
| - `c` dropped here while still borrowed

src/test/ui/nll/user-annotations/method-call.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0597]: `c` does not live long enough
22
--> $DIR/method-call.rs:36:34
33
|
44
LL | a.method::<&'static u32>(b, &c);
5-
| ------ ^^ borrowed value does not live long enough
5+
| ---------------------- ^^ borrowed value does not live long enough
66
| |
77
| type annotation requires that `c` is borrowed for `'static`
88
LL | }
@@ -15,7 +15,7 @@ LL | fn annot_reference_named_lifetime<'a>(_d: &'a u32) {
1515
| -- lifetime `'a` defined here
1616
...
1717
LL | a.method::<&'a u32>(b, &c);
18-
| ------ ^^ borrowed value does not live long enough
18+
| ----------------- ^^ borrowed value does not live long enough
1919
| |
2020
| type annotation requires that `c` is borrowed for `'a`
2121
LL | }
@@ -31,7 +31,7 @@ LL | let _closure = || {
3131
| - `c` dropped here while still borrowed
3232
LL | let c = 66;
3333
LL | a.method::<&'a u32>(b, &c);
34-
| ------ ^^ borrowed value does not live long enough
34+
| ----------------- ^^ borrowed value does not live long enough
3535
| |
3636
| type annotation requires that `c` is borrowed for `'a`
3737

src/test/ui/regions/regions-bounded-method-type-parameters.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: lifetime may not live long enough
44
LL | fn caller<'a>(x: &isize) {
55
| -- lifetime `'a` defined here
66
LL | Foo.some_method::<&'a isize>();
7-
| ^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
7+
| ^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
88

99
error: aborting due to previous error
1010

src/test/ui/self/issue-61882-2.stderr

+2-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ error[E0597]: `x` does not live long enough
22
--> $DIR/issue-61882-2.rs:6:14
33
|
44
LL | Self(&x);
5-
| -----^^-
6-
| | |
7-
| | borrowed value does not live long enough
5+
| ---- ^^ borrowed value does not live long enough
6+
| |
87
| type annotation requires that `x` is borrowed for `'static`
98
LL |
109
LL | }

0 commit comments

Comments
 (0)