Skip to content

fix(hir): Use expression store of parent body if available - #23202

Merged
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
Suryansh-Dey:expression
Aug 23, 2026
Merged

fix(hir): Use expression store of parent body if available#23202
ChayimFriedman2 merged 1 commit into
rust-lang:masterfrom
Suryansh-Dey:expression

Conversation

@Suryansh-Dey

@Suryansh-Dey Suryansh-Dey commented Aug 20, 2026

Copy link
Copy Markdown
Contributor

Closes #23192

Reason of the issue:

Unlike named constants (like const C: ...), anonymous constants (like 2 + 2 inside an array type) do not get their own independent Body. Instead, they are stored directly inside the ExpressionStore of their parent owner.
But in crates/hir/src/source_analyzer.rs the code that creates the TyLoweringContext hardcodes the owner to be the generic definition of the parent instead of checking that it is currently inside the Body of parent.

Fix

Simply check if the type is inside an expression scope then assigns the Body as the owner instead of the Signature.

How to test

Re-run the repro provided in issue and it passes!
image

Run cargo test -p hir-ty and all 1038 passed.

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 20, 2026

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is correct (worth noting though that after #23196 the bug won't trigger anymore, but this change will still be correct) but can you please write a test?

View changes since this review

@Suryansh-Dey

Suryansh-Dey commented Aug 21, 2026

Copy link
Copy Markdown
Contributor Author

Can't believe how I missed your message. I am writing one. Sorry for such a delay

@Suryansh-Dey

Copy link
Copy Markdown
Contributor Author

Test added. Also fixed similar issue in source_analyzer.rs.

@rustbot

This comment has been minimized.

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you verify the test fail without the changes? It does not fail for me.

View changes since this review

@Suryansh-Dey
Suryansh-Dey force-pushed the expression branch 2 times, most recently from 0335ad3 to 95f1bdb Compare August 23, 2026 07:37
@rustbot

rustbot commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@Suryansh-Dey

Copy link
Copy Markdown
Contributor Author

Did you verify the test fail without the changes? It does not fail for me.

View changes since this review

Sorry, I accidentally messed git stash. Fixed now
image

@ChayimFriedman2 ChayimFriedman2 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

#[test]
fn resolve_array_type_with_anon_const_panic() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This probably shouldn't be here but I don't have a better place 😢

@ChayimFriedman2
ChayimFriedman2 added this pull request to the merge queue Aug 23, 2026
Merged via the queue into rust-lang:master with commit 5c156cd Aug 23, 2026
18 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 23, 2026
@Suryansh-Dey
Suryansh-Dey deleted the expression branch August 23, 2026 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

panic: should have ExpressionStore::expr_onlyType::as_array on an array type whose length is an anon const in a body-local annotation

3 participants