pessimistically treat all function items as containing an opaque type

This commit is contained in:
Oli Scherer 2022-06-29 14:25:44 +00:00
parent 493c960a3e
commit 9dbfcbcbb5
7 changed files with 63 additions and 4 deletions

View File

@ -207,6 +207,11 @@ fn add_kind(&mut self, kind: &ty::TyKind<'_>) {
&ty::FnDef(_, substs) => {
self.add_substs(substs);
// HACK(#98608, oli-obk): Function items with opaque types in their signature will
// end up not having the HAS_TY_OPAQUE flag set, causing `evaluate_obligation` to
// optimistically assume the function item matches any signature. See documentation
// on `HAS_FREE_LOCAL_NAMES` for details.
self.add_flags(TypeFlags::HAS_TY_OPAQUE);
}
&ty::FnPtr(fn_sig) => self.bound_computation(fn_sig, |computation, fn_sig| {

View File

@ -3,7 +3,7 @@
type Foo = impl Fn() -> Foo;
fn foo() -> Foo {
foo //~ ERROR: overflow evaluating the requirement `fn() -> Foo {foo}: Sized`
foo //~ ERROR: overflow evaluating the requirement `<fn() -> Foo {foo} as FnOnce<()>>::Output == fn() -> Foo {foo}`
}
fn main() {}

View File

@ -1,10 +1,8 @@
error[E0275]: overflow evaluating the requirement `fn() -> Foo {foo}: Sized`
error[E0275]: overflow evaluating the requirement `<fn() -> Foo {foo} as FnOnce<()>>::Output == fn() -> Foo {foo}`
--> $DIR/issue-53398-cyclic-types.rs:6:5
|
LL | foo
| ^^^
|
= help: consider increasing the recursion limit by adding a `#![recursion_limit = "256"]` attribute to your crate (`issue_53398_cyclic_types`)
error: aborting due to previous error

View File

@ -0,0 +1,13 @@
// edition:2018
type AsyncFnPtr = Box<
dyn Fn() -> std::pin::Pin<Box<dyn std::future::Future<Output = ()>>>,
>;
async fn test() {}
#[allow(unused_must_use)]
fn main() {
Box::new(test) as AsyncFnPtr;
//~^ ERROR type mismatch
}

View File

@ -0,0 +1,18 @@
error[E0271]: type mismatch resolving `<fn() -> impl Future<Output = ()> {test} as FnOnce<()>>::Output == Pin<Box<(dyn Future<Output = ()> + 'static)>>`
--> $DIR/issue-98604.rs:11:5
|
LL | Box::new(test) as AsyncFnPtr;
| ^^^^^^^^^^^^^^ expected struct `Pin`, found opaque type
|
note: while checking the return type of the `async fn`
--> $DIR/issue-98604.rs:7:17
|
LL | async fn test() {}
| ^ checked the `Output` of this `async fn`, found opaque type
= note: expected struct `Pin<Box<(dyn Future<Output = ()> + 'static)>>`
found opaque type `impl Future<Output = ()>`
= note: required for the cast from `fn() -> impl Future<Output = ()> {test}` to the object type `dyn Fn() -> Pin<Box<(dyn Future<Output = ()> + 'static)>>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0271`.

View File

@ -0,0 +1,9 @@
fn hi() -> impl Sized { std::ptr::null::<u8>() }
fn main() {
let b: Box<dyn Fn() -> Box<u8>> = Box::new(hi);
//~^ ERROR type mismatch resolving `<fn() -> impl Sized {hi} as FnOnce<()>>::Output == Box<u8>`
let boxed = b();
let null = *boxed;
println!("{null:?}");
}

View File

@ -0,0 +1,16 @@
error[E0271]: type mismatch resolving `<fn() -> impl Sized {hi} as FnOnce<()>>::Output == Box<u8>`
--> $DIR/issue-98608.rs:4:39
|
LL | fn hi() -> impl Sized { std::ptr::null::<u8>() }
| ---------- the found opaque type
...
LL | let b: Box<dyn Fn() -> Box<u8>> = Box::new(hi);
| ^^^^^^^^^^^^ expected struct `Box`, found opaque type
|
= note: expected struct `Box<u8>`
found opaque type `impl Sized`
= note: required for the cast from `fn() -> impl Sized {hi}` to the object type `dyn Fn() -> Box<u8>`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0271`.