Skip to content

Commit 1e0dd7a

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

File tree

4 files changed

+10
-2
lines changed

4 files changed

+10
-2
lines changed

include/swift/AST/Attr.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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/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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,9 @@ 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}}

0 commit comments

Comments
 (0)