use more accurate spans for user type ascriptions

This commit is contained in:
Lukas Markeffsky 2024-09-10 18:47:10 +02:00
parent b52dea8230
commit d1e82d438f
13 changed files with 52 additions and 38 deletions

View File

@ -453,12 +453,14 @@ pub enum ExprKind<'tcx> {
source: ExprId,
/// Type that the user gave to this expression
user_ty: UserTy<'tcx>,
user_ty_span: Span,
},
/// A type ascription on a value, e.g. `42: i32`.
/// A type ascription on a value, e.g. `type_ascribe!(42, i32)` or `42 as i32`.
ValueTypeAscription {
source: ExprId,
/// Type that the user gave to this expression
user_ty: UserTy<'tcx>,
user_ty_span: Span,
},
/// A closure definition.
Closure(Box<ClosureExpr<'tcx>>),

View File

@ -129,7 +129,8 @@ pub fn walk_expr<'thir, 'tcx: 'thir, V: Visitor<'thir, 'tcx>>(
visitor.visit_expr(&visitor.thir()[base.base]);
}
}
PlaceTypeAscription { source, user_ty: _ } | ValueTypeAscription { source, user_ty: _ } => {
PlaceTypeAscription { source, user_ty: _, user_ty_span: _ }
| ValueTypeAscription { source, user_ty: _, user_ty_span: _ } => {
visitor.visit_expr(&visitor.thir()[source])
}
Closure(box ClosureExpr {

View File

@ -470,21 +470,22 @@ fn expr_as_place(
block.and(place_builder)
}
ExprKind::PlaceTypeAscription { source, ref user_ty } => {
ExprKind::PlaceTypeAscription { source, ref user_ty, user_ty_span } => {
let place_builder = unpack!(
block = this.expr_as_place(block, source, mutability, fake_borrow_temps,)
);
if let Some(user_ty) = user_ty {
let ty_source_info = this.source_info(user_ty_span);
let annotation_index =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
span: user_ty_span,
user_ty: user_ty.clone(),
inferred_ty: expr.ty,
});
let place = place_builder.to_place(this);
this.cfg.push(block, Statement {
source_info,
source_info: ty_source_info,
kind: StatementKind::AscribeUserType(
Box::new((place, UserTypeProjection {
base: annotation_index,
@ -496,20 +497,21 @@ fn expr_as_place(
}
block.and(place_builder)
}
ExprKind::ValueTypeAscription { source, ref user_ty } => {
ExprKind::ValueTypeAscription { source, ref user_ty, user_ty_span } => {
let source_expr = &this.thir[source];
let temp = unpack!(
block = this.as_temp(block, source_expr.temp_lifetime, source, mutability)
);
if let Some(user_ty) = user_ty {
let ty_source_info = this.source_info(user_ty_span);
let annotation_index =
this.canonical_user_type_annotations.push(CanonicalUserTypeAnnotation {
span: source_info.span,
span: user_ty_span,
user_ty: user_ty.clone(),
inferred_ty: expr.ty,
});
this.cfg.push(block, Statement {
source_info,
source_info: ty_source_info,
kind: StatementKind::AscribeUserType(
Box::new((Place::from(temp), UserTypeProjection {
base: annotation_index,

View File

@ -840,6 +840,7 @@ fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx>
ExprKind::ValueTypeAscription {
source: cast_expr,
user_ty: Some(Box::new(*user_ty)),
user_ty_span: cast_ty.span,
}
} else {
cast
@ -851,9 +852,17 @@ fn make_mirror_unadjusted(&mut self, expr: &'tcx hir::Expr<'tcx>) -> Expr<'tcx>
debug!("make_mirror_unadjusted: (type) user_ty={:?}", user_ty);
let mirrored = self.mirror_expr(source);
if source.is_syntactic_place_expr() {
ExprKind::PlaceTypeAscription { source: mirrored, user_ty }
ExprKind::PlaceTypeAscription {
source: mirrored,
user_ty,
user_ty_span: ty.span,
}
} else {
ExprKind::ValueTypeAscription { source: mirrored, user_ty }
ExprKind::ValueTypeAscription {
source: mirrored,
user_ty,
user_ty_span: ty.span,
}
}
}
hir::ExprKind::DropTemps(source) => ExprKind::Use { source: self.mirror_expr(source) },

View File

@ -454,16 +454,18 @@ fn print_expr_kind(&mut self, expr_kind: &ExprKind<'tcx>, depth_lvl: usize) {
self.print_adt_expr(&**adt_expr, depth_lvl + 1);
print_indented!(self, "}", depth_lvl);
}
PlaceTypeAscription { source, user_ty } => {
PlaceTypeAscription { source, user_ty, user_ty_span } => {
print_indented!(self, "PlaceTypeAscription {", depth_lvl);
print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
print_indented!(self, format!("user_ty_span: {:?}", user_ty_span), depth_lvl + 1);
print_indented!(self, "source:", depth_lvl + 1);
self.print_expr(*source, depth_lvl + 2);
print_indented!(self, "}", depth_lvl);
}
ValueTypeAscription { source, user_ty } => {
ValueTypeAscription { source, user_ty, user_ty_span } => {
print_indented!(self, "ValueTypeAscription {", depth_lvl);
print_indented!(self, format!("user_ty: {:?}", user_ty), depth_lvl + 1);
print_indented!(self, format!("user_ty_span: {:?}", user_ty_span), depth_lvl + 1);
print_indented!(self, "source:", depth_lvl + 1);
self.print_expr(*source, depth_lvl + 2);
print_indented!(self, "}", depth_lvl);

View File

@ -1,8 +1,8 @@
// MIR for `address_of_reborrow` after SimplifyCfg-initial
| User Type Annotations
| 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]
| 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
| 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]
| 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
| 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]
| 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]
| 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 @@
| 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
| 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]
| 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]
| 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]
| 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
| 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]
| 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
| 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]
| 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]
| 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 @@
| 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
| 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]
| 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]
| 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]
| 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
| 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]
| 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
| 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]
| 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]
| 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]

View File

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

View File

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

View File

@ -112,13 +112,13 @@ LL | struct Foo; // does not impl Copy
|
error: lifetime may not live long enough
--> $DIR/kindck-impl-type-params.rs:30:13
--> $DIR/kindck-impl-type-params.rs:30:19
|
LL | fn foo<'a>() {
| -- lifetime `'a` defined here
LL | let t: S<&'a isize> = S(marker::PhantomData);
LL | let a = &t as &dyn Gettable<&'a isize>;
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
| ^^^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: aborting due to 7 previous errors

View File

@ -4,10 +4,9 @@ error[E0597]: `x` does not live long enough
LL | let x = 22_u32;
| - binding `x` declared here
LL | let y: &u32 = (&x) as &'static u32;
| ^^^^----------------
| ^^^^ ------------ type annotation requires that `x` is borrowed for `'static`
| |
| borrowed value does not live long enough
| type annotation requires that `x` is borrowed for `'static`
LL | }
| - `x` dropped here while still borrowed

View File

@ -4,10 +4,9 @@ error[E0597]: `x` does not live long enough
LL | let x = 22_u32;
| - binding `x` declared here
LL | let y: &u32 = type_ascribe!(&x, &'static u32);
| --------------^^---------------
| | |
| | borrowed value does not live long enough
| type annotation requires that `x` is borrowed for `'static`
| ^^ ------------ type annotation requires that `x` is borrowed for `'static`
| |
| borrowed value does not live long enough
LL | }
| - `x` dropped here while still borrowed

View File

@ -1,18 +1,18 @@
error: lifetime may not live long enough
--> $DIR/type-checking-test-3.rs:11:13
--> $DIR/type-checking-test-3.rs:11:18
|
LL | fn test_wrong1<'a>(x: &dyn Foo<'static>, y: &'a u32) {
| -- lifetime `'a` defined here
LL | let _ = x as &dyn Bar<'a>; // Error
| ^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
| ^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: lifetime may not live long enough
--> $DIR/type-checking-test-3.rs:16:13
--> $DIR/type-checking-test-3.rs:16:18
|
LL | fn test_wrong2<'a>(x: &dyn Foo<'a>) {
| -- lifetime `'a` defined here
LL | let _ = x as &dyn Bar<'static>; // Error
| ^^^^^^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
| ^^^^^^^^^^^^^^^^^ type annotation requires that `'a` must outlive `'static`
error: aborting due to 2 previous errors

View File

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