Skip to content

Commit 1526393

Browse files
Validate that we're only matching on unit struct for path pattern
1 parent 1447f9d commit 1526393

File tree

3 files changed

+26
-8
lines changed

3 files changed

+26
-8
lines changed

compiler/rustc_hir_typeck/src/pat.rs

+19-2
Original file line numberDiff line numberDiff line change
@@ -919,8 +919,25 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
919919
let e = report_unexpected_variant_res(tcx, res, qpath, pat.span, E0533, expected);
920920
return Ty::new_error(tcx, e);
921921
}
922-
Res::SelfCtor(..)
923-
| Res::Def(
922+
Res::SelfCtor(def_id) => {
923+
if let ty::Adt(adt_def, _) = *tcx.type_of(def_id).skip_binder().kind()
924+
&& adt_def.is_struct()
925+
&& let Some((CtorKind::Const, _)) = adt_def.non_enum_variant().ctor
926+
{
927+
// Ok, we allow unit struct ctors in patterns only.
928+
} else {
929+
let e = report_unexpected_variant_res(
930+
tcx,
931+
res,
932+
qpath,
933+
pat.span,
934+
E0533,
935+
"unit struct",
936+
);
937+
return Ty::new_error(tcx, e);
938+
}
939+
}
940+
Res::Def(
924941
DefKind::Ctor(_, CtorKind::Const)
925942
| DefKind::Const
926943
| DefKind::AssocConst

tests/ui/parser/issues/issue-70549-resolve-after-recovered-self-ctor.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ impl S {
44
fn foo(&mur Self) {}
55
//~^ ERROR expected identifier, found keyword `Self`
66
//~| ERROR expected one of `:`, `@`
7-
//~| ERROR the `Self` constructor can only be used with
7+
//~| ERROR expected unit struct, found self constructor `Self`
88
fn bar(&'static mur Self) {}
99
//~^ ERROR unexpected lifetime
1010
//~| ERROR expected identifier, found keyword `Self`
1111
//~| ERROR expected one of `:`, `@`
12-
//~| ERROR the `Self` constructor can only be used with
12+
//~| ERROR expected unit struct, found self constructor `Self`
1313

1414
fn baz(&mur Self @ _) {}
1515
//~^ ERROR expected one of `:`, `@`

tests/ui/parser/issues/issue-70549-resolve-after-recovered-self-ctor.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,18 @@ error: expected one of `:`, `@`, or `|`, found keyword `Self`
4040
LL | fn baz(&mur Self @ _) {}
4141
| ^^^^ expected one of `:`, `@`, or `|`
4242

43-
error: the `Self` constructor can only be used with tuple or unit structs
43+
error[E0533]: expected unit struct, found self constructor `Self`
4444
--> $DIR/issue-70549-resolve-after-recovered-self-ctor.rs:4:17
4545
|
4646
LL | fn foo(&mur Self) {}
47-
| ^^^^ help: use curly brackets: `Self { /* fields */ }`
47+
| ^^^^ not a unit struct
4848

49-
error: the `Self` constructor can only be used with tuple or unit structs
49+
error[E0533]: expected unit struct, found self constructor `Self`
5050
--> $DIR/issue-70549-resolve-after-recovered-self-ctor.rs:8:25
5151
|
5252
LL | fn bar(&'static mur Self) {}
53-
| ^^^^ help: use curly brackets: `Self { /* fields */ }`
53+
| ^^^^ not a unit struct
5454

5555
error: aborting due to 8 previous errors
5656

57+
For more information about this error, try `rustc --explain E0533`.

0 commit comments

Comments
 (0)