Skip to content

Commit 1ba8d9d

Browse files
committed
Custom diagnostics
1 parent 07f91f1 commit 1ba8d9d

11 files changed

+175
-12
lines changed

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
410410
cx.add_sized_or_copy_bound_info(err, category, &path);
411411

412412
if let ConstraintCategory::Cast {
413+
is_raw_ptr_dyn_type_cast: _,
413414
is_implicit_coercion: true,
414415
unsize_to: Some(unsize_ty),
415416
} = category

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,23 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
538538
self.add_placeholder_from_predicate_note(&mut diag, &path);
539539
self.add_sized_or_copy_bound_info(&mut diag, category, &path);
540540

541+
for constraint in &path {
542+
if let ConstraintCategory::Cast { is_raw_ptr_dyn_type_cast: true, .. } =
543+
constraint.category
544+
{
545+
diag.span_note(
546+
constraint.span,
547+
format!("raw pointer casts of trait objects do not cast away lifetimes"),
548+
);
549+
diag.note(format!(
550+
"this was previously accepted by the compiler but was changed recently"
551+
));
552+
diag.help(format!(
553+
"see <https://github.com/rust-lang/rust/issues/141402> for more information"
554+
));
555+
}
556+
}
557+
541558
self.buffer_error(diag);
542559
}
543560

compiler/rustc_borrowck/src/region_infer/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,7 @@ impl<'tcx> RegionInferenceContext<'tcx> {
17401740
// should be as limited as possible; the note is prone to false positives and this
17411741
// constraint usually isn't best to blame.
17421742
ConstraintCategory::Cast {
1743+
is_raw_ptr_dyn_type_cast: _,
17431744
unsize_to: Some(unsize_ty),
17441745
is_implicit_coercion: true,
17451746
} if target_region == self.universal_regions().fr_static

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,15 +1098,23 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10981098
self.prove_predicate(
10991099
ty::ClauseKind::WellFormed(src_ty.into()),
11001100
location.to_locations(),
1101-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1101+
ConstraintCategory::Cast {
1102+
is_raw_ptr_dyn_type_cast: false,
1103+
is_implicit_coercion,
1104+
unsize_to: None,
1105+
},
11021106
);
11031107

11041108
let src_ty = self.normalize(src_ty, location);
11051109
if let Err(terr) = self.sub_types(
11061110
src_ty,
11071111
*ty,
11081112
location.to_locations(),
1109-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1113+
ConstraintCategory::Cast {
1114+
is_raw_ptr_dyn_type_cast: false,
1115+
is_implicit_coercion,
1116+
unsize_to: None,
1117+
},
11101118
) {
11111119
span_mirbug!(
11121120
self,
@@ -1127,7 +1135,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11271135
self.prove_predicate(
11281136
ty::ClauseKind::WellFormed(src_ty.into()),
11291137
location.to_locations(),
1130-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1138+
ConstraintCategory::Cast {
1139+
is_raw_ptr_dyn_type_cast: false,
1140+
is_implicit_coercion,
1141+
unsize_to: None,
1142+
},
11311143
);
11321144

11331145
// The type that we see in the fcx is like
@@ -1140,7 +1152,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11401152
src_ty,
11411153
*ty,
11421154
location.to_locations(),
1143-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1155+
ConstraintCategory::Cast {
1156+
is_raw_ptr_dyn_type_cast: false,
1157+
is_implicit_coercion,
1158+
unsize_to: None,
1159+
},
11441160
) {
11451161
span_mirbug!(
11461162
self,
@@ -1169,7 +1185,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11691185
ty_fn_ptr_from,
11701186
*ty,
11711187
location.to_locations(),
1172-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1188+
ConstraintCategory::Cast {
1189+
is_raw_ptr_dyn_type_cast: false,
1190+
is_implicit_coercion,
1191+
unsize_to: None,
1192+
},
11731193
) {
11741194
span_mirbug!(
11751195
self,
@@ -1202,7 +1222,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12021222
ty_fn_ptr_from,
12031223
*ty,
12041224
location.to_locations(),
1205-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1225+
ConstraintCategory::Cast {
1226+
is_raw_ptr_dyn_type_cast: false,
1227+
is_implicit_coercion,
1228+
unsize_to: None,
1229+
},
12061230
) {
12071231
span_mirbug!(
12081232
self,
@@ -1231,6 +1255,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12311255
trait_ref,
12321256
location.to_locations(),
12331257
ConstraintCategory::Cast {
1258+
is_raw_ptr_dyn_type_cast: false,
12341259
is_implicit_coercion,
12351260
unsize_to: Some(unsize_to),
12361261
},
@@ -1256,7 +1281,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12561281
*ty_from,
12571282
*ty_to,
12581283
location.to_locations(),
1259-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1284+
ConstraintCategory::Cast {
1285+
is_raw_ptr_dyn_type_cast: false,
1286+
is_implicit_coercion,
1287+
unsize_to: None,
1288+
},
12601289
) {
12611290
span_mirbug!(
12621291
self,
@@ -1319,7 +1348,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
13191348
*ty_elem,
13201349
*ty_to,
13211350
location.to_locations(),
1322-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1351+
ConstraintCategory::Cast {
1352+
is_raw_ptr_dyn_type_cast: false,
1353+
is_implicit_coercion,
1354+
unsize_to: None,
1355+
},
13231356
) {
13241357
span_mirbug!(
13251358
self,
@@ -1514,6 +1547,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
15141547
dst_obj,
15151548
location.to_locations(),
15161549
ConstraintCategory::Cast {
1550+
is_raw_ptr_dyn_type_cast: true,
15171551
is_implicit_coercion: false,
15181552
unsize_to: None,
15191553
},

compiler/rustc_middle/src/mir/query.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ pub enum ConstraintCategory<'tcx> {
116116
UseAsStatic,
117117
TypeAnnotation(AnnotationSource),
118118
Cast {
119+
is_raw_ptr_dyn_type_cast: bool,
119120
/// Whether this cast is a coercion that was automatically inserted by the compiler.
120121
is_implicit_coercion: bool,
121122
/// Whether this is an unsizing coercion and if yes, this contains the target type.

tests/ui/cast/ptr-to-ptr-different-regions.stderr

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,23 @@ LL | ptr as _
99
= note: requirement occurs because of a mutable pointer to `dyn Trait`
1010
= note: mutable pointers are invariant over their type parameter
1111
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
12+
note: raw pointer casts of trait objects do not cast away lifetimes
13+
--> $DIR/ptr-to-ptr-different-regions.rs:17:5
14+
|
15+
LL | ptr as _
16+
| ^^^^^^^^
17+
= note: this was previously accepted by the compiler but was changed recently
18+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
1219
help: consider changing the trait object's explicit `'static` bound to the lifetime of argument `ptr`
1320
|
14-
LL | fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'a) {
15-
| ~~
21+
LL - fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'static) {
22+
LL + fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'a) {
23+
|
1624
help: alternatively, add an explicit `'static` bound to this reference
1725
|
18-
LL | fn assert_static<'a>(ptr: *mut (dyn Trait + 'static)) -> *mut (dyn Trait + 'static) {
19-
| ~~~~~~~~~~~~~~~~~~~~~~~~~~
26+
LL - fn assert_static<'a>(ptr: *mut (dyn Trait + 'a)) -> *mut (dyn Trait + 'static) {
27+
LL + fn assert_static<'a>(ptr: *mut (dyn Trait + 'static)) -> *mut (dyn Trait + 'static) {
28+
|
2029

2130
error: aborting due to 1 previous error
2231

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ LL | let _send = unsend as *const S<dyn Cat<'static>>;
1010
= note: requirement occurs because of the type `S<dyn Cat<'_>>`, which makes the generic argument `dyn Cat<'_>` invariant
1111
= note: the struct `S<T>` is invariant over the parameter `T`
1212
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
13+
note: raw pointer casts of trait objects do not cast away lifetimes
14+
--> $DIR/ptr-to-trait-obj-different-regions-id-trait.rs:24:17
15+
|
16+
LL | let _send = unsend as *const S<dyn Cat<'static>>;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
= note: this was previously accepted by the compiler but was changed recently
19+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
1320

1421
error: aborting due to 1 previous error
1522

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ LL | let _send = unsend as *const S<dyn Cat<'static>>;
1010
= note: requirement occurs because of the type `S<dyn Cat<'_>>`, which makes the generic argument `dyn Cat<'_>` invariant
1111
= note: the struct `S<T>` is invariant over the parameter `T`
1212
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
13+
note: raw pointer casts of trait objects do not cast away lifetimes
14+
--> $DIR/ptr-to-trait-obj-different-regions-id-trait.rs:24:17
15+
|
16+
LL | let _send = unsend as *const S<dyn Cat<'static>>;
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
18+
= note: this was previously accepted by the compiler but was changed recently
19+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
1320

1421
error: aborting due to 1 previous error
1522

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ LL | fn bad_cast<'a>(x: *const dyn Static<'static>) -> *const dyn Static<'a> {
55
| -- lifetime `'a` defined here
66
LL | x as _
77
| ^^^^^^ returning this value requires that `'a` must outlive `'static`
8+
|
9+
note: raw pointer casts of trait objects do not cast away lifetimes
10+
--> $DIR/ptr-to-trait-obj-different-regions-lt-ext.rs:12:5
11+
|
12+
LL | x as _
13+
| ^^^^^^
14+
= note: this was previously accepted by the compiler but was changed recently
15+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
816

917
error: aborting due to 1 previous error
1018

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

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ LL | x as _
1212
= note: requirement occurs because of a mutable pointer to `dyn Trait<'_>`
1313
= note: mutable pointers are invariant over their type parameter
1414
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
15+
note: raw pointer casts of trait objects do not cast away lifetimes
16+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:7:5
17+
|
18+
LL | x as _
19+
| ^^^^^^
20+
= note: this was previously accepted by the compiler but was changed recently
21+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
1522

1623
error: lifetime may not live long enough
1724
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:7:5
@@ -27,6 +34,13 @@ LL | x as _
2734
= note: requirement occurs because of a mutable pointer to `dyn Trait<'_>`
2835
= note: mutable pointers are invariant over their type parameter
2936
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
37+
note: raw pointer casts of trait objects do not cast away lifetimes
38+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:7:5
39+
|
40+
LL | x as _
41+
| ^^^^^^
42+
= note: this was previously accepted by the compiler but was changed recently
43+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
3044

3145
help: `'b` and `'a` must be the same: replace one with the other
3246

@@ -44,6 +58,13 @@ LL | x as _
4458
= note: requirement occurs because of a mutable pointer to `dyn Trait<'_>`
4559
= note: mutable pointers are invariant over their type parameter
4660
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
61+
note: raw pointer casts of trait objects do not cast away lifetimes
62+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:12:5
63+
|
64+
LL | x as _
65+
| ^^^^^^
66+
= note: this was previously accepted by the compiler but was changed recently
67+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
4768

4869
error: lifetime may not live long enough
4970
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:16:5
@@ -59,6 +80,13 @@ LL | x as _
5980
= note: requirement occurs because of a mutable pointer to `dyn Trait<'_>`
6081
= note: mutable pointers are invariant over their type parameter
6182
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
83+
note: raw pointer casts of trait objects do not cast away lifetimes
84+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:16:5
85+
|
86+
LL | x as _
87+
| ^^^^^^
88+
= note: this was previously accepted by the compiler but was changed recently
89+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
6290

6391
error: lifetime may not live long enough
6492
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:20:5
@@ -97,6 +125,13 @@ LL | x as _
97125
= note: requirement occurs because of a mutable pointer to `dyn Assocked<Assoc = dyn Send>`
98126
= note: mutable pointers are invariant over their type parameter
99127
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
128+
note: raw pointer casts of trait objects do not cast away lifetimes
129+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:32:5
130+
|
131+
LL | x as _
132+
| ^^^^^^
133+
= note: this was previously accepted by the compiler but was changed recently
134+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
100135

101136
error: lifetime may not live long enough
102137
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:32:5
@@ -113,6 +148,13 @@ LL | x as _
113148
= note: requirement occurs because of a mutable pointer to `dyn Assocked<Assoc = dyn Send>`
114149
= note: mutable pointers are invariant over their type parameter
115150
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
151+
note: raw pointer casts of trait objects do not cast away lifetimes
152+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:32:5
153+
|
154+
LL | x as _
155+
| ^^^^^^
156+
= note: this was previously accepted by the compiler but was changed recently
157+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
116158

117159
help: `'b` and `'a` must be the same: replace one with the other
118160
|
@@ -133,6 +175,13 @@ LL | x as _
133175
= note: requirement occurs because of a mutable pointer to `dyn Assocked<Assoc = dyn Trait<'_>>`
134176
= note: mutable pointers are invariant over their type parameter
135177
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
178+
note: raw pointer casts of trait objects do not cast away lifetimes
179+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:39:5
180+
|
181+
LL | x as _
182+
| ^^^^^^
183+
= note: this was previously accepted by the compiler but was changed recently
184+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
136185

137186
error: lifetime may not live long enough
138187
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:39:5
@@ -149,6 +198,13 @@ LL | x as _
149198
= note: requirement occurs because of a mutable pointer to `dyn Assocked<Assoc = dyn Trait<'_>>`
150199
= note: mutable pointers are invariant over their type parameter
151200
= help: see <https://doc.rust-lang.org/nomicon/subtyping.html> for more information about variance
201+
note: raw pointer casts of trait objects do not cast away lifetimes
202+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:39:5
203+
|
204+
LL | x as _
205+
| ^^^^^^
206+
= note: this was previously accepted by the compiler but was changed recently
207+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
152208

153209
help: `'b` and `'a` must be the same: replace one with the other
154210
|
@@ -168,6 +224,14 @@ LL | require_static(ptr as _)
168224
| |
169225
| `ptr` escapes the function body here
170226
| argument requires that `'a` must outlive `'static`
227+
|
228+
note: raw pointer casts of trait objects do not cast away lifetimes
229+
--> $DIR/ptr-to-trait-obj-different-regions-misc.rs:46:20
230+
|
231+
LL | require_static(ptr as _)
232+
| ^^^^^^^^
233+
= note: this was previously accepted by the compiler but was changed recently
234+
= help: see <https://github.com/rust-lang/rust/issues/141402> for more information
171235

172236
error: aborting due to 11 previous errors
173237

0 commit comments

Comments
 (0)