diff options
| author | Michael Goulet <michael@errs.io> | 2024-03-22 16:56:13 -0400 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2024-03-22 20:53:42 -0400 |
| commit | 08235b1603f77f39468e7b2e552945501c0cc048 (patch) | |
| tree | f263d0e91eda04103c30d994b50dafc157364ec5 /compiler | |
| parent | 1447f9d38ca388ca178a544534b3cff72945fa1e (diff) | |
| download | rust-08235b1603f77f39468e7b2e552945501c0cc048.tar.gz rust-08235b1603f77f39468e7b2e552945501c0cc048.zip | |
Validate that we're only matching on unit struct for path pattern
Diffstat (limited to 'compiler')
| -rw-r--r-- | compiler/rustc_hir_typeck/src/pat.rs | 21 |
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 dad43cb8abe..8cb3b4b8e47 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 |
