Skip to content

Commit 93c80da

Browse files
committed
Parse: __shared and __owned should be contextual keywords
This was a source compatibility regression, someone actually had an identifier named __shared.
1 parent 4970d7d commit 93c80da

File tree

8 files changed

+50
-44
lines changed

8 files changed

+50
-44
lines changed

include/swift/Parse/Parser.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,10 @@ class Parser {
787787
bool parseTypeAttributeList(VarDecl::Specifier &Specifier,
788788
SourceLoc &SpecifierLoc,
789789
TypeAttributes &Attributes) {
790-
if (Tok.isAny(tok::at_sign, tok::kw_inout, tok::kw___shared, tok::kw___owned))
790+
if (Tok.isAny(tok::at_sign, tok::kw_inout) ||
791+
(Tok.is(tok::identifier) &&
792+
(Tok.getRawText().equals("__shared") ||
793+
Tok.getRawText().equals("__owned"))))
791794
return parseTypeAttributeListPresent(Specifier, SpecifierLoc, Attributes);
792795
return false;
793796
}

include/swift/Parse/Token.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,17 @@ class Token {
179179
/// used
180180
bool canBeArgumentLabel() const {
181181
// Identifiers, escaped identifiers, and '_' can be argument labels.
182-
if (is(tok::identifier) || isEscapedIdentifier() || is(tok::kw__))
182+
if (is(tok::identifier) || isEscapedIdentifier() || is(tok::kw__)) {
183+
// ... except for '__shared' and '__owned'.
184+
if (getRawText().equals("__shared") ||
185+
getRawText().equals("__owned"))
186+
return false;
187+
183188
return true;
189+
}
184190

185-
// 'let', 'var', 'inout', '__shared', and '__owned'
186-
// cannot be argument labels.
187-
if (isAny(tok::kw_let, tok::kw_var, tok::kw_inout,
188-
tok::kw___owned, tok::kw___shared))
191+
// 'let', 'var', and 'inout' cannot be argument labels.
192+
if (isAny(tok::kw_let, tok::kw_var, tok::kw_inout))
189193
return false;
190194

191195
// All other keywords can be argument labels.

include/swift/Syntax/TokenKinds.def

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,6 @@ DECL_KEYWORD(struct)
140140
DECL_KEYWORD(subscript)
141141
DECL_KEYWORD(typealias)
142142
DECL_KEYWORD(var)
143-
DECL_KEYWORD(__shared)
144-
DECL_KEYWORD(__owned)
145143

146144
DECL_KEYWORD(fileprivate)
147145
DECL_KEYWORD(internal)

lib/Parse/ParseDecl.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1820,19 +1820,22 @@ bool Parser::parseTypeAttributeListPresent(VarDecl::Specifier &Specifier,
18201820
SourceLoc &SpecifierLoc,
18211821
TypeAttributes &Attributes) {
18221822
Specifier = VarDecl::Specifier::Owned;
1823-
while (Tok.isAny(tok::kw_inout, tok::kw___shared, tok::kw___owned)) {
1823+
while (Tok.is(tok::kw_inout) ||
1824+
(Tok.is(tok::identifier) &&
1825+
(Tok.getRawText().equals("__shared") ||
1826+
Tok.getRawText().equals("__owned")))) {
18241827
if (SpecifierLoc.isValid()) {
18251828
diagnose(Tok, diag::parameter_specifier_repeated)
18261829
.fixItRemove(SpecifierLoc);
18271830
} else {
1828-
if (Tok.is(tok::kw___owned)) {
1829-
Specifier = VarDecl::Specifier::Owned;
1830-
} else if (Tok.is(tok::kw_inout)) {
1831+
if (Tok.is(tok::kw_inout)) {
18311832
Specifier = VarDecl::Specifier::InOut;
1832-
} else if (Tok.is(tok::kw___shared)) {
1833-
Specifier = VarDecl::Specifier::Shared;
1834-
} else {
1835-
llvm_unreachable("unhandled specifier kind?");
1833+
} else if (Tok.is(tok::identifier)) {
1834+
if (Tok.getRawText().equals("__shared")) {
1835+
Specifier = VarDecl::Specifier::Shared;
1836+
} else if (Tok.getRawText().equals("__owned")) {
1837+
Specifier = VarDecl::Specifier::Owned;
1838+
}
18361839
}
18371840
}
18381841
SpecifierLoc = consumeToken();

lib/Parse/ParsePattern.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,18 @@ Parser::parseParameterClause(SourceLoc &leftParenLoc,
182182

183183
// ('inout' | 'let' | 'var' | '__shared' | '__owned')?
184184
bool hasSpecifier = false;
185-
while (Tok.isAny(tok::kw_inout, tok::kw_let, tok::kw_var,
186-
tok::kw___shared, tok::kw___owned)) {
185+
while (Tok.isAny(tok::kw_inout, tok::kw_let, tok::kw_var) ||
186+
(Tok.is(tok::identifier) &&
187+
(Tok.getRawText().equals("__shared") ||
188+
Tok.getRawText().equals("__owned")))) {
187189
if (!hasSpecifier) {
188190
if (Tok.is(tok::kw_inout)) {
189191
// This case is handled later when mapping to ParamDecls for
190192
// better fixits.
191193
param.SpecifierKind = VarDecl::Specifier::InOut;
192194
param.SpecifierLoc = consumeToken();
193-
} else if (Tok.is(tok::kw___shared)) {
195+
} else if (Tok.is(tok::identifier) &&
196+
Tok.getRawText().equals("__shared")) {
194197
// This case is handled later when mapping to ParamDecls for
195198
// better fixits.
196199
param.SpecifierKind = VarDecl::Specifier::Shared;

lib/Parse/ParseType.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,13 +175,16 @@ ParserResult<TypeRepr> Parser::parseTypeSimple(Diag<> MessageID,
175175
if (Tok.is(tok::kw_inout)) {
176176
SpecifierLoc = consumeToken();
177177
TypeSpecifier = VarDecl::Specifier::InOut;
178-
} else if (Tok.is(tok::kw___shared)) {
179-
SpecifierLoc = consumeToken();
180-
TypeSpecifier = VarDecl::Specifier::Shared;
181-
} else if (Tok.is(tok::kw___owned)) {
182-
SpecifierLoc = consumeToken();
183-
TypeSpecifier = VarDecl::Specifier::Owned;
184-
178+
} else if (Tok.is(tok::identifier)) {
179+
if (Tok.getRawText().equals("__shared")) {
180+
assert(false);
181+
SpecifierLoc = consumeToken();
182+
TypeSpecifier = VarDecl::Specifier::Shared;
183+
} else if (Tok.getRawText().equals("__owned")) {
184+
assert(false);
185+
SpecifierLoc = consumeToken();
186+
TypeSpecifier = VarDecl::Specifier::Owned;
187+
}
185188
}
186189

187190
switch (Tok.getKind()) {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// RUN: %target-swift-frontend -typecheck %s
2+
3+
func __shared() {}
4+
5+
func __owned() {}
6+
7+
func foo() {
8+
__shared()
9+
__owned()
10+
}

test/SourceKit/CodeComplete/complete_override.swift.response

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
11
{
22
key.results: [
3-
{
4-
key.kind: source.lang.swift.keyword,
5-
key.name: "__owned",
6-
key.sourcetext: "__owned",
7-
key.description: "__owned",
8-
key.typename: "",
9-
key.context: source.codecompletion.context.none,
10-
key.num_bytes_to_erase: 0
11-
},
12-
{
13-
key.kind: source.lang.swift.keyword,
14-
key.name: "__shared",
15-
key.sourcetext: "__shared",
16-
key.description: "__shared",
17-
key.typename: "",
18-
key.context: source.codecompletion.context.none,
19-
key.num_bytes_to_erase: 0
20-
},
213
{
224
key.kind: source.lang.swift.keyword,
235
key.name: "associatedtype",

0 commit comments

Comments
 (0)