Skip to content

Commit 09f3461

Browse files
committed
[Diagnostics] Adjust OoO fix-it intervals to account for replacement of first argument
Current logic fix-it didn't account for the first argument being re-ordered. The start position for the first argument should always be the next token after left paren denoting a start of the argument list. Resolves: rdar://problem/70764991
1 parent 5938a67 commit 09f3461

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4823,10 +4823,19 @@ bool OutOfOrderArgumentFailure::diagnoseAsError() {
48234823
auto text = SM.extractText(
48244824
Lexer::getCharSourceRangeFromSourceRange(SM, firstRange));
48254825

4826-
auto removalRange =
4827-
SourceRange(Lexer::getLocForEndOfToken(
4828-
SM, tuple->getElement(ArgIdx - 1)->getEndLoc()),
4829-
firstRange.End);
4826+
SourceLoc removalStartLoc;
4827+
// For the first argument, start is always next token after `(`.
4828+
if (ArgIdx == 0) {
4829+
removalStartLoc = tuple->getLParenLoc();
4830+
} else {
4831+
// For all other arguments, start is the next token past
4832+
// the previous argument.
4833+
removalStartLoc = tuple->getElement(ArgIdx - 1)->getEndLoc();
4834+
}
4835+
4836+
SourceRange removalRange{Lexer::getLocForEndOfToken(SM, removalStartLoc),
4837+
firstRange.End};
4838+
48304839
diag.fixItRemove(removalRange);
48314840
diag.fixItInsert(secondRange.Start,
48324841
text.str() + (isExpr<BinaryExpr>(anchor) ? "" : ", "));

test/Constraints/argument_matching.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1745,3 +1745,17 @@ func --- (_ lhs: String, _ rhs: String) -> Bool { true }
17451745

17461746
let x = 1
17471747
x --- x // expected-error 2 {{cannot convert value of type 'Int' to expected argument type 'String'}}
1748+
1749+
// rdar://problem/70764991 - out-of-order diagnostic crashes the compiler
1750+
func rdar70764991() {
1751+
struct S {
1752+
static var foo: S { get { S() } }
1753+
}
1754+
1755+
func bar(_: Any, foo: String) {
1756+
}
1757+
1758+
func test(_ str: String) {
1759+
bar(str, foo: S.foo) // expected-error {{unnamed argument #1 must precede argument 'foo'}} {{9-12=}} {{14-14=str, }}
1760+
}
1761+
}

0 commit comments

Comments
 (0)