Skip to content

Commit

Permalink
[AST] Rework diagnostic about init accessors inside of extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
xedin committed Jun 19, 2024
1 parent 115d26a commit afa66b1
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 6 deletions.
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsParse.def
Original file line number Diff line number Diff line change
Expand Up @@ -2159,7 +2159,7 @@ ERROR(init_accessor_is_not_on_property,none,
())

ERROR(init_accessor_is_not_in_the_primary_declaration,none,
"init accessors could only be declared in the primary declaration",
"init accessors cannot be declared in an extension",
())

ERROR(missing_storage_restrictions_attr_label,none,
Expand Down
7 changes: 3 additions & 4 deletions lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8497,12 +8497,11 @@ void Parser::ParsedAccessors::classify(Parser &P, AbstractStorageDecl *storage,
}

if (Init) {
if (storage->getDeclContext()->getContextKind() ==
DeclContextKind::ExtensionDecl) {
auto *DC = storage->getDeclContext();
if (isa<ExtensionDecl>(DC)) {
P.diagnose(Init->getLoc(),
diag::init_accessor_is_not_in_the_primary_declaration);
} else if (!storage->getDeclContext()->getSelfNominalTypeDecl() ||
isa<SubscriptDecl>(storage)) {
} else if (!DC->isTypeContext() || isa<SubscriptDecl>(storage)) {
P.diagnose(Init->getLoc(), diag::init_accessor_is_not_on_property);
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/decl/var/init_accessors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ extension Test2ForExtension {
var extendedX: Int {
@storageRestrictions(initializes: _x)
init {
// expected-error@-1 {{init accessors could only be declared in the primary declaration}}
// expected-error@-1 {{init accessors cannot be declared in an extension}}
self._x = newValue
}
get { _x }
Expand Down

0 comments on commit afa66b1

Please sign in to comment.