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

ICE: adding a def'n for node-id NodeId(18) and def kind AnonConst but a previous def'n exists #128016

Closed
matthiaskrgr opened this issue Jul 20, 2024 · 9 comments · Fixed by #129137 or #129246
Assignees
Labels
C-bug Category: This is a bug. I-ICE Issue: The compiler panicked, giving an Internal Compilation Error (ICE) ❄️ P-critical Critical priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matthiaskrgr
Copy link
Member

auto-reduced (treereduce-rust):

macro_rules! len {
    () => {
        target
    };
}

fn main() {
    let val: [str; len!()] = [];
}

original:

#![deny("{}", equal(&1, &2))]

macro_rules! len {
    () => (target) //~  ERROR trailing semicolon
                  //~| WARN this was previously accepted
}

fn main() {
    let val: [str; len!()] = [];
}

Version information

rustc 1.81.0-nightly (2e6fc4254 2024-07-20)
binary: rustc
commit-hash: 2e6fc425411280c658d852813de01c5157255088
commit-date: 2024-07-20
host: x86_64-unknown-linux-gnu
release: 1.81.0-nightly
LLVM version: 18.1.7

Command:
/home/matthias/.rustup/toolchains/master/bin/rustc

Program output

error[E0425]: cannot find value `target` in this scope
 --> /tmp/icemaker_global_tempdir.1khAqktZak0k/rustc_testrunner_tmpdir_reporting.F2c9o1Y4kXrG/mvce.rs:3:9
  |
3 |         target
  |         ^^^^^^ not found in this scope
...
8 |     let val: [str; len!()] = [];
  |                    ------ in this macro invocation
  |
  = note: this error originates in the macro `len` (in Nightly builds, run with -Z macro-backtrace for more info)

thread 'rustc' panicked at compiler/rustc_ast_lowering/src/lib.rs:503:9:
adding a def'n for node-id NodeId(18) and def kind AnonConst but a previous def'n exists: DefKey { parent: Some(DefIndex(4)), disambiguated_data: DisambiguatedDefPathData { data: AnonConst, disambiguator: 0 } }
stack backtrace:
   0:     0x7e8412c083a5 - std::backtrace_rs::backtrace::libunwind::trace::h160e615c518ecb01
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/../../backtrace/src/backtrace/libunwind.rs:116:5
   1:     0x7e8412c083a5 - std::backtrace_rs::backtrace::trace_unsynchronized::he30c6d2f98d2efdc
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7e8412c083a5 - std::sys::backtrace::_print_fmt::hb87cad28f2e90a0b
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/sys/backtrace.rs:65:5
   3:     0x7e8412c083a5 - <std::sys::backtrace::BacktraceLock::print::DisplayBacktrace as core::fmt::Display>::fmt::h362429114acc76fb
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/sys/backtrace.rs:40:26
   4:     0x7e8412c57eab - core::fmt::rt::Argument::fmt::h2dd3e5813b018ccc
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/core/src/fmt/rt.rs:173:76
   5:     0x7e8412c57eab - core::fmt::write::haceebbcd70688c9b
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/core/src/fmt/mod.rs:1182:21
   6:     0x7e8412bfce7f - std::io::Write::write_fmt::h7c7b83fdf5105f1d
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/io/mod.rs:1827:15
   7:     0x7e8412c0ab91 - std::sys::backtrace::BacktraceLock::print::hd97590b35fdf3d35
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/sys/backtrace.rs:43:9
   8:     0x7e8412c0ab91 - std::panicking::default_hook::{{closure}}::h587635c60073753d
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/panicking.rs:269:22
   9:     0x7e8412c0a86c - std::panicking::default_hook::h41bedcdd5e6a0e44
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/panicking.rs:296:9
  10:     0x7e840f024e1a - std[f9e60369e5bdbf77]::panicking::update_hook::<alloc[f72c5bad8b9f51f]::boxed::Box<rustc_driver_impl[23129d3f9f762646]::install_ice_hook::{closure#0}>>::{closure#0}
  11:     0x7e8412c0b55f - <alloc::boxed::Box<F,A> as core::ops::function::Fn<Args>>::call::h7651476b4c558d70
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/alloc/src/boxed.rs:2084:9
  12:     0x7e8412c0b55f - std::panicking::rust_panic_with_hook::heee9f152ffb5d0c8
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/panicking.rs:808:13
  13:     0x7e8412c0b187 - std::panicking::begin_panic_handler::{{closure}}::h808b135ae5150977
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/panicking.rs:674:13
  14:     0x7e8412c08869 - std::sys::backtrace::__rust_end_short_backtrace::h3c8a02e9918c1a26
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/sys/backtrace.rs:168:18
  15:     0x7e8412c0ae14 - rust_begin_unwind
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/panicking.rs:665:5
  16:     0x7e8412c54463 - core::panicking::panic_fmt::h124ff2929e2b97c8
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/core/src/panicking.rs:74:14
  17:     0x7e8412045276 - <rustc_ast_lowering[d5c02056e301041c]::LoweringContext>::create_def.cold
  18:     0x7e8410ccdfdd - <rustc_ast_lowering[d5c02056e301041c]::LoweringContext>::lower_anon_const_to_anon_const
  19:     0x7e8410ccdc72 - <rustc_ast_lowering[d5c02056e301041c]::LoweringContext>::lower_anon_const_to_const_arg_direct
  20:     0x7e8410ccd9f3 - <rustc_ast_lowering[d5c02056e301041c]::LoweringContext>::lower_array_length
  21:     0x7e8410cf9b8f - <rustc_ast_lowering[d5c02056e301041c]::LoweringContext>::lower_ty_direct
  22:     0x7e8410cae238 - <rustc_ast_lowering[d5c02056e301041c]::LoweringContext>::lower_block_noalloc
  23:     0x7e8410cd4d10 - <rustc_ast_lowering[d5c02056e301041c]::LoweringContext>::lower_maybe_coroutine_body
  24:     0x7e8410cee69f - <rustc_ast_lowering[d5c02056e301041c]::LoweringContext>::lower_item_kind
  25:     0x7e8410cd215f - <rustc_ast_lowering[d5c02056e301041c]::item::ItemLowerer>::lower_node
  26:     0x7e8410ccfb79 - rustc_ast_lowering[d5c02056e301041c]::lower_to_hir
  27:     0x7e84114c5260 - rustc_query_impl[7076d3a069d488ea]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7076d3a069d488ea]::query_impl::hir_crate::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f5371164b7025427]::query::erase::Erased<[u8; 8usize]>>
  28:     0x7e84114c2302 - rustc_query_system[eb2b374bcd8a9022]::query::plumbing::try_execute_query::<rustc_query_impl[7076d3a069d488ea]::DynamicConfig<rustc_query_system[eb2b374bcd8a9022]::query::caches::SingleCache<rustc_middle[f5371164b7025427]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[7076d3a069d488ea]::plumbing::QueryCtxt, false>
  29:     0x7e84114c1f1e - rustc_query_impl[7076d3a069d488ea]::query_impl::hir_crate::get_query_non_incr::__rust_end_short_backtrace
  30:     0x7e8410e9a064 - rustc_query_impl[7076d3a069d488ea]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7076d3a069d488ea]::query_impl::hir_attrs::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f5371164b7025427]::query::erase::Erased<[u8; 8usize]>>
  31:     0x7e8410e9961f - rustc_query_system[eb2b374bcd8a9022]::query::plumbing::try_execute_query::<rustc_query_impl[7076d3a069d488ea]::DynamicConfig<rustc_query_system[eb2b374bcd8a9022]::query::caches::VecCache<rustc_hir[2699c16870db6088]::hir_id::OwnerId, rustc_middle[f5371164b7025427]::query::erase::Erased<[u8; 8usize]>>, false, false, false>, rustc_query_impl[7076d3a069d488ea]::plumbing::QueryCtxt, false>
  32:     0x7e8410e9938f - rustc_query_impl[7076d3a069d488ea]::query_impl::hir_attrs::get_query_non_incr::__rust_end_short_backtrace
  33:     0x7e8410878006 - <rustc_middle[f5371164b7025427]::hir::map::Map>::attrs
  34:     0x7e84117af401 - rustc_passes[141401c4575c1f66]::entry::entry_fn
  35:     0x7e84117af398 - rustc_query_impl[7076d3a069d488ea]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7076d3a069d488ea]::query_impl::entry_fn::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f5371164b7025427]::query::erase::Erased<[u8; 12usize]>>
  36:     0x7e84117af36f - <rustc_query_impl[7076d3a069d488ea]::query_impl::entry_fn::dynamic_query::{closure#2} as core[c927cdc4da4eb0de]::ops::function::FnOnce<(rustc_middle[f5371164b7025427]::ty::context::TyCtxt, ())>>::call_once
  37:     0x7e84117aec88 - rustc_query_system[eb2b374bcd8a9022]::query::plumbing::try_execute_query::<rustc_query_impl[7076d3a069d488ea]::DynamicConfig<rustc_query_system[eb2b374bcd8a9022]::query::caches::SingleCache<rustc_middle[f5371164b7025427]::query::erase::Erased<[u8; 12usize]>>, false, false, false>, rustc_query_impl[7076d3a069d488ea]::plumbing::QueryCtxt, false>
  38:     0x7e84117aea29 - rustc_query_impl[7076d3a069d488ea]::query_impl::entry_fn::get_query_non_incr::__rust_end_short_backtrace
  39:     0x7e8410b0ffb2 - rustc_interface[635a4d36e766d1e0]::passes::analysis
  40:     0x7e8410b0fdc7 - rustc_query_impl[7076d3a069d488ea]::plumbing::__rust_begin_short_backtrace::<rustc_query_impl[7076d3a069d488ea]::query_impl::analysis::dynamic_query::{closure#2}::{closure#0}, rustc_middle[f5371164b7025427]::query::erase::Erased<[u8; 1usize]>>
  41:     0x7e84114c3725 - rustc_query_system[eb2b374bcd8a9022]::query::plumbing::try_execute_query::<rustc_query_impl[7076d3a069d488ea]::DynamicConfig<rustc_query_system[eb2b374bcd8a9022]::query::caches::SingleCache<rustc_middle[f5371164b7025427]::query::erase::Erased<[u8; 1usize]>>, false, false, false>, rustc_query_impl[7076d3a069d488ea]::plumbing::QueryCtxt, false>
  42:     0x7e84114c348f - rustc_query_impl[7076d3a069d488ea]::query_impl::analysis::get_query_non_incr::__rust_end_short_backtrace
  43:     0x7e84114002c5 - rustc_interface[635a4d36e766d1e0]::interface::run_compiler::<core[c927cdc4da4eb0de]::result::Result<(), rustc_span[8f3a5487dfe29266]::ErrorGuaranteed>, rustc_driver_impl[23129d3f9f762646]::run_compiler::{closure#0}>::{closure#1}
  44:     0x7e8411394189 - std[f9e60369e5bdbf77]::sys::backtrace::__rust_begin_short_backtrace::<rustc_interface[635a4d36e766d1e0]::util::run_in_thread_with_globals<rustc_interface[635a4d36e766d1e0]::util::run_in_thread_pool_with_globals<rustc_interface[635a4d36e766d1e0]::interface::run_compiler<core[c927cdc4da4eb0de]::result::Result<(), rustc_span[8f3a5487dfe29266]::ErrorGuaranteed>, rustc_driver_impl[23129d3f9f762646]::run_compiler::{closure#0}>::{closure#1}, core[c927cdc4da4eb0de]::result::Result<(), rustc_span[8f3a5487dfe29266]::ErrorGuaranteed>>::{closure#0}, core[c927cdc4da4eb0de]::result::Result<(), rustc_span[8f3a5487dfe29266]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c927cdc4da4eb0de]::result::Result<(), rustc_span[8f3a5487dfe29266]::ErrorGuaranteed>>
  45:     0x7e8411393f3a - <<std[f9e60369e5bdbf77]::thread::Builder>::spawn_unchecked_<rustc_interface[635a4d36e766d1e0]::util::run_in_thread_with_globals<rustc_interface[635a4d36e766d1e0]::util::run_in_thread_pool_with_globals<rustc_interface[635a4d36e766d1e0]::interface::run_compiler<core[c927cdc4da4eb0de]::result::Result<(), rustc_span[8f3a5487dfe29266]::ErrorGuaranteed>, rustc_driver_impl[23129d3f9f762646]::run_compiler::{closure#0}>::{closure#1}, core[c927cdc4da4eb0de]::result::Result<(), rustc_span[8f3a5487dfe29266]::ErrorGuaranteed>>::{closure#0}, core[c927cdc4da4eb0de]::result::Result<(), rustc_span[8f3a5487dfe29266]::ErrorGuaranteed>>::{closure#0}::{closure#0}, core[c927cdc4da4eb0de]::result::Result<(), rustc_span[8f3a5487dfe29266]::ErrorGuaranteed>>::{closure#1} as core[c927cdc4da4eb0de]::ops::function::FnOnce<()>>::call_once::{shim:vtable#0}
  46:     0x7e8412c155bb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::haeea406f77e87cac
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/alloc/src/boxed.rs:2070:9
  47:     0x7e8412c155bb - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::h024bb75f9bcc5dfb
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/alloc/src/boxed.rs:2070:9
  48:     0x7e8412c155bb - std::sys::pal::unix::thread::Thread::new::thread_start::h106d07b62ccda4e0
                               at /rustc/2e6fc425411280c658d852813de01c5157255088/library/std/src/sys/pal/unix/thread.rs:108:17
  49:     0x7e84129a9ded - <unknown>
  50:     0x7e8412a2d0dc - <unknown>
  51:                0x0 - <unknown>

error: 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: please make sure that you have updated to the latest nightly

note: rustc 1.81.0-nightly (2e6fc4254 2024-07-20) running on x86_64-unknown-linux-gnu

query stack during panic:
#0 [hir_crate] getting the crate HIR
#1 [hir_attrs] getting HIR owner attributes in ``
end of query stack
error: aborting due to 1 previous error

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

@matthiaskrgr matthiaskrgr added 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. C-bug Category: This is a bug. labels Jul 20, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jul 20, 2024
@matthiaskrgr
Copy link
Member Author

another one for #125915 cc @camelid

@matthiaskrgr matthiaskrgr added the S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. label Jul 23, 2024
@saethlin saethlin removed the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Aug 4, 2024
@matthiaskrgr matthiaskrgr added the regression-from-stable-to-beta Performance or correctness regression from stable to beta. label Aug 6, 2024
@rustbot rustbot added the I-prioritize Issue: Indicates that prioritization has been requested for this issue. label Aug 6, 2024
@matthiaskrgr
Copy link
Member Author

matthiaskrgr commented Aug 6, 2024

This also showed up in the crater reports for #128468
build-fail/gh/rustindia/mpw-rs/d0150eff7a0eb1f43524d29b1a932f966d24f6a8/beta-2024-07-26.txt or regressed/gh/georgebarwood/RustWeb2/57ef036e3c07701d2579b4b1542a5a1131460f4d/beta-2024-07-26.txt for example

@BoxyUwU BoxyUwU self-assigned this Aug 6, 2024
@camelid
Copy link
Member

camelid commented Aug 6, 2024

Minimized a bit more (avoid incorrect str usage):

macro_rules! len {
    () => {
        target
    };
}

fn main() {
    let val: [bool; len!()] = [];
}

@BoxyUwU
Copy link
Member

BoxyUwU commented Aug 6, 2024

I'll look into this since this is on beta now so its somewhat time sensitive ^^'

@camelid
Copy link
Member

camelid commented Aug 6, 2024

It seems like this is Res::Err-related because this code compiles fine:

macro_rules! len {
    () => {{
        let target = 0;
        target
    }};
}

fn main() {
    let val: [bool; len!()] = [];
}

Which is actually really weird because it's allowing use of a non-const in const context?? EDIT: it is a const, because the let is inside a const context

@compiler-errors
Copy link
Member

How is it a non-const?

fn main() {
    let val: [bool; {
        let target = 0;
        target
    }] = [];
}

is valid code.

@BoxyUwU
Copy link
Member

BoxyUwU commented Aug 6, 2024

macro_rules! len {
    () => {
        BAR
    };
}

const BAR: usize = 0;

fn bar() {
    let val: [bool; len!()] = [];
}

This is an example of stable->beta pass->ICE

@BoxyUwU
Copy link
Member

BoxyUwU commented Aug 6, 2024

macro_rules! len {
    ($x:ident) => {
        $x
    };
}

fn bar<const N: usize>() {
    let val: [bool; len!(N)] = [true; N];
}

This has a slightly different ICE message but is the same root cause and is also a stable->beta pass->ICE

@BoxyUwU BoxyUwU assigned camelid and unassigned BoxyUwU Aug 6, 2024
@apiraino
Copy link
Contributor

apiraino commented Aug 7, 2024

WG-prioritization assigning priority (Zulip discussion).

@rustbot label -I-prioritize +P-critical

@rustbot rustbot added P-critical Critical priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Aug 7, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Aug 24, 2024
…h, r=cjgillot

Retroactively feature gate `ConstArgKind::Path`

This puts the lowering introduced by rust-lang#125915 under a feature gate until we fix the regressions introduced by it. Alternative to whole sale reverting the PR since it didn't seem like a very clean revert and I think this is generally a step in the right direction and don't want to get stuck landing and reverting the PR over and over :)

cc rust-lang#129137 `@camelid,` tests taken from there. beta is branching soon so I think it makes sense to not try and rush that fix through since it wont have much time to bake and if it has issues we can't simply revert it on beta.

Fixes rust-lang#128016
@bors bors closed this as completed in c0bedb9 Aug 25, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Aug 25, 2024
Rollup merge of rust-lang#129246 - BoxyUwU:feature_gate_const_arg_path, r=cjgillot

Retroactively feature gate `ConstArgKind::Path`

This puts the lowering introduced by rust-lang#125915 under a feature gate until we fix the regressions introduced by it. Alternative to whole sale reverting the PR since it didn't seem like a very clean revert and I think this is generally a step in the right direction and don't want to get stuck landing and reverting the PR over and over :)

cc rust-lang#129137 ``@camelid,`` tests taken from there. beta is branching soon so I think it makes sense to not try and rush that fix through since it wont have much time to bake and if it has issues we can't simply revert it on beta.

Fixes rust-lang#128016
bors added a commit to rust-lang-ci/rust that referenced this issue Sep 13, 2024
Fix anon const def-creation when macros are involved

Fixes rust-lang#128016.

Ever since rust-lang#125915, some `ast::AnonConst`s turn into `hir::ConstArgKind::Path`s,
which don't have associated `DefId`s. To deal with the fact that we don't have
resolution information in `DefCollector`, we decided to implement a process
where if the anon const *appeared* to be trivial (i.e., `N` or `{ N }`), we
would avoid creating a def for it in `DefCollector`. If later, in AST lowering,
we realized it turned out to be a unit struct literal, or we were lowering it
to something that didn't use `hir::ConstArg`, we'd create its def there.

However, let's say we have a macro `m!()` that expands to a reference to a free
constant `FOO`. If we use `m!()` in the body of an anon const (e.g., `Foo<{ m!() }>`),
then in def collection, it appears to be a nontrivial anon const and we create
a def. But the macro expands to something that looks like a trivial const arg,
but is not, so in AST lowering we "fix" the mistake we assumed def collection
made and create a def for it. This causes a duplicate definition ICE.

The long-term fix for this is to delay the creation of defs for all expression-like
nodes until AST lowering (see rust-lang#128844 for an incomplete attempt at this). This
would avoid issues like this one that are caused by hacky workarounds. However,
doing this uncovers a pre-existing bug with opaque types that is quite involved
to fix (see rust-lang#129023).

In the meantime, this PR fixes the bug by delaying def creation for anon consts
whose bodies are macro invocations until after we expand the macro and know
what is inside it. This is accomplished by adding information to create the
anon const's def to the data in `Resolver.invocation_parents`.

r? `@BoxyUwU`
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) ❄️ P-critical Critical priority regression-from-stable-to-beta Performance or correctness regression from stable to beta. S-bug-has-test Status: This bug is tracked inside the repo by a `known-bug` test. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
7 participants