Skip to content

Commit a6f7a8e

Browse files
committed
[Parser] Set local discriminator to ParamDecls
We have to discriminate between params and local variables.
1 parent 6496175 commit a6f7a8e

14 files changed

+152
-36
lines changed

include/swift/Parse/Parser.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -761,6 +761,7 @@ class Parser {
761761
ParserStatus parseLineDirective(bool isLine = false);
762762

763763
void setLocalDiscriminator(ValueDecl *D);
764+
void setLocalDiscriminatorToParamList(ParameterList *PL);
764765

765766
/// Parse the optional attributes before a declaration.
766767
bool parseDeclAttributeList(DeclAttributes &Attributes,

lib/Parse/ParseDecl.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,6 +2409,14 @@ void Parser::setLocalDiscriminator(ValueDecl *D) {
24092409
D->setLocalDiscriminator(discriminator);
24102410
}
24112411

2412+
void Parser::setLocalDiscriminatorToParamList(ParameterList *PL) {
2413+
for (auto P : *PL) {
2414+
if (!P->hasName() || P->isImplicit())
2415+
continue;
2416+
setLocalDiscriminator(P);
2417+
}
2418+
}
2419+
24122420
void Parser::delayParseFromBeginningToHere(ParserPosition BeginParserPosition,
24132421
ParseDeclOptions Flags) {
24142422
auto CurLoc = Tok.getLoc();
@@ -4282,6 +4290,8 @@ bool Parser::parseGetSetImpl(ParseDeclOptions Flags,
42824290

42834291
// Establish the new context.
42844292
ParseFunctionBody CC(*this, TheDecl);
4293+
for (auto PL : TheDecl->getParameterLists())
4294+
setLocalDiscriminatorToParamList(PL);
42854295

42864296
// Parse the body.
42874297
SmallVector<ASTNode, 16> Entries;
@@ -4373,6 +4383,8 @@ void Parser::parseAccessorBodyDelayed(AbstractFunctionDecl *AFD) {
43734383
// Re-enter the lexical scope.
43744384
Scope S(this, AccessorParserState->takeScope());
43754385
ParseFunctionBody CC(*this, AFD);
4386+
for (auto PL : AFD->getParameterLists())
4387+
setLocalDiscriminatorToParamList(PL);
43764388

43774389
SmallVector<ASTNode, 16> Entries;
43784390
parseBraceItems(Entries);
@@ -5325,6 +5337,8 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
53255337

53265338
// Establish the new context.
53275339
ParseFunctionBody CC(*this, FD);
5340+
for (auto PL : FD->getParameterLists())
5341+
setLocalDiscriminatorToParamList(PL);
53285342

53295343
// Check to see if we have a "{" to start a brace statement.
53305344
if (Tok.is(tok::l_brace)) {
@@ -5395,6 +5409,8 @@ bool Parser::parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD) {
53955409
// Re-enter the lexical scope.
53965410
Scope S(this, FunctionParserState->takeScope());
53975411
ParseFunctionBody CC(*this, AFD);
5412+
for (auto PL : AFD->getParameterLists())
5413+
setLocalDiscriminatorToParamList(PL);
53985414

53995415
ParserResult<BraceStmt> Body =
54005416
parseBraceItemList(diag::func_decl_without_brace);
@@ -6257,6 +6273,8 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
62576273
} else {
62586274
// Parse the body.
62596275
ParseFunctionBody CC(*this, CD);
6276+
for (auto PL : CD->getParameterLists())
6277+
setLocalDiscriminatorToParamList(PL);
62606278

62616279
if (!isDelayedParsingEnabled()) {
62626280
if (Context.Stats)

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2731,6 +2731,7 @@ ParserResult<Expr> Parser::parseExprClosure() {
27312731
if (params) {
27322732
// Add the parameters into scope.
27332733
addParametersToScope(params);
2734+
setLocalDiscriminatorToParamList(params);
27342735
} else {
27352736
// There are no parameters; allow anonymous closure variables.
27362737
// FIXME: We could do this all the time, and then provide Fix-Its

test/refactoring/rename/Outputs/local/casebind_1.swift.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func test4(arg: () throws -> Void) {
3939
}
4040
}
4141

42-
43-
44-
45-
42+
func test5(_ x: Int) {
43+
let x = x
44+
print(x)
45+
}
4646

test/refactoring/rename/Outputs/local/casebind_2.swift.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func test4(arg: () throws -> Void) {
3939
}
4040
}
4141

42-
43-
44-
45-
42+
func test5(_ x: Int) {
43+
let x = x
44+
print(x)
45+
}
4646

test/refactoring/rename/Outputs/local/catch_1.swift.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func test4(arg: () throws -> Void) {
3939
}
4040
}
4141

42-
43-
44-
45-
42+
func test5(_ x: Int) {
43+
let x = x
44+
print(x)
45+
}
4646

test/refactoring/rename/Outputs/local/catch_2.swift.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func test4(arg: () throws -> Void) {
3939
}
4040
}
4141

42-
43-
44-
45-
42+
func test5(_ x: Int) {
43+
let x = x
44+
print(x)
45+
}
4646

test/refactoring/rename/Outputs/local/ifbind_1.swift.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func test4(arg: () throws -> Void) {
3939
}
4040
}
4141

42-
43-
44-
45-
42+
func test5(_ x: Int) {
43+
let x = x
44+
print(x)
45+
}
4646

test/refactoring/rename/Outputs/local/ifbind_2.swift.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func test4(arg: () throws -> Void) {
3939
}
4040
}
4141

42-
43-
44-
45-
42+
func test5(_ x: Int) {
43+
let x = x
44+
print(x)
45+
}
4646

test/refactoring/rename/Outputs/local/localvar_1.swift.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func test4(arg: () throws -> Void) {
3939
}
4040
}
4141

42-
43-
44-
45-
42+
func test5(_ x: Int) {
43+
let x = x
44+
print(x)
45+
}
4646

test/refactoring/rename/Outputs/local/localvar_2.swift.expected

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ func test4(arg: () throws -> Void) {
3939
}
4040
}
4141

42-
43-
44-
45-
42+
func test5(_ x: Int) {
43+
let x = x
44+
print(x)
45+
}
4646

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
func test1() {
2+
if true {
3+
let x = 1
4+
print(x)
5+
} else {
6+
let x = 2
7+
print(x)
8+
}
9+
}
10+
11+
func test2(arg1: Int?, arg2: (Int, String)?) {
12+
if let x = arg1 {
13+
print(x)
14+
} else if let (x, y) = arg2 {
15+
print(x, y)
16+
}
17+
}
18+
19+
func test3(arg: Int?) {
20+
switch arg {
21+
case let .some(x) where x == 0:
22+
print(x)
23+
case let .some(x) where x == 1,
24+
let .some(x) where x == 2: // FIXME: This 'x' in '.some(x)' isn't properly renamed in 'casebind_2' case.
25+
print(x)
26+
default:
27+
break
28+
}
29+
}
30+
31+
struct Err1 : Error { }
32+
func test4(arg: () throws -> Void) {
33+
do {
34+
try arg()
35+
} catch let x as Err1 {
36+
print(x)
37+
} catch let x {
38+
print(x)
39+
}
40+
}
41+
42+
func test5(_ xRenamed: Int) {
43+
let x = xRenamed
44+
print(x)
45+
}
46+
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
func test1() {
2+
if true {
3+
let x = 1
4+
print(x)
5+
} else {
6+
let x = 2
7+
print(x)
8+
}
9+
}
10+
11+
func test2(arg1: Int?, arg2: (Int, String)?) {
12+
if let x = arg1 {
13+
print(x)
14+
} else if let (x, y) = arg2 {
15+
print(x, y)
16+
}
17+
}
18+
19+
func test3(arg: Int?) {
20+
switch arg {
21+
case let .some(x) where x == 0:
22+
print(x)
23+
case let .some(x) where x == 1,
24+
let .some(x) where x == 2: // FIXME: This 'x' in '.some(x)' isn't properly renamed in 'casebind_2' case.
25+
print(x)
26+
default:
27+
break
28+
}
29+
}
30+
31+
struct Err1 : Error { }
32+
func test4(arg: () throws -> Void) {
33+
do {
34+
try arg()
35+
} catch let x as Err1 {
36+
print(x)
37+
} catch let x {
38+
print(x)
39+
}
40+
}
41+
42+
func test5(_ x: Int) {
43+
let xRenamed = x
44+
print(xRenamed)
45+
}
46+

test/refactoring/rename/local.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,29 @@ func test4(arg: () throws -> Void) {
3939
}
4040
}
4141

42-
42+
func test5(_ x: Int) {
43+
let x = x
44+
print(x)
45+
}
4346

4447
// RUN: %empty-directory(%t.result)
4548
// RUN: %refactor -rename -source-filename %s -pos=3:9 -new-name="xRenamed" >> %t.result/localvar_1.swift
4649
// RUN: %refactor -rename -source-filename %s -pos=7:11 -new-name="xRenamed" >> %t.result/localvar_2.swift
4750
// RUN: diff -u %S/Outputs/local/localvar_1.swift.expected %t.result/localvar_1.swift
4851
// RUN: diff -u %S/Outputs/local/localvar_2.swift.expected %t.result/localvar_2.swift
49-
5052
// RUN: %refactor -rename -source-filename %s -pos=12:10 -new-name="xRenamed" >> %t.result/ifbind_1.swift
5153
// RUN: %refactor -rename -source-filename %s -pos=15:11 -new-name="xRenamed" >> %t.result/ifbind_2.swift
5254
// RUN: diff -u %S/Outputs/local/ifbind_1.swift.expected %t.result/ifbind_1.swift
5355
// RUN: diff -u %S/Outputs/local/ifbind_2.swift.expected %t.result/ifbind_2.swift
54-
5556
// RUN: %refactor -rename -source-filename %s -pos=21:18 -new-name="xRenamed" >> %t.result/casebind_1.swift
5657
// RUN: %refactor -rename -source-filename %s -pos=25:11 -new-name="xRenamed" >> %t.result/casebind_2.swift
5758
// RUN: diff -u %S/Outputs/local/casebind_1.swift.expected %t.result/casebind_1.swift
5859
// RUN: diff -u %S/Outputs/local/casebind_2.swift.expected %t.result/casebind_2.swift
59-
6060
// RUN: %refactor -rename -source-filename %s -pos=35:15 -new-name="xRenamed" >> %t.result/catch_1.swift
6161
// RUN: %refactor -rename -source-filename %s -pos=38:11 -new-name="xRenamed" >> %t.result/catch_2.swift
6262
// RUN: diff -u %S/Outputs/local/catch_1.swift.expected %t.result/catch_1.swift
6363
// RUN: diff -u %S/Outputs/local/catch_2.swift.expected %t.result/catch_2.swift
64+
// RUN: %refactor -rename -source-filename %s -pos=42:14 -new-name="xRenamed" >> %t.result/param_1.swift
65+
// RUN: %refactor -rename -source-filename %s -pos=44:9 -new-name="xRenamed" >> %t.result/param_2.swift
66+
// RUN: diff -u %S/Outputs/local/param_1.swift.expected %t.result/param_1.swift
67+
// RUN: diff -u %S/Outputs/local/param_2.swift.expected %t.result/param_2.swift

0 commit comments

Comments
 (0)