Skip to content

Commit 9e96a70

Browse files
authored
Merge pull request swiftlang#35490 from xedin/rdar-70764991
[Diagnostics] Adjust OoO fix-it intervals to account for replacement of first argument
2 parents 50f55c1 + e086896 commit 9e96a70

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4823,13 +4823,27 @@ 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+
4839+
// Move requires postfix comma only if argument is moved in-between
4840+
// other arguments.
4841+
bool requiresComma = !isExpr<BinaryExpr>(anchor) &&
4842+
PrevArgIdx != tuple->getNumElements() - 1;
4843+
48304844
diag.fixItRemove(removalRange);
48314845
diag.fixItInsert(secondRange.Start,
4832-
text.str() + (isExpr<BinaryExpr>(anchor) ? "" : ", "));
4846+
text.str() + (requiresComma ? ", " : ""));
48334847
};
48344848

48354849
// There are 4 diagnostic messages variations depending on

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, _: String) {
1756+
}
1757+
1758+
func test(_ str: String) {
1759+
bar(str, S.foo) // expected-error {{unnamed argument #1 must precede unnamed argument #2}} {{9-12=}} {{14-14=str}}
1760+
}
1761+
}

0 commit comments

Comments
 (0)