Skip to content

[Diagnostics] Improve diagnostics on invalid open extensions #38989

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1499,7 +1499,7 @@ ERROR(access_control_extension_more,none,
"be declared %select{%error|fileprivate|internal|public|%error}2",
(AccessLevel, DescriptiveDeclKind, AccessLevel))
ERROR(access_control_extension_open,none,
"extensions cannot use 'open' as their default access; use 'public'",
"extensions cannot be declared 'open'; declare individual members as 'open' instead",
())
ERROR(access_control_open_bad_decl,none,
"only classes and overridable class members can be declared 'open';"
Expand Down
21 changes: 19 additions & 2 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -737,8 +737,25 @@ void AttributeChecker::visitAccessControlAttr(AccessControlAttr *attr) {

if (auto extension = dyn_cast<ExtensionDecl>(D)) {
if (attr->getAccess() == AccessLevel::Open) {
diagnose(attr->getLocation(), diag::access_control_extension_open)
.fixItReplace(attr->getRange(), "public");
auto diag =
diagnose(attr->getLocation(), diag::access_control_extension_open);
diag.fixItRemove(attr->getRange());
for (auto Member : extension->getMembers()) {
if (auto *VD = dyn_cast<ValueDecl>(Member)) {
if (VD->getAttrs().hasAttribute<AccessControlAttr>())
continue;

StringRef accessLevel = VD->isObjC() ? "open " : "public ";

if (auto *FD = dyn_cast<FuncDecl>(VD))
diag.fixItInsert(FD->getFuncLoc(), accessLevel);

if (auto *VAD = dyn_cast<VarDecl>(VD))
diag.fixItInsert(VAD->getParentPatternBinding()->getLoc(),
accessLevel);
}
}

attr->setInvalid();
return;
}
Expand Down
9 changes: 9 additions & 0 deletions test/attr/open.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,12 @@ open class OpenSubClass : OpenSuperClass {
open override subscript(index: MarkerForNonOpenSubscripts) -> Int { return 0 }

}

class InvalidOpenExtensionClass { }

open extension InvalidOpenExtensionClass { // expected-error {{extensions cannot be declared 'open'; declare individual members as 'open' instead}} {{1-6=}} {{3-3=public }} {{3-3=public }}
func C() { } // Insert public
private func A() { } // OK
var F: Int { 3 } // Insert public
private var G: Int { 3 } // Okay
}
31 changes: 31 additions & 0 deletions test/attr/open_objc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %empty-directory(%t)
// RUN: %target-typecheck-verify-swift -sdk %clang-importer-sdk -I %t
// REQUIRES: objc_interop

import Foundation

class InvalidOpenExtensionClass { }

open extension InvalidOpenExtensionClass { // expected-error {{extensions cannot be declared 'open'; declare individual members as 'open' instead}} {{1-6=}} {{3-3=public }} {{9-9=open }} {{9-9=open }} {{3-3=public }}
private func A() { } // OK
@objc func B() { } // Insert open
func C() { } // Insert public
@objc private var D: Int { 3 } // Okay
@objc var E: Int { 3 } // Insert open
var F: Int { 3 } // Insert public
private var G: Int { 3 } // Okay
}

@objc
@objcMembers
class InvalidOpenExtensionObjcClass: NSObject { }

open extension InvalidOpenExtensionObjcClass { // expected-error {{extensions cannot be declared 'open'; declare individual members as 'open' instead}} {{1-6=}} {{3-3=open }} {{9-9=open }} {{9-9=open }} {{3-3=open }}
private func A() { } // OK
@objc func B() { } // Insert open
func C() { } // Insert open
@objc private var D: Int { 3 } // Okay
@objc var E: Int { 3 } // Insert open
var F: Int { 3 } // Insert open
private var G: Int { 3 } // Okay
}