Skip to content

Commit 679771f

Browse files
Rollup merge of #103699 - compiler-errors:dyn-star-cast-bad, r=TaKO8Ki
Emit proper error when casting to `dyn*` Fixes #103679
2 parents 05ab16b + c442013 commit 679771f

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -869,7 +869,13 @@ impl<'a, 'tcx> CastCheck<'tcx> {
869869

870870
(Int(_) | Float, Int(_) | Float) => Ok(CastKind::NumericCast),
871871

872-
(_, DynStar) | (DynStar, _) => bug!("should be handled by `try_coerce`"),
872+
(_, DynStar) | (DynStar, _) => {
873+
if fcx.tcx.features().dyn_star {
874+
bug!("should be handled by `try_coerce`")
875+
} else {
876+
Err(CastError::IllegalCast)
877+
}
878+
}
873879
}
874880
}
875881

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use std::fmt::Debug;
2+
3+
fn make_dyn_star() {
4+
let i = 42usize;
5+
let dyn_i: dyn* Debug = i as dyn* Debug;
6+
//~^ ERROR casting `usize` as `dyn* Debug` is invalid
7+
//~| ERROR dyn* trait objects are unstable
8+
//~| ERROR dyn* trait objects are unstable
9+
}
10+
11+
fn main() {
12+
make_dyn_star();
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0658]: dyn* trait objects are unstable
2+
--> $DIR/no-explicit-dyn-star-cast.rs:5:16
3+
|
4+
LL | let dyn_i: dyn* Debug = i as dyn* Debug;
5+
| ^^^^^^^^^^
6+
|
7+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
8+
= help: add `#![feature(dyn_star)]` to the crate attributes to enable
9+
10+
error[E0658]: dyn* trait objects are unstable
11+
--> $DIR/no-explicit-dyn-star-cast.rs:5:34
12+
|
13+
LL | let dyn_i: dyn* Debug = i as dyn* Debug;
14+
| ^^^^^^^^^^
15+
|
16+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
17+
= help: add `#![feature(dyn_star)]` to the crate attributes to enable
18+
19+
error[E0606]: casting `usize` as `dyn* Debug` is invalid
20+
--> $DIR/no-explicit-dyn-star-cast.rs:5:29
21+
|
22+
LL | let dyn_i: dyn* Debug = i as dyn* Debug;
23+
| ^^^^^^^^^^^^^^^
24+
25+
error: aborting due to 3 previous errors
26+
27+
Some errors have detailed explanations: E0606, E0658.
28+
For more information about an error, try `rustc --explain E0606`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// aux-build:dyn-star-foreign.rs
2+
3+
extern crate dyn_star_foreign;
4+
5+
fn main() {
6+
dyn_star_foreign::require_dyn_star_display(1usize as _);
7+
//~^ ERROR casting `usize` as `dyn* std::fmt::Display` is invalid
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0606]: casting `usize` as `dyn* std::fmt::Display` is invalid
2+
--> $DIR/no-explicit-dyn-star.rs:6:48
3+
|
4+
LL | dyn_star_foreign::require_dyn_star_display(1usize as _);
5+
| ^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0606`.

0 commit comments

Comments
 (0)