diff --git a/include/swift/AST/DiagnosticsParse.def b/include/swift/AST/DiagnosticsParse.def index 9a99e0dd2bb..53e71b410ae 100644 --- a/include/swift/AST/DiagnosticsParse.def +++ b/include/swift/AST/DiagnosticsParse.def @@ -2156,6 +2156,10 @@ ERROR(init_accessor_is_not_on_property,none, "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, "missing label in @storageRestrictions attribute", ()) diff --git a/lib/Parse/ParseDecl.cpp b/lib/Parse/ParseDecl.cpp index 060ea5441e5..706611022ac 100644 --- a/lib/Parse/ParseDecl.cpp +++ b/lib/Parse/ParseDecl.cpp @@ -8432,8 +8432,12 @@ void Parser::ParsedAccessors::classify(Parser &P, AbstractStorageDecl *storage, } if (Init) { - if (!storage->getDeclContext()->getSelfNominalTypeDecl() || - isa(storage)) { + if (storage->getDeclContext()->getContextKind() == + DeclContextKind::ExtensionDecl) { + P.diagnose(Init->getLoc(), + diag::init_accessor_is_not_in_the_primary_declaration); + } else if (!storage->getDeclContext()->getSelfNominalTypeDecl() || + isa(storage)) { P.diagnose(Init->getLoc(), diag::init_accessor_is_not_on_property); } }