Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2409,9 +2409,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.map(|elem| self.lower_const_arg(elem, *elem_ty))
.collect::<Vec<_>>();

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

}

fn lower_const_arg_tuple_call(
Expand Down Expand Up @@ -2509,9 +2507,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
None
};

let valtree = ty::ValTree::from_branches(tcx, opt_discr_const.into_iter().chain(fields));
let adt_ty = Ty::new_adt(tcx, adt_def, adt_args);
ty::Const::new_value(tcx, valtree, adt_ty)
ty::Const::new_value_from_branches(
tcx,
opt_discr_const.into_iter().chain(fields),
Ty::new_adt(tcx, adt_def, adt_args),
)
}

fn lower_const_arg_tup(
Expand All @@ -2537,8 +2537,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
.map(|(expr, ty)| self.lower_const_arg(expr, ty))
.collect::<Vec<_>>();

let valtree = ty::ValTree::from_branches(tcx, exprs);
ty::Const::new_value(tcx, valtree, ty)
ty::Const::new_value_from_branches(tcx, exprs, ty)
}

fn lower_const_arg_struct(
Expand Down Expand Up @@ -2680,8 +2679,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
None
};

let valtree = ty::ValTree::from_branches(tcx, opt_discr_const.into_iter().chain(fields));
ty::Const::new_value(tcx, valtree, ty)
ty::Const::new_value_from_branches(tcx, opt_discr_const.into_iter().chain(fields), ty)
}

pub fn lower_path_for_struct_expr(
Expand Down
18 changes: 17 additions & 1 deletion compiler/rustc_middle/src/ty/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use rustc_data_structures::intern::Interned;
use rustc_error_messages::MultiSpan;
use rustc_macros::StableHash;
use rustc_type_ir::walk::TypeWalker;
use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
use rustc_type_ir::{self as ir, TypeFlags, TypeVisitableExt, WithCachedTypeInfo};

use crate::mir::interpret::Scalar;
use crate::ty::{self, Ty, TyCtxt};
Expand Down Expand Up @@ -116,6 +116,22 @@ impl<'tcx> Const<'tcx> {
Const::new(tcx, ty::ConstKind::Value(ty::Value { ty, valtree }))
}

pub fn new_value_from_branches(
tcx: TyCtxt<'tcx>,
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.

if let Some(e) = branches.iter().find_map(|c| c.error_reported().err()) {
return Const::new_error(tcx, e);
}
if let Err(e) = ty.error_reported() {
return Const::new_error(tcx, e);
}

Const::new_value(tcx, ty::ValTree::from_branches(tcx, branches), ty)
}

#[inline]
pub fn new_expr(tcx: TyCtxt<'tcx>, expr: ty::Expr<'tcx>) -> Const<'tcx> {
Const::new(tcx, ty::ConstKind::Expr(expr))
Expand Down
13 changes: 13 additions & 0 deletions tests/ui/const-generics/mgca/type_const-adt-expr-missing-field.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Regression test for https://github.com/rust-lang/rust/issues/154632

#![feature(generic_const_exprs)]
#![feature(min_generic_const_args)]
#![feature(generic_const_items)]

type const ADD1<const N : usize> : usize = const { N + 1 };
//~^ ERROR: unconstrained generic constant
type const AliasFnUnused: ADD1 = ADD1::<{ Some::<usize> {} }>;
//~^ ERROR: expected type, found constant `ADD1` [E0573]
//~| ERROR: struct expression with missing field initialiser for `0`

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0573]: expected type, found constant `ADD1`
--> $DIR/type_const-adt-expr-missing-field.rs:9:27
|
LL | type const AliasFnUnused: ADD1 = ADD1::<{ Some::<usize> {} }>;
| ^^^^ not a type

error: unconstrained generic constant
--> $DIR/type_const-adt-expr-missing-field.rs:7:1
|
LL | type const ADD1<const N : usize> : usize = const { N + 1 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try adding a `where` bound
|
LL | type const ADD1<const N : usize> : usize where [(); const { N + 1 }]: = const { N + 1 };
| ++++++++++++++++++++++++++++

error: struct expression with missing field initialiser for `0`
--> $DIR/type_const-adt-expr-missing-field.rs:9:43
|
LL | type const AliasFnUnused: ADD1 = ADD1::<{ Some::<usize> {} }>;
| ^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

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

@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

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#![feature(generic_const_exprs)]
#![feature(min_generic_const_args)]
#![feature(adt_const_params)]
#![feature(generic_const_items)]

use std::marker::ConstParamTy;

#[derive(Eq, PartialEq, ConstParamTy)]
struct Wrap(usize);

type const ADD1<const N: usize>: usize = const { N + 1 };
//~^ ERROR: unconstrained generic constant
type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some::<usize> {}) }>;
//~^ ERROR: expected type, found constant `ADD1` [E0573]
//~| ERROR: struct expression with missing field initialiser for `0`

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
error[E0573]: expected type, found constant `ADD1`
--> $DIR/type_const-tuple-call-expr-missing-field.rs:13:27
|
LL | type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some::<usize> {}) }>;
| ^^^^ not a type

error: unconstrained generic constant
--> $DIR/type_const-tuple-call-expr-missing-field.rs:11:1
|
LL | type const ADD1<const N: usize>: usize = const { N + 1 };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
help: try adding a `where` bound
|
LL | type const ADD1<const N: usize>: usize where [(); const { N + 1 }]: = const { N + 1 };
| ++++++++++++++++++++++++++++

error: struct expression with missing field initialiser for `0`
--> $DIR/type_const-tuple-call-expr-missing-field.rs:13:48
|
LL | type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some::<usize> {}) }>;
| ^^^^^^^^^^^^^^^^

error: aborting due to 3 previous errors

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