Skip to content

Commit c94f774

Browse files
author
Lukas Markeffsky
committed
use more accurate spans for user type ascriptions
1 parent 38ab4be commit c94f774

12 files changed

+43
-32
lines changed

compiler/rustc_middle/src/thir.rs

+3-1
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ 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, .. } | ValueTypeAscription { source, .. } => {
133133
visitor.visit_expr(&visitor.thir()[source])
134134
}
135135
Closure(box ClosureExpr {

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

+8-6
Original file line numberDiff line numberDiff line change
@@ -470,14 +470,15 @@ 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
});
@@ -486,7 +487,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
486487
this.cfg.push(
487488
block,
488489
Statement {
489-
source_info,
490+
source_info: ty_source_info,
490491
kind: StatementKind::AscribeUserType(
491492
Box::new((
492493
place,
@@ -499,22 +500,23 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
499500
}
500501
block.and(place_builder)
501502
}
502-
ExprKind::ValueTypeAscription { source, ref user_ty } => {
503+
ExprKind::ValueTypeAscription { source, ref user_ty, user_ty_span } => {
503504
let source_expr = &this.thir[source];
504505
let temp = unpack!(
505506
block = this.as_temp(block, source_expr.temp_lifetime, source, mutability)
506507
);
507508
if let Some(user_ty) = user_ty {
509+
let ty_source_info = this.source_info(user_ty_span);
508510
let annotation_index =
509511
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
510-
span: source_info.span,
512+
span: user_ty_span,
511513
user_ty: user_ty.clone(),
512514
inferred_ty: expr.ty,
513515
});
514516
this.cfg.push(
515517
block,
516518
Statement {
517-
source_info,
519+
source_info: ty_source_info,
518520
kind: StatementKind::AscribeUserType(
519521
Box::new((
520522
Place::from(temp),

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,7 @@ impl<'tcx> Cx<'tcx> {
773773
ExprKind::ValueTypeAscription {
774774
source: cast_expr,
775775
user_ty: Some(Box::new(*user_ty)),
776+
user_ty_span: cast_ty.span,
776777
}
777778
} else {
778779
cast
@@ -784,9 +785,17 @@ impl<'tcx> Cx<'tcx> {
784785
debug!("make_mirror_unadjusted: (type) user_ty={:?}", user_ty);
785786
let mirrored = self.mirror_expr(source);
786787
if source.is_syntactic_place_expr() {
787-
ExprKind::PlaceTypeAscription { source: mirrored, user_ty }
788+
ExprKind::PlaceTypeAscription {
789+
source: mirrored,
790+
user_ty,
791+
user_ty_span: ty.span,
792+
}
788793
} else {
789-
ExprKind::ValueTypeAscription { source: mirrored, user_ty }
794+
ExprKind::ValueTypeAscription {
795+
source: mirrored,
796+
user_ty,
797+
user_ty_span: ty.span,
798+
}
790799
}
791800
}
792801
hir::ExprKind::DropTemps(source) => ExprKind::Use { source: self.mirror_expr(source) },

compiler/rustc_mir_build/src/thir/print.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -454,14 +454,14 @@ 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, .. } => {
458458
print_indented!(self, "PlaceTypeAscription {", depth_lvl);
459459
print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
460460
print_indented!(self, "source:", depth_lvl + 1);
461461
self.print_expr(*source, depth_lvl + 2);
462462
print_indented!(self, "}", depth_lvl);
463463
}
464-
ValueTypeAscription { source, user_ty } => {
464+
ValueTypeAscription { source, user_ty, .. } => {
465465
print_indented!(self, "ValueTypeAscription {", depth_lvl);
466466
print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
467467
print_indented!(self, "source:", depth_lvl + 1);

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

+2-2
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

+2-2
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

+2-2
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

+1-2
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

+3-4
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

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

+4-4
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)