Skip to content

Commit 262a68f

Browse files
committed
Propagate errors in const-to-valtree lowering
1 parent f28ac76 commit 262a68f

6 files changed

Lines changed: 107 additions & 11 deletions

File tree

‎compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,9 +2409,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
24092409
.map(|elem| self.lower_const_arg(elem, *elem_ty))
24102410
.collect::<Vec<_>>();
24112411

2412-
let valtree = ty::ValTree::from_branches(tcx, elems);
2413-
2414-
ty::Const::new_value(tcx, valtree, ty)
2412+
ty::Const::new_value_from_branches(tcx, elems, ty)
24152413
}
24162414

24172415
fn lower_const_arg_tuple_call(
@@ -2509,9 +2507,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
25092507
None
25102508
};
25112509

2512-
let valtree = ty::ValTree::from_branches(tcx, opt_discr_const.into_iter().chain(fields));
2513-
let adt_ty = Ty::new_adt(tcx, adt_def, adt_args);
2514-
ty::Const::new_value(tcx, valtree, adt_ty)
2510+
ty::Const::new_value_from_branches(
2511+
tcx,
2512+
opt_discr_const.into_iter().chain(fields),
2513+
Ty::new_adt(tcx, adt_def, adt_args),
2514+
)
25152515
}
25162516

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

2540-
let valtree = ty::ValTree::from_branches(tcx, exprs);
2541-
ty::Const::new_value(tcx, valtree, ty)
2540+
ty::Const::new_value_from_branches(tcx, exprs, ty)
25422541
}
25432542

25442543
fn lower_const_arg_struct(
@@ -2680,8 +2679,7 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
26802679
None
26812680
};
26822681

2683-
let valtree = ty::ValTree::from_branches(tcx, opt_discr_const.into_iter().chain(fields));
2684-
ty::Const::new_value(tcx, valtree, ty)
2682+
ty::Const::new_value_from_branches(tcx, opt_discr_const.into_iter().chain(fields), ty)
26852683
}
26862684

26872685
pub fn lower_path_for_struct_expr(

‎compiler/rustc_middle/src/ty/consts.rs‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_data_structures::intern::Interned;
44
use rustc_error_messages::MultiSpan;
55
use rustc_macros::StableHash;
66
use rustc_type_ir::walk::TypeWalker;
7-
use rustc_type_ir::{self as ir, TypeFlags, WithCachedTypeInfo};
7+
use rustc_type_ir::{self as ir, TypeFlags, TypeVisitableExt, WithCachedTypeInfo};
88

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

119+
pub fn new_value_from_branches(
120+
tcx: TyCtxt<'tcx>,
121+
branches: impl IntoIterator<Item = ty::Const<'tcx>>,
122+
ty: Ty<'tcx>,
123+
) -> Const<'tcx> {
124+
let branches: Vec<_> = branches.into_iter().collect();
125+
if let Some(e) = branches.iter().find_map(|c| c.error_reported().err()) {
126+
return Const::new_error(tcx, e);
127+
}
128+
if let Err(e) = ty.error_reported() {
129+
return Const::new_error(tcx, e);
130+
}
131+
132+
Const::new_value(tcx, ty::ValTree::from_branches(tcx, branches), ty)
133+
}
134+
119135
#[inline]
120136
pub fn new_expr(tcx: TyCtxt<'tcx>, expr: ty::Expr<'tcx>) -> Const<'tcx> {
121137
Const::new(tcx, ty::ConstKind::Expr(expr))
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Regression test for https://github.com/rust-lang/rust/issues/154632
2+
3+
#![feature(generic_const_exprs)]
4+
#![feature(min_generic_const_args)]
5+
#![feature(generic_const_items)]
6+
7+
type const ADD1<const N : usize> : usize = const { N + 1 };
8+
//~^ ERROR: unconstrained generic constant
9+
type const AliasFnUnused: ADD1 = ADD1::<{ Some::<usize> {} }>;
10+
//~^ ERROR: expected type, found constant `ADD1` [E0573]
11+
//~| ERROR: struct expression with missing field initialiser for `0`
12+
13+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0573]: expected type, found constant `ADD1`
2+
--> $DIR/type_const-adt-expr-missing-field.rs:9:27
3+
|
4+
LL | type const AliasFnUnused: ADD1 = ADD1::<{ Some::<usize> {} }>;
5+
| ^^^^ not a type
6+
7+
error: unconstrained generic constant
8+
--> $DIR/type_const-adt-expr-missing-field.rs:7:1
9+
|
10+
LL | type const ADD1<const N : usize> : usize = const { N + 1 };
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
|
13+
help: try adding a `where` bound
14+
|
15+
LL | type const ADD1<const N : usize> : usize where [(); const { N + 1 }]: = const { N + 1 };
16+
| ++++++++++++++++++++++++++++
17+
18+
error: struct expression with missing field initialiser for `0`
19+
--> $DIR/type_const-adt-expr-missing-field.rs:9:43
20+
|
21+
LL | type const AliasFnUnused: ADD1 = ADD1::<{ Some::<usize> {} }>;
22+
| ^^^^^^^^^^^^^^^^
23+
24+
error: aborting due to 3 previous errors
25+
26+
For more information about this error, try `rustc --explain E0573`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#![feature(generic_const_exprs)]
2+
#![feature(min_generic_const_args)]
3+
#![feature(adt_const_params)]
4+
#![feature(generic_const_items)]
5+
6+
use std::marker::ConstParamTy;
7+
8+
#[derive(Eq, PartialEq, ConstParamTy)]
9+
struct Wrap(usize);
10+
11+
type const ADD1<const N: usize>: usize = const { N + 1 };
12+
//~^ ERROR: unconstrained generic constant
13+
type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some::<usize> {}) }>;
14+
//~^ ERROR: expected type, found constant `ADD1` [E0573]
15+
//~| ERROR: struct expression with missing field initialiser for `0`
16+
17+
fn main() {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0573]: expected type, found constant `ADD1`
2+
--> $DIR/type_const-tuple-call-expr-missing-field.rs:13:27
3+
|
4+
LL | type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some::<usize> {}) }>;
5+
| ^^^^ not a type
6+
7+
error: unconstrained generic constant
8+
--> $DIR/type_const-tuple-call-expr-missing-field.rs:11:1
9+
|
10+
LL | type const ADD1<const N: usize>: usize = const { N + 1 };
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
12+
|
13+
help: try adding a `where` bound
14+
|
15+
LL | type const ADD1<const N: usize>: usize where [(); const { N + 1 }]: = const { N + 1 };
16+
| ++++++++++++++++++++++++++++
17+
18+
error: struct expression with missing field initialiser for `0`
19+
--> $DIR/type_const-tuple-call-expr-missing-field.rs:13:48
20+
|
21+
LL | type const AliasFnUnused: ADD1 = ADD1::<{ Wrap(Some::<usize> {}) }>;
22+
| ^^^^^^^^^^^^^^^^
23+
24+
error: aborting due to 3 previous errors
25+
26+
For more information about this error, try `rustc --explain E0573`.

0 commit comments

Comments
 (0)