about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorMatthias Krüger <matthias.krueger@famsik.de>2024-03-25 17:05:33 +0100
committerGitHub <noreply@github.com>2024-03-25 17:05:33 +0100
commite9ec44251c32dfe2a8e0b82a72b402df615bbd8b (patch)
treec405692cef9241f9742b3889bcc00d2c6f48c78c /compiler
parentccc5310922807e168d3be5c22b77cf3d76ce4f09 (diff)
parent08235b1603f77f39468e7b2e552945501c0cc048 (diff)
downloadrust-e9ec44251c32dfe2a8e0b82a72b402df615bbd8b.tar.gz
rust-e9ec44251c32dfe2a8e0b82a72b402df615bbd8b.zip
Rollup merge of #122910 - compiler-errors:unit-struct-in-path-pat-only, r=petrochenkov
Validate that we're only matching on unit struct for path pattern

Resolution doesn't validate that we only really take `CtorKind::Unit` in path patterns, since all it sees is `Res::SelfCtor(def_id)`. Check this instead during pattern typeck.

r? petrochenkov

Fixes #122809
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_hir_typeck/src/pat.rs21
1 files changed, 19 insertions, 2 deletions
diff --git a/compiler/rustc_hir_typeck/src/pat.rs b/compiler/rustc_hir_typeck/src/pat.rs
index 7ecd380ebeb..4dc60f7c6da 100644
--- a/compiler/rustc_hir_typeck/src/pat.rs
+++ b/compiler/rustc_hir_typeck/src/pat.rs
@@ -919,8 +919,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 let e = report_unexpected_variant_res(tcx, res, qpath, pat.span, E0533, expected);
                 return Ty::new_error(tcx, e);
             }
-            Res::SelfCtor(..)
-            | Res::Def(
+            Res::SelfCtor(def_id) => {
+                if let ty::Adt(adt_def, _) = *tcx.type_of(def_id).skip_binder().kind()
+                    && adt_def.is_struct()
+                    && let Some((CtorKind::Const, _)) = adt_def.non_enum_variant().ctor
+                {
+                    // Ok, we allow unit struct ctors in patterns only.
+                } else {
+                    let e = report_unexpected_variant_res(
+                        tcx,
+                        res,
+                        qpath,
+                        pat.span,
+                        E0533,
+                        "unit struct",
+                    );
+                    return Ty::new_error(tcx, e);
+                }
+            }
+            Res::Def(
                 DefKind::Ctor(_, CtorKind::Const)
                 | DefKind::Const
                 | DefKind::AssocConst