Skip to content

Commit cfa1e3a

Browse files
committed
Custom diagnostics
1 parent 19659db commit cfa1e3a

11 files changed

+185
-14
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
@@ -565,6 +565,23 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
565565
self.add_placeholder_from_predicate_note(&mut diag, &path);
566566
self.add_sized_or_copy_bound_info(&mut diag, category, &path);
567567

568+
for constraint in &path {
569+
if let ConstraintCategory::Cast { is_raw_ptr_dyn_type_cast: true, .. } =
570+
constraint.category
571+
{
572+
diag.span_note(
573+
constraint.span,
574+
format!("raw pointer casts of trait objects do not cast away lifetimes"),
575+
);
576+
diag.note(format!(
577+
"this was previously accepted by the compiler but was changed recently"
578+
));
579+
diag.help(format!(
580+
"see <https://github.com/rust-lang/rust/issues/141402> for more information"
581+
));
582+
}
583+
}
584+
568585
self.buffer_error(diag);
569586
}
570587

compiler/rustc_borrowck/src/region_infer/mod.rs

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

compiler/rustc_borrowck/src/type_check/mod.rs

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,15 +1104,23 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11041104
self.prove_predicate(
11051105
ty::ClauseKind::WellFormed(src_ty.into()),
11061106
location.to_locations(),
1107-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1107+
ConstraintCategory::Cast {
1108+
is_raw_ptr_dyn_type_cast: false,
1109+
is_implicit_coercion,
1110+
unsize_to: None,
1111+
},
11081112
);
11091113

11101114
let src_ty = self.normalize(src_ty, location);
11111115
if let Err(terr) = self.sub_types(
11121116
src_ty,
11131117
*ty,
11141118
location.to_locations(),
1115-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1119+
ConstraintCategory::Cast {
1120+
is_raw_ptr_dyn_type_cast: false,
1121+
is_implicit_coercion,
1122+
unsize_to: None,
1123+
},
11161124
) {
11171125
span_mirbug!(
11181126
self,
@@ -1133,7 +1141,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11331141
self.prove_predicate(
11341142
ty::ClauseKind::WellFormed(src_ty.into()),
11351143
location.to_locations(),
1136-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1144+
ConstraintCategory::Cast {
1145+
is_raw_ptr_dyn_type_cast: false,
1146+
is_implicit_coercion,
1147+
unsize_to: None,
1148+
},
11371149
);
11381150

11391151
// The type that we see in the fcx is like
@@ -1146,7 +1158,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11461158
src_ty,
11471159
*ty,
11481160
location.to_locations(),
1149-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1161+
ConstraintCategory::Cast {
1162+
is_raw_ptr_dyn_type_cast: false,
1163+
is_implicit_coercion,
1164+
unsize_to: None,
1165+
},
11501166
) {
11511167
span_mirbug!(
11521168
self,
@@ -1175,7 +1191,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11751191
ty_fn_ptr_from,
11761192
*ty,
11771193
location.to_locations(),
1178-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1194+
ConstraintCategory::Cast {
1195+
is_raw_ptr_dyn_type_cast: false,
1196+
is_implicit_coercion,
1197+
unsize_to: None,
1198+
},
11791199
) {
11801200
span_mirbug!(
11811201
self,
@@ -1208,7 +1228,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12081228
ty_fn_ptr_from,
12091229
*ty,
12101230
location.to_locations(),
1211-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1231+
ConstraintCategory::Cast {
1232+
is_raw_ptr_dyn_type_cast: false,
1233+
is_implicit_coercion,
1234+
unsize_to: None,
1235+
},
12121236
) {
12131237
span_mirbug!(
12141238
self,
@@ -1237,6 +1261,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12371261
trait_ref,
12381262
location.to_locations(),
12391263
ConstraintCategory::Cast {
1264+
is_raw_ptr_dyn_type_cast: false,
12401265
is_implicit_coercion,
12411266
unsize_to: Some(unsize_to),
12421267
},
@@ -1260,7 +1285,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12601285
.iter()
12611286
.map(|predicate| predicate.with_self_ty(tcx, self_ty)),
12621287
location.to_locations(),
1263-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1288+
ConstraintCategory::Cast {
1289+
is_raw_ptr_dyn_type_cast: false,
1290+
is_implicit_coercion,
1291+
unsize_to: None,
1292+
},
12641293
);
12651294

12661295
let outlives_predicate = tcx.mk_predicate(Binder::dummy(
@@ -1271,7 +1300,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12711300
self.prove_predicate(
12721301
outlives_predicate,
12731302
location.to_locations(),
1274-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1303+
ConstraintCategory::Cast {
1304+
is_raw_ptr_dyn_type_cast: false,
1305+
is_implicit_coercion,
1306+
unsize_to: None,
1307+
},
12751308
);
12761309
}
12771310

@@ -1294,7 +1327,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12941327
*ty_from,
12951328
*ty_to,
12961329
location.to_locations(),
1297-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1330+
ConstraintCategory::Cast {
1331+
is_raw_ptr_dyn_type_cast: false,
1332+
is_implicit_coercion,
1333+
unsize_to: None,
1334+
},
12981335
) {
12991336
span_mirbug!(
13001337
self,
@@ -1357,7 +1394,11 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
13571394
*ty_elem,
13581395
*ty_to,
13591396
location.to_locations(),
1360-
ConstraintCategory::Cast { is_implicit_coercion, unsize_to: None },
1397+
ConstraintCategory::Cast {
1398+
is_raw_ptr_dyn_type_cast: false,
1399+
is_implicit_coercion,
1400+
unsize_to: None,
1401+
},
13611402
) {
13621403
span_mirbug!(
13631404
self,
@@ -1536,6 +1577,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
15361577
dst_obj,
15371578
location.to_locations(),
15381579
ConstraintCategory::Cast {
1580+
is_raw_ptr_dyn_type_cast: true,
15391581
is_implicit_coercion: false,
15401582
unsize_to: None,
15411583
},

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)