Closed
Description
auto-reduced (treereduce-rust):
use std::mem;
macro_rules! define_reify_functions {
($(
fn $name:ident $(<$($param:ident),*>)?
for $(extern $abi:tt)? fn($($arg:ident: $arg_ty:ty),*) -> $ret_ty:ty;
)+) => {
$(pub const fn $name<
$($($param,)*)?
F: Fn($($arg_ty),*) -> $ret_ty + Copy
>(f: F) -> $(extern $abi)? fn($($arg_ty),*) -> $ret_ty {
assert!(mem::size_of::<F>() == 0, );
$(extern $abi)? fn wrapper<
$($($param,)*)?
F: Fn($($arg_ty),*) -> $ret_ty + Copy
>($($arg: $arg_ty),*) -> $ret_ty {
let f = unsafe {
mem::MaybeUninit::<F>::uninit().assume_init()
};
f($($arg),*)
}
let _f_proof = f;
wrapper::<
$($($param,)*)?
F
>
})+
}
}
define_reify_functions! {
fn _reify_to_extern_c_fn_unary<A, R> for extern "C" fn(arg: A) -> R;
fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: super::BridgeConfig<'_>) -> R;
}
original code
original:
//! Abstraction for creating `fn` pointers from any callable that *effectively*
//! has the equivalent of implementing `Default`, even if the compiler neither
//! provides `Default` nor allows reifying closures (i.e. creating `fn` pointers)
//! other than those with absolutely no captures.
//!
//! More specifically, for a closure-like type to be "effectively `Default`":
//! * it must be a ZST (zero-sized type): no information contained within, so
//! that `Default`'s return value (if it were implemented) is unambiguous
//! * it must be `Copy`: no captured "unique ZST tokens" or any other similar
//! types that would make duplicating values at will unsound
//! * combined with the ZST requirement, this confers a kind of "telecopy"
//! ability: similar to `Copy`, but without keeping the value around, and
//! instead "reconstructing" it (a noop given it's a ZST) when needed
//! * it must be *provably* inhabited: no captured uninhabited types or any
//! other types that cannot be constructed by the user of this abstraction
//! * the proof is a value of the closure-like type itself, in a sense the
//! "seed" for the "telecopy" process made possible by ZST + `Copy`
//! * this requirement is the only reason an abstraction limited to a specific
//! usecase is required: ZST + `Copy` can be checked with *at worst* a panic
//! at the "attempted `::default()` call" time, but that doesn't guarantee
//! that the value can be soundly created, and attempting to use the typical
//! "proof ZST token" approach leads yet again to having a ZST + `Copy` type
//! that is not proof of anything without a value (i.e. isomorphic to a
//! newtype of the type it's trying to prove the inhabitation of)
//!
//! A more flexible (and safer) solution to the general problem could exist once
//! `const`-generic parameters can have type parameters in their types:
//!
//! ```rust,ignore (needs future const-generics)
//! extern "C" fn ffi_wrapper<
//! A, R,
//! F: Fn(A) -> R,
//! const f: F, // <-- this `const`-generic is not yet allowed
//! >(arg: A) -> R {
//! f(arg)
//! }
//! ```
use std::mem;
// FIXME(eddyb) this could be `trait` impls except for the `const fn` requirement.
macro_rules! define_reify_functions {
($(
fn $name:ident $(<$($param:ident),*>)?
for $(extern $abi:tt)? fn($($arg:ident: $arg_ty:ty),*) -> $ret_ty:ty;
)+) => {
$(pub const fn $name<
$($($param,)*)?
F: Fn($($arg_ty),*) -> $ret_ty + Copy
>(f: F) -> $(extern $abi)? fn($($arg_ty),*) -> $ret_ty {
// FIXME(eddyb) describe the `F` type (e.g. via `type_name::<F>`) once panic
// formatting becomes possible in `const fn`.
assert!(mem::size_of::<F>() == 0, "selfless_reify: closure must be zero-sized");
$(extern $abi)? fn wrapper<
$($($param,)*)?
F: Fn($($arg_ty),*) -> $ret_ty + Copy
>($($arg: $arg_ty),*) -> $ret_ty {
let f = unsafe {
// SAFETY: `F` satisfies all criteria for "out of thin air"
// reconstructability (see module-level doc comment).
mem::MaybeUninit::<F>::uninit().assume_init()
};
f($($arg),*)
}
let _f_proof = f;
wrapper::<
$($($param,)*)?
F
>
})+
}
}
define_reify_functions! {
fn _reify_to_extern_c_fn_unary<A, R> for extern "C" fn(arg: A) -> R;
// HACK(eddyb) this abstraction is used with `for<'a> fn(BridgeConfig<'a>)
// -> T` but that doesn't work with just `reify_to_extern_c_fn_unary`
// because of the `fn` pointer type being "higher-ranked" (i.e. the
// `for<'a>` binder).
// FIXME(eddyb) try to remove the lifetime from `BridgeConfig`, that'd help.
fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: super::BridgeConfig<'_>) -> R;
}
Version information
rustc 1.78.0-nightly (f70f19fef 2024-02-22)
binary: rustc
commit-hash: f70f19fef41cfdda75c92f163434c29ad046cf09
commit-date: 2024-02-22
host: x86_64-unknown-linux-gnu
release: 1.78.0-nightly
LLVM version: 18.1.0
Command:
/home/gh-matthiaskrgr/.rustup/toolchains/master/bin/rustc --crate-type=lib
Program output
error: internal compiler error: compiler/rustc_lint/src/types.rs:1439:32: unexpected type in foreign function: {type error}
thread 'rustc' panicked at compiler/rustc_middle/src/util/bug.rs:35:44:
Box<dyn Any>
stack backtrace:
0: 0x7f9ec8d8caf6 - std::backtrace_rs::backtrace::libunwind::trace::hef019eae7250a090
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
1: 0x7f9ec8d8caf6 - std::backtrace_rs::backtrace::trace_unsynchronized::h163c9c558e7f1e4e
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
2: 0x7f9ec8d8caf6 - std::sys_common::backtrace::_print_fmt::h84a7f2dbeae8e017
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/sys_common/backtrace.rs:68:5
3: 0x7f9ec8d8caf6 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hf7737af35c1403cd
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/sys_common/backtrace.rs:44:22
4: 0x7f9ec8ddda7c - core::fmt::rt::Argument::fmt::h027f33016919ab54
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/core/src/fmt/rt.rs:142:9
5: 0x7f9ec8ddda7c - core::fmt::write::hed9b631feef55974
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/core/src/fmt/mod.rs:1120:17
6: 0x7f9ec8d814bf - std::io::Write::write_fmt::h3b1c8483aebfcef8
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/io/mod.rs:1846:15
7: 0x7f9ec8d8c8a4 - std::sys_common::backtrace::_print::h526b95c0b60f4650
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/sys_common/backtrace.rs:47:5
8: 0x7f9ec8d8c8a4 - std::sys_common::backtrace::print::h3322d8b0a1d6207a
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/sys_common/backtrace.rs:34:9
9: 0x7f9ec8d8f5eb - std::panicking::default_hook::{{closure}}::h9f07e6147b1c8a2e
10: 0x7f9ec8d8f339 - std::panicking::default_hook::h395b386ace4fbcb6
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/panicking.rs:292:9
11: 0x7f9ecbc7073c - std[d26ecb90c771e100]::panicking::update_hook::<alloc[100d4e65626f4394]::boxed::Box<rustc_driver_impl[ab901dc85bea4c82]::install_ice_hook::{closure#0}>>::{closure#0}
12: 0x7f9ec8d8fd50 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h1a188bbe7a6bf646
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/alloc/src/boxed.rs:2030:9
13: 0x7f9ec8d8fd50 - std::panicking::rust_panic_with_hook::h1eec7c1097026ee4
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/panicking.rs:786:13
14: 0x7f9ecbc9ce14 - std[d26ecb90c771e100]::panicking::begin_panic::<rustc_errors[e9339b16b54c0bc0]::ExplicitBug>::{closure#0}
15: 0x7f9ecbc99d36 - std[d26ecb90c771e100]::sys_common::backtrace::__rust_end_short_backtrace::<std[d26ecb90c771e100]::panicking::begin_panic<rustc_errors[e9339b16b54c0bc0]::ExplicitBug>::{closure#0}, !>
16: 0x7f9ecbc99a16 - std[d26ecb90c771e100]::panicking::begin_panic::<rustc_errors[e9339b16b54c0bc0]::ExplicitBug>
17: 0x7f9eca618a41 - <rustc_errors[e9339b16b54c0bc0]::diagnostic::BugAbort as rustc_errors[e9339b16b54c0bc0]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
18: 0x7f9ecc07ae3c - <rustc_errors[e9339b16b54c0bc0]::DiagCtxt>::bug::<alloc[100d4e65626f4394]::string::String>
19: 0x7f9ecc11faab - rustc_middle[771c523b66a87e77]::util::bug::opt_span_bug_fmt::<rustc_span[4d20b27f335b5d92]::span_encoding::Span>::{closure#0}
20: 0x7f9ecc1033aa - rustc_middle[771c523b66a87e77]::ty::context::tls::with_opt::<rustc_middle[771c523b66a87e77]::util::bug::opt_span_bug_fmt<rustc_span[4d20b27f335b5d92]::span_encoding::Span>::{closure#0}, !>::{closure#0}
21: 0x7f9ecc103248 - rustc_middle[771c523b66a87e77]::ty::context::tls::with_context_opt::<rustc_middle[771c523b66a87e77]::ty::context::tls::with_opt<rustc_middle[771c523b66a87e77]::util::bug::opt_span_bug_fmt<rustc_span[4d20b27f335b5d92]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
22: 0x7f9eca590ee0 - rustc_middle[771c523b66a87e77]::util::bug::bug_fmt
23: 0x7f9ece1cc3af - <rustc_lint[ee0ae86ad0d71ee5]::types::ImproperCTypesVisitor>::check_type_for_ffi.cold.0
24: 0x7f9ecd1421ca - <rustc_lint[ee0ae86ad0d71ee5]::types::ImproperCTypesVisitor>::check_type_for_ffi
25: 0x7f9ecd14161e - <rustc_lint[ee0ae86ad0d71ee5]::types::ImproperCTypesVisitor>::check_type_for_ffi_and_report_errors
26: 0x7f9ecd75d352 - <rustc_lint[ee0ae86ad0d71ee5]::types::ImproperCTypesVisitor>::check_fn
27: 0x7f9ecd75c0be - <rustc_lint[ee0ae86ad0d71ee5]::BuiltinCombinedModuleLateLintPass as rustc_lint[ee0ae86ad0d71ee5]::passes::LateLintPass>::check_fn
28: 0x7f9ecd3a8cbe - <rustc_lint[ee0ae86ad0d71ee5]::late::LateContextAndPass<rustc_lint[ee0ae86ad0d71ee5]::BuiltinCombinedModuleLateLintPass> as rustc_hir[c73f1de04e7d2c16]::intravisit::Visitor>::visit_nested_item
29: 0x7f9ecd3a70bf - rustc_lint[ee0ae86ad0d71ee5]::lint_mod
30: 0x7f9ecd3a6eb9 - rustc_query_impl[9d2321c4f9b7ef6e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9d2321c4f9b7ef6e]::query_impl::lint_mod::dynamic_query::{closure#2}::{closure#0}, rustc_middle[771c523b66a87e77]::query::erase::Erased<[u8; 0usize]>>
31: 0x7f9ecdba4cad - rustc_query_system[1927276894a91dd8]::query::plumbing::try_execute_query::<rustc_query_impl[9d2321c4f9b7ef6e]::DynamicConfig<rustc_query_system[1927276894a91dd8]::query::caches::DefaultCache<rustc_span[4d20b27f335b5d92]::def_id::LocalModDefId, rustc_middle[771c523b66a87e77]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[9d2321c4f9b7ef6e]::plumbing::QueryCtxt, false>
32: 0x7f9ecdba35bf - rustc_query_impl[9d2321c4f9b7ef6e]::query_impl::lint_mod::get_query_non_incr::__rust_end_short_backtrace
33: 0x7f9ecdba315f - rustc_lint[ee0ae86ad0d71ee5]::late::check_crate::{closure#1}
34: 0x7f9ecdba2b5e - rustc_lint[ee0ae86ad0d71ee5]::late::check_crate
35: 0x7f9ecdb9ca65 - rustc_interface[863afa250f767019]::passes::analysis
36: 0x7f9ecdb9b759 - rustc_query_impl[9d2321c4f9b7ef6e]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[9d2321c4f9b7ef6e]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[771c523b66a87e77]::query::erase::Erased<[u8; 1usize]>>
37: 0x7f9ecdd45303 - rustc_query_system[1927276894a91dd8]::query::plumbing::try_execute_query::<rustc_query_impl[9d2321c4f9b7ef6e]::DynamicConfig<rustc_query_system[1927276894a91dd8]::query::caches::SingleCache<rustc_middle[771c523b66a87e77]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[9d2321c4f9b7ef6e]::plumbing::QueryCtxt, false>
38: 0x7f9ecdd4507f - rustc_query_impl[9d2321c4f9b7ef6e]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
39: 0x7f9ecdd54556 - rustc_interface[863afa250f767019]::interface::run_compiler::<core[17d6b7ce805ba05e]::result::Result<(), rustc_span[4d20b27f335b5d92]::ErrorGuaranteed>, rustc_driver_impl[ab901dc85bea4c82]::run_compiler::{closure#0}>::{closure#0}
40: 0x7f9ecdfbe305 - std[d26ecb90c771e100]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[863afa250f767019]::util::run_in_thread_with_globals<rustc_interface[863afa250f767019]::util::run_in_thread_pool_with_globals<rustc_interface[863afa250f767019]::interface::run_compiler<core[17d6b7ce805ba05e]::result::Result<(), rustc_span[4d20b27f335b5d92]::ErrorGuaranteed>, rustc_driver_impl[ab901dc85bea4c82]::run_compiler::{closure#0}>::{closure#0}, core[17d6b7ce805ba05e]::result::Result<(), rustc_span[4d20b27f335b5d92]::ErrorGuaranteed>>::{closure#0}, core[17d6b7ce805ba05e]::result::Result<(), rustc_span[4d20b27f335b5d92]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[17d6b7ce805ba05e]::result::Result<(), rustc_span[4d20b27f335b5d92]::ErrorGuaranteed>>
41: 0x7f9ecdfbe132 - <<std[d26ecb90c771e100]::thread::Builder>::spawn_unchecked_<rustc_interface[863afa250f767019]::util::run_in_thread_with_globals<rustc_interface[863afa250f767019]::util::run_in_thread_pool_with_globals<rustc_interface[863afa250f767019]::interface::run_compiler<core[17d6b7ce805ba05e]::result::Result<(), rustc_span[4d20b27f335b5d92]::ErrorGuaranteed>, rustc_driver_impl[ab901dc85bea4c82]::run_compiler::{closure#0}>::{closure#0}, core[17d6b7ce805ba05e]::result::Result<(), rustc_span[4d20b27f335b5d92]::ErrorGuaranteed>>::{closure#0}, core[17d6b7ce805ba05e]::result::Result<(), rustc_span[4d20b27f335b5d92]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[17d6b7ce805ba05e]::result::Result<(), rustc_span[4d20b27f335b5d92]::ErrorGuaranteed>>::{closure#1} as core[17d6b7ce805ba05e]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
42: 0x7f9ec8d99725 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h1e27cc9781dd2875
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/alloc/src/boxed.rs:2016:9
43: 0x7f9ec8d99725 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h4e0d9eddec111fd1
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/alloc/src/boxed.rs:2016:9
44: 0x7f9ec8d99725 - std::sys::pal::unix::thread::Thread::new::thread_start::h8ca0c4b428df1792
at /rustc/f70f19fef41cfdda75c92f163434c29ad046cf09/library/std/src/sys/pal/unix/thread.rs:108:17
45: 0x7f9ec8a94ac3 - start_thread
at ./nptl/pthread_create.c:442:8
46: 0x7f9ec8b26850 - __GI___clone3
at ./misc/../sysdeps/unix/sysv/linux/x86_64/clone3.S:81
47: 0x0 - <unknown>
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.78.0-nightly (f70f19fef 2024-02-22) running on x86_64-unknown-linux-gnu
note: compiler flags: --crate-type lib -Z dump-mir-dir=dir
query stack during panic:
#0 [lint_mod] linting top-level module
#1 [analysis] running analysis passes on this crate
end of query stack
error[E0433]: failed to resolve: there are too many leading `super` keywords
--> /tmp/icemaker_global_tempdir.lTYtezpoweDB/rustc_testrunner_tmpdir_reporting.5A2nqZBfiMNU/mvce.rs:53:69
|
53 | fn reify_to_extern_c_fn_hrt_bridge<R> for extern "C" fn(bridge: super::BridgeConfig<'_>) -> R;
| ^^^^^ there are too many leading `super` keywords
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0433`.