Skip to content

Commit a8edf08

Browse files
authored
Rollup merge of #134635 - compiler-errors:dyn-dyn, r=fmease
Don't ICE on illegal `dyn*` casts Fixes #134544 Fixes #132127
2 parents 7f36ae4 + f67a739 commit a8edf08

File tree

5 files changed

+52
-18
lines changed

5 files changed

+52
-18
lines changed

compiler/rustc_hir_typeck/src/cast.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -721,13 +721,11 @@ impl<'a, 'tcx> CastCheck<'tcx> {
721721
use rustc_middle::ty::cast::IntTy::*;
722722

723723
if self.cast_ty.is_dyn_star() {
724-
if fcx.tcx.features().dyn_star() {
725-
span_bug!(self.span, "should be handled by `coerce`");
726-
} else {
727-
// Report "casting is invalid" rather than "non-primitive cast"
728-
// if the feature is not enabled.
729-
return Err(CastError::IllegalCast);
730-
}
724+
// This coercion will fail if the feature is not enabled, OR
725+
// if the coercion is (currently) illegal (e.g. `dyn* Foo + Send`
726+
// to `dyn* Foo`). Report "casting is invalid" rather than
727+
// "non-primitive cast".
728+
return Err(CastError::IllegalCast);
731729
}
732730

733731
let (t_from, t_cast) = match (CastTy::from_ty(self.expr_ty), CastTy::from_ty(self.cast_ty))

compiler/rustc_hir_typeck/src/coercion.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -737,8 +737,10 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
737737
return Err(TypeError::Mismatch);
738738
}
739739

740-
if let ty::Dynamic(a_data, _, _) = a.kind()
741-
&& let ty::Dynamic(b_data, _, _) = b.kind()
740+
// FIXME(dyn_star): We should probably allow things like casting from
741+
// `dyn* Foo + Send` to `dyn* Foo`.
742+
if let ty::Dynamic(a_data, _, ty::DynStar) = a.kind()
743+
&& let ty::Dynamic(b_data, _, ty::DynStar) = b.kind()
742744
&& a_data.principal_def_id() == b_data.principal_def_id()
743745
{
744746
return self.unify_and(a, b, |_| vec![]);

tests/crashes/132127.rs

-9
This file was deleted.

tests/ui/dyn-star/illegal.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(dyn_star)]
2+
//~^ WARN the feature `dyn_star` is incomplete
3+
4+
trait Foo {}
5+
6+
pub fn lol(x: dyn* Foo + Send) {
7+
x as dyn* Foo;
8+
//~^ ERROR casting `(dyn* Foo + Send + 'static)` as `dyn* Foo` is invalid
9+
}
10+
11+
fn lol2(x: &dyn Foo) {
12+
*x as dyn* Foo;
13+
//~^ ERROR `dyn Foo` needs to have the same ABI as a pointer
14+
}
15+
16+
fn main() {}

tests/ui/dyn-star/illegal.stderr

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
warning: the feature `dyn_star` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/illegal.rs:1:12
3+
|
4+
LL | #![feature(dyn_star)]
5+
| ^^^^^^^^
6+
|
7+
= note: see issue #102425 <https://github.com/rust-lang/rust/issues/102425> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0606]: casting `(dyn* Foo + Send + 'static)` as `dyn* Foo` is invalid
11+
--> $DIR/illegal.rs:7:5
12+
|
13+
LL | x as dyn* Foo;
14+
| ^^^^^^^^^^^^^
15+
16+
error[E0277]: `dyn Foo` needs to have the same ABI as a pointer
17+
--> $DIR/illegal.rs:12:5
18+
|
19+
LL | *x as dyn* Foo;
20+
| ^^ `dyn Foo` needs to be a pointer-like type
21+
|
22+
= help: the trait `PointerLike` is not implemented for `dyn Foo`
23+
24+
error: aborting due to 2 previous errors; 1 warning emitted
25+
26+
Some errors have detailed explanations: E0277, E0606.
27+
For more information about an error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)