Skip to content

Commit 0428adf

Browse files
committed
[Parse/Sema] Diagnose invalid attributes for GenericTypeParamDecl
Previously, invalid attributes were silently accepted and ignored. https://bugs.swift.org/browse/SR-8202
1 parent 1e0dd7a commit 0428adf

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ DECL_ATTR(_silgen_name, SILGenName,
114114
0)
115115
DECL_ATTR(available, Available,
116116
OnAbstractFunction | OnGenericType | OnVar | OnSubscript | OnEnumElement |
117-
OnExtension |
117+
OnExtension | OnGenericTypeParam |
118118
AllowMultipleAttributes | LongAttribute,
119119
1)
120120
CONTEXTUAL_SIMPLE_DECL_ATTR(final, Final,

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ TypeChecker::prepareGenericParamList(GenericParamList *gp,
427427

428428
unsigned depth = gp->getDepth();
429429
for (auto paramDecl : *gp) {
430+
checkDeclAttributesEarly(paramDecl);
430431
paramDecl->setDepth(depth);
431432
if (!paramDecl->hasAccess())
432433
paramDecl->setAccess(access);

test/Parse/invalid.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,10 @@ func sr8202_bar(@available(iOS, deprecated: 0) x: Int) {} // expected-error {{'@
150150
func sr8202_baz(@discardableResult x: Int) {} // expected-error {{'@discardableResult' attribute cannot be applied to this declaration}}
151151
func sr8202_qux(@objcMembers x: String) {} // expected-error {{@objcMembers may only be used on 'class' declarations}}
152152
func sr8202_quux(@weak x: String) {} // expected-error {{'weak' is a declaration modifier, not an attribute}} expected-error {{'weak' may only be used on 'var' declarations}}
153+
154+
class sr8202_cls<@NSApplicationMain T: AnyObject> {} // expected-error {{@NSApplicationMain may only be used on 'class' declarations}}
155+
func sr8202_func<@discardableResult T>(x: T) {} // expected-error {{'@discardableResult' attribute cannot be applied to this declaration}}
156+
enum sr8202_enum<@indirect T> {} // expected-error {{'indirect' is a declaration modifier, not an attribute}} expected-error {{'indirect' modifier cannot be applied to this declaration}}
157+
protocol P {
158+
@available(swift, introduced: 4.2) associatedtype Assoc // expected-error {{'@availability' attribute cannot be applied to this declaration}}
159+
}

0 commit comments

Comments
 (0)