Skip to content

Commit 85f4395

Browse files
author
Lukas Markeffsky
committed
replace "cast" with "coercion" where applicable
This changes the remaining span for the cast, because the new `Cast` category has a higher priority (lower `Ord`) than the old `Coercion` category, so we no longer report the region error for the "unsizing" coercion from `*const Trait` to itself.
1 parent ded776d commit 85f4395

12 files changed

+38
-29
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,9 @@ impl<'tcx> BorrowExplanation<'tcx> {
333333
}
334334
}
335335

336-
if let ConstraintCategory::Cast { unsize_to: Some(unsize_ty) } = category {
336+
if let ConstraintCategory::Cast { is_coercion: true, unsize_to: Some(unsize_ty) } =
337+
category
338+
{
337339
self.add_object_lifetime_default_note(tcx, err, unsize_ty);
338340
}
339341
self.add_lifetime_bound_suggestion_to_diagnostic(err, &category, span, region_name);

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +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 ",
50+
ConstraintCategory::Cast { is_coercion: false, .. } => "cast ",
51+
ConstraintCategory::Cast { is_coercion: true, .. } => "coercion ",
5152
ConstraintCategory::CallArgument(_) => "argument ",
5253
ConstraintCategory::TypeAnnotation => "type annotation ",
5354
ConstraintCategory::ClosureBounds => "closure body ",

compiler/rustc_borrowck/src/type_check/mod.rs

+15-11
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::Cast { 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::Cast { 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::Cast { 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::Cast { 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::Cast { 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::Cast { unsize_to: None },
2108+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
21092109
) {
21102110
span_mirbug!(
21112111
self,
@@ -2130,6 +2130,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21302130
trait_ref,
21312131
location.to_locations(),
21322132
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::Cast { 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::Cast { 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::Cast { 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::Cast { 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 { unsize_to: None },
2433+
ConstraintCategory::Cast {
2434+
is_coercion: false,
2435+
unsize_to: None,
2436+
},
24332437
)
24342438
.unwrap();
24352439
}

compiler/rustc_middle/src/mir/query.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,9 @@ pub enum ConstraintCategory<'tcx> {
234234
UseAsStatic,
235235
TypeAnnotation,
236236
Cast {
237-
/// Whether this is an unsizing cast and if yes, this contains the target type.
237+
/// Whether this cast is a coercion.
238+
is_coercion: bool,
239+
/// Whether this is an unsizing coercion and if yes, this contains the target type.
238240
/// Region variables are erased to ReErased.
239241
#[derive_where(skip)]
240242
unsize_to: Option<Ty<'tcx>>,

tests/ui/borrowck/two-phase-surprise-no-conflict.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ LL | reg.register_univ(Box::new(CapturePass::new(&reg.sess_mut)));
7575
| ^^^^^^^^^^^^^^^^^^-----------------------------------------^
7676
| | | |
7777
| | | immutable borrow occurs here
78-
| | cast requires that `reg.sess_mut` is borrowed for `'a`
78+
| | coercion requires that `reg.sess_mut` is borrowed for `'a`
7979
| mutable borrow occurs here
8080
|
8181
= note: due to object lifetime defaults, `Box<dyn for<'b> LateLintPass<'b>>` actually means `Box<(dyn for<'b> LateLintPass<'b> + 'static)>`
@@ -119,7 +119,7 @@ LL | reg.register_univ(Box::new(CapturePass::new_mut(&mut reg.sess_mut)));
119119
| ^^^^^^^^^^^^^^^^^^-------------------------------------------------^
120120
| | | |
121121
| | | first mutable borrow occurs here
122-
| | cast requires that `reg.sess_mut` is borrowed for `'a`
122+
| | coercion requires that `reg.sess_mut` is borrowed for `'a`
123123
| second mutable borrow occurs here
124124
|
125125
= note: due to object lifetime defaults, `Box<dyn for<'b> LateLintPass<'b>>` actually means `Box<(dyn for<'b> LateLintPass<'b> + 'static)>`

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ error: lifetime may not live long enough
138138
LL | fn extend_to_static<'a>(ptr: *const dyn Trait<'a>) {
139139
| -- lifetime `'a` defined here
140140
LL | require_static(ptr as _)
141-
| ^^^ cast requires that `'a` must outlive `'static`
141+
| ^^^^^^^^ cast requires that `'a` must outlive `'static`
142142

143143
error: aborting due to 9 previous errors
144144

tests/ui/dropck/dropck_trait_cycle_checked.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0597]: `o2` does not live long enough
22
--> $DIR/dropck_trait_cycle_checked.rs:111:13
33
|
44
LL | let (o1, o2, o3): (Box<dyn Obj>, Box<dyn Obj>, Box<dyn Obj>) = (O::new(), O::new(), O::new());
5-
| -- binding `o2` declared here -------- cast requires that `o2` is borrowed for `'static`
5+
| -- binding `o2` declared here -------- coercion requires that `o2` is borrowed for `'static`
66
LL | o1.set0(&o2);
77
| ^^^ borrowed value does not live long enough
88
...
@@ -15,7 +15,7 @@ error[E0597]: `o3` does not live long enough
1515
--> $DIR/dropck_trait_cycle_checked.rs:112:13
1616
|
1717
LL | let (o1, o2, o3): (Box<dyn Obj>, Box<dyn Obj>, Box<dyn Obj>) = (O::new(), O::new(), O::new());
18-
| -- binding `o3` declared here -------- cast requires that `o3` is borrowed for `'static`
18+
| -- binding `o3` declared here -------- coercion requires that `o3` is borrowed for `'static`
1919
LL | o1.set0(&o2);
2020
LL | o1.set1(&o3);
2121
| ^^^ borrowed value does not live long enough
@@ -29,7 +29,7 @@ error[E0597]: `o2` does not live long enough
2929
--> $DIR/dropck_trait_cycle_checked.rs:113:13
3030
|
3131
LL | let (o1, o2, o3): (Box<dyn Obj>, Box<dyn Obj>, Box<dyn Obj>) = (O::new(), O::new(), O::new());
32-
| -- binding `o2` declared here -------- cast requires that `o2` is borrowed for `'static`
32+
| -- binding `o2` declared here -------- coercion requires that `o2` is borrowed for `'static`
3333
...
3434
LL | o2.set0(&o2);
3535
| ^^^ borrowed value does not live long enough
@@ -43,7 +43,7 @@ error[E0597]: `o3` does not live long enough
4343
--> $DIR/dropck_trait_cycle_checked.rs:114:13
4444
|
4545
LL | let (o1, o2, o3): (Box<dyn Obj>, Box<dyn Obj>, Box<dyn Obj>) = (O::new(), O::new(), O::new());
46-
| -- binding `o3` declared here -------- cast requires that `o3` is borrowed for `'static`
46+
| -- binding `o3` declared here -------- coercion requires that `o3` is borrowed for `'static`
4747
...
4848
LL | o2.set1(&o3);
4949
| ^^^ borrowed value does not live long enough
@@ -57,7 +57,7 @@ error[E0597]: `o1` does not live long enough
5757
--> $DIR/dropck_trait_cycle_checked.rs:115:13
5858
|
5959
LL | let (o1, o2, o3): (Box<dyn Obj>, Box<dyn Obj>, Box<dyn Obj>) = (O::new(), O::new(), O::new());
60-
| -- binding `o1` declared here -------- cast requires that `o1` is borrowed for `'static`
60+
| -- binding `o1` declared here -------- coercion requires that `o1` is borrowed for `'static`
6161
...
6262
LL | o3.set0(&o1);
6363
| ^^^ borrowed value does not live long enough
@@ -71,7 +71,7 @@ error[E0597]: `o2` does not live long enough
7171
--> $DIR/dropck_trait_cycle_checked.rs:116:13
7272
|
7373
LL | let (o1, o2, o3): (Box<dyn Obj>, Box<dyn Obj>, Box<dyn Obj>) = (O::new(), O::new(), O::new());
74-
| -- binding `o2` declared here -------- cast requires that `o2` is borrowed for `'static`
74+
| -- binding `o2` declared here -------- coercion requires that `o2` is borrowed for `'static`
7575
...
7676
LL | o3.set1(&o2);
7777
| ^^^ borrowed value does not live long enough

tests/ui/lifetimes/issue-90600-expected-return-static-indirect.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let refcell = RefCell::new(&mut foo);
77
| ^^^^^^^^ borrowed value does not live long enough
88
LL |
99
LL | let read = &refcell as &RefCell<dyn Read>;
10-
| -------- cast requires that `foo` is borrowed for `'static`
10+
| -------- coercion requires that `foo` is borrowed for `'static`
1111
...
1212
LL | }
1313
| - `foo` dropped here while still borrowed
@@ -19,7 +19,7 @@ LL | fn inner(mut foo: &[u8]) {
1919
| - let's call the lifetime of this reference `'1`
2020
...
2121
LL | let read = &refcell as &RefCell<dyn Read>;
22-
| ^^^^^^^^ cast requires that `'1` must outlive `'static`
22+
| ^^^^^^^^ coercion requires that `'1` must outlive `'static`
2323

2424
error: aborting due to 2 previous errors
2525

tests/ui/nll/issue-54779-anon-static-lifetime.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | cx: &dyn DebugContext,
55
| - let's call the lifetime of this reference `'1`
66
...
77
LL | bar.debug_with(cx);
8-
| ^^ cast requires that `'1` must outlive `'static`
8+
| ^^ coercion requires that `'1` must outlive `'static`
99

1010
error: aborting due to 1 previous error
1111

tests/ui/suggestions/lifetimes/suggest-using-tick-underscore-lifetime-in-return-trait-object.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: lifetime may not live long enough
44
LL | fn foo<T: Any>(value: &T) -> Box<dyn Any> {
55
| - let's call the lifetime of this reference `'1`
66
LL | Box::new(value) as Box<dyn Any>
7-
| ^^^^^^^^^^^^^^^ cast requires that `'1` must outlive `'static`
7+
| ^^^^^^^^^^^^^^^ coercion requires that `'1` must outlive `'static`
88
|
99
help: to declare that the trait object captures data from argument `value`, you can add an explicit `'_` lifetime bound
1010
|

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 cast 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

tests/ui/traits/trait-object-lifetime-default-note.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | let r = &local;
77
| ^^^^^^ borrowed value does not live long enough
88
...
99
LL | require_box(Box::new(r));
10-
| ----------- cast requires that `local` is borrowed for `'static`
10+
| ----------- coercion requires that `local` is borrowed for `'static`
1111
...
1212
LL | }
1313
| - `local` dropped here while still borrowed

0 commit comments

Comments
 (0)