Skip to content

Commit 5e60d1f

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 d1e82d4 commit 5e60d1f

12 files changed

+38
-29
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 3 additions & 1 deletion
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

Lines changed: 2 additions & 1 deletion
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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,15 +2007,15 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20072007
self.prove_predicate(
20082008
ty::ClauseKind::WellFormed(src_ty.into()),
20092009
location.to_locations(),
2010-
ConstraintCategory::Cast { unsize_to: None },
2010+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20112011
);
20122012

20132013
let src_ty = self.normalize(src_ty, location);
20142014
if let Err(terr) = self.sub_types(
20152015
src_ty,
20162016
*ty,
20172017
location.to_locations(),
2018-
ConstraintCategory::Cast { unsize_to: None },
2018+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20192019
) {
20202020
span_mirbug!(
20212021
self,
@@ -2036,7 +2036,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20362036
self.prove_predicate(
20372037
ty::ClauseKind::WellFormed(src_ty.into()),
20382038
location.to_locations(),
2039-
ConstraintCategory::Cast { unsize_to: None },
2039+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20402040
);
20412041

20422042
// The type that we see in the fcx is like
@@ -2049,7 +2049,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20492049
src_ty,
20502050
*ty,
20512051
location.to_locations(),
2052-
ConstraintCategory::Cast { unsize_to: None },
2052+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20532053
) {
20542054
span_mirbug!(
20552055
self,
@@ -2074,7 +2074,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
20742074
ty_fn_ptr_from,
20752075
*ty,
20762076
location.to_locations(),
2077-
ConstraintCategory::Cast { unsize_to: None },
2077+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
20782078
) {
20792079
span_mirbug!(
20802080
self,
@@ -2103,7 +2103,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21032103
ty_fn_ptr_from,
21042104
*ty,
21052105
location.to_locations(),
2106-
ConstraintCategory::Cast { unsize_to: None },
2106+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
21072107
) {
21082108
span_mirbug!(
21092109
self,
@@ -2128,6 +2128,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21282128
trait_ref,
21292129
location.to_locations(),
21302130
ConstraintCategory::Cast {
2131+
is_coercion: true,
21312132
unsize_to: Some(tcx.fold_regions(ty, |r, _| {
21322133
if let ty::ReVar(_) = r.kind() {
21332134
tcx.lifetimes.re_erased
@@ -2155,7 +2156,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21552156
.iter()
21562157
.map(|predicate| predicate.with_self_ty(tcx, self_ty)),
21572158
location.to_locations(),
2158-
ConstraintCategory::Cast { unsize_to: None },
2159+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
21592160
);
21602161

21612162
let outlives_predicate = tcx.mk_predicate(Binder::dummy(
@@ -2166,7 +2167,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21662167
self.prove_predicate(
21672168
outlives_predicate,
21682169
location.to_locations(),
2169-
ConstraintCategory::Cast { unsize_to: None },
2170+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
21702171
);
21712172
}
21722173

@@ -2184,7 +2185,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
21842185
*ty_from,
21852186
*ty_to,
21862187
location.to_locations(),
2187-
ConstraintCategory::Cast { unsize_to: None },
2188+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
21882189
) {
21892190
span_mirbug!(
21902191
self,
@@ -2246,7 +2247,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
22462247
*ty_elem,
22472248
*ty_to,
22482249
location.to_locations(),
2249-
ConstraintCategory::Cast { unsize_to: None },
2250+
ConstraintCategory::Cast { is_coercion: true, unsize_to: None },
22502251
) {
22512252
span_mirbug!(
22522253
self,
@@ -2427,7 +2428,10 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
24272428
src_obj,
24282429
dst_obj,
24292430
location.to_locations(),
2430-
ConstraintCategory::Cast { unsize_to: None },
2431+
ConstraintCategory::Cast {
2432+
is_coercion: false,
2433+
unsize_to: None,
2434+
},
24312435
)
24322436
.unwrap();
24332437
}

compiler/rustc_middle/src/mir/query.rs

Lines changed: 3 additions & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 6 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)