Skip to content

Commit e5a2c7c

Browse files
committed
[QoI] Improve fix-it when renamed method has contextual member lookup argument
Currently we generate incorrect fix-it in situations where we have contextual member lookup as one of the arguments. Resolves: rdar://problem/32526620
1 parent 970f020 commit e5a2c7c

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/Sema/TypeCheckAvailability.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1628,7 +1628,26 @@ static void fixItAvailableAttrRename(TypeChecker &TC,
16281628
SmallString<64> selfReplace;
16291629
if (needsParens)
16301630
selfReplace.push_back('(');
1631-
selfReplace += sourceMgr.extractText(selfExprRange);
1631+
1632+
// If the base is contextual member lookup and we know the type,
1633+
// let's just prepend it, otherwise we'll end up with an incorrect fix-it.
1634+
auto base = sourceMgr.extractText(selfExprRange);
1635+
if (!base.empty() && base.front() == '.') {
1636+
auto newName = attr->Rename;
1637+
// If this is not a rename, let's not
1638+
// even try to emit a fix-it because
1639+
// it's going to be invalid.
1640+
if (newName.empty())
1641+
return;
1642+
1643+
auto parts = newName.split('.');
1644+
auto nominalName = parts.first;
1645+
assert(!nominalName.empty());
1646+
1647+
selfReplace += nominalName;
1648+
}
1649+
1650+
selfReplace += base;
16321651
if (needsParens)
16331652
selfReplace.push_back(')');
16341653
selfReplace.push_back('.');

test/attr/attr_availability.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -783,3 +783,25 @@ func testVariadic() {
783783
variadic3(1, b: 2, 3, d: "test") // expected-error {{'variadic3(_:b:c:d:)' has been renamed to 'after(x:_:y:z:)'}} {{3-12=after}} {{13-13=x: }} {{16-19=}} {{25-26=z}} {{none}}
784784
variadic3(1, d:"test") // expected-error {{'variadic3(_:b:c:d:)' has been renamed to 'after(x:_:y:z:)'}} {{3-12=after}} {{13-13=x: }} {{16-17=z}} {{none}}
785785
}
786+
787+
enum E_32526620 {
788+
case foo
789+
case bar
790+
791+
func set() {}
792+
}
793+
794+
@available(*, unavailable, renamed: "E_32526620.set(self:)")
795+
func rdar32526620_1(a: E_32526620) {} // expected-note {{here}}
796+
rdar32526620_1(a: .foo)
797+
// expected-error@-1 {{'rdar32526620_1(a:)' has been replaced by instance method 'E_32526620.set()'}} {{1-15=E_32526620.foo.set}} {{16-23=}}
798+
799+
@available(*, unavailable, renamed: "E_32526620.set(a:self:)")
800+
func rdar32526620_2(a: Int, b: E_32526620) {} // expected-note {{here}}
801+
rdar32526620_2(a: 42, b: .bar)
802+
// expected-error@-1 {{'rdar32526620_2(a:b:)' has been replaced by instance method 'E_32526620.set(a:)'}} {{1-15=E_32526620.bar.set}} {{21-30=}}
803+
804+
@available(*, unavailable, renamed: "E_32526620.set(a:self:c:)")
805+
func rdar32526620_3(a: Int, b: E_32526620, c: String) {} // expected-note {{here}}
806+
rdar32526620_3(a: 42, b: .bar, c: "question")
807+
// expected-error@-1 {{'rdar32526620_3(a:b:c:)' has been replaced by instance method 'E_32526620.set(a:c:)'}} {{1-15=E_32526620.bar.set}} {{23-32=}}

0 commit comments

Comments
 (0)