Skip to content

Commit 0762b2c

Browse files
author
Lukas Markeffsky
committed
address review comments (round 1)
1 parent 1dc3411 commit 0762b2c

File tree

8 files changed

+61
-24
lines changed

8 files changed

+61
-24
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
295295
}
296296
}
297297

298-
if let ConstraintCategory::Coercion { unsize_to: Some(unsize_ty) } = category {
298+
if let ConstraintCategory::Cast { is_coercion: true, unsize_to: Some(unsize_ty) } =
299+
category
300+
{
299301
self.add_object_lifetime_default_note(tcx, err, unsize_ty);
300302
}
301303
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ impl<'tcx> ConstraintDescription for ConstraintCategory<'tcx> {
4747
ConstraintCategory::Yield => "yielding this value ",
4848
ConstraintCategory::UseAsConst => "using this value as a constant ",
4949
ConstraintCategory::UseAsStatic => "using this value as a static ",
50-
ConstraintCategory::Cast => "cast ",
51-
ConstraintCategory::Coercion { .. } => "coercion ",
50+
ConstraintCategory::Cast { is_coercion: false, .. } => "cast ",
51+
ConstraintCategory::Cast { is_coercion: true, .. } => "coercion ",
5252
ConstraintCategory::CallArgument(_) => "argument ",
5353
ConstraintCategory::TypeAnnotation => "type annotation ",
5454
ConstraintCategory::ClosureBounds => "closure body ",

compiler/rustc_borrowck/src/type_check/mod.rs

+16-12
Original file line numberDiff line numberDiff line change
@@ -2009,15 +2009,15 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20092009
self.prove_predicate(
20102010
ty::ClauseKind::WellFormed(src_ty.into()),
20112011
location.to_locations(),
2012-
ConstraintCategory::Coercion { unsize_to: None },
2012+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20132013
);
20142014

20152015
let src_ty = self.normalize(src_ty, location);
20162016
if let Err(terr) = self.sub_types(
20172017
src_ty,
20182018
*ty,
20192019
location.to_locations(),
2020-
ConstraintCategory::Coercion { unsize_to: None },
2020+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20212021
) {
20222022
span_mirbug!(
20232023
self,
@@ -2038,7 +2038,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20382038
self.prove_predicate(
20392039
ty::ClauseKind::WellFormed(src_ty.into()),
20402040
location.to_locations(),
2041-
ConstraintCategory::Coercion { unsize_to: None },
2041+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20422042
);
20432043

20442044
// The type that we see in the fcx is like
@@ -2051,7 +2051,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20512051
src_ty,
20522052
*ty,
20532053
location.to_locations(),
2054-
ConstraintCategory::Coercion { unsize_to: None },
2054+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20552055
) {
20562056
span_mirbug!(
20572057
self,
@@ -2076,7 +2076,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20762076
ty_fn_ptr_from,
20772077
*ty,
20782078
location.to_locations(),
2079-
ConstraintCategory::Coercion { unsize_to: None },
2079+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20802080
) {
20812081
span_mirbug!(
20822082
self,
@@ -2105,7 +2105,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21052105
ty_fn_ptr_from,
21062106
*ty,
21072107
location.to_locations(),
2108-
ConstraintCategory::Coercion { unsize_to: None },
2108+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
21092109
) {
21102110
span_mirbug!(
21112111
self,
@@ -2129,7 +2129,8 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21292129
self.prove_trait_ref(
21302130
trait_ref,
21312131
location.to_locations(),
2132-
ConstraintCategory::Coercion {
2132+
ConstraintCategory::Cast {
2133+
is_coercion: true,
21332134
unsize_to: Some(tcx.fold_regions(ty, |r, _| {
21342135
if let ty::ReVar(_) = r.kind() {
21352136
tcx.lifetimes.re_erased
@@ -2157,7 +2158,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21572158
.iter()
21582159
.map(|predicate| predicate.with_self_ty(tcx, self_ty)),
21592160
location.to_locations(),
2160-
ConstraintCategory::Coercion { unsize_to: None },
2161+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
21612162
);
21622163

21632164
let outlives_predicate = tcx.mk_predicate(Binder::dummy(
@@ -2168,7 +2169,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21682169
self.prove_predicate(
21692170
outlives_predicate,
21702171
location.to_locations(),
2171-
ConstraintCategory::Coercion { unsize_to: None },
2172+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
21722173
);
21732174
}
21742175

@@ -2186,7 +2187,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21862187
*ty_from,
21872188
*ty_to,
21882189
location.to_locations(),
2189-
ConstraintCategory::Coercion { unsize_to: None },
2190+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
21902191
) {
21912192
span_mirbug!(
21922193
self,
@@ -2248,7 +2249,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22482249
*ty_elem,
22492250
*ty_to,
22502251
location.to_locations(),
2251-
ConstraintCategory::Coercion { unsize_to: None },
2252+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
22522253
) {
22532254
span_mirbug!(
22542255
self,
@@ -2429,7 +2430,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24292430
src_obj,
24302431
dst_obj,
24312432
location.to_locations(),
2432-
ConstraintCategory::Cast,
2433+
ConstraintCategory::Cast {
2434+
is_coercion: false,
2435+
unsize_to: None,
2436+
},
24332437
)
24342438
.unwrap();
24352439
}

compiler/rustc_hir_typeck/src/cast.rs

+30-3
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,41 @@ impl<'a, 'tcx> CastCheck<'tcx> {
318318
(PointerKind::VTable(_), PointerKind::VTable(_)) => {
319319
err.note("the trait objects may have different vtables");
320320
}
321-
(PointerKind::OfParam(_) | PointerKind::OfAlias(_), _)
322-
| (_, PointerKind::OfParam(_) | PointerKind::OfAlias(_)) => {
321+
(
322+
PointerKind::OfParam(_) | PointerKind::OfAlias(_),
323+
PointerKind::OfParam(_)
324+
| PointerKind::OfAlias(_)
325+
| PointerKind::VTable(_)
326+
| PointerKind::Length,
327+
)
328+
| (
329+
PointerKind::VTable(_) | PointerKind::Length,
330+
PointerKind::OfParam(_) | PointerKind::OfAlias(_),
331+
) => {
323332
err.note("the pointers may have different metadata");
324333
}
325334
(PointerKind::VTable(_), PointerKind::Length)
326335
| (PointerKind::Length, PointerKind::VTable(_)) => {
327336
err.note("the pointers have different metadata");
328337
}
329-
_ => span_bug!(self.span, "unexpected cast error: {e:?}"),
338+
(
339+
PointerKind::Thin,
340+
PointerKind::Thin
341+
| PointerKind::VTable(_)
342+
| PointerKind::Length
343+
| PointerKind::OfParam(_)
344+
| PointerKind::OfAlias(_),
345+
)
346+
| (
347+
PointerKind::VTable(_)
348+
| PointerKind::Length
349+
| PointerKind::OfParam(_)
350+
| PointerKind::OfAlias(_),
351+
PointerKind::Thin,
352+
)
353+
| (PointerKind::Length, PointerKind::Length) => {
354+
span_bug!(self.span, "unexpected cast error: {e:?}")
355+
}
330356
}
331357

332358
err.emit();
@@ -892,6 +918,7 @@ impl<'a, 'tcx> CastCheck<'tcx> {
892918
));
893919

894920
// `dyn Src = dyn Dst`, this checks for matching traits/generics
921+
// This is `demand_eqtype`, but inlined to give a better error.
895922
let cause = fcx.misc(self.span);
896923
if fcx
897924
.at(&cause, fcx.param_env)

compiler/rustc_middle/src/mir/query.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,9 @@ pub enum ConstraintCategory<'tcx> {
233233
UseAsConst,
234234
UseAsStatic,
235235
TypeAnnotation,
236-
Cast,
237-
Coercion {
236+
Cast {
237+
/// Whether this cast is a coercion.
238+
is_coercion: bool,
238239
/// Whether this is an unsizing coercion and if yes, this contains the target type.
239240
/// Region variables are erased to ReErased.
240241
#[derive_where(skip)]

compiler/rustc_middle/src/thir/visit.rs

+2-1
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, .. } | ValueTypeAscription { source, .. } => {
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/thir/print.rs

+4-2
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/ui/traits/trait-object-lifetime-default-note.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ fn main() {
88
//~| NOTE borrowed value does not live long enough
99
//~| NOTE due to object lifetime defaults, `Box<dyn A>` actually means `Box<(dyn A + 'static)>`
1010
require_box(Box::new(r));
11-
//~^ NOTE requires that `local` is borrowed for `'static`
11+
//~^ NOTE coercion requires that `local` is borrowed for `'static`
1212

1313
let _ = 0;
1414
} //~ NOTE `local` dropped here while still borrowed

0 commit comments

Comments
 (0)