rust-lang / rust-lang/rust

ICE in `check_mod_const_bodies` when compiling a modified libcore

Open
#122,948 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-stability C-bug F-rustc_attrs I-ICE P-low requires-internal-features T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Code

In the Rust repository, modify libcore with this patch:

diff --git a/library/core/src/num/int_macros.rs b/library/core/src/num/int_macros.rs
index fa37ee4ffb2..136d048025d 100644
--- a/library/core/src/num/int_macros.rs
+++ b/library/core/src/num/int_macros.rs
@@ -2653,19 +2653,24 @@ pub const fn pow(self, mut exp: u32) -> Self {
             let mut base = self;
             let mut acc = 1;
 
-            while exp > 1 {
-                if (exp & 1) == 1 {
+            loop {
+                let n_zeros = exp.trailing_zeros();
+                for _ in 0..n_zeros {
+                    base = base * base;
+                }
+                exp >>= n_zeros;
+                let n_ones = exp.trailing_ones();
+                for _ in 0..(n_ones - 1) {
                     acc = acc * base;
+                    base = base * base;
+                }
+                acc = acc * base;
+                exp >>= n_ones;
+                if exp == 0 {
+                    return acc;
                 }
-                exp /= 2;
                 base = base * base;
             }
-
-            // since exp!=0, finally the exp must be 1.
-            // Deal with the final bit of the exponent separately, since
-            // squaring the base afterwards is not necessary and may cause a
-            // needless overflow.
-            acc * base
         }
 
diff --git a/library/core/src/num/uint_macros.rs b/library/core/src/num/uint_macros.rs
index 081a3c0b118..adbc48d3cd2 100644
--- a/library/core/src/num/uint_macros.rs
+++ b/library/core/src/num/uint_macros.rs
@@ -2439,19 +2439,24 @@ pub const fn pow(self, mut exp: u32) -> Self {
             let mut base = self;
             let mut acc = 1;
 
-            while exp > 1 {
-                if (exp & 1) == 1 {
+            loop {
+                let n_zeros = exp.trailing_zeros();
+                for _ in 0..n_zeros {
+                    base = base * base;
+                }
+                exp >>= n_zeros;
+                let n_ones = exp.trailing_ones();
+                for _ in 0..(n_ones - 1) {
                     acc = acc * base;
+                    base = base * base;
+                }
+                acc = acc * base;
+                exp >>= n_ones;
+                if exp == 0 {
+                    return acc;
                 }
-                exp /= 2;
                 base = base * base;
             }
-
-            // since exp!=0, finally the exp must be 1.
-            // Deal with the final bit of the exponent separately, since
-            // squaring the base afterwards is not necessary and may cause a
-            // needless overflow.
-            acc * base
         }
 

Changed settings in config.toml:

codegen-units-std = 1
incremental = false

Then build the library with ./x build library/core --stage 0

Meta

rustc version: 1.78.0-beta.1 (efd9d2df1 2024-03-18)

Error output
error: internal compiler error: compiler/rustc_passes/src/check_const.rs:142:17: we should not have reached this point, since `.await` is denied earlier
    --> library/core/src/num/int_macros.rs:2658:17
     |
1    | /   macro_rules! int_impl {
2    | |       (
3    | |           Self = $SelfT:ty,
4    | |           ActualT = $ActualT:ident,
...    |
2658 | | /                 for _ in 0..n_zeros {
2659 | | |                     base = base * base;
2660 | | |                 }
     | | |_________________^
...    |
3533 | |       }
3534 | |   }
     | |___- in this expansion of `int_impl!` (#1)
     |
    ::: library/core/src/num/mod.rs:301:5
     |
301  |   /     int_impl! {
302  |   |         Self = i8,
303  |   |         ActualT = i8,
304  |   |         UnsignedT = u8,
...      |
319  |   |         bound_condition = "",
320  |   |     }
     |   |_____- in this macro invocation (#1)

thread 'rustc' panicked at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/compiler/rustc_middle/src/util/bug.rs:34:50:
Box<dyn Any>

rustc-ice-to-report.txt

Backtrace

   0:     0x7f38bdf8930f - std::backtrace_rs::backtrace::libunwind::trace::h539c03b0789a3163
                               at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/library/std/src/../../backtrace/src/backtrace/libunwind.rs:105:5
   1:     0x7f38bdf8930f - std::backtrace_rs::backtrace::trace_unsynchronized::h90a51b49b444fc01
                               at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7f38bdf8930f - std::backtrace::Backtrace::create::h52a6c1d2ae0a3112
                               at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/library/std/src/backtrace.rs:331:13
   3:     0x7f38bdf89250 - std::backtrace::Backtrace::force_capture::h70557b27472ed963
                               at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/library/std/src/backtrace.rs:312:9
   4:     0x7f38bac2ccb7 - std[999bdecce25602ed]::panicking::update_hook::<alloc[5d2a9fe8278cb303]::boxed::Box<rustc_driver_impl[e6c4295f52574c92]::install_ice_hook::{closure#0}>>::{closure#0}
   5:     0x7f38bdfa3b50 - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h8e05c89c9dfd827c
                               at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/library/alloc/src/boxed.rs:2034:9
   6:     0x7f38bdfa3b50 - std::panicking::rust_panic_with_hook::h6987b578e7dec23c
                               at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/library/std/src/panicking.rs:783:13
   7:     0x7f38bac55b94 - std[999bdecce25602ed]::panicking::begin_panic::<rustc_errors[17bedd29219f425c]::ExplicitBug>::{closure#0}
   8:     0x7f38bac52906 - std[999bdecce25602ed]::sys_common::backtrace::__rust_end_short_backtrace::<std[999bdecce25602ed]::panicking::begin_panic<rustc_errors[17bedd29219f425c]::ExplicitBug>::{closure#0}, !>
   9:     0x7f38bac525e6 - std[999bdecce25602ed]::panicking::begin_panic::<rustc_errors[17bedd29219f425c]::ExplicitBug>
  10:     0x7f38bac5f2b1 - <rustc_errors[17bedd29219f425c]::diagnostic::BugAbort as rustc_errors[17bedd29219f425c]::diagnostic::EmissionGuarantee>::emit_producing_guarantee
  11:     0x7f38bb2d6c1c - <rustc_errors[17bedd29219f425c]::DiagCtxt>::span_bug::<rustc_span[ebf54beea9088bcf]::span_encoding::Span, alloc[5d2a9fe8278cb303]::string::String>
  12:     0x7f38bb2e844d - rustc_middle[f6bb74e4dd5a8f75]::util::bug::opt_span_bug_fmt::<rustc_span[ebf54beea9088bcf]::span_encoding::Span>::{closure#0}
  13:     0x7f38bb2e858a - rustc_middle[f6bb74e4dd5a8f75]::ty::context::tls::with_opt::<rustc_middle[f6bb74e4dd5a8f75]::util::bug::opt_span_bug_fmt<rustc_span[ebf54beea9088bcf]::span_encoding::Span>::{closure#0}, !>::{closure#0}
  14:     0x7f38bb2e3178 - rustc_middle[f6bb74e4dd5a8f75]::ty::context::tls::with_context_opt::<rustc_middle[f6bb74e4dd5a8f75]::ty::context::tls::with_opt<rustc_middle[f6bb74e4dd5a8f75]::util::bug::opt_span_bug_fmt<rustc_span[ebf54beea9088bcf]::span_encoding::Span>::{closure#0}, !>::{closure#0}, !>
  15:     0x7f38bb2e24d4 - rustc_middle[f6bb74e4dd5a8f75]::util::bug::span_bug_fmt::<rustc_span[ebf54beea9088bcf]::span_encoding::Span>
  16:     0x7f38bcead8f1 - <rustc_passes[6fa7bc5fc5542eb4]::check_const::CheckConstVisitor>::const_check_violated
  17:     0x7f38b948bb28 - <rustc_passes[6fa7bc5fc5542eb4]::check_const::CheckConstVisitor as rustc_hir[121f359932e59ede]::intravisit::Visitor>::visit_expr
  18:     0x7f38b948bc6a - <rustc_passes[6fa7bc5fc5542eb4]::check_const::CheckConstVisitor as rustc_hir[121f359932e59ede]::intravisit::Visitor>::visit_expr
  19:     0x7f38bc084c42 - <rustc_passes[6fa7bc5fc5542eb4]::check_const::CheckConstVisitor as rustc_hir[121f359932e59ede]::intravisit::Visitor>::visit_block
  20:     0x7f38bc084b4d - <rustc_passes[6fa7bc5fc5542eb4]::check_const::CheckConstVisitor as rustc_hir[121f359932e59ede]::intravisit::Visitor>::visit_body
  21:     0x7f38bc081d46 - rustc_passes[6fa7bc5fc5542eb4]::check_const::check_mod_const_bodies
  22:     0x7f38bc081839 - rustc_query_impl[e29825a6f88888a7]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e29825a6f88888a7]::query_impl::check_mod_const_bodies::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f6bb74e4dd5a8f75]::query::erase::Erased<[u8; 0usize]>>
  23:     0x7f38bc6528ca - rustc_query_system[3350c2d9ca0121e2]::query::plumbing::try_execute_query::<rustc_query_impl[e29825a6f88888a7]::DynamicConfig<rustc_query_system[3350c2d9ca0121e2]::query::caches::DefaultCache<rustc_span[ebf54beea9088bcf]::def_id::LocalModDefId, rustc_middle[f6bb74e4dd5a8f75]::query::erase::Erased<[u8; 0usize]>>, false, false, false>, rustc_query_impl[e29825a6f88888a7]::plumbing::QueryCtxt, false>
  24:     0x7f38bc65243f - rustc_query_impl[e29825a6f88888a7]::query_impl::check_mod_const_bodies::get_query_non_incr::__rust_end_short_backtrace
  25:     0x7f38bc655482 - rustc_interface[2cd87b402a7bbb07]::passes::analysis
  26:     0x7f38bc654f65 - rustc_query_impl[e29825a6f88888a7]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[e29825a6f88888a7]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f6bb74e4dd5a8f75]::query::erase::Erased<[u8; 1usize]>>
  27:     0x7f38bcb01ba2 - rustc_query_system[3350c2d9ca0121e2]::query::plumbing::try_execute_query::<rustc_query_impl[e29825a6f88888a7]::DynamicConfig<rustc_query_system[3350c2d9ca0121e2]::query::caches::SingleCache<rustc_middle[f6bb74e4dd5a8f75]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[e29825a6f88888a7]::plumbing::QueryCtxt, false>
  28:     0x7f38bcb01949 - rustc_query_impl[e29825a6f88888a7]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  29:     0x7f38bc9a5894 - rustc_interface[2cd87b402a7bbb07]::interface::run_compiler::<core[75bed1c93f3984e1]::result::Result<(), rustc_span[ebf54beea9088bcf]::ErrorGuaranteed>, rustc_driver_impl[e6c4295f52574c92]::run_compiler::{closure#0}>::{closure#0}
  30:     0x7f38bccb1b2e - std[999bdecce25602ed]::sys_common::backtrace::__rust_begin_short_backtrace::<rustc_interface[2cd87b402a7bbb07]::util::run_in_thread_with_globals<rustc_interface[2cd87b402a7bbb07]::interface::run_compiler<core[75bed1c93f3984e1]::result::Result<(), rustc_span[ebf54beea9088bcf]::ErrorGuaranteed>, rustc_driver_impl[e6c4295f52574c92]::run_compiler::{closure#0}>::{closure#0}, core[75bed1c93f3984e1]::result::Result<(), rustc_span[ebf54beea9088bcf]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[75bed1c93f3984e1]::result::Result<(), rustc_span[ebf54beea9088bcf]::ErrorGuaranteed>>
  31:     0x7f38bccb198a - <<std[999bdecce25602ed]::thread::Builder>::spawn_unchecked_<rustc_interface[2cd87b402a7bbb07]::util::run_in_thread_with_globals<rustc_interface[2cd87b402a7bbb07]::interface::run_compiler<core[75bed1c93f3984e1]::result::Result<(), rustc_span[ebf54beea9088bcf]::ErrorGuaranteed>, rustc_driver_impl[e6c4295f52574c92]::run_compiler::{closure#0}>::{closure#0}, core[75bed1c93f3984e1]::result::Result<(), rustc_span[ebf54beea9088bcf]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[75bed1c93f3984e1]::result::Result<(), rustc_span[ebf54beea9088bcf]::ErrorGuaranteed>>::{closure#1} as core[75bed1c93f3984e1]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  32:     0x7f38bdfad1e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h3b366b3d6b556d67
                               at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/library/alloc/src/boxed.rs:2020:9
  33:     0x7f38bdfad1e5 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h49756348eb63eea1
                               at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/library/alloc/src/boxed.rs:2020:9
  34:     0x7f38bdfad1e5 - std::sys::pal::unix::thread::Thread::new::thread_start::hdf802a7c2e3fdf20
                               at /rustc/efd9d2df12b5e17fac0b4d0fb89f612ecd79f259/library/std/src/sys/pal/unix/thread.rs:108:17

note: compiler flags: --crate-type lib -C opt-level=3 -C embed-bitcode=no -C codegen-units=1 -Z unstable-options -C symbol-mangling-version=legacy -Z unstable-options -Z unstable-options -Z macro-backtrace -C split-debuginfo=off -C prefer-dynamic -Z inline-mir -C link-args=-Wl,-z,origin -C link-args=-Wl,-rpath,$ORIGIN/../lib -C force-frame-pointers=yes -Z crate-attr=doc(html_root_url="https://doc.rust-lang.org/nightly/") -Z binary-dep-depinfo -Z force-unstable-if-unmarked

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the ICE by applying the shown changes to library/core/src/num/int_macros.rs and library/core/src/num/uint_macros.rs, updating config.toml, and running ./x build library/core --stage 0. Start with compiler/rustc_passes/src/check_const.rs and the reported check_mod_const_bodies path. Done means the modified libcore builds without the internal compiler error.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.