about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOli Scherer <github333195615777966@oli-obk.de>2025-01-07 12:33:32 +0000
committerOli Scherer <github333195615777966@oli-obk.de>2025-01-09 08:48:00 +0000
commit07fcead0732d9460978dd3b428cd5edcfa299f9a (patch)
treeb87dde24347f43c54345023c0db5de75af59b3fb
parent787af97babcb911f2ce70d3d28959cdccc7901bd (diff)
downloadrust-07fcead0732d9460978dd3b428cd5edcfa299f9a.tar.gz
rust-07fcead0732d9460978dd3b428cd5edcfa299f9a.zip
Always take the `Ok` path in `lit_to_const` and produce error constants instead
-rw-r--r--compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs17
-rw-r--r--compiler/rustc_mir_build/src/thir/constant.rs2
-rw-r--r--tests/crashes/114317.rs6
-rw-r--r--tests/crashes/126182.rs10
-rw-r--r--tests/ui/const-generics/generic_const_exprs/lit_type_mismatch.rs22
-rw-r--r--tests/ui/const-generics/generic_const_exprs/lit_type_mismatch.stderr21
-rw-r--r--tests/ui/const-generics/min_const_generics/invalid-patterns.32bit.stderr48
-rw-r--r--tests/ui/const-generics/min_const_generics/invalid-patterns.64bit.stderr48
-rw-r--r--tests/ui/repeat-expr/repeat_count.stderr12
9 files changed, 103 insertions, 83 deletions
diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
index 5a65d5bafee..6ed5f92e995 100644
--- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
+++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs
@@ -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());
             }
         }
 
diff --git a/compiler/rustc_mir_build/src/thir/constant.rs b/compiler/rustc_mir_build/src/thir/constant.rs
index 0e4efbdcbb0..82830562c03 100644
--- a/compiler/rustc_mir_build/src/thir/constant.rs
+++ b/compiler/rustc_mir_build/src/thir/constant.rs
@@ -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))
diff --git a/tests/crashes/114317.rs b/tests/crashes/114317.rs
deleted file mode 100644
index 09fd2beeba8..00000000000
--- a/tests/crashes/114317.rs
+++ /dev/null
@@ -1,6 +0,0 @@
-//@ known-bug: #114317
-#![feature(generic_const_exprs)]
-
-struct A<const B: str = 1, C>;
-
-fn main() {}
diff --git a/tests/crashes/126182.rs b/tests/crashes/126182.rs
deleted file mode 100644
index 2219a6cb5fa..00000000000
--- a/tests/crashes/126182.rs
+++ /dev/null
@@ -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 {}
diff --git a/tests/ui/const-generics/generic_const_exprs/lit_type_mismatch.rs b/tests/ui/const-generics/generic_const_exprs/lit_type_mismatch.rs
new file mode 100644
index 00000000000..1ed0965e1bd
--- /dev/null
+++ b/tests/ui/const-generics/generic_const_exprs/lit_type_mismatch.rs
@@ -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() {}
diff --git a/tests/ui/const-generics/generic_const_exprs/lit_type_mismatch.stderr b/tests/ui/const-generics/generic_const_exprs/lit_type_mismatch.stderr
new file mode 100644
index 00000000000..e4613e498b2
--- /dev/null
+++ b/tests/ui/const-generics/generic_const_exprs/lit_type_mismatch.stderr
@@ -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`.
diff --git a/tests/ui/const-generics/min_const_generics/invalid-patterns.32bit.stderr b/tests/ui/const-generics/min_const_generics/invalid-patterns.32bit.stderr
index be92429e3ab..11a824ba73b 100644
--- a/tests/ui/const-generics/min_const_generics/invalid-patterns.32bit.stderr
+++ b/tests/ui/const-generics/min_const_generics/invalid-patterns.32bit.stderr
@@ -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
    |
@@ -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.
diff --git a/tests/ui/const-generics/min_const_generics/invalid-patterns.64bit.stderr b/tests/ui/const-generics/min_const_generics/invalid-patterns.64bit.stderr
index be92429e3ab..11a824ba73b 100644
--- a/tests/ui/const-generics/min_const_generics/invalid-patterns.64bit.stderr
+++ b/tests/ui/const-generics/min_const_generics/invalid-patterns.64bit.stderr
@@ -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
    |
@@ -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.
diff --git a/tests/ui/repeat-expr/repeat_count.stderr b/tests/ui/repeat-expr/repeat_count.stderr
index 350ac287507..c4aebfb0e20 100644
--- a/tests/ui/repeat-expr/repeat_count.stderr
+++ b/tests/ui/repeat-expr/repeat_count.stderr
@@ -16,6 +16,12 @@ 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
    |
 LL |     let c = [0; true];
@@ -34,12 +40,6 @@ 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
    |
 LL |     let f = [0; -4_isize];