Skip to content

Commit dbb9d31

Browse files
committed
Sema: Fix a couple of crashes in tuple arguments tests
There's a general problem where a SubscriptExpr has an argument that's a LoadExpr loading a tuple from an lvalue. For some reason we don't construct the ParenExpr in this case, which confused CSDiag. Also, in Swift 3 mode, add a total hack to fudge things in matchCallArguments() in the case where we erroneously lost ParenType sugar.
1 parent 3ede1ab commit dbb9d31

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4702,6 +4702,15 @@ Expr *ExprRewriter::coerceCallArguments(
47024702
bool hasTrailingClosure,
47034703
ConstraintLocatorBuilder locator) {
47044704

4705+
// Total hack: In Swift 3 mode, we can end up with an arity mismatch due to
4706+
// loss of ParenType sugar.
4707+
if (cs.getASTContext().isSwiftVersion3()) {
4708+
if (isa<TupleExpr>(arg))
4709+
if (auto *parenType = dyn_cast<ParenType>(paramType.getPointer()))
4710+
if (isa<TupleType>(parenType->getUnderlyingType().getPointer()))
4711+
paramType = parenType->getUnderlyingType();
4712+
}
4713+
47054714
bool allParamsMatch = cs.getType(arg)->isEqual(paramType);
47064715

47074716
// Find the callee declaration.

lib/Sema/CSDiag.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5051,7 +5051,13 @@ static bool diagnoseSingleCandidateFailures(CalleeCandidateInfo &CCI,
50515051
TC.Context.SourceMgr, FnExpr->getEndLoc());
50525052
}
50535053
} else {
5054-
llvm_unreachable("unexpected argument expression type");
5054+
// FIXME: Due to a quirk of CSApply, we can end up without a
5055+
// ParenExpr if the argument has an '@lvalue TupleType'.
5056+
assert((isa<TupleType>(ArgExpr->getType().getPointer()) ||
5057+
isa<ParenType>(ArgExpr->getType().getPointer())) &&
5058+
"unexpected argument expression type");
5059+
insertLoc = ArgExpr->getLoc();
5060+
50555061
// Can't be TupleShuffleExpr because this argExpr is not yet resolved.
50565062
}
50575063

test/Compatibility/tuple_arguments.swift

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ do {
549549
}
550550

551551
struct SubscriptTwo {
552-
subscript(_ x: Int, _ y: Int) -> Int { get { return 0 } set { } } // expected-note 2 {{'subscript' declared here}}
552+
subscript(_ x: Int, _ y: Int) -> Int { get { return 0 } set { } } // expected-note 3 {{'subscript' declared here}}
553553
}
554554

555555
struct SubscriptTuple {
@@ -590,8 +590,7 @@ do {
590590
var s1 = SubscriptTwo()
591591
_ = s1[a, b]
592592
_ = s1[(a, b)] // expected-error {{missing argument for parameter #2 in call}}
593-
594-
// _ = s1[d] // FIXME: Crashes in Swift 3 mode
593+
_ = s1[d] // expected-error {{missing argument for parameter #2 in call}}
595594

596595
var s2 = SubscriptTuple()
597596
_ = s2[a, b] // expected-error {{extra argument in call}}}
@@ -1023,7 +1022,7 @@ do {
10231022
var d = (a, b)
10241023

10251024
var s1 = GenericSubscript<(Double, Double)>()
1026-
// _ = s1[a, b] // FIXME: Crashes in Swift 3 mode
1025+
_ = s1[a, b]
10271026
_ = s1[(a, b)] // expected-error {{expression type '@lvalue Int' is ambiguous without more context}}
10281027
_ = s1[d] // expected-error {{expression type '@lvalue Int' is ambiguous without more context}}
10291028

test/Constraints/tuple_arguments.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,7 @@ do {
534534
}
535535

536536
struct SubscriptTwo {
537-
subscript(_ x: Int, _ y: Int) -> Int { get { return 0 } set { } } // expected-note 4 {{'subscript' declared here}}
537+
subscript(_ x: Int, _ y: Int) -> Int { get { return 0 } set { } } // expected-note 5 {{'subscript' declared here}}
538538
}
539539

540540
struct SubscriptTuple {
@@ -575,9 +575,7 @@ do {
575575
var s1 = SubscriptTwo()
576576
_ = s1[a, b]
577577
_ = s1[(a, b)] // expected-error {{missing argument for parameter #2 in call}}
578-
579-
// FIXME: Crashes in CSDiag
580-
// _ = s1[d]
578+
_ = s1[d] // expected-error {{missing argument for parameter #2 in call}}
581579

582580
var s2 = SubscriptTuple()
583581
_ = s2[a, b] // expected-error {{extra argument in call}}}

0 commit comments

Comments
 (0)