Skip to content

Commit 56dd382

Browse files
committed
Always take the Ok path in lit_to_const and produce error constants instead
1 parent 7666bbe commit 56dd382

File tree

5 files changed

+60
-67
lines changed

5 files changed

+60
-67
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

+5-12
Original file line numberDiff line numberDiff line change
@@ -2265,18 +2265,11 @@ impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
22652265
if let Some(lit_input) = lit_input {
22662266
// If an error occurred, ignore that it's a literal and leave reporting the error up to
22672267
// mir.
2268-
match tcx.at(expr.span).lit_to_const(lit_input) {
2269-
Ok(c) => return Some(c),
2270-
Err(_) if lit_input.ty.has_aliases() => {
2271-
// allow the `ty` to be an alias type, though we cannot handle it here
2272-
return None;
2273-
}
2274-
Err(e) => {
2275-
tcx.dcx().span_delayed_bug(
2276-
expr.span,
2277-
format!("try_lower_anon_const_lit: couldn't lit_to_const {e:?}"),
2278-
);
2279-
}
2268+
2269+
// Allow the `ty` to be an alias type, though we cannot handle it here, we just go through
2270+
// the more expensive anon const code path.
2271+
if !lit_input.ty.has_aliases() {
2272+
return Some(tcx.at(expr.span).lit_to_const(lit_input).unwrap());
22802273
}
22812274
}
22822275

compiler/rustc_mir_build/src/thir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub(crate) fn lit_to_const<'tcx>(
6969
}
7070
(ast::LitKind::Char(c), ty::Char) => ty::ValTree::from_scalar_int((*c).into()),
7171
(ast::LitKind::Err(guar), _) => return Ok(ty::Const::new_error(tcx, *guar)),
72-
_ => return Err(LitToConstError::TypeError),
72+
_ => return Ok(ty::Const::new_misc_error(tcx)),
7373
};
7474

7575
Ok(ty::Const::new_value(tcx, valtree, ty))

tests/ui/const-generics/min_const_generics/invalid-patterns.32bit.stderr

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/invalid-patterns.rs:29:21
3-
|
4-
LL | get_flag::<false, 0xFF>();
5-
| ^^^^ expected `char`, found `u8`
6-
7-
error[E0308]: mismatched types
8-
--> $DIR/invalid-patterns.rs:31:14
9-
|
10-
LL | get_flag::<7, 'c'>();
11-
| ^ expected `bool`, found integer
12-
13-
error[E0308]: mismatched types
14-
--> $DIR/invalid-patterns.rs:33:14
15-
|
16-
LL | get_flag::<42, 0x5ad>();
17-
| ^^ expected `bool`, found integer
18-
19-
error[E0308]: mismatched types
20-
--> $DIR/invalid-patterns.rs:33:18
21-
|
22-
LL | get_flag::<42, 0x5ad>();
23-
| ^^^^^ expected `char`, found `u8`
24-
251
error[E0080]: evaluation of constant value failed
262
--> $DIR/invalid-patterns.rs:38:32
273
|
@@ -56,6 +32,30 @@ error[E0080]: evaluation of constant value failed
5632
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
5733
| ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
5834

35+
error[E0308]: mismatched types
36+
--> $DIR/invalid-patterns.rs:29:21
37+
|
38+
LL | get_flag::<false, 0xFF>();
39+
| ^^^^ expected `char`, found `u8`
40+
41+
error[E0308]: mismatched types
42+
--> $DIR/invalid-patterns.rs:31:14
43+
|
44+
LL | get_flag::<7, 'c'>();
45+
| ^ expected `bool`, found integer
46+
47+
error[E0308]: mismatched types
48+
--> $DIR/invalid-patterns.rs:33:14
49+
|
50+
LL | get_flag::<42, 0x5ad>();
51+
| ^^ expected `bool`, found integer
52+
53+
error[E0308]: mismatched types
54+
--> $DIR/invalid-patterns.rs:33:18
55+
|
56+
LL | get_flag::<42, 0x5ad>();
57+
| ^^^^^ expected `char`, found `u8`
58+
5959
error: aborting due to 8 previous errors
6060

6161
Some errors have detailed explanations: E0080, E0308.

tests/ui/const-generics/min_const_generics/invalid-patterns.64bit.stderr

+24-24
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
error[E0308]: mismatched types
2-
--> $DIR/invalid-patterns.rs:29:21
3-
|
4-
LL | get_flag::<false, 0xFF>();
5-
| ^^^^ expected `char`, found `u8`
6-
7-
error[E0308]: mismatched types
8-
--> $DIR/invalid-patterns.rs:31:14
9-
|
10-
LL | get_flag::<7, 'c'>();
11-
| ^ expected `bool`, found integer
12-
13-
error[E0308]: mismatched types
14-
--> $DIR/invalid-patterns.rs:33:14
15-
|
16-
LL | get_flag::<42, 0x5ad>();
17-
| ^^ expected `bool`, found integer
18-
19-
error[E0308]: mismatched types
20-
--> $DIR/invalid-patterns.rs:33:18
21-
|
22-
LL | get_flag::<42, 0x5ad>();
23-
| ^^^^^ expected `char`, found `u8`
24-
251
error[E0080]: evaluation of constant value failed
262
--> $DIR/invalid-patterns.rs:38:32
273
|
@@ -56,6 +32,30 @@ error[E0080]: evaluation of constant value failed
5632
LL | get_flag::<{ unsafe { bool_raw.boolean } }, { unsafe { char_raw.character } }>();
5733
| ^^^^^^^^^^^^^^^^^^ using uninitialized data, but this operation requires initialized memory
5834

35+
error[E0308]: mismatched types
36+
--> $DIR/invalid-patterns.rs:29:21
37+
|
38+
LL | get_flag::<false, 0xFF>();
39+
| ^^^^ expected `char`, found `u8`
40+
41+
error[E0308]: mismatched types
42+
--> $DIR/invalid-patterns.rs:31:14
43+
|
44+
LL | get_flag::<7, 'c'>();
45+
| ^ expected `bool`, found integer
46+
47+
error[E0308]: mismatched types
48+
--> $DIR/invalid-patterns.rs:33:14
49+
|
50+
LL | get_flag::<42, 0x5ad>();
51+
| ^^ expected `bool`, found integer
52+
53+
error[E0308]: mismatched types
54+
--> $DIR/invalid-patterns.rs:33:18
55+
|
56+
LL | get_flag::<42, 0x5ad>();
57+
| ^^^^^ expected `char`, found `u8`
58+
5959
error: aborting due to 8 previous errors
6060

6161
Some errors have detailed explanations: E0080, E0308.

tests/ui/repeat-expr/repeat_count.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ error[E0308]: mismatched types
1515
LL | let b = [0; ()];
1616
| ^^ expected `usize`, found `()`
1717

18+
error[E0308]: mismatched types
19+
--> $DIR/repeat_count.rs:31:17
20+
|
21+
LL | let g = [0; G { g: () }];
22+
| ^^^^^^^^^^^ expected `usize`, found `G`
23+
1824
error[E0308]: mismatched types
1925
--> $DIR/repeat_count.rs:10:17
2026
|
@@ -33,12 +39,6 @@ error[E0308]: mismatched types
3339
LL | let e = [0; "foo"];
3440
| ^^^^^ expected `usize`, found `&str`
3541

36-
error[E0308]: mismatched types
37-
--> $DIR/repeat_count.rs:31:17
38-
|
39-
LL | let g = [0; G { g: () }];
40-
| ^^^^^^^^^^^ expected `usize`, found `G`
41-
4242
error[E0308]: mismatched types
4343
--> $DIR/repeat_count.rs:19:17
4444
|

0 commit comments

Comments
 (0)