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

Broken Mir Resulting From Async Function Pointer Cast #98604

Closed
KSBilodeau opened this issue Jun 28, 2022 · 11 comments · Fixed by #98614
Closed

Broken Mir Resulting From Async Function Pointer Cast #98604

KSBilodeau opened this issue Jun 28, 2022 · 11 comments · Fixed by #98614
Assignees
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@KSBilodeau
Copy link
Contributor

Code

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;
}

Meta

rustc --version --verbose:

rustc 1.63.0-nightly (fdca237d5 2022-06-24)
binary: rustc
commit-hash: fdca237d5194bf8a1c9b437ebd2114d1c2ba6195
commit-date: 2022-06-24
host: x86_64-apple-darwin
release: 1.63.0-nightly
LLVM version: 14.0.5

Error output

error: internal compiler error: no errors encountered even though `delay_span_bug` issued

error: internal compiler error: broken MIR in DefId(0:7 ~ playground[5af9]::main) (NoSolution): could not prove Binder(TraitPredicate(<std::boxed::Box<fn() -> impl std::future::Future<Output = ()> {test}> as std::ops::CoerceUnsized<std::boxed::Box<dyn std::ops::Fn() -> std::pin::Pin<std::boxed::Box<dyn std::future::Future<Output = ()>>>>>>, polarity:Positive), [])
 --> src/main.rs:9:5
  |
9 |     Box::new(test) as AsyncFnPtr;
  |     ^^^^^^^^^^^^^^
  |
  = note: delayed at compiler/rustc_borrowck/src/type_check/canonical.rs:151:13

thread 'rustc' panicked at 'Box<dyn Any>', compiler/rustc_errors/src/lib.rs:1425:13
stack backtrace:
   0:     0x7fd68ac845dd - std::backtrace_rs::backtrace::libunwind::trace::ha6648b7014364bc2
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x7fd68ac845dd - std::backtrace_rs::backtrace::trace_unsynchronized::h12a4ddf96a4e2ff5
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7fd68ac845dd - std::sys_common::backtrace::_print_fmt::hf87c549bd1cc89dc
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x7fd68ac845dd - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h4ffedd9ce9224034
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x7fd68acdd91c - core::fmt::write::hf124b4c1c8ae0d2b
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/core/src/fmt/mod.rs:1198:17
   5:     0x7fd68ac75c51 - std::io::Write::write_fmt::hbe26d06d41c58912
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/io/mod.rs:1672:15
   6:     0x7fd68ac872a5 - std::sys_common::backtrace::_print::h6d5a85ca9b3358b3
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x7fd68ac872a5 - std::sys_common::backtrace::print::h7666fc3156204d9f
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x7fd68ac872a5 - std::panicking::default_hook::{{closure}}::hfa94c116b7086896
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/panicking.rs:295:22
   9:     0x7fd68ac86fc6 - std::panicking::default_hook::hc8deb5a55c834c81
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/panicking.rs:314:9
  10:     0x7fd68b481524 - rustc_driver[3a72345c5a3a9278]::DEFAULT_HOOK::{closure#0}::{closure#0}
  11:     0x7fd68ac8797a - std::panicking::rust_panic_with_hook::h16757dfa430f6014
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/panicking.rs:702:17
  12:     0x7fd68c63a111 - std[a5eff6be7f10e3b3]::panicking::begin_panic::<rustc_errors[8af9c38e420da921]::ExplicitBug>::{closure#0}
  13:     0x7fd68c637cf6 - std[a5eff6be7f10e3b3]::sys_common::backtrace::__rust_end_short_backtrace::<std[a5eff6be7f10e3b3]::panicking::begin_panic<rustc_errors[8af9c38e420da921]::ExplicitBug>::{closure#0}, !>
  14:     0x7fd68c6545f6 - std[a5eff6be7f10e3b3]::panicking::begin_panic::<rustc_errors[8af9c38e420da921]::ExplicitBug>
  15:     0x7fd68c651596 - std[a5eff6be7f10e3b3]::panic::panic_any::<rustc_errors[8af9c38e420da921]::ExplicitBug>
  16:     0x7fd68df2266c - <rustc_errors[8af9c38e420da921]::HandlerInner as core[993276dbfd88d183]::ops::drop::Drop>::drop
  17:     0x7fd68d64d488 - core[993276dbfd88d183]::ptr::drop_in_place::<rustc_session[ba178145ab1da357]::parse::ParseSess>
  18:     0x7fd68d64fda3 - <alloc[40aff515e5175a27]::rc::Rc<rustc_session[ba178145ab1da357]::session::Session> as core[993276dbfd88d183]::ops::drop::Drop>::drop
  19:     0x7fd68d6763bd - core[993276dbfd88d183]::ptr::drop_in_place::<rustc_interface[48a3913523791f79]::interface::Compiler>
  20:     0x7fd68d675da4 - rustc_span[a7833de75ef67468]::with_source_map::<core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>, rustc_interface[48a3913523791f79]::interface::create_compiler_and_run<core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>, rustc_driver[3a72345c5a3a9278]::run_compiler::{closure#1}>::{closure#1}>
  21:     0x7fd68d652532 - <scoped_tls[5373c8190146815]::ScopedKey<rustc_span[a7833de75ef67468]::SessionGlobals>>::set::<rustc_interface[48a3913523791f79]::interface::run_compiler<core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>, rustc_driver[3a72345c5a3a9278]::run_compiler::{closure#1}>::{closure#0}, core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>>
  22:     0x7fd68d66751f - std[a5eff6be7f10e3b3]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[48a3913523791f79]::util::run_in_thread_pool_with_globals<rustc_interface[48a3913523791f79]::interface::run_compiler<core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>, rustc_driver[3a72345c5a3a9278]::run_compiler::{closure#1}>::{closure#0}, core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>>::{closure#0}, core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>>
  23:     0x7fd68d667659 - <<std[a5eff6be7f10e3b3]::thread::Builder>::spawn_unchecked_<rustc_interface[48a3913523791f79]::util::run_in_thread_pool_with_globals<rustc_interface[48a3913523791f79]::interface::run_compiler<core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>, rustc_driver[3a72345c5a3a9278]::run_compiler::{closure#1}>::{closure#0}, core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>>::{closure#0}, core[993276dbfd88d183]::result::Result<(), rustc_errors[8af9c38e420da921]::ErrorGuaranteed>>::{closure#1} as core[993276dbfd88d183]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  24:     0x7fd68ac914f3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h63ee45655a45e444
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/alloc/src/boxed.rs:1951:9
  25:     0x7fd68ac914f3 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::ha9e4bd17f57e4e2f
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/alloc/src/boxed.rs:1951:9
  26:     0x7fd68ac914f3 - std::sys::unix::thread::Thread::new::thread_start::h4bd37e41a2fb87e0
                               at /rustc/2f3ddd9f594adf9773547aa7cedb43c4ac8ffd2f/library/std/src/sys/unix/thread.rs:108:17
  27:     0x7fd68abb4609 - start_thread
  28:     0x7fd68aad7133 - clone
  29:                0x0 - <unknown>

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.64.0-nightly (2f3ddd9f5 2022-06-27) running on x86_64-unknown-linux-gnu

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

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

query stack during panic:
end of query stack
error: could not compile `playground`
Backtrace

stack backtrace:
   0: std::panicking::begin_panic::<rustc_errors::ExplicitBug>
   1: std::panic::panic_any::<rustc_errors::ExplicitBug>
   2: <rustc_errors::HandlerInner as core::ops::drop::Drop>::drop
   3: core::ptr::drop_in_place::<rustc_session::parse::ParseSess>
   4: <alloc::rc::Rc<rustc_session::session::Session> as core::ops::drop::Drop>::drop
   5: core::ptr::drop_in_place::<rustc_interface::interface::Compiler>
   6: rustc_span::with_source_map::<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_interface::interface::create_compiler_and_run<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#1}>
   7: <scoped_tls::ScopedKey<rustc_span::SessionGlobals>>::set::<rustc_interface::interface::run_compiler<core::result::Result<(), rustc_errors::ErrorGuaranteed>, rustc_driver::run_compiler::{closure#1}>::{closure#0}, core::result::Result<(), rustc_errors::ErrorGuaranteed>>

@KSBilodeau KSBilodeau 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 Jun 28, 2022
@KSBilodeau
Copy link
Contributor Author

This ICE is also triggered by unsizing coercion such as follows:

type AsyncFnPtr = Box<
    dyn Fn() -> std::pin::Pin<Box<dyn std::future::Future<Output = ()>>>,
>;

async fn test() {}

fn main() {
    let _: AsyncFnPtr = Box::new(test);
}

@Noratrieb
Copy link
Member

Noratrieb commented Jun 28, 2022

Borrowck is correct in rejecting this code, since the cast is not allowed like this. Looks like some earlier pass didn't check it it correctly.

@compiler-errors
Copy link
Member

cc @oli-obk this regressed in #94081

@oli-obk
Copy link
Contributor

oli-obk commented Jun 28, 2022

Yea regular typeck should already reject that cast/coercion

@compiler-errors
Copy link
Member

Strangely, inlining the type alias's definition gives us:

error[[E0271]](https://doc.rust-lang.org/nightly/error-index.html#E0271): type mismatch resolving `<fn() -> impl Future<Output = ()> {test} as FnOnce<()>>::Output == Pin<Box<dyn Future<Output = ()>>>`
 --> src/main.rs:5:5
  |
5 |     Box::new(test) as Box<
  |     ^^^^^^^^^^^^^^ expected struct `Pin`, found opaque type
  |
note: while checking the return type of the `async fn`
 --> src/main.rs:1:17
  |
1 | async fn test() {}
  |                 ^ checked the `Output` of this `async fn`, found opaque type
  = note:   expected struct `Pin<Box<dyn Future<Output = ()>>>`
          found opaque type `impl Future<Output = ()>`
  = note: required for the cast to the object type `dyn Fn() -> Pin<Box<dyn Future<Output = ()>>>`

For more information about this error, try `rustc --explain E0271`.
error: could not compile `playground` due to previous error

@Noratrieb
Copy link
Member

I investigated this closer and.. found a soundness bug! #98608

@Noratrieb
Copy link
Member

Seems like the soundness bug is caused by the same issue, so fixing that should fix this as well. It's just that here borrowck managed to find the broken code and bail out, but there, no one was there to catch it.

@dtolnay
Copy link
Member

dtolnay commented Jun 28, 2022

Correct error in 1.60.0, and nightly prior to f132bcf:

error[E0271]: type mismatch resolving `<fn() -> impl Future<Output = ()> {test} as FnOnce<()>>::Output == Pin<Box<(dyn Future<Output = ()> + 'static)>>`
 --> src/main.rs:9:5
  |
9 |     Box::new(test) as AsyncFnPtr;
  |     ^^^^^^^^^^^^^^ expected struct `Pin`, found opaque type
  |
note: while checking the return type of the `async fn`
 --> src/main.rs:5:17
  |
5 | 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 to the object type `dyn Fn() -> Pin<Box<(dyn Future<Output = ()> + 'static)>>`

For more information about this error, try `rustc --explain E0271`.

@dtolnay dtolnay added the regression-from-stable-to-stable Performance or correctness regression from one stable version to another. label Jun 28, 2022
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Jun 28, 2022
@compiler-errors
Copy link
Member

just for the record, oli figured out the bug, so other ppl don't waste too much time investigating

matthiaskrgr added a commit to matthiaskrgr/glacier that referenced this issue Jun 28, 2022
@rust-lang-glacier-bot rust-lang-glacier-bot added the glacier ICE tracked in rust-lang/glacier. label Jun 28, 2022
@Dylan-DPC Dylan-DPC added P-medium Medium priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Jun 30, 2022
@Dylan-DPC
Copy link
Member

Marked p-medium as per zulip triage discussion

@apiraino
Copy link
Contributor

Link for WG-prioritization Zulip discussion

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. glacier ICE tracked in rust-lang/glacier. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-medium Medium priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. 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.

9 participants