about summary refs log tree commit diff
path: root/compiler/rustc_hir_analysis/src/coherence/builtin.rs
diff options
context:
space:
mode:
authorMichael Goulet <michael@errs.io>2025-01-08 00:29:22 +0000
committerMichael Goulet <michael@errs.io>2025-01-08 00:45:47 +0000
commit11bc805369b7e2aa641e885c137b2c86ce701e2c (patch)
tree89a7727d32909c991dcc90b366b287bc8affcf1a /compiler/rustc_hir_analysis/src/coherence/builtin.rs
parent3c3186148e4a66a507e606f191c35ed49d873b08 (diff)
downloadrust-11bc805369b7e2aa641e885c137b2c86ce701e2c.tar.gz
rust-11bc805369b7e2aa641e885c137b2c86ce701e2c.zip
Don't allow DispatchFromDyn impls that transmute ZST to non-ZST
Diffstat (limited to 'compiler/rustc_hir_analysis/src/coherence/builtin.rs')
-rw-r--r--compiler/rustc_hir_analysis/src/coherence/builtin.rs26
1 files changed, 13 insertions, 13 deletions
diff --git a/compiler/rustc_hir_analysis/src/coherence/builtin.rs b/compiler/rustc_hir_analysis/src/coherence/builtin.rs
index 760c09a1e72..a661e588b95 100644
--- a/compiler/rustc_hir_analysis/src/coherence/builtin.rs
+++ b/compiler/rustc_hir_analysis/src/coherence/builtin.rs
@@ -267,20 +267,20 @@ fn visit_implementation_of_dispatch_from_dyn(checker: &Checker<'_>) -> Result<()
                     let ty_a = field.ty(tcx, args_a);
                     let ty_b = field.ty(tcx, args_b);
 
-                    // Allow 1-ZSTs that don't mention type params.
-                    //
-                    // Allowing type params here would allow us to possibly transmute
-                    // between ZSTs, which may be used to create library unsoundness.
-                    if let Ok(layout) =
-                        tcx.layout_of(infcx.typing_env(param_env).as_query_input(ty_a))
-                        && layout.is_1zst()
-                        && !ty_a.has_non_region_param()
-                    {
-                        // ignore 1-ZST fields
-                        return false;
-                    }
-
                     if ty_a == ty_b {
+                        // Allow 1-ZSTs that don't mention type params.
+                        //
+                        // Allowing type params here would allow us to possibly transmute
+                        // between ZSTs, which may be used to create library unsoundness.
+                        if let Ok(layout) =
+                            tcx.layout_of(infcx.typing_env(param_env).as_query_input(ty_a))
+                            && layout.is_1zst()
+                            && !ty_a.has_non_region_param()
+                        {
+                            // ignore 1-ZST fields
+                            return false;
+                        }
+
                         res = Err(tcx.dcx().emit_err(errors::DispatchFromDynZST {
                             span,
                             name: field.name,