Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Higher-ranked function pointers implement Fn traits without enforcing necessary trait bounds to make sure the return type exists #85455

Closed
steffahn opened this issue May 19, 2021 · 1 comment · Fixed by #88640
Labels
A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system A-type-system Area: Type system C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-unboxed_closures `#![feature(unboxed_closures)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@steffahn
Copy link
Member

steffahn commented May 19, 2021

cargo build

on the following code produces an ICE (just cargo check doesn’t ICE)

#![feature(unboxed_closures)]

trait SomeTrait<'a> {
    type Associated;
}

fn give_me_ice<T>() {
    callee::<fn(&()) -> <T as SomeTrait<'_>>::Associated>();
}

fn callee<T: Fn<(&'static (),)>>() {
    println!(
        "{}",
        std::any::type_name::<<T as FnOnce<(&'static (),)>>::Output>()
    );
}

fn main() {
    give_me_ice::<()>();
}
   Compiling playground v0.0.1 (/playground)
error: internal compiler error: compiler/rustc_traits/src/normalize_erasing_regions.rs:54:32: could not fully normalize `fn() -> &'static str {std::any::type_name::<<for<'r> fn(&'r ()) -> <() as SomeTrait<'r>>::Associated as std::ops::FnOnce<(&(),)>>::Output>}`

thread 'rustc' panicked at 'Box<Any>', /rustc/3e99439f4dacc8ba0d2ca48d221694362d587927/library/std/src/panic.rs:59:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

note: the compiler unexpectedly panicked. this is a bug.

note: we would appreciate a bug report: https://github.com/rust-lang/rust/issues/new?labels=C-bug%2C+I-ICE%2C+T-compiler&template=ice.md

note: rustc 1.54.0-nightly (3e99439f4 2021-05-17) running on x86_64-unknown-linux-gnu

note: compiler flags: -C embed-bitcode=no -C codegen-units=1 -C debuginfo=2 --crate-type bin

note: some of the compiler flags provided by cargo are hidden

query stack during panic:
#0 [normalize_generic_arg_after_erasing_regions] normalizing `fn() -> &'static str {std::any::type_name::<<for<'r> fn(&'r ()) -> <() as SomeTrait<'r>>::Associated as std::ops::FnOnce<(&(),)>>::Output>}`
#1 [collect_and_partition_mono_items] collect_and_partition_mono_items
end of query stack
error: aborting due to previous error

error: could not compile `playground`

To learn more, run the command again with --verbose.

(playground)

Possibly, this bug can also lead to unsoundness in some way. (I haven’t fully explored that [yet].) Notably, Box<dyn Fn…> doesn’t come with the same problem:

#![feature(unboxed_closures)]

trait SomeTrait<'a> {
    type Associated;
}

fn give_me_ice<T>() {
    callee::<Box<dyn Fn(&()) -> <T as SomeTrait<'_>>::Associated>>();
}

fn callee<T: Fn<(&'static (),)>>() {
    println!(
        "{}",
        std::any::type_name::<<T as FnOnce<(&'static (),)>>::Output>()
    );
}

fn main() {
    give_me_ice::<()>();
}
   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `T: SomeTrait<'_>` is not satisfied
  --> src/main.rs:8:5
   |
8  |     callee::<Box<dyn Fn(&()) -> <T as SomeTrait<'_>>::Associated>>();
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `SomeTrait<'_>` is not implemented for `T`
...
11 | fn callee<T: Fn<(&'static (),)>>() {
   |              ------------------ required by this bound in `callee`
   |
   = note: required because of the requirements on the impl of `Fn<(&'static (),)>` for `Box<dyn for<'r> Fn(&'r ()) -> <T as SomeTrait<'_>>::Associated>`
help: consider restricting type parameter `T`
   |
7  | fn give_me_ice<T: SomeTrait<'_>>() {
   |                 ^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`

To learn more, run the command again with --verbose.

I would’ve expected a similar error message for the fn… case as well.

(Further, note that the way things currently stand, the type Box<dyn Fn(&()) -> <T as SomeTrait<'_>>::Associated> itself is not leading to any error. It’s only the call to callee which has an Fn<…> bound. The error also successfully appears if callee has a higher-ranked Fn<…> bound and/or is not actually called but just instantiated with the type argument, here’s a playground doing both.)

@rustbot label requires-nightly, F-unboxed_closures, A-traits, A-typesystem, A-associated-items

@steffahn steffahn added C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 19, 2021
@rustbot rustbot added A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system A-type-system Area: Type system F-unboxed_closures `#![feature(unboxed_closures)]` requires-nightly This issue requires a nightly compiler in some way. labels May 19, 2021
@steffahn steffahn changed the title Higher-ranked function pointers implement Fn traits without enforcing necessary trait bounds on return type Higher-ranked function pointers implement Fn traits without enforcing necessary trait bounds to make sure the return type exists May 19, 2021
fanninpm added a commit to fanninpm/glacier that referenced this issue Jun 4, 2021
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jun 5, 2021
@jackh726
Copy link
Member

Fixed by #85499, different enough that marking as needs-test

@jackh726 jackh726 added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Aug 29, 2021
@bors bors closed this as completed in 0e0ce83 Sep 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-trait-system Area: Trait system A-type-system Area: Type system C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. F-unboxed_closures `#![feature(unboxed_closures)]` glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants