about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorJana Dönszelmann <jonathan@donsz.nl>2025-06-25 22:14:56 +0200
committerGitHub <noreply@github.com>2025-06-25 22:14:56 +0200
commitda42289cd75487d0f75cafd7bbcc39c18aec8f83 (patch)
treea27c0f68e0760ba574e9bc2842cf7a7d3c46284f /compiler
parent69b11c64ebf9798984f8ceb250cb1b755488aaf1 (diff)
parent1c9f7955d4cf126a75e21b0d643880dc429a466f (diff)
Rollup merge of #142976 - compiler-errors:coerce-ice, r=fee1-dead
Check CoerceUnsized impl validity before coercing

Self-explanatory from the title.

Fixes rust-lang/rust#126982
Fixes rust-lang/rust#131048
Fixes rust-lang/rust#134217
Fixes rust-lang/rust#126269
Fixes rust-lang/rust#138265
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir_typeck/src/coercion.rs15
1 files changed, 14 insertions, 1 deletions
diff --git a/compiler/rustc_hir_typeck/src/coercion.rs b/compiler/rustc_hir_typeck/src/coercion.rs
index 24092c01125..0ce0bc313c7 100644
--- a/compiler/rustc_hir_typeck/src/coercion.rs
+++ b/compiler/rustc_hir_typeck/src/coercion.rs
@@ -46,7 +46,7 @@ use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
 use rustc_infer::infer::relate::RelateResult;
 use rustc_infer::infer::{Coercion, DefineOpaqueTypes, InferOk, InferResult};
 use rustc_infer::traits::{
-    IfExpressionCause, MatchExpressionArmCause, Obligation, PredicateObligation,
+    IfExpressionCause, ImplSource, MatchExpressionArmCause, Obligation, PredicateObligation,
     PredicateObligations, SelectionError,
 };
 use rustc_middle::span_bug;
@@ -704,6 +704,19 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
                     // be silent, as it causes a type mismatch later.
                 }
 
+                Ok(Some(ImplSource::UserDefined(impl_source))) => {
+                    queue.extend(impl_source.nested);
+                    // Certain incoherent `CoerceUnsized` implementations may cause ICEs,
+                    // so check the impl's validity. Taint the body so that we don't try
+                    // to evaluate these invalid coercions in CTFE. We only need to do this
+                    // for local impls, since upstream impls should be valid.
+                    if impl_source.impl_def_id.is_local()
+                        && let Err(guar) =
+                            self.tcx.ensure_ok().coerce_unsized_info(impl_source.impl_def_id)
+                    {
+                        self.fcx.set_tainted_by_errors(guar);
+                    }
+                }
                 Ok(Some(impl_source)) => queue.extend(impl_source.nested_obligations()),
             }
         }