Skip to content

mgca: Propagate errors in const-to-valtree lowering#158401

Open
lapla-cogito wants to merge 1 commit into
rust-lang:mainfrom
lapla-cogito:issue_154632
Open

mgca: Propagate errors in const-to-valtree lowering#158401
lapla-cogito wants to merge 1 commit into
rust-lang:mainfrom
lapla-cogito:issue_154632

Conversation

@lapla-cogito

@lapla-cogito lapla-cogito commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

Fixes #154632

When lowering const expressions, ValTree::from_branches() was called without checking whether any of the branches or the type contained prior errors. This produced a ConstKind::Value wrapping an error constant, which later caused an ICE in valtree_to_const_value(). Therefore this PR introduces Const::new_value_from_branches, which checks for error_reported() on both the branches and the type before constructing the ValTree, and returns Const::new_error if any error is found.

@rustbot

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

rustbot commented Jun 25, 2026

Copy link
Copy Markdown
Collaborator

r? @camelid

rustbot has assigned @camelid.
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 19 candidates

@camelid camelid added the A-const-generics Area: const generics (parameters and arguments) label Jun 25, 2026

@lapla-cogito lapla-cogito Jun 25, 2026

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.

The case reported in the original issue goes through lower_const_arg_struct(). I found that a similar ICE can also occur through lower_const_arg_tuple_call(), and this serves as a regression test for that case.

View changes since the review

let valtree = ty::ValTree::from_branches(tcx, elems);

ty::Const::new_value(tcx, valtree, ty)
ty::Const::new_value_from_branches(tcx, elems, ty)

@lapla-cogito lapla-cogito Jun 25, 2026

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.

I was not able to find a case that reproduces the similar ICE through lower_const_arg_{array, tuple}() (unlike struct/tuple_call cases I mentioned above, I think there are few situations in which these would cause an ICE under the current evaluation order 🤔 ). However, I don't think we can rule out the possibility that these paths may receive branches containing error constants in the future. Therefore, I think it's reasonable to apply the same fix to these paths as well.

View changes since the review

@camelid camelid left a comment

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.

I'm not sure if this is the right place to be fixing the problem. Given that ValTrees can have arbitrary ty::Const, it feels like they should be able to have error constants in them.

@BoxyUwU do you have thoughts about the best way to handle this?

View changes since this review

branches: impl IntoIterator<Item = ty::Const<'tcx>>,
ty: Ty<'tcx>,
) -> Const<'tcx> {
let branches: Vec<_> = branches.into_iter().collect();

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.

Hmm I don't love that this requires collecting the iterator, looking through all the branches, and then passing it on.

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.

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.

No worries. The TLDR is that we should probably instead make other parts of the compiler handle errors inside valtrees gracefully, rather than avoiding creating valtrees that contain errors in an ad hoc way like this PR does.

@camelid camelid 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 Jul 13, 2026
@rust-bors

rust-bors Bot commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

☔ The latest upstream changes (presumably #159246) made this pull request unmergeable. Please resolve the merge conflicts by rebasing.

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

Labels

A-const-generics Area: const generics (parameters and arguments) 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.

[ICE]: expected ConstKind::Value, got {const error}

3 participants