1
Fork 0

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

@ -470,21 +470,22 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
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 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
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 @@ impl<'tcx> Cx<'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 @@ impl<'tcx> Cx<'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 @@ impl<'a, 'tcx> ThirPrinter<'a, 'tcx> {
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);