Skip to content

Commit 8c7e836

Browse files
Address nits, add test for implicit dyn-star coercion without feature gate
1 parent 0cb217d commit 8c7e836

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

Diff for: compiler/rustc_codegen_ssa/src/base.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,11 @@ pub fn cast_to_dyn_star<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
270270
dst_ty: Ty<'tcx>,
271271
old_info: Option<Bx::Value>,
272272
) -> (Bx::Value, Bx::Value) {
273-
debug!("unsize_ptr: {:?} => {:?}", src_ty_and_layout.ty, dst_ty);
274-
assert!(matches!(dst_ty.kind(), ty::Dynamic(_, _, ty::DynStar)));
273+
debug!("cast_to_dyn_star: {:?} => {:?}", src_ty_and_layout.ty, dst_ty);
274+
assert!(
275+
matches!(dst_ty.kind(), ty::Dynamic(_, _, ty::DynStar)),
276+
"destination type must be a dyn*"
277+
);
275278
// FIXME(dyn-star): this is probably not the best way to check if this is
276279
// a pointer, and really we should ensure that the value is a suitable
277280
// pointer earlier in the compilation process.

Diff for: compiler/rustc_hir_analysis/src/check/coercion.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
777777
}
778778
}
779779

780+
// Check the obligations of the cast -- for example, when casting
781+
// `usize` to `dyn* Clone + 'static`:
780782
let obligations = predicates
781783
.iter()
782784
.map(|predicate| {
@@ -787,7 +789,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
787789
let predicate = predicate.with_self_ty(self.tcx, a);
788790
Obligation::new(self.cause.clone(), self.param_env, predicate)
789791
})
790-
// Enforce the region bound `'static` (e.g., `usize: 'static`, in our example).
792+
// Enforce the region bound (e.g., `usize: 'static`, in our example).
791793
.chain([Obligation::new(
792794
self.cause.clone(),
793795
self.param_env,

Diff for: src/test/ui/dyn-star/auxiliary/dyn-star-foreign.rs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#![feature(dyn_star)]
2+
#![allow(incomplete_features)]
3+
4+
use std::fmt::Display;
5+
6+
pub fn require_dyn_star_display(_: dyn* Display) {}
7+
8+
fn works_locally() {
9+
require_dyn_star_display(1usize);
10+
}

Diff for: src/test/ui/dyn-star/make-dyn-star.rs

+5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ fn make_dyn_star(i: usize) {
88
let _dyn_i: dyn* Debug = i;
99
}
1010

11+
fn make_dyn_star_explicit(i: usize) {
12+
let _dyn_i: dyn* Debug = i as dyn* Debug;
13+
}
14+
1115
fn main() {
1216
make_dyn_star(42);
17+
make_dyn_star_explicit(42);
1318
}

Diff for: src/test/ui/dyn-star/no-implicit-dyn-star.rs

+8
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);
7+
//~^ ERROR mismatched types
8+
}

Diff for: src/test/ui/dyn-star/no-implicit-dyn-star.stderr

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/no-implicit-dyn-star.rs:6:48
3+
|
4+
LL | dyn_star_foreign::require_dyn_star_display(1usize);
5+
| ------------------------------------------ ^^^^^^ expected trait object `dyn std::fmt::Display`, found `usize`
6+
| |
7+
| arguments to this function are incorrect
8+
|
9+
= note: expected trait object `(dyn* std::fmt::Display + 'static)`
10+
found type `usize`
11+
note: function defined here
12+
--> $DIR/auxiliary/dyn-star-foreign.rs:6:8
13+
|
14+
LL | pub fn require_dyn_star_display(_: dyn* Display) {}
15+
| ^^^^^^^^^^^^^^^^^^^^^^^^
16+
17+
error: aborting due to previous error
18+
19+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)