Skip to content

Commit 274114d

Browse files
authored
Merge pull request #17852 from rintaro/parse-attr-invalid-sr8202
[Parse/Sema] Diagnose invalid attributes for ParamDecl and GenericTypeParamDecl
2 parents 95acbb9 + 0428adf commit 274114d

File tree

5 files changed

+19
-3
lines changed

5 files changed

+19
-3
lines changed

include/swift/AST/Attr.def

Lines changed: 2 additions & 2 deletions
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,
@@ -265,7 +265,7 @@ DECL_ATTR(__raw_doc_comment, RawDocComment,
265265
RejectByParser |
266266
NotSerialized, 48)
267267
CONTEXTUAL_DECL_ATTR(weak, ReferenceOwnership,
268-
OnVar | OnParam |
268+
OnVar |
269269
DeclModifier |
270270
NotSerialized, 49)
271271
CONTEXTUAL_DECL_ATTR_ALIAS(unowned, ReferenceOwnership)

lib/Parse/ParsePattern.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ mapParsedParameters(Parser &parser,
430430
argNameLoc, argName,
431431
paramNameLoc, paramName, Type(),
432432
parser.CurDeclContext);
433-
433+
param->getAttrs() = paramInfo.Attrs;
434434
bool parsingEnumElt
435435
= (paramContext == Parser::ParameterContextKind::EnumElement);
436436
// If we're not parsing an enum case, lack of a SourceLoc for both

lib/Sema/TypeCheckGeneric.cpp

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

453453
unsigned depth = gp->getDepth();
454454
for (auto paramDecl : *gp) {
455+
checkDeclAttributesEarly(paramDecl);
455456
paramDecl->setDepth(depth);
456457
if (!paramDecl->hasAccess())
457458
paramDecl->setAccess(access);

lib/Sema/TypeCheckPattern.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,8 @@ bool TypeChecker::typeCheckParameterList(ParameterList *PL, DeclContext *DC,
828828
bool hadError = false;
829829

830830
for (auto param : *PL) {
831+
checkDeclAttributesEarly(param);
832+
831833
auto typeRepr = param->getTypeLoc().getTypeRepr();
832834
if (!typeRepr &&
833835
param->hasInterfaceType()) {

test/Parse/invalid.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,16 @@ let x: () = ()
144144
!(()) // expected-error {{cannot convert value of type '()' to expected argument type 'Bool'}}
145145
!(x) // expected-error {{cannot convert value of type '()' to expected argument type 'Bool'}}
146146
!x // expected-error {{cannot convert value of type '()' to expected argument type 'Bool'}}
147+
148+
func sr8202_foo(@NSApplicationMain x: Int) {} // expected-error {{@NSApplicationMain may only be used on 'class' declarations}}
149+
func sr8202_bar(@available(iOS, deprecated: 0) x: Int) {} // expected-error {{'@availability' attribute cannot be applied to this declaration}}
150+
func sr8202_baz(@discardableResult x: Int) {} // expected-error {{'@discardableResult' attribute cannot be applied to this declaration}}
151+
func sr8202_qux(@objcMembers x: String) {} // expected-error {{@objcMembers may only be used on 'class' declarations}}
152+
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)