Skip to content

Commit fd1e14f

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

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
@@ -763,6 +763,7 @@ class Parser {
763763
ParserStatus parseLineDirective(bool isLine = false);
764764

765765
void setLocalDiscriminator(ValueDecl *D);
766+
void setLocalDiscriminatorToParamList(ParameterList *PL);
766767

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

lib/Parse/ParseDecl.cpp

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

2344+
void Parser::setLocalDiscriminatorToParamList(ParameterList *PL) {
2345+
for (auto P : *PL) {
2346+
if (!P->hasName() || P->isImplicit())
2347+
continue;
2348+
setLocalDiscriminator(P);
2349+
}
2350+
}
2351+
23442352
void Parser::delayParseFromBeginningToHere(ParserPosition BeginParserPosition,
23452353
ParseDeclOptions Flags) {
23462354
auto CurLoc = Tok.getLoc();
@@ -4196,6 +4204,8 @@ bool Parser::parseGetSetImpl(ParseDeclOptions Flags,
41964204

41974205
// Establish the new context.
41984206
ParseFunctionBody CC(*this, TheDecl);
4207+
for (auto PL : TheDecl->getParameterLists())
4208+
setLocalDiscriminatorToParamList(PL);
41994209

42004210
// Parse the body.
42014211
SmallVector<ASTNode, 16> Entries;
@@ -4287,6 +4297,8 @@ void Parser::parseAccessorBodyDelayed(AbstractFunctionDecl *AFD) {
42874297
// Re-enter the lexical scope.
42884298
Scope S(this, AccessorParserState->takeScope());
42894299
ParseFunctionBody CC(*this, AFD);
4300+
for (auto PL : AFD->getParameterLists())
4301+
setLocalDiscriminatorToParamList(PL);
42904302

42914303
SmallVector<ASTNode, 16> Entries;
42924304
parseBraceItems(Entries);
@@ -5239,6 +5251,8 @@ Parser::parseDeclFunc(SourceLoc StaticLoc, StaticSpellingKind StaticSpelling,
52395251

52405252
// Establish the new context.
52415253
ParseFunctionBody CC(*this, FD);
5254+
for (auto PL : FD->getParameterLists())
5255+
setLocalDiscriminatorToParamList(PL);
52425256

52435257
// Check to see if we have a "{" to start a brace statement.
52445258
if (Tok.is(tok::l_brace)) {
@@ -5309,6 +5323,8 @@ bool Parser::parseAbstractFunctionBodyDelayed(AbstractFunctionDecl *AFD) {
53095323
// Re-enter the lexical scope.
53105324
Scope S(this, FunctionParserState->takeScope());
53115325
ParseFunctionBody CC(*this, AFD);
5326+
for (auto PL : AFD->getParameterLists())
5327+
setLocalDiscriminatorToParamList(PL);
53125328

53135329
ParserResult<BraceStmt> Body =
53145330
parseBraceItemList(diag::func_decl_without_brace);
@@ -6148,6 +6164,8 @@ Parser::parseDeclInit(ParseDeclOptions Flags, DeclAttributes &Attributes) {
61486164
} else {
61496165
// Parse the body.
61506166
ParseFunctionBody CC(*this, CD);
6167+
for (auto PL : CD->getParameterLists())
6168+
setLocalDiscriminatorToParamList(PL);
61516169

61526170
if (!isDelayedParsingEnabled()) {
61536171
if (Context.Stats)

lib/Parse/ParseExpr.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2727,6 +2727,7 @@ ParserResult<Expr> Parser::parseExprClosure() {
27272727
if (params) {
27282728
// Add the parameters into scope.
27292729
addParametersToScope(params);
2730+
setLocalDiscriminatorToParamList(params);
27302731
} else {
27312732
// There are no parameters; allow anonymous closure variables.
27322733
// 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)