1
Fork 0

Always take the Ok path in lit_to_const and produce error constants instead

This commit is contained in:
Oli Scherer 2025-01-07 12:33:32 +00:00
parent 787af97bab
commit 07fcead073
9 changed files with 103 additions and 83 deletions

View file

@ -2265,18 +2265,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
if let Some(lit_input) = lit_input { if let Some(lit_input) = lit_input {
// If an error occurred, ignore that it's a literal and leave reporting the error up to // If an error occurred, ignore that it's a literal and leave reporting the error up to
// mir. // mir.
match tcx.at(expr.span).lit_to_const(lit_input) {
Ok(c) => return Some(c), // Allow the `ty` to be an alias type, though we cannot handle it here, we just go through
Err(_) if lit_input.ty.has_aliases() => { // the more expensive anon const code path.
// allow the `ty` to be an alias type, though we cannot handle it here if !lit_input.ty.has_aliases() {
return None; return Some(tcx.at(expr.span).lit_to_const(lit_input).unwrap());
}
Err(e) => {
tcx.dcx().span_delayed_bug(
expr.span,
format!("try_lower_anon_const_lit: couldn't lit_to_const {e:?}"),
);
}
} }
} }

View file

@ -69,7 +69,7 @@ pub(crate) fn lit_to_const<'tcx>(
} }
(ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()), (ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()),
(ast::LitKind::Err(guar), _) => return Ok(ty::Const::new_error(tcx, *guar)), (ast::LitKind::Err(guar), _) => return Ok(ty::Const::new_error(tcx, *guar)),
_ => return Err(LitToConstError::TypeError), _ => return Ok(ty::Const::new_misc_error(tcx)),
}; };
Ok(ty::Const::new_value(tcx, valtree, ty)) Ok(ty::Const::new_value(tcx, valtree, ty))

View file

@ -1,6 +0,0 @@
//@ known-bug: #114317
#![feature(generic_const_exprs)]
struct A<const B: str = 1, C>;
fn main() {}

View file

@ -1,10 +0,0 @@
//@ known-bug: rust-lang/rust#126182
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]
struct Cond<const B: bool>;
struct Thing<T = Cond<0>>(T);
impl Thing {}

View file

@ -0,0 +1,22 @@
//! ICE regression test for #114317 and #126182
//! Type mismatches of literals cause errors int typeck,
//! but those errors cannot be propagated to the various
//! `lit_to_const` call sites. Now `lit_to_const` just delays
//! a bug and produces an error constant on its own.
#![feature(adt_const_params)]
#![feature(generic_const_exprs)]
#![allow(incomplete_features)]
struct A<const B: () = 1, C>(C);
//~^ ERROR: generic parameters with a default must be trailing
//~| ERROR: mismatched types
struct Cond<const B: bool>;
struct Thing<T = Cond<0>>(T);
//~^ ERROR: mismatched types
impl Thing {}
fn main() {}

View file

@ -0,0 +1,21 @@
error: generic parameters with a default must be trailing
--> $DIR/lit_type_mismatch.rs:11:16
|
LL | struct A<const B: () = 1, C>(C);
| ^
error[E0308]: mismatched types
--> $DIR/lit_type_mismatch.rs:11:24
|
LL | struct A<const B: () = 1, C>(C);
| ^ expected `()`, found integer
error[E0308]: mismatched types
--> $DIR/lit_type_mismatch.rs:17:23
|
LL | struct Thing<T = Cond<0>>(T);
| ^ expected `bool`, found integer
error: aborting due to 3 previous errors
For more information about this error, try `rustc --explain E0308`.

View file

@ -1,27 +1,3 @@
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:29:21
|
LL | get_flag::<false, 0xFF>();
| ^^^^ expected `char`, found `u8`
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:31:14
|
LL | get_flag::<7, 'c'>();
| ^ expected `bool`, found integer
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:14
|
LL | get_flag::<42, 0x5ad>();
| ^^ expected `bool`, found integer
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:18
|
LL | get_flag::<42, 0x5ad>();
| ^^^^^ expected `char`, found `u8`
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/invalid-patterns.rs:38:32 --> $DIR/invalid-patterns.rs:38:32
| |
@ -56,6 +32,30 @@ error[E0080]: evaluation of constant value failed
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>(); LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory | ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:29:21
|
LL | get_flag::<false, 0xFF>();
| ^^^^ expected `char`, found `u8`
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:31:14
|
LL | get_flag::<7, 'c'>();
| ^ expected `bool`, found integer
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:14
|
LL | get_flag::<42, 0x5ad>();
| ^^ expected `bool`, found integer
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:18
|
LL | get_flag::<42, 0x5ad>();
| ^^^^^ expected `char`, found `u8`
error: aborting due to 8 previous errors error: aborting due to 8 previous errors
Some errors have detailed explanations: E0080, E0308. Some errors have detailed explanations: E0080, E0308.

View file

@ -1,27 +1,3 @@
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:29:21
|
LL | get_flag::<false, 0xFF>();
| ^^^^ expected `char`, found `u8`
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:31:14
|
LL | get_flag::<7, 'c'>();
| ^ expected `bool`, found integer
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:14
|
LL | get_flag::<42, 0x5ad>();
| ^^ expected `bool`, found integer
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:18
|
LL | get_flag::<42, 0x5ad>();
| ^^^^^ expected `char`, found `u8`
error[E0080]: evaluation of constant value failed error[E0080]: evaluation of constant value failed
--> $DIR/invalid-patterns.rs:38:32 --> $DIR/invalid-patterns.rs:38:32
| |
@ -56,6 +32,30 @@ error[E0080]: evaluation of constant value failed
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>(); LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory | ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:29:21
|
LL | get_flag::<false, 0xFF>();
| ^^^^ expected `char`, found `u8`
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:31:14
|
LL | get_flag::<7, 'c'>();
| ^ expected `bool`, found integer
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:14
|
LL | get_flag::<42, 0x5ad>();
| ^^ expected `bool`, found integer
error[E0308]: mismatched types
--> $DIR/invalid-patterns.rs:33:18
|
LL | get_flag::<42, 0x5ad>();
| ^^^^^ expected `char`, found `u8`
error: aborting due to 8 previous errors error: aborting due to 8 previous errors
Some errors have detailed explanations: E0080, E0308. Some errors have detailed explanations: E0080, E0308.

View file

@ -15,6 +15,12 @@ error[E0308]: mismatched types
LL | let b = [0; ()]; LL | let b = [0; ()];
| ^^ expected `usize`, found `()` | ^^ expected `usize`, found `()`
error[E0308]: mismatched types
--> $DIR/repeat_count.rs:31:17
|
LL | let g = [0; G { g: () }];
| ^^^^^^^^^^^ expected `usize`, found `G`
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/repeat_count.rs:10:17 --> $DIR/repeat_count.rs:10:17
| |
@ -33,12 +39,6 @@ error[E0308]: mismatched types
LL | let e = [0; "foo"]; LL | let e = [0; "foo"];
| ^^^^^ expected `usize`, found `&str` | ^^^^^ expected `usize`, found `&str`
error[E0308]: mismatched types
--> $DIR/repeat_count.rs:31:17
|
LL | let g = [0; G { g: () }];
| ^^^^^^^^^^^ expected `usize`, found `G`
error[E0308]: mismatched types error[E0308]: mismatched types
--> $DIR/repeat_count.rs:19:17 --> $DIR/repeat_count.rs:19:17
| |