Skip to content

Commit 12161fa

Browse files
committed
[Sema] Diagnose invalid $ prefixes on closure parameters after the
closure is resolved. This can't be diagnosed earlier because a property wrapper attribute may be inferred in the constraint system.
1 parent ea3fe03 commit 12161fa

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

include/swift/AST/Identifier.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ class Identifier {
166166
}
167167

168168
bool hasDollarPrefix() const {
169-
return str().startswith("$");
169+
return str().startswith("$") && !(getLength() == 1);
170170
}
171171

172172
const void *getAsOpaquePointer() const {

lib/Sema/TypeCheckStmt.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "swift/Basic/TopCollection.h"
4141
#include "swift/Parse/Lexer.h"
4242
#include "swift/Parse/LocalContext.h"
43+
#include "swift/Parse/Parser.h"
4344
#include "swift/Sema/IDETypeChecking.h"
4445
#include "swift/Syntax/TokenKinds.h"
4546
#include "llvm/ADT/DenseMap.h"
@@ -2080,6 +2081,13 @@ bool TypeChecker::typeCheckClosureBody(ClosureExpr *closure) {
20802081
TypeChecker::checkClosureAttributes(closure);
20812082
TypeChecker::checkParameterList(closure->getParameters(), closure);
20822083

2084+
for (auto *param : *closure->getParameters()) {
2085+
if (!param->isImplicit() && param->getName().hasDollarPrefix() &&
2086+
!param->hasAttachedPropertyWrapper()) {
2087+
param->diagnose(diag::dollar_identifier_decl, param->getName());
2088+
}
2089+
}
2090+
20832091
BraceStmt *body = closure->getBody();
20842092

20852093
Optional<FunctionBodyTimer> timer;

test/Parse/dollar_identifier.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,12 @@ func $declareWithDollar() { // expected-error{{cannot declare entity named '$dec
7777
$b c: Int) { } // expected-error{{cannot declare entity named '$b'}}
7878
let _: (Int) -> Int = {
7979
[$capture = 0] // expected-error{{cannot declare entity named '$capture'}}
80-
$a in // expected-error{{cannot declare entity named '$a'}}
80+
$a in // diagnosed after type checking the closure
8181
$capture
8282
}
83+
let _: (Int) -> Void = {
84+
$a in // expected-error{{cannot declare entity named '$a'}}
85+
}
8386
let ($a: _, _) = (0, 0) // expected-error{{cannot declare entity named '$a'}}
8487
$label: if true { // expected-error{{cannot declare entity named '$label'}}
8588
break $label

0 commit comments

Comments
 (0)