Skip to content

Commit d1e82d4

Browse files
author
Lukas Markeffsky
committed
use more accurate spans for user type ascriptions
1 parent b52dea8 commit d1e82d4

13 files changed

+52
-38
lines changed

compiler/rustc_middle/src/thir.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -453,12 +453,14 @@ pub enum ExprKind<'tcx> {
453453
source: ExprId,
454454
/// Type that the user gave to this expression
455455
user_ty: UserTy<'tcx>,
456+
user_ty_span: Span,
456457
},
457-
/// A type ascription on a value, e.g. `42: i32`.
458+
/// A type ascription on a value, e.g. `type_ascribe!(42, i32)` or `42 as i32`.
458459
ValueTypeAscription {
459460
source: ExprId,
460461
/// Type that the user gave to this expression
461462
user_ty: UserTy<'tcx>,
463+
user_ty_span: Span,
462464
},
463465
/// A closure definition.
464466
Closure(Box<ClosureExpr<'tcx>>),

compiler/rustc_middle/src/thir/visit.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
129129
visitor.visit_expr(&visitor.thir()[base.base]);
130130
}
131131
}
132-
PlaceTypeAscription { source, user_ty: _ } | ValueTypeAscription { source, user_ty: _ } => {
132+
PlaceTypeAscription { source, user_ty: _, user_ty_span: _ }
133+
| ValueTypeAscription { source, user_ty: _, user_ty_span: _ } => {
133134
visitor.visit_expr(&visitor.thir()[source])
134135
}
135136
Closure(box ClosureExpr {

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -470,21 +470,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
470470
block.and(place_builder)
471471
}
472472

473-
ExprKind::PlaceTypeAscription { source, ref user_ty } => {
473+
ExprKind::PlaceTypeAscription { source, ref user_ty, user_ty_span } => {
474474
let place_builder = unpack!(
475475
block = this.expr_as_place(block, source, mutability, fake_borrow_temps,)
476476
);
477477
if let Some(user_ty) = user_ty {
478+
let ty_source_info = this.source_info(user_ty_span);
478479
let annotation_index =
479480
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
480-
span: source_info.span,
481+
span: user_ty_span,
481482
user_ty: user_ty.clone(),
482483
inferred_ty: expr.ty,
483484
});
484485

485486
let place = place_builder.to_place(this);
486487
this.cfg.push(block, Statement {
487-
source_info,
488+
source_info: ty_source_info,
488489
kind: StatementKind::AscribeUserType(
489490
Box::new((place, UserTypeProjection {
490491
base: annotation_index,
@@ -496,20 +497,21 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
496497
}
497498
block.and(place_builder)
498499
}
499-
ExprKind::ValueTypeAscription { source, ref user_ty } => {
500+
ExprKind::ValueTypeAscription { source, ref user_ty, user_ty_span } => {
500501
let source_expr = &this.thir[source];
501502
let temp = unpack!(
502503
block = this.as_temp(block, source_expr.temp_lifetime, source, mutability)
503504
);
504505
if let Some(user_ty) = user_ty {
506+
let ty_source_info = this.source_info(user_ty_span);
505507
let annotation_index =
506508
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
507-
span: source_info.span,
509+
span: user_ty_span,
508510
user_ty: user_ty.clone(),
509511
inferred_ty: expr.ty,
510512
});
511513
this.cfg.push(block, Statement {
512-
source_info,
514+
source_info: ty_source_info,
513515
kind: StatementKind::AscribeUserType(
514516
Box::new((Place::from(temp), UserTypeProjection {
515517
base: annotation_index,

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,7 @@ impl<'tcx> Cx<'tcx> {
840840
ExprKind::ValueTypeAscription {
841841
source: cast_expr,
842842
user_ty: Some(Box::new(*user_ty)),
843+
user_ty_span: cast_ty.span,
843844
}
844845
} else {
845846
cast
@@ -851,9 +852,17 @@ impl<'tcx> Cx<'tcx> {
851852
debug!("make_mirror_unadjusted: (type) user_ty={:?}", user_ty);
852853
let mirrored = self.mirror_expr(source);
853854
if source.is_syntactic_place_expr() {
854-
ExprKind::PlaceTypeAscription { source: mirrored, user_ty }
855+
ExprKind::PlaceTypeAscription {
856+
source: mirrored,
857+
user_ty,
858+
user_ty_span: ty.span,
859+
}
855860
} else {
856-
ExprKind::ValueTypeAscription { source: mirrored, user_ty }
861+
ExprKind::ValueTypeAscription {
862+
source: mirrored,
863+
user_ty,
864+
user_ty_span: ty.span,
865+
}
857866
}
858867
}
859868
hir::ExprKind::DropTemps(source) => ExprKind::Use { source: self.mirror_expr(source) },

compiler/rustc_mir_build/src/thir/print.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,18 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
454454
self.print_adt_expr(&**adt_expr, depth_lvl + 1);
455455
print_indented!(self, "}", depth_lvl);
456456
}
457-
PlaceTypeAscription { source, user_ty } => {
457+
PlaceTypeAscription { source, user_ty, user_ty_span } => {
458458
print_indented!(self, "PlaceTypeAscription {", depth_lvl);
459459
print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
460+
print_indented!(self, format!("user_ty_span: {:?}", user_ty_span), depth_lvl + 1);
460461
print_indented!(self, "source:", depth_lvl + 1);
461462
self.print_expr(*source, depth_lvl + 2);
462463
print_indented!(self, "}", depth_lvl);
463464
}
464-
ValueTypeAscription { source, user_ty } => {
465+
ValueTypeAscription { source, user_ty, user_ty_span } => {
465466
print_indented!(self, "ValueTypeAscription {", depth_lvl);
466467
print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
468+
print_indented!(self, format!("user_ty_span: {:?}", user_ty_span), depth_lvl + 1);
467469
print_indented!(self, "source:", depth_lvl + 1);
468470
self.print_expr(*source, depth_lvl + 2);
469471
print_indented!(self, "}", depth_lvl);

tests/mir-opt/address_of.address_of_reborrow.SimplifyCfg-initial.after.mir

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// MIR for `address_of_reborrow` after SimplifyCfg-initial
22

33
| User Type Annotations
4-
| 0: user_ty: Canonical { value: Ty(*const ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:8:5: 8:18, inferred_ty: *const [i32; 10]
5-
| 1: user_ty: Canonical { value: Ty(*const dyn std::marker::Send), max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:10:5: 10:25, inferred_ty: *const dyn std::marker::Send
4+
| 0: user_ty: Canonical { value: Ty(*const ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:8:10: 8:18, inferred_ty: *const [i32; 10]
5+
| 1: user_ty: Canonical { value: Ty(*const dyn std::marker::Send), max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:10:10: 10:25, inferred_ty: *const dyn std::marker::Send
66
| 2: user_ty: Canonical { value: Ty(*const ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:14:12: 14:20, inferred_ty: *const [i32; 10]
77
| 3: user_ty: Canonical { value: Ty(*const ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:14:12: 14:20, inferred_ty: *const [i32; 10]
88
| 4: user_ty: Canonical { value: Ty(*const [i32; 10]), max_universe: U0, variables: [], defining_opaque_types: [] }, span: $DIR/address_of.rs:15:12: 15:28, inferred_ty: *const [i32; 10]
@@ -11,8 +11,8 @@
1111
| 7: user_ty: Canonical { value: Ty(*const dyn std::marker::Send), max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:16:12: 16:27, inferred_ty: *const dyn std::marker::Send
1212
| 8: user_ty: Canonical { value: Ty(*const [i32]), max_universe: U0, variables: [], defining_opaque_types: [] }, span: $DIR/address_of.rs:17:12: 17:24, inferred_ty: *const [i32]
1313
| 9: user_ty: Canonical { value: Ty(*const [i32]), max_universe: U0, variables: [], defining_opaque_types: [] }, span: $DIR/address_of.rs:17:12: 17:24, inferred_ty: *const [i32]
14-
| 10: user_ty: Canonical { value: Ty(*const ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:19:5: 19:18, inferred_ty: *const [i32; 10]
15-
| 11: user_ty: Canonical { value: Ty(*const dyn std::marker::Send), max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:21:5: 21:25, inferred_ty: *const dyn std::marker::Send
14+
| 10: user_ty: Canonical { value: Ty(*const ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:19:10: 19:18, inferred_ty: *const [i32; 10]
15+
| 11: user_ty: Canonical { value: Ty(*const dyn std::marker::Send), max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:21:10: 21:25, inferred_ty: *const dyn std::marker::Send
1616
| 12: user_ty: Canonical { value: Ty(*const ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:24:12: 24:20, inferred_ty: *const [i32; 10]
1717
| 13: user_ty: Canonical { value: Ty(*const ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:24:12: 24:20, inferred_ty: *const [i32; 10]
1818
| 14: user_ty: Canonical { value: Ty(*const [i32; 10]), max_universe: U0, variables: [], defining_opaque_types: [] }, span: $DIR/address_of.rs:25:12: 25:28, inferred_ty: *const [i32; 10]
@@ -21,8 +21,8 @@
2121
| 17: user_ty: Canonical { value: Ty(*const dyn std::marker::Send), max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:26:12: 26:27, inferred_ty: *const dyn std::marker::Send
2222
| 18: user_ty: Canonical { value: Ty(*const [i32]), max_universe: U0, variables: [], defining_opaque_types: [] }, span: $DIR/address_of.rs:27:12: 27:24, inferred_ty: *const [i32]
2323
| 19: user_ty: Canonical { value: Ty(*const [i32]), max_universe: U0, variables: [], defining_opaque_types: [] }, span: $DIR/address_of.rs:27:12: 27:24, inferred_ty: *const [i32]
24-
| 20: user_ty: Canonical { value: Ty(*mut ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:29:5: 29:16, inferred_ty: *mut [i32; 10]
25-
| 21: user_ty: Canonical { value: Ty(*mut dyn std::marker::Send), max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:31:5: 31:23, inferred_ty: *mut dyn std::marker::Send
24+
| 20: user_ty: Canonical { value: Ty(*mut ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:29:10: 29:16, inferred_ty: *mut [i32; 10]
25+
| 21: user_ty: Canonical { value: Ty(*mut dyn std::marker::Send), max_universe: U0, variables: [CanonicalVarInfo { kind: Region(U0) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:31:10: 31:23, inferred_ty: *mut dyn std::marker::Send
2626
| 22: user_ty: Canonical { value: Ty(*mut ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:34:12: 34:18, inferred_ty: *mut [i32; 10]
2727
| 23: user_ty: Canonical { value: Ty(*mut ^0), max_universe: U0, variables: [CanonicalVarInfo { kind: Ty(General(U0)) }], defining_opaque_types: [] }, span: $DIR/address_of.rs:34:12: 34:18, inferred_ty: *mut [i32; 10]
2828
| 24: user_ty: Canonical { value: Ty(*mut [i32; 10]), max_universe: U0, variables: [], defining_opaque_types: [] }, span: $DIR/address_of.rs:35:12: 35:26, inferred_ty: *mut [i32; 10]

tests/ui/cast/ptr-to-trait-obj-different-regions-id-trait.current.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: lifetime may not live long enough
2-
--> $DIR/ptr-to-trait-obj-different-regions-id-trait.rs:24:17
2+
--> $DIR/ptr-to-trait-obj-different-regions-id-trait.rs:24:27
33
|
44
LL | fn m<'a>() {
55
| -- lifetime `'a` defined here
66
LL | let unsend: *const dyn Cat<'a> = &();
77
LL | let _send = unsend as *const S<dyn Cat<'static>>;
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
99
|
1010
= note: requirement occurs because of the type `S<dyn Cat<'_>>`, which makes the generic argument `dyn Cat<'_>` invariant
1111
= note: the struct `S<T>` is invariant over the parameter `T`

tests/ui/cast/ptr-to-trait-obj-different-regions-id-trait.next.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: lifetime may not live long enough
2-
--> $DIR/ptr-to-trait-obj-different-regions-id-trait.rs:24:17
2+
--> $DIR/ptr-to-trait-obj-different-regions-id-trait.rs:24:27
33
|
44
LL | fn m<'a>() {
55
| -- lifetime `'a` defined here
66
LL | let unsend: *const dyn Cat<'a> = &();
77
LL | let _send = unsend as *const S<dyn Cat<'static>>;
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
8+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
99
|
1010
= note: requirement occurs because of the type `S<dyn Cat<'_>>`, which makes the generic argument `dyn Cat<'_>` invariant
1111
= note: the struct `S<T>` is invariant over the parameter `T`

tests/ui/kindck/kindck-impl-type-params.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ LL | struct Foo; // does not impl Copy
112112
|
113113

114114
error: lifetime may not live long enough
115-
--> $DIR/kindck-impl-type-params.rs:30:13
115+
--> $DIR/kindck-impl-type-params.rs:30:19
116116
|
117117
LL | fn foo<'a>() {
118118
| -- lifetime `'a` defined here
119119
LL | let t: S<&'a isize> = S(marker::PhantomData);
120120
LL | let a = &t as &dyn Gettable<&'a isize>;
121-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
121+
| ^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
122122

123123
error: aborting due to 7 previous errors
124124

tests/ui/nll/user-annotations/cast_static_lifetime.stderr

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ error[E0597]: `x` does not live long enough
44
LL | let x = 22_u32;
55
| - binding `x` declared here
66
LL | let y: &u32 = (&x) as &'static u32;
7-
| ^^^^----------------
7+
| ^^^^ ------------ type annotation requires that `x` is borrowed for `'static`
88
| |
99
| borrowed value does not live long enough
10-
| type annotation requires that `x` is borrowed for `'static`
1110
LL | }
1211
| - `x` dropped here while still borrowed
1312

tests/ui/nll/user-annotations/type_ascription_static_lifetime.stderr

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@ error[E0597]: `x` does not live long enough
44
LL | let x = 22_u32;
55
| - binding `x` declared here
66
LL | let y: &u32 = type_ascribe!(&x, &'static u32);
7-
| --------------^^---------------
8-
| | |
9-
| | borrowed value does not live long enough
10-
| type annotation requires that `x` is borrowed for `'static`
7+
| ^^ ------------ type annotation requires that `x` is borrowed for `'static`
8+
| |
9+
| borrowed value does not live long enough
1110
LL | }
1211
| - `x` dropped here while still borrowed
1312

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
error: lifetime may not live long enough
2-
--> $DIR/type-checking-test-3.rs:11:13
2+
--> $DIR/type-checking-test-3.rs:11:18
33
|
44
LL | fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) {
55
| -- lifetime `'a` defined here
66
LL | let _ = x as &dyn Bar<'a>; // Error
7-
| ^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
7+
| ^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
88

99
error: lifetime may not live long enough
10-
--> $DIR/type-checking-test-3.rs:16:13
10+
--> $DIR/type-checking-test-3.rs:16:18
1111
|
1212
LL | fn test_wrong2<'a>(x: &dyn Foo<'a>) {
1313
| -- lifetime `'a` defined here
1414
LL | let _ = x as &dyn Bar<'static>; // Error
15-
| ^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
15+
| ^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
1616

1717
error: aborting due to 2 previous errors
1818

tests/ui/traits/trait-upcasting/type-checking-test-4.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
error: lifetime may not live long enough
2-
--> $DIR/type-checking-test-4.rs:19:13
2+
--> $DIR/type-checking-test-4.rs:19:18
33
|
44
LL | fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) {
55
| -- lifetime `'a` defined here
66
LL | let _ = x as &dyn Bar<'static, 'a>; // Error
7-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
7+
| ^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
88

99
error: lifetime may not live long enough
10-
--> $DIR/type-checking-test-4.rs:24:13
10+
--> $DIR/type-checking-test-4.rs:24:18
1111
|
1212
LL | fn test_wrong2<'a>(x: &dyn Foo<'static>, y: &'a u32) {
1313
| -- lifetime `'a` defined here
1414
LL | let _ = x as &dyn Bar<'a, 'static>; // Error
15-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
15+
| ^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
1616

1717
error: lifetime may not live long enough
1818
--> $DIR/type-checking-test-4.rs:30:5

0 commit comments

Comments
 (0)