Skip to content

delegation: fix ICE with double definition inside block in impl header#159140

Open
mansiverma897993 wants to merge 1 commit into
rust-lang:mainfrom
mansiverma897993:fix-ice-delegation-159127
Open

delegation: fix ICE with double definition inside block in impl header#159140
mansiverma897993 wants to merge 1 commit into
rust-lang:mainfrom
mansiverma897993:fix-ice-delegation-159127

Conversation

@mansiverma897993

@mansiverma897993 mansiverma897993 commented Jul 11, 2026

Copy link
Copy Markdown

When a function is defined inside a block (such as an anonymous constant or a macro like \core::direct_const_arg!) in an impl header, its parent is the enclosing \Impl\ item. \ n_kind\ determines if a function/delegation item is associated or free based on its parent's DefKind. When the parent is \DefKind::Impl, \ n_kind\ returned \FnKind::AssocInherentImpl\ or \FnKind::AssocTraitImpl, even though the function is actually block-local (free). This incorrect classification propagates, hitting \unreachable!\ panics in delegation helper functions.

Fix this by returning \FnKind::Free\ immediately if the function has \DefKind::Fn\ (since associated functions always have \DefKind::AssocFn).

Fixes #159127

When a function is defined inside a block (such as an anonymous constant or a macro like \core::direct_const_arg!\) in an impl header, its parent is the enclosing \Impl\ item.
\n_kind\ determines if a function/delegation item is associated or free based on its parent's DefKind. When the parent is \DefKind::Impl\, \n_kind\ returned \FnKind::AssocInherentImpl\ or \FnKind::AssocTraitImpl\, even though the function is actually block-local (free). This incorrect classification propagates, hitting \unreachable!\ panics in delegation helper functions.

Fix this by returning \FnKind::Free\ immediately if the function has \DefKind::Fn\ (since associated functions always have \DefKind::AssocFn\).

Fixes rust-lang#159127
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 11, 2026
@rustbot

rustbot commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the pull request, and welcome! The Rust Project is excited to review your changes, and you should hear from @jackh726 (or someone else) some time within the next two weeks.

Please see the contribution instructions for more information. Namely, in order to ensure the minimum review times lag, PR authors and assigned reviewers should ensure that the review label (S-waiting-on-review and S-waiting-on-author) stays updated, invoking these commands when appropriate:

  • @rustbot author: the review is finished, PR author should check the comments and take action accordingly
  • @rustbot review: the author is ready for a review, this PR will be queued again in the reviewer's queue
Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 18 candidates

@rustbot

rustbot commented Jul 11, 2026

Copy link
Copy Markdown
Collaborator

⚠️ Warning ⚠️

  • There are issue links (such as #123) in the commit messages of the following commits.
    Please move them to the PR description, to avoid spamming the issues with references to the commit, and so this bot can automatically canonicalize them to avoid issues with subtree.

@rust-log-analyzer

Copy link
Copy Markdown
Collaborator

The job aarch64-gnu-llvm-21-1 failed! Check out the build log: (web) (plain enhanced) (plain)

Click to see the possible cause of the failure (guessed by this bot)
test [ui] tests/ui/zero-sized/zero-sized-btreemap-insert.rs ... ok

failures:

---- [ui] tests/ui/delegation/hir-crate-items-before-lowering-ices.rs#ice_159127 stdout ----
Saved the actual stderr to `/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/delegation/hir-crate-items-before-lowering-ices.ice_159127/hir-crate-items-before-lowering-ices.ice_159127.stderr`
diff of stderr:

2   --> $DIR/hir-crate-items-before-lowering-ices.rs:80:13
3    |
4 LL |             reuse foo::<>as bar;
-    |             ------------------- previous definition of the value `bar` here
+    |             -------------------- previous definition of the value `bar` here
6 LL |             reuse bar;
7    |             ^^^^^^^^^^ `bar` redefined here
8    |

11 error: expected type, found `direct_const_arg!()` constant
12   --> $DIR/hir-crate-items-before-lowering-ices.rs:75:9
13    |
---
22 LL | |
23 LL | |
24 LL | |             fn foo() {}

25 LL | |             reuse foo::<>as bar;
- ...
27 LL | |             reuse bar;
28 LL | |         })
-    | |_________^
+    | |__________^
---
The actual stderr differed from the expected stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args delegation/hir-crate-items-before-lowering-ices.rs`

error in revision `ice_159127`: 1 errors occurred comparing output.
status: exit status: 1
command: env -u RUSTC_LOG_COLOR RUSTC_ICE="0" RUST_BACKTRACE="short" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/delegation/hir-crate-items-before-lowering-ices.rs" "-Zthreads=1" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Z" "ignore-directory-in-diagnostics-source-blocks=/cargo" "-Z" "ignore-directory-in-diagnostics-source-blocks=/checkout/vendor" "--sysroot" "/checkout/obj/build/aarch64-unknown-linux-gnu/stage2" "--target=aarch64-unknown-linux-gnu" "--cfg" "ice_159127" "--check-cfg" "cfg(test,FALSE,ice_155125,ice_155127,ice_155128,ice_155164,ice_155202,ice_159127)" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Zwrite-long-types-to-disk=no" "-Cstrip=debuginfo" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/aarch64-unknown-linux-gnu/test/ui/delegation/hir-crate-items-before-lowering-ices.ice_159127" "-A" "unused" "-W" "unused_attributes" "-A" "internal_features" "-A" "incomplete_features" "-A" "unused_parens" "-A" "unused_braces" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/aarch64-unknown-linux-gnu/native/rust-test-helpers"
stdout: none
--- stderr -------------------------------
error[E0428]: the name `bar` is defined multiple times
##[error]  --> /checkout/tests/ui/delegation/hir-crate-items-before-lowering-ices.rs:80:13
   |
LL |             reuse foo::<>as bar;
   |             -------------------- previous definition of the value `bar` here
LL |             reuse bar; //[ice_159127]~ ERROR: the name `bar` is defined multiple times
   |             ^^^^^^^^^^ `bar` redefined here
   |
   = note: `bar` must be defined only once in the value namespace of this block

error: expected type, found `direct_const_arg!()` constant
##[error]  --> /checkout/tests/ui/delegation/hir-crate-items-before-lowering-ices.rs:75:9
   |
LL | /         core::direct_const_arg!({
LL | |         //[ice_159127]~^ ERROR: expected type, found `direct_const_arg!()` constant
LL | |         //[ice_159127]~| ERROR: complex const arguments must be placed inside of a `const` block
LL | |             fn foo() {}
LL | |             reuse foo::<>as bar;
LL | |             reuse bar; //[ice_159127]~ ERROR: the name `bar` is defined multiple times
LL | |         })
   | |__________^

error: aborting due to 2 previous errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[ICE]: unreachable with double definition inside direct_const_arg!

4 participants