Skip to content

Commit

Permalink
pessimistically treat all function items as containing an opaque type
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jun 29, 2022
1 parent 493c960 commit 9dbfcbc
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 4 deletions.
5 changes: 5 additions & 0 deletions compiler/rustc_middle/src/ty/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ impl FlagComputation {

&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| {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
Original file line number Diff line number Diff line change
@@ -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

Expand Down
13 changes: 13 additions & 0 deletions src/test/ui/type-alias-impl-trait/issue-98604.rs
Original file line number Diff line number Diff line change
@@ -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
}
18 changes: 18 additions & 0 deletions src/test/ui/type-alias-impl-trait/issue-98604.stderr
Original file line number Diff line number Diff line change
@@ -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`.
9 changes: 9 additions & 0 deletions src/test/ui/type-alias-impl-trait/issue-98608.rs
Original file line number Diff line number Diff line change
@@ -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:?}");
}
16 changes: 16 additions & 0 deletions src/test/ui/type-alias-impl-trait/issue-98608.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit 9dbfcbc

Please sign in to comment.