Skip to content

Commit

Permalink
Address nits, add test for implicit dyn-star coercion without feature…
Browse files Browse the repository at this point in the history
… gate
  • Loading branch information
compiler-errors committed Oct 14, 2022
1 parent 0cb217d commit 8c7e836
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 3 deletions.
7 changes: 5 additions & 2 deletions compiler/rustc_codegen_ssa/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion compiler/rustc_hir_analysis/src/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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,
Expand Down
10 changes: 10 additions & 0 deletions src/test/ui/dyn-star/auxiliary/dyn-star-foreign.rs
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions src/test/ui/dyn-star/make-dyn-star.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
8 changes: 8 additions & 0 deletions src/test/ui/dyn-star/no-implicit-dyn-star.rs
Original file line number Diff line number Diff line change
@@ -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
}
19 changes: 19 additions & 0 deletions src/test/ui/dyn-star/no-implicit-dyn-star.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 8c7e836

Please sign in to comment.