Address nits, add test for implicit dyn-star coercion without feature gate

This commit is contained in:
Michael Goulet 2022-10-14 04:55:07 +00:00
parent 0cb217d29b
commit 8c7e836abb
6 changed files with 50 additions and 3 deletions

View File

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

View File

@ -777,6 +777,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
}
}
// Check the obligations of the cast -- for example, when casting
// `usize` to `dyn* Clone + 'static`:
let obligations = predicates
.iter()
.map(|predicate| {
@ -787,7 +789,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
let predicate = predicate.with_self_ty(self.tcx, a);
Obligation::new(self.cause.clone(), self.param_env, predicate)
})
// Enforce the region bound `'static` (e.g., `usize: 'static`, in our example).
// Enforce the region bound (e.g., `usize: 'static`, in our example).
.chain([Obligation::new(
self.cause.clone(),
self.param_env,

View File

@ -0,0 +1,10 @@
#![feature(dyn_star)]
#![allow(incomplete_features)]
use std::fmt::Display;
pub fn require_dyn_star_display(_: dyn* Display) {}
fn works_locally() {
require_dyn_star_display(1usize);
}

View File

@ -8,6 +8,11 @@ fn make_dyn_star(i: usize) {
let _dyn_i: dyn* Debug = i;
}
fn make_dyn_star_explicit(i: usize) {
let _dyn_i: dyn* Debug = i as dyn* Debug;
}
fn main() {
make_dyn_star(42);
make_dyn_star_explicit(42);
}

View File

@ -0,0 +1,8 @@
// aux-build:dyn-star-foreign.rs
extern crate dyn_star_foreign;
fn main() {
dyn_star_foreign::require_dyn_star_display(1usize);
//~^ ERROR mismatched types
}

View File

@ -0,0 +1,19 @@
error[E0308]: mismatched types
--> $DIR/no-implicit-dyn-star.rs:6:48
|
LL | dyn_star_foreign::require_dyn_star_display(1usize);
| ------------------------------------------ ^^^^^^ expected trait object `dyn std::fmt::Display`, found `usize`
| |
| arguments to this function are incorrect
|
= note: expected trait object `(dyn* std::fmt::Display + 'static)`
found type `usize`
note: function defined here
--> $DIR/auxiliary/dyn-star-foreign.rs:6:8
|
LL | pub fn require_dyn_star_display(_: dyn* Display) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.