[ParseDecl] Disallow init accessor decl outside of primary declaration

This commit is contained in:
li3zhen1
2024-05-02 20:48:29 -07:00
parent da5dd94bb7
commit b82fb851f9
2 changed files with 10 additions and 2 deletions

View File

@@ -2156,6 +2156,10 @@ ERROR(init_accessor_is_not_on_property,none,
"init accessors could only be associated with properties", "init accessors could only be associated with properties",
()) ())
ERROR(init_accessor_is_not_in_the_primary_declaration,none,
"init accessors could only be declared in the primary declaration",
())
ERROR(missing_storage_restrictions_attr_label,none, ERROR(missing_storage_restrictions_attr_label,none,
"missing label in @storageRestrictions attribute", ()) "missing label in @storageRestrictions attribute", ())

View File

@@ -8432,8 +8432,12 @@ void Parser::ParsedAccessors::classify(Parser &P, AbstractStorageDecl *storage,
} }
if (Init) { if (Init) {
if (!storage->getDeclContext()->getSelfNominalTypeDecl() || if (storage->getDeclContext()->getContextKind() ==
isa<SubscriptDecl>(storage)) { DeclContextKind::ExtensionDecl) {
P.diagnose(Init->getLoc(),
diag::init_accessor_is_not_in_the_primary_declaration);
} else if (!storage->getDeclContext()->getSelfNominalTypeDecl() ||
isa<SubscriptDecl>(storage)) {
P.diagnose(Init->getLoc(), diag::init_accessor_is_not_on_property); P.diagnose(Init->getLoc(), diag::init_accessor_is_not_on_property);
} }
} }