Skip to content

Commit afa66b1

Browse files
committed
[AST] Rework diagnostic about init accessors inside of extensions
1 parent 115d26a commit afa66b1

File tree

3 files changed

+5
-6
lines changed

3 files changed

+5
-6
lines changed

include/swift/AST/DiagnosticsParse.def

+1-1
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,7 @@ ERROR(init_accessor_is_not_on_property,none,
21592159
())
21602160

21612161
ERROR(init_accessor_is_not_in_the_primary_declaration,none,
2162-
"init accessors could only be declared in the primary declaration",
2162+
"init accessors cannot be declared in an extension",
21632163
())
21642164

21652165
ERROR(missing_storage_restrictions_attr_label,none,

lib/Parse/ParseDecl.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -8497,12 +8497,11 @@ void Parser::ParsedAccessors::classify(Parser &P, AbstractStorageDecl *storage,
84978497
}
84988498

84998499
if (Init) {
8500-
if (storage->getDeclContext()->getContextKind() ==
8501-
DeclContextKind::ExtensionDecl) {
8500+
auto *DC = storage->getDeclContext();
8501+
if (isa<ExtensionDecl>(DC)) {
85028502
P.diagnose(Init->getLoc(),
85038503
diag::init_accessor_is_not_in_the_primary_declaration);
8504-
} else if (!storage->getDeclContext()->getSelfNominalTypeDecl() ||
8505-
isa<SubscriptDecl>(storage)) {
8504+
} else if (!DC->isTypeContext() || isa<SubscriptDecl>(storage)) {
85068505
P.diagnose(Init->getLoc(), diag::init_accessor_is_not_on_property);
85078506
}
85088507
}

test/decl/var/init_accessors.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ extension Test2ForExtension {
699699
var extendedX: Int {
700700
@storageRestrictions(initializes: _x)
701701
init {
702-
// expected-error@-1 {{init accessors could only be declared in the primary declaration}}
702+
// expected-error@-1 {{init accessors cannot be declared in an extension}}
703703
self._x = newValue
704704
}
705705
get { _x }

0 commit comments

Comments
 (0)