forked from rust-lang/miri
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #107728 - RalfJung:miri-dyn-star, r=RalfJung,oli-obk
Miri: basic dyn* support As usual I am very unsure about the dynamic dispatch stuff, but it passes even the `Pin<&mut dyn* Trait>` test so that is something. TBH I think it was a mistake to make `dyn Trait` and `dyn* Trait` part of the same `TyKind` variant. Almost everywhere in Miri this lead to the wrong default behavior, resulting in strange ICEs instead of nice "unimplemented" messages. The two types describe pretty different runtime data layout after all. Strangely I did not need to do the equivalent of [this diff](rust-lang/rust#106532 (comment)) in Miri. Maybe that is because the unsizing logic matches on `ty::Dynamic(.., ty::Dyn)` already? In `unsized_info` I don't think the `target_dyn_kind` can be `DynStar`, since then it wouldn't be unsized! r? `@oli-obk` Cc `@eholk` (dyn-star) rust-lang/rust#102425
- Loading branch information
Showing
6 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
#![feature(dyn_star)] | ||
#![allow(incomplete_features)] | ||
|
||
use std::fmt::{Debug, Display}; | ||
|
||
fn main() { | ||
make_dyn_star(); | ||
method(); | ||
box_(); | ||
dispatch_on_pin_mut(); | ||
dyn_star_to_dyn(); | ||
dyn_to_dyn_star(); | ||
} | ||
|
||
fn dyn_star_to_dyn() { | ||
let x: dyn* Debug = &42; | ||
let x = Box::new(x) as Box<dyn Debug>; | ||
assert_eq!("42", format!("{x:?}")); | ||
} | ||
|
||
fn dyn_to_dyn_star() { | ||
let x: Box<dyn Debug> = Box::new(42); | ||
let x = &x as dyn* Debug; | ||
assert_eq!("42", format!("{x:?}")); | ||
} | ||
|
||
fn make_dyn_star() { | ||
fn make_dyn_star_coercion(i: usize) { | ||
let _dyn_i: dyn* Debug = i; | ||
} | ||
|
||
fn make_dyn_star_explicit(i: usize) { | ||
let _dyn_i: dyn* Debug = i as dyn* Debug; | ||
} | ||
|
||
make_dyn_star_coercion(42); | ||
make_dyn_star_explicit(42); | ||
} | ||
|
||
fn method() { | ||
trait Foo { | ||
fn get(&self) -> usize; | ||
} | ||
|
||
impl Foo for usize { | ||
fn get(&self) -> usize { | ||
*self | ||
} | ||
} | ||
|
||
fn invoke_dyn_star(i: dyn* Foo) -> usize { | ||
i.get() | ||
} | ||
|
||
fn make_and_invoke_dyn_star(i: usize) -> usize { | ||
let dyn_i: dyn* Foo = i; | ||
invoke_dyn_star(dyn_i) | ||
} | ||
|
||
assert_eq!(make_and_invoke_dyn_star(42), 42); | ||
} | ||
|
||
fn box_() { | ||
fn make_dyn_star() -> dyn* Display { | ||
Box::new(42) as dyn* Display | ||
} | ||
|
||
let x = make_dyn_star(); | ||
assert_eq!(format!("{x}"), "42"); | ||
} | ||
|
||
fn dispatch_on_pin_mut() { | ||
use std::future::Future; | ||
|
||
async fn foo(f: dyn* Future<Output = i32>) { | ||
println!("dispatch_on_pin_mut: value: {}", f.await); | ||
} | ||
|
||
async fn async_main() { | ||
foo(Box::pin(async { 1 })).await | ||
} | ||
|
||
// ------------------------------------------------------------------------- // | ||
// Implementation Details Below... | ||
|
||
use std::pin::Pin; | ||
use std::task::*; | ||
|
||
pub fn noop_waker() -> Waker { | ||
let raw = RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE); | ||
|
||
// SAFETY: the contracts for RawWaker and RawWakerVTable are upheld | ||
unsafe { Waker::from_raw(raw) } | ||
} | ||
|
||
const NOOP_WAKER_VTABLE: RawWakerVTable = RawWakerVTable::new(noop_clone, noop, noop, noop); | ||
|
||
unsafe fn noop_clone(_p: *const ()) -> RawWaker { | ||
RawWaker::new(std::ptr::null(), &NOOP_WAKER_VTABLE) | ||
} | ||
|
||
unsafe fn noop(_p: *const ()) {} | ||
|
||
let mut fut = async_main(); | ||
|
||
// Poll loop, just to test the future... | ||
let waker = noop_waker(); | ||
let ctx = &mut Context::from_waker(&waker); | ||
|
||
loop { | ||
match unsafe { Pin::new_unchecked(&mut fut).poll(ctx) } { | ||
Poll::Pending => {} | ||
Poll::Ready(()) => break, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dispatch_on_pin_mut: value: 1 |