Skip to content

Commit b82fb85

Browse files
committed
[ParseDecl] Disallow init accessor decl outside of primary declaration
1 parent da5dd94 commit b82fb85

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

include/swift/AST/DiagnosticsParse.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2156,6 +2156,10 @@ ERROR(init_accessor_is_not_on_property,none,
21562156
"init accessors could only be associated with properties",
21572157
())
21582158

2159+
ERROR(init_accessor_is_not_in_the_primary_declaration,none,
2160+
"init accessors could only be declared in the primary declaration",
2161+
())
2162+
21592163
ERROR(missing_storage_restrictions_attr_label,none,
21602164
"missing label in @storageRestrictions attribute", ())
21612165

lib/Parse/ParseDecl.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8432,8 +8432,12 @@ void Parser::ParsedAccessors::classify(Parser &P, AbstractStorageDecl *storage,
84328432
}
84338433

84348434
if (Init) {
8435-
if (!storage->getDeclContext()->getSelfNominalTypeDecl() ||
8436-
isa<SubscriptDecl>(storage)) {
8435+
if (storage->getDeclContext()->getContextKind() ==
8436+
DeclContextKind::ExtensionDecl) {
8437+
P.diagnose(Init->getLoc(),
8438+
diag::init_accessor_is_not_in_the_primary_declaration);
8439+
} else if (!storage->getDeclContext()->getSelfNominalTypeDecl() ||
8440+
isa<SubscriptDecl>(storage)) {
84378441
P.diagnose(Init->getLoc(), diag::init_accessor_is_not_on_property);
84388442
}
84398443
}

0 commit comments

Comments
 (0)