Skip to content

check if len of array const arg matches the expected len of the type when lowering to valtree - #158587

Open
sjwang05 wants to merge 1 commit into
rust-lang:mainfrom
sjwang05:issue-155168
Open

check if len of array const arg matches the expected len of the type when lowering to valtree#158587
sjwang05 wants to merge 1 commit into
rust-lang:mainfrom
sjwang05:issue-155168

Conversation

@sjwang05

@sjwang05 sjwang05 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

When creating valtrees for arrays passed as const args in lower_const_arg_array, we assume the array has the correct length without actually checking the length of the arg, leading to UB at runtime (#155168) or an ICE during CTFE (#151079). This PR adds a check comparing the expected length of the type with the actual number of elements, similar to lower_const_arg_tup.

fixes #155168

@rustbot

rustbot commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

HIR ty lowering was modified

cc @fmease

@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 Jun 29, 2026
@rustbot

rustbot commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

r? @JohnTitor

rustbot has assigned @JohnTitor.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

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

@sjwang05 sjwang05 changed the title check if len of array const arg matches the expected len check if len of array const arg matches the expected len of the type when lowering to valtree Jun 29, 2026
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

.map(|elem| self.lower_const_arg(elem, *elem_ty))
.collect::<Vec<_>>();

if let Some(expected_len) = len.try_to_target_usize(tcx)

@JohnTitor JohnTitor Jul 3, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

IMO it should catch more cases if len is normalized, e.g. type const LEN (I haven't checked deeply so it may be caught by somewhere already)

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I changed it to call try_normalize_erasing_regions with an empty ParamEnv on the expected len and added a test for it, so now something like fn baz<const A: [u8; <S as Trait>::LEN]>() {} produces the correct error. Something like fn baz<T: Trait, const A: [u8; <T as Trait>::LEN]>() {} still ICEs if too long/produces UB if too short, however, since we can't resolve T to an actual type: trying to do try_evaluate_const with a non-empty ParamEnv led to cycle errors.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you open another issue and add an ICE test for that case (if not exists yet)? At least we should track it.

@JohnTitor JohnTitor added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

constructing invalid value of type [u8; 2]: at [0], encountered uninitialized memory, but expected an integer

4 participants