Skip to content

Instantly share code, notes, and snippets.

@Aaron1011
Created January 10, 2020 08:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aaron1011/575aac86c92fba2ec6ede6c638119743 to your computer and use it in GitHub Desktop.
Save Aaron1011/575aac86c92fba2ec6ede6c638119743 to your computer and use it in GitHub Desktop.
Never-type fallback bug
#![feature(never_type)]
//#![feature(never_type_fallback)]
fn get_type<T>(_: T) -> &'static str {
std::any::type_name::<T>()
}
fn unconstrained_return<T>() -> Result<T, String> {
Err("Hi".to_string())
}
fn foo() {
let a = || {
match unconstrained_return::<_>() {
Ok(x) => x, // `x` has type `_`, which is unconstrained
Err(s) => panic!(s), // … except for unifying with the type of `panic!()`
// so that both `match` arms have the same type.
// Therefore `_` resolves to `!` and we "return" an `Ok(!)` value.
}
};
let cast: &dyn FnOnce() -> _ = &a;
println!("Return type: {:?}", get_type(cast));
}
fn main() {
foo()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment