Skip to content

Commit

Permalink
Always take the Ok path in lit_to_const and produce error constan…
Browse files Browse the repository at this point in the history
…ts instead
  • Loading branch information
oli-obk committed Jan 9, 2025
1 parent 787af97 commit 07fcead
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 83 deletions.
17 changes: 5 additions & 12 deletions compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2265,18 +2265,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
if let Some(lit_input) = lit_input {
// If an error occurred, ignore that it's a literal and leave reporting the error up to
// mir.
match tcx.at(expr.span).lit_to_const(lit_input) {
Ok(c) => return Some(c),
Err(_) if lit_input.ty.has_aliases() => {
// allow the `ty` to be an alias type, though we cannot handle it here
return None;
}
Err(e) => {
tcx.dcx().span_delayed_bug(
expr.span,
format!("try_lower_anon_const_lit: couldn't lit_to_const {e:?}"),
);
}

// Allow the `ty` to be an alias type, though we cannot handle it here, we just go through
// the more expensive anon const code path.
if !lit_input.ty.has_aliases() {
return Some(tcx.at(expr.span).lit_to_const(lit_input).unwrap());
}
}

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_mir_build/src/thir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::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))
Expand Down
6 changes: 0 additions & 6 deletions tests/crashes/114317.rs

This file was deleted.

10 changes: 0 additions & 10 deletions tests/crashes/126182.rs

This file was deleted.

22 changes: 22 additions & 0 deletions tests/ui/const-generics/generic_const_exprs/lit_type_mismatch.rs
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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`.
Original file line number Diff line number Diff line change
@@ -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
--> $DIR/invalid-patterns.rs:38:32
|
Expand Down Expand Up @@ -56,6 +32,30 @@ error[E0080]: evaluation of constant value failed
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^ 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

Some errors have detailed explanations: E0080, E0308.
Expand Down
Original file line number Diff line number Diff line change
@@ -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
--> $DIR/invalid-patterns.rs:38:32
|
Expand Down Expand Up @@ -56,6 +32,30 @@ error[E0080]: evaluation of constant value failed
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
| ^^^^^^^^^^^^^^^^^^ 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

Some errors have detailed explanations: E0080, E0308.
Expand Down
12 changes: 6 additions & 6 deletions tests/ui/repeat-expr/repeat_count.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ error[E0308]: mismatched types
LL | let b = [0; ()];
| ^^ 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
--> $DIR/repeat_count.rs:10:17
|
Expand All @@ -33,12 +39,6 @@ error[E0308]: mismatched types
LL | let e = [0; "foo"];
| ^^^^^ 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
--> $DIR/repeat_count.rs:19:17
|
Expand Down

0 comments on commit 07fcead

Please sign in to comment.