about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2024-04-16 15:19:11 +0200
committerGitHub <noreply@github.com>2024-04-16 15:19:11 +0200
commitea7eb713d90d667c9f1c99afc7d3dff2ecacee65 (patch)
tree0efcfb9d6b82e2cf886359ed72946b5d4c66affe
parent4232361d89d36ce84b4bc144cbcb3467d8127e7b (diff)
parentc30e15adedba70d99a09146707814d14a93364a1 (diff)
downloadrust-ea7eb713d90d667c9f1c99afc7d3dff2ecacee65.tar.gz
rust-ea7eb713d90d667c9f1c99afc7d3dff2ecacee65.zip
Rollup merge of #123491 - gurry:123154-ice-unsized-struct-eval, r=oli-obk
Fix ICE in `eval_body_using_ecx`

Ensures `TypeckResults` is tainted by failing candidate assembly for types with error

Fixes #123154
-rw-r--r--compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs5
-rw-r--r--compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs8
-rw-r--r--compiler/rustc_ty_utils/src/ty.rs4
-rw-r--r--tests/crashes/123154.rs12
-rw-r--r--tests/ui/closures/issue-78720.rs1
-rw-r--r--tests/ui/closures/issue-78720.stderr16
-rw-r--r--tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs1
-rw-r--r--tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr8
-rw-r--r--tests/ui/consts/const-eval/ice-unsized-struct-const-eval-123154.rs15
-rw-r--r--tests/ui/consts/const-eval/ice-unsized-struct-const-eval-123154.stderr24
-rw-r--r--tests/ui/specialization/issue-68830-spurious-diagnostics.rs1
-rw-r--r--tests/ui/specialization/issue-68830-spurious-diagnostics.stderr14
12 files changed, 72 insertions, 37 deletions
diff --git a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs
index 3dc54b33801..925fe98d293 100644
--- a/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs
+++ b/compiler/rustc_trait_selection/src/traits/error_reporting/type_err_ctxt_ext.rs
@@ -984,7 +984,10 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
             // Already reported in the query.
             SelectionError::NotConstEvaluatable(NotConstEvaluatable::Error(guar)) |
             // Already reported.
-            Overflow(OverflowError::Error(guar)) => return guar,
+            Overflow(OverflowError::Error(guar)) => {
+                self.set_tainted_by_errors(guar);
+                return guar
+            },
 
             Overflow(_) => {
                 bug!("overflow should be handled before the `report_selection_error` path");
diff --git a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
index 974e5ef0e16..3ef7cc01f90 100644
--- a/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
+++ b/compiler/rustc_trait_selection/src/traits/select/candidate_assembly.rs
@@ -87,6 +87,14 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
             } else if lang_items.sized_trait() == Some(def_id) {
                 // Sized is never implementable by end-users, it is
                 // always automatically computed.
+
+                // FIXME: Consider moving this check to the top level as it
+                // may also be useful for predicates other than `Sized`
+                // Error type cannot possibly implement `Sized` (fixes #123154)
+                if let Err(e) = obligation.predicate.skip_binder().self_ty().error_reported() {
+                    return Err(SelectionError::Overflow(e.into()));
+                }
+
                 let sized_conditions = self.sized_conditions(obligation);
                 self.assemble_builtin_bound_candidates(sized_conditions, &mut candidates);
             } else if lang_items.unsize_trait() == Some(def_id) {
diff --git a/compiler/rustc_ty_utils/src/ty.rs b/compiler/rustc_ty_utils/src/ty.rs
index f33234122c9..2139b8c665b 100644
--- a/compiler/rustc_ty_utils/src/ty.rs
+++ b/compiler/rustc_ty_utils/src/ty.rs
@@ -91,8 +91,8 @@ fn adt_sized_constraint<'tcx>(
     let tail_ty = tcx.type_of(tail_def.did).instantiate_identity();
 
     let constraint_ty = sized_constraint_for_ty(tcx, tail_ty)?;
-    if constraint_ty.references_error() {
-        return None;
+    if let Err(guar) = constraint_ty.error_reported() {
+        return Some(ty::EarlyBinder::bind(Ty::new_error(tcx, guar)));
     }
 
     // perf hack: if there is a `constraint_ty: Sized` bound, then we know
diff --git a/tests/crashes/123154.rs b/tests/crashes/123154.rs
deleted file mode 100644
index 510ae8adf35..00000000000
--- a/tests/crashes/123154.rs
+++ /dev/null
@@ -1,12 +0,0 @@
-//@ known-bug: #123154
-struct AA {
-    pub data: [&usize]
-}
-
-impl AA {
-    const fn new() -> Self { }
-}
-
-static AA = AA::new();
-
-fn main() { }
diff --git a/tests/ui/closures/issue-78720.rs b/tests/ui/closures/issue-78720.rs
index 0c4f337ba57..81af030fe55 100644
--- a/tests/ui/closures/issue-78720.rs
+++ b/tests/ui/closures/issue-78720.rs
@@ -1,6 +1,5 @@
 fn server() -> impl {
     //~^ ERROR at least one trait must be specified
-    //~| ERROR type annotations needed
     ().map2(|| "")
 }
 
diff --git a/tests/ui/closures/issue-78720.stderr b/tests/ui/closures/issue-78720.stderr
index 2f57c7616f1..5d65c87b0fd 100644
--- a/tests/ui/closures/issue-78720.stderr
+++ b/tests/ui/closures/issue-78720.stderr
@@ -5,7 +5,7 @@ LL | fn server() -> impl {
    |                ^^^^
 
 error[E0412]: cannot find type `F` in this scope
-  --> $DIR/issue-78720.rs:14:12
+  --> $DIR/issue-78720.rs:13:12
    |
 LL |     _func: F,
    |            ^
@@ -22,14 +22,8 @@ help: you might be missing a type parameter
 LL | struct Map2<Segment2, F> {
    |                     +++
 
-error[E0282]: type annotations needed
-  --> $DIR/issue-78720.rs:1:16
-   |
-LL | fn server() -> impl {
-   |                ^^^^ cannot infer type
-
 error[E0308]: mismatched types
-  --> $DIR/issue-78720.rs:8:39
+  --> $DIR/issue-78720.rs:7:39
    |
 LL |     fn map2<F>(self, f: F) -> Map2<F> {}
    |                                       ^^ expected `Map2<F>`, found `()`
@@ -38,7 +32,7 @@ LL |     fn map2<F>(self, f: F) -> Map2<F> {}
            found unit type `()`
 
 error[E0277]: the size for values of type `Self` cannot be known at compilation time
-  --> $DIR/issue-78720.rs:8:16
+  --> $DIR/issue-78720.rs:7:16
    |
 LL |     fn map2<F>(self, f: F) -> Map2<F> {}
    |                ^^^^ doesn't have a size known at compile-time
@@ -53,7 +47,7 @@ help: function arguments must have a statically known size, borrowed types alway
 LL |     fn map2<F>(&self, f: F) -> Map2<F> {}
    |                +
 
-error: aborting due to 5 previous errors
+error: aborting due to 4 previous errors
 
-Some errors have detailed explanations: E0277, E0282, E0308, E0412.
+Some errors have detailed explanations: E0277, E0308, E0412.
 For more information about an error, try `rustc --explain E0277`.
diff --git a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs
index dc9782295c1..a41a159c1fd 100644
--- a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs
+++ b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.rs
@@ -13,7 +13,6 @@ impl Opcode2 {
 pub fn example2(msg_type: Opcode2) -> impl FnMut(&[u8]) {
     move |i| match msg_type {
         Opcode2::OP2 => unimplemented!(),
-        //~^ ERROR could not evaluate constant pattern
     }
 }
 
diff --git a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr
index 9442eac0cf5..d95a8861230 100644
--- a/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr
+++ b/tests/ui/const_prop/ice-type-mismatch-when-copying-112824.stderr
@@ -17,13 +17,7 @@ help: you might be missing a type parameter
 LL | pub struct Opcode2<S>(&'a S);
    |                   +++
 
-error: could not evaluate constant pattern
-  --> $DIR/ice-type-mismatch-when-copying-112824.rs:15:9
-   |
-LL |         Opcode2::OP2 => unimplemented!(),
-   |         ^^^^^^^^^^^^
-
-error: aborting due to 3 previous errors
+error: aborting due to 2 previous errors
 
 Some errors have detailed explanations: E0261, E0412.
 For more information about an error, try `rustc --explain E0261`.
diff --git a/tests/ui/consts/const-eval/ice-unsized-struct-const-eval-123154.rs b/tests/ui/consts/const-eval/ice-unsized-struct-const-eval-123154.rs
new file mode 100644
index 00000000000..24a2cd19b18
--- /dev/null
+++ b/tests/ui/consts/const-eval/ice-unsized-struct-const-eval-123154.rs
@@ -0,0 +1,15 @@
+// Regression test for #123154
+
+struct AA {
+    pub data: [&usize]
+    //~^ ERROR missing lifetime specifier
+}
+
+impl AA {
+    const fn new() -> Self { }
+    //~^ ERROR mismatched types
+}
+
+static ST: AA = AA::new();
+
+fn main() {}
diff --git a/tests/ui/consts/const-eval/ice-unsized-struct-const-eval-123154.stderr b/tests/ui/consts/const-eval/ice-unsized-struct-const-eval-123154.stderr
new file mode 100644
index 00000000000..9657e5cdda1
--- /dev/null
+++ b/tests/ui/consts/const-eval/ice-unsized-struct-const-eval-123154.stderr
@@ -0,0 +1,24 @@
+error[E0106]: missing lifetime specifier
+  --> $DIR/ice-unsized-struct-const-eval-123154.rs:4:16
+   |
+LL |     pub data: [&usize]
+   |                ^ expected named lifetime parameter
+   |
+help: consider introducing a named lifetime parameter
+   |
+LL ~ struct AA<'a> {
+LL ~     pub data: [&'a usize]
+   |
+
+error[E0308]: mismatched types
+  --> $DIR/ice-unsized-struct-const-eval-123154.rs:9:23
+   |
+LL |     const fn new() -> Self { }
+   |              ---      ^^^^ expected `AA`, found `()`
+   |              |
+   |              implicitly returns `()` as its body has no tail or `return` expression
+
+error: aborting due to 2 previous errors
+
+Some errors have detailed explanations: E0106, E0308.
+For more information about an error, try `rustc --explain E0106`.
diff --git a/tests/ui/specialization/issue-68830-spurious-diagnostics.rs b/tests/ui/specialization/issue-68830-spurious-diagnostics.rs
index d11ec798332..a7487b8aecb 100644
--- a/tests/ui/specialization/issue-68830-spurious-diagnostics.rs
+++ b/tests/ui/specialization/issue-68830-spurious-diagnostics.rs
@@ -17,6 +17,7 @@ impl<T, D> MyTrait<T> for D {
 }
 
 impl<T> MyTrait<T> for BadStruct {
+//~^ ERROR: conflicting implementations of trait `MyTrait<_>` for type `BadStruct`
     fn foo() {}
 }
 
diff --git a/tests/ui/specialization/issue-68830-spurious-diagnostics.stderr b/tests/ui/specialization/issue-68830-spurious-diagnostics.stderr
index 0ecec03a023..13f6ae0805d 100644
--- a/tests/ui/specialization/issue-68830-spurious-diagnostics.stderr
+++ b/tests/ui/specialization/issue-68830-spurious-diagnostics.stderr
@@ -4,6 +4,16 @@ error[E0412]: cannot find type `MissingType` in this scope
 LL |     err: MissingType
    |          ^^^^^^^^^^^ not found in this scope
 
-error: aborting due to 1 previous error
+error[E0119]: conflicting implementations of trait `MyTrait<_>` for type `BadStruct`
+  --> $DIR/issue-68830-spurious-diagnostics.rs:19:1
+   |
+LL | impl<T, D> MyTrait<T> for D {
+   | --------------------------- first implementation here
+...
+LL | impl<T> MyTrait<T> for BadStruct {
+   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `BadStruct`
+
+error: aborting due to 2 previous errors
 
-For more information about this error, try `rustc --explain E0412`.
+Some errors have detailed explanations: E0119, E0412.
+For more information about an error, try `rustc --explain E0119`.