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

Intrinsics declared with wrong arg count ICE #123587

Closed
gurry opened this issue Apr 7, 2024 · 5 comments
Closed

Intrinsics declared with wrong arg count ICE #123587

gurry opened this issue Apr 7, 2024 · 5 comments
Labels
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.

Comments

@gurry
Copy link
Contributor

gurry commented Apr 7, 2024

An ICE occurs if you declare an intrinsic with the wrong number of args and then try to use it.

Code

#![feature(intrinsics)]

fn main() {
   unsafe { read_via_copy(); }
}

extern "rust-intrinsic" 
{
    fn read_via_copy();
}

Meta

rustc --version --verbose:

rustc 1.79.0-nightly (9d79cd5f7 2024-04-05)
binary: rustc
commit-hash: 9d79cd5f79e75bd0d2083260271307ce9acd9081
commit-date: 2024-04-05
host: x86_64-unknown-linux-gnu
release: 1.79.0-nightly
LLVM version: 18.1.2

Error output

  Compiling playground v0.0.1 (/playground)
warning: the feature `intrinsics` is internal to the compiler or standard library
 --> src/main.rs:1:12
  |
1 | #![feature(intrinsics)]
  |            ^^^^^^^^^^
  |
  = note: using it is strongly discouraged
  = note: `#[warn(internal_features)]` on by default

error[E0094]: intrinsic has wrong number of type parameters: found 0, expected 1
 --> src/main.rs:9:21
  |
9 |     fn read_via_copy();
  |                     ^ expected 1 type parameter
Backtrace

error: internal compiler error: compiler/rustc_mir_transform/src/lower_intrinsics.rs:177:29: Wrong number of arguments
 --> src/main.rs:4:13
  |
4 |    unsafe { read_via_copy(); }
  |             ^^^^^^^^^^^^^^^

thread 'rustc' panicked at compiler/rustc_mir_transform/src/lower_intrinsics.rs:177:29:
Box<dyn Any>
stack backtrace:
   0:     0x7f5e62c0b885 - std::backtrace_rs::backtrace::libunwind::trace::h2ac23be513f598d8
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7f5e62c0b885 - std::backtrace_rs::backtrace::trace_unsynchronized::h714851d0c638595f
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f5e62c0b885 - std::sys_common::backtrace::_print_fmt::hde31106e881318f4
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7f5e62c0b885 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h5e44711e8e3e9bff
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7f5e62c5ab9b - core::fmt::rt::Argument::fmt::h9965750ec07aa88a
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/core/src/fmt/rt.rs:142:9
   5:     0x7f5e62c5ab9b - core::fmt::write::hc2ec2e3627fd006a
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/core/src/fmt/mod.rs:1153:17
   6:     0x7f5e62c003ff - std::io::Write::write_fmt::h1f82f51174f3abb0
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/io/mod.rs:1843:15
   7:     0x7f5e62c0b65e - std::sys_common::backtrace::_print::h003159894b30f46f
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7f5e62c0b65e - std::sys_common::backtrace::print::h787b5c1503f03df7
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7f5e62c0e159 - std::panicking::default_hook::{{closure}}::h5939a448f377040a
  10:     0x7f5e62c0de75 - std::panicking::default_hook::h3651e83a5c639e04
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/panicking.rs:291:9
  11:     0x7f5e65cb7aff - std[f2945facf614d202]::panicking::update_hook::<alloc[406fac374097d7f1]::boxed::Box<rustc_driver_impl[3d4f9373b5a704b5]::install_ice_hook::{closure#0}>>::{closure#0}
  12:     0x7f5e62c0e85c - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h53e9ce019f51ba71
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/alloc/src/boxed.rs:2032:9
  13:     0x7f5e62c0e85c - std::panicking::rust_panic_with_hook::h71ac878b54cf8b64
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/panicking.rs:792:13
  14:     0x7f5e65ce73c4 - std[f2945facf614d202]::panicking::begin_panic::<rustc_errors[39c86ef847bc51a8]::ExplicitBug>::{closure#0}
  15:     0x7f5e65ce3e66 - std[f2945facf614d202]::sys_common::backtrace::__rust_end_short_backtrace::<std[f2945facf614d202]::panicking::begin_panic<rustc_errors[39c86ef847bc51a8]::ExplicitBug>::{closure#0}, !>
  16:     0x7f5e65cdf486 - std[f2945facf614d202]::panicking::begin_panic::<rustc_errors[39c86ef847bc51a8]::ExplicitBug>
  17:     0x7f5e65cf1101 - <rustc_errors[39c86ef847bc51a8]::diagnostic::BugAbort as rustc_errors[39c86ef847bc51a8]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  18:     0x7f5e66288628 - <rustc_errors[39c86ef847bc51a8]::DiagCtxt>::span_bug::<rustc_span[a0270aa614b2185f]::span_encoding::Span, alloc[406fac374097d7f1]::string::String>
  19:     0x7f5e662a788d - rustc_middle[ed380b410dad9775]::util::bug::opt_span_bug_fmt::<rustc_span[a0270aa614b2185f]::span_encoding::Span>::{closure#0}
  20:     0x7f5e662a78ba - rustc_middle[ed380b410dad9775]::ty::context::tls::with_opt::<rustc_middle[ed380b410dad9775]::util::bug::opt_span_bug_fmt<rustc_span[a0270aa614b2185f]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  21:     0x7f5e662a082b - rustc_middle[ed380b410dad9775]::ty::context::tls::with_context_opt::<rustc_middle[ed380b410dad9775]::ty::context::tls::with_opt<rustc_middle[ed380b410dad9775]::util::bug::opt_span_bug_fmt<rustc_span[a0270aa614b2185f]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  22:     0x7f5e6629fa77 - rustc_middle[ed380b410dad9775]::util::bug::span_bug_fmt::<rustc_span[a0270aa614b2185f]::span_encoding::Span>
  23:     0x7f5e67763b16 - <rustc_mir_transform[aadc44431e968acb]::lower_intrinsics::LowerIntrinsics as rustc_middle[ed380b410dad9775]::mir::MirPass>::run_pass
  24:     0x7f5e6708088d - rustc_mir_transform[aadc44431e968acb]::pass_manager::run_passes_inner
  25:     0x7f5e67782242 - rustc_mir_transform[aadc44431e968acb]::mir_drops_elaborated_and_const_checked
  26:     0x7f5e67781b13 - rustc_query_impl[f28317b5059654ba]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[f28317b5059654ba]::query_impl::mir_drops_elaborated_and_const_checked::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ed380b410dad9775]::query::erase::Erased<[u8; 8usize]>>
  27:     0x7f5e673550f1 - rustc_query_system[4cc60357b0579df9]::query::plumbing::try_execute_query::<rustc_query_impl[f28317b5059654ba]::DynamicConfig<rustc_query_system[4cc60357b0579df9]::query::caches::VecCache<rustc_span[a0270aa614b2185f]::def_id::LocalDefId, rustc_middle[ed380b410dad9775]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[f28317b5059654ba]::plumbing::QueryCtxt, false>
  28:     0x7f5e67354aa4 - rustc_query_impl[f28317b5059654ba]::query_impl::mir_drops_elaborated_and_const_checked::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7f5e679414f2 - rustc_interface[54c6bd1de51731e6]::passes::analysis
  30:     0x7f5e679402d5 - rustc_query_impl[f28317b5059654ba]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[f28317b5059654ba]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[ed380b410dad9775]::query::erase::Erased<[u8; 1usize]>>
  31:     0x7f5e67e9f6e5 - rustc_query_system[4cc60357b0579df9]::query::plumbing::try_execute_query::<rustc_query_impl[f28317b5059654ba]::DynamicConfig<rustc_query_system[4cc60357b0579df9]::query::caches::SingleCache<rustc_middle[ed380b410dad9775]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[f28317b5059654ba]::plumbing::QueryCtxt, false>
  32:     0x7f5e67e9f449 - rustc_query_impl[f28317b5059654ba]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  33:     0x7f5e67c4f914 - rustc_interface[54c6bd1de51731e6]::interface::run_compiler::<core[faf3c98050982095]::result::Result<(), rustc_span[a0270aa614b2185f]::ErrorGuaranteed>, rustc_driver_impl[3d4f9373b5a704b5]::run_compiler::{closure#0}>::{closure#0}
  34:     0x7f5e67e2c41d - std[f2945facf614d202]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[54c6bd1de51731e6]::util::run_in_thread_with_globals<rustc_interface[54c6bd1de51731e6]::util::run_in_thread_pool_with_globals<rustc_interface[54c6bd1de51731e6]::interface::run_compiler<core[faf3c98050982095]::result::Result<(), rustc_span[a0270aa614b2185f]::ErrorGuaranteed>, rustc_driver_impl[3d4f9373b5a704b5]::run_compiler::{closure#0}>::{closure#0}, core[faf3c98050982095]::result::Result<(), rustc_span[a0270aa614b2185f]::ErrorGuaranteed>>::{closure#0}, core[faf3c98050982095]::result::Result<(), rustc_span[a0270aa614b2185f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[faf3c98050982095]::result::Result<(), rustc_span[a0270aa614b2185f]::ErrorGuaranteed>>
  35:     0x7f5e67e2c22a - <<std[f2945facf614d202]::thread::Builder>::spawn_unchecked_<rustc_interface[54c6bd1de51731e6]::util::run_in_thread_with_globals<rustc_interface[54c6bd1de51731e6]::util::run_in_thread_pool_with_globals<rustc_interface[54c6bd1de51731e6]::interface::run_compiler<core[faf3c98050982095]::result::Result<(), rustc_span[a0270aa614b2185f]::ErrorGuaranteed>, rustc_driver_impl[3d4f9373b5a704b5]::run_compiler::{closure#0}>::{closure#0}, core[faf3c98050982095]::result::Result<(), rustc_span[a0270aa614b2185f]::ErrorGuaranteed>>::{closure#0}, core[faf3c98050982095]::result::Result<(), rustc_span[a0270aa614b2185f]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[faf3c98050982095]::result::Result<(), rustc_span[a0270aa614b2185f]::ErrorGuaranteed>>::{closure#1} as core[faf3c98050982095]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  36:     0x7f5e62c1828b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h63394803e5a3b584
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/alloc/src/boxed.rs:2018:9
  37:     0x7f5e62c1828b - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::hbe514eade38645bb
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/alloc/src/boxed.rs:2018:9
  38:     0x7f5e62c1828b - std::sys::pal::unix::thread::Thread::new::thread_start::hb4be61ed23447ea6
                               at /rustc/aa1c45908df252a5b0c14e1bcb38c6c55ae02efe/library/std/src/sys/pal/unix/thread.rs:108:17
  39:     0x7f5e62b25609 - start_thread
  40:     0x7f5e62a48353 - clone
  41:                0x0 - <unknown>

note: using internal features is not supported and expected to cause internal compiler errors when used incorrectly

note: please attach the file at `/playground/rustc-ice-2024-04-07T07_42_23-24.txt` to your bug report

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:
#0 [mir_drops_elaborated_and_const_checked] elaborating drops for `main`
#1 [analysis] running analysis passes on this crate
end of query stack

The ICE originates in lower_intrinsics.rs e.g. over here for read_via_copy:

let [arg] = args.as_slice() else {
span_bug!(terminator.source_info.span, "Wrong number of arguments");
};

Since there are a lot of occurrences of span_bug!(..., "Wrong number of arguments...") in this file, many other intrinsics will also ICE under the given conditions.

Related issue: #123442 which is a special case of this issue for the transmute intrinsic.

@gurry gurry 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 Apr 7, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 7, 2024
@gurry gurry changed the title Intrinsic declared with wrong arg count ICE Intrinsics declared with wrong arg count ICE Apr 7, 2024
@Noratrieb
Copy link
Member

Noratrieb commented Apr 7, 2024

note: using internal features is not supported and expected to cause internal compiler errors when used incorrectly

It's a bit hidden in the ICE message, understandable that you've missed it.

@Noratrieb Noratrieb closed this as not planned Won't fix, can't repro, duplicate, stale Apr 7, 2024
@gurry
Copy link
Contributor Author

gurry commented Apr 7, 2024

Oh. I should've seen that. Thanks :)

@jieyouxu jieyouxu removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 7, 2024
@estebank
Copy link
Contributor

estebank commented Apr 7, 2024

Copying from #123526 (comment):

we [should] introduce a new, separate, version of panicking that doesn't "look" like an ICE but rather a generic "invalid language state" error that stops the compilation, with no stack trace.

I think this ticket is a good example of a case where having a distinction between "the compiler made a mistake" and "the user modifiable language state is incorrect" in the panic output would be useful. It'd be already great if it only applied to the "expect" methods (like "assume the Sized trait is available" methods). If those weren't an automatic ICE, I think we could clean up rustc code a bunch.

@Noratrieb
Copy link
Member

You could replace all of the bugs with that but you can't replace things like indexing with it without making the compiler code uglier. I am not sure where rustc could be cleaned up by making this code less panicky... if there's anything that would be cleaner in the compiler code without panicking then please just do that today!

@Noratrieb
Copy link
Member

i think we should just make the ICE message here a bit clearer
keep the stack trace or whatever but mention way more clearly that internal features are not supported and that if the ICE is related to that, that we don't want a bug report.

fmease added a commit to fmease/rust that referenced this issue Apr 15, 2024
…tebank

Don't even parse an intrinsic unless the feature gate is enabled

Don't return true in `tcx.is_intrinsic` if the function is defined locally and `#![feature(intrinsics)]` is not enabled. This is a slightly more general fix than rust-lang#123526, since rust-lang#123587 shows that we have simplifying assumptions about intrinsics elsewhere in the compiler.

This will make the code ICE again if the user **enables** `#[feature(intrinsics)]`, but I kind of feel like if we want to fix that, we should make the `INTERNAL_FEATURES` lint `Deny` again. Perhaps we could do that on non-nightly compilers. Or we should just stop compilation altogether if they have `#![feature]` enabled on a non-nightly compiler.

As for the UX of *real* cases of hitting these ICEs, I believe pretty strongly that if a compiler/stdlib dev is modifying internal intrinsics (intentionally, like when making a change to rustc) we have no guarantee to make the ICE better looking for them. Honestly, *not* spitting out a stack trace is probably a disservice to the people who hit those ICEs in that case.

r? `@Nilstrieb` `@estebank`
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Apr 16, 2024
Rollup merge of rust-lang#123603 - compiler-errors:no-intrinsic, r=estebank

Don't even parse an intrinsic unless the feature gate is enabled

Don't return true in `tcx.is_intrinsic` if the function is defined locally and `#![feature(intrinsics)]` is not enabled. This is a slightly more general fix than rust-lang#123526, since rust-lang#123587 shows that we have simplifying assumptions about intrinsics elsewhere in the compiler.

This will make the code ICE again if the user **enables** `#[feature(intrinsics)]`, but I kind of feel like if we want to fix that, we should make the `INTERNAL_FEATURES` lint `Deny` again. Perhaps we could do that on non-nightly compilers. Or we should just stop compilation altogether if they have `#![feature]` enabled on a non-nightly compiler.

As for the UX of *real* cases of hitting these ICEs, I believe pretty strongly that if a compiler/stdlib dev is modifying internal intrinsics (intentionally, like when making a change to rustc) we have no guarantee to make the ICE better looking for them. Honestly, *not* spitting out a stack trace is probably a disservice to the people who hit those ICEs in that case.

r? `@Nilstrieb` `@estebank`
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. 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.
Projects
None yet
Development

No branches or pull requests

5 participants