6

Function type with opaque return type can be coerced to `dyn Fn` with arbitrary...

 2 years ago
source link: https://github.com/rust-lang/rust/issues/98608
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Contributor

Nilstrieb commented 22 days ago

edited

I tried this code:

fn hi() -> impl Sized { std::ptr::null::<u8>() }

fn main() {
    let b: Box<dyn Fn() -> Box<u8>> = Box::new(hi);
    let boxed = b();
    let null = *boxed;
    println!("{null:?}");
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=a78a1c65a7788a1fc16bbb49823e31b1

I expected to see this happen: Compiler error, since impl Sized cannot be coerced to Box<u8>.

Instead, this happened: Code compiles, segfault

rustc --version --verbose:

rustc 1.64.0-nightly (2f3ddd9f5 2022-06-27)
binary: rustc
commit-hash: 2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f
commit-date: 2022-06-27
host: x86_64-unknown-linux-gnu
release: 1.64.0-nightly
LLVM version: 14.0.6
compiler-errors, steffahn, ChayimFriedman2, AngelMunoz, Restioson, khvzak, seandewar, ClementNerma, 71, Polish-Civil, and 11 more reacted with laugh emojidanielhenrymantilla, 5hir0kur0, seandewar, jdahlstrom, 71, Polish-Civil, jplatte, zebp, hybras, Coca162, and 6 more reacted with eyes emoji All reactions

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK