Skip to content

Change message of invalid_iboutlet and invalid_ibinspectable to reflect accurate usage #59198

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 2 commits into from
Jun 4, 2022
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
6 changes: 2 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -1406,8 +1406,6 @@ ERROR(transparent_in_protocols_not_supported,none,
ERROR(transparent_in_classes_not_supported,none,
"'@_transparent' attribute is not supported on declarations within classes", ())

ERROR(invalid_iboutlet,none,
"only instance properties can be declared @IBOutlet", ())
ERROR(iboutlet_nonobjc_class,none,
"@IBOutlet property cannot %select{have|be an array of}0 "
"non-'@objc' class type %1", (bool, Type))
Expand All @@ -1429,8 +1427,8 @@ NOTE(note_make_implicitly_unwrapped_optional,none,
ERROR(invalid_ibdesignable_extension,none,
"@IBDesignable can only be applied to classes and extensions "
"of classes", ())
ERROR(invalid_ibinspectable,none,
"only instance properties can be declared @%0", (StringRef))
ERROR(attr_must_be_used_on_class_instance,none,
"only class instance properties can be declared @%0", (StringRef))
ERROR(invalid_ibaction_decl,none,
"only instance methods can be declared @%0", (StringRef))
ERROR(invalid_ibaction_result,none,
Expand Down
5 changes: 2 additions & 3 deletions include/swift/Migrator/FixitFilter.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,12 @@ struct FixitFilter {
return false;
// The following interact badly with the swift migrator by removing @IB*
// attributes when there is some unrelated type issue.
if (Info.ID == diag::invalid_iboutlet.ID ||
Info.ID == diag::iboutlet_nonobjc_class.ID ||
if (Info.ID == diag::iboutlet_nonobjc_class.ID ||
Info.ID == diag::iboutlet_nonobjc_protocol.ID ||
Info.ID == diag::iboutlet_nonobject_type.ID ||
Info.ID == diag::iboutlet_only_mutable.ID ||
Info.ID == diag::invalid_ibdesignable_extension.ID ||
Info.ID == diag::invalid_ibinspectable.ID ||
Info.ID == diag::attr_must_be_used_on_class_instance.ID ||
Info.ID == diag::invalid_ibaction_decl.ID)
return false;

Expand Down
11 changes: 6 additions & 5 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,16 +632,16 @@ void AttributeChecker::visitIBInspectableAttr(IBInspectableAttr *attr) {
// Only instance properties can be 'IBInspectable'.
auto *VD = cast<VarDecl>(D);
if (!VD->getDeclContext()->getSelfClassDecl() || VD->isStatic())
diagnoseAndRemoveAttr(attr, diag::invalid_ibinspectable,
attr->getAttrName());
diagnoseAndRemoveAttr(attr, diag::attr_must_be_used_on_class_instance,
attr->getAttrName());
}

void AttributeChecker::visitGKInspectableAttr(GKInspectableAttr *attr) {
// Only instance properties can be 'GKInspectable'.
auto *VD = cast<VarDecl>(D);
if (!VD->getDeclContext()->getSelfClassDecl() || VD->isStatic())
diagnoseAndRemoveAttr(attr, diag::invalid_ibinspectable,
attr->getAttrName());
diagnoseAndRemoveAttr(attr, diag::attr_must_be_used_on_class_instance,
attr->getAttrName());
}

static Optional<Diag<bool,Type>>
Expand Down Expand Up @@ -690,7 +690,8 @@ void AttributeChecker::visitIBOutletAttr(IBOutletAttr *attr) {
// Only instance properties can be 'IBOutlet'.
auto *VD = cast<VarDecl>(D);
if (!VD->getDeclContext()->getSelfClassDecl() || VD->isStatic())
diagnoseAndRemoveAttr(attr, diag::invalid_iboutlet);
diagnoseAndRemoveAttr(attr, diag::attr_must_be_used_on_class_instance,
attr->getAttrName());

if (!VD->isSettable(nullptr)) {
// Allow non-mutable IBOutlet properties in module interfaces,
Expand Down
4 changes: 2 additions & 2 deletions test/attr/attr_iboutlet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import Foundation

// expected-error@+1 {{@IBOutlet property cannot have non-object type 'Int'}}
@IBOutlet // expected-error {{only instance properties can be declared @IBOutlet}} {{1-11=}}
@IBOutlet // expected-error {{only class instance properties can be declared @IBOutlet}} {{1-11=}}
var iboutlet_global: Int?

@IBOutlet // expected-error {{@IBOutlet may only be used on 'var' declarations}} {{1-11=}}
Expand All @@ -22,7 +22,7 @@ class IBOutletWrapperTy {

@IBOutlet
class var staticValue: IBOutletWrapperTy? = 52 // expected-error {{cannot convert value of type 'Int' to specified type 'IBOutletWrapperTy?'}}
// expected-error@-2 {{only instance properties can be declared @IBOutlet}} {{3-12=}}
// expected-error@-2 {{only class instance properties can be declared @IBOutlet}} {{3-12=}}
// expected-error@-2 {{class stored properties not supported}}

@IBOutlet // expected-error {{@IBOutlet may only be used on 'var' declarations}} {{3-13=}}
Expand Down
12 changes: 8 additions & 4 deletions test/attr/attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,16 @@ class Inspect {
@IBInspectable func foo() {} // expected-error {{@IBInspectable may only be used on 'var' declarations}} {{3-18=}}
@GKInspectable func foo2() {} // expected-error {{@GKInspectable may only be used on 'var' declarations}} {{3-18=}}

@IBInspectable class var cval: Int { return 0 } // expected-error {{only instance properties can be declared @IBInspectable}} {{3-18=}}
@GKInspectable class var cval2: Int { return 0 } // expected-error {{only instance properties can be declared @GKInspectable}} {{3-18=}}
@IBInspectable class var cval: Int { return 0 } // expected-error {{only class instance properties can be declared @IBInspectable}} {{3-18=}}
@GKInspectable class var cval2: Int { return 0 } // expected-error {{only class instance properties can be declared @GKInspectable}} {{3-18=}}
}
@IBInspectable var ibinspectable_global : Int // expected-error {{only instance properties can be declared @IBInspectable}} {{1-16=}}
@GKInspectable var gkinspectable_global : Int // expected-error {{only instance properties can be declared @GKInspectable}} {{1-16=}}
@IBInspectable var ibinspectable_global : Int // expected-error {{only class instance properties can be declared @IBInspectable}} {{1-16=}}
@GKInspectable var gkinspectable_global : Int // expected-error {{only class instance properties can be declared @GKInspectable}} {{1-16=}}

struct inspectableWithStruct {
@IBInspectable var IBInspectableInStruct: Int // expected-error {{only class instance properties can be declared @IBInspectable}} {{3-18=}}
@GKInspectable var GKInspectableInStruct: Int // expected-error {{only class instance properties can be declared @GKInspectable}} {{3-18=}}
}

func foo(x: @convention(block) Int) {} // expected-error {{@convention attribute only applies to function types}}
func foo(x: @convention(block) (Int) -> Int) {}
Expand Down