Skip to content

Commit c468ca1

Browse files
authored
Handle stray LoadExprs in RawRepresentable fix-its (#17804)
When there are multiple possible overloads for a call, the partially- type-checked argument expression might end up with a LoadExpr outside of the call ParenExpr instead of inside it. Account for this in a one- off way for the 'rawValue' / 'init(rawValue:)' fix-its. Yet more https://bugs.swift.org/browse/SR-8150
1 parent d21eedb commit c468ca1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4608,8 +4608,14 @@ static bool diagnoseRawRepresentableMismatch(CalleeCandidateInfo &CCI,
46084608
return false;
46094609

46104610
const Expr *expr = argExpr;
4611-
if (auto *tupleArgs = dyn_cast<TupleExpr>(argExpr))
4611+
if (auto *tupleArgs = dyn_cast<TupleExpr>(argExpr)) {
46124612
expr = tupleArgs->getElement(bestMatchIndex);
4613+
} else if (auto *misplacedLoad = dyn_cast<LoadExpr>(argExpr)) {
4614+
// If there are multiple possible overloads for a single-argument call
4615+
// expression, the partially-typed-checked AST may have a load around the
4616+
// call parentheses instead of inside them.
4617+
expr = misplacedLoad->getSubExpr();
4618+
}
46134619
expr = expr->getValueProvidingExpr();
46144620

46154621
auto parameters = bestMatchCandidate->getParameters();

test/Sema/enum_raw_representable.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,19 @@ func sr8150(bar: Bar) {
168168
// expected-error@-1 {{cannot convert value of type 'Double' to expected argument type 'Bar'}} {{18-18=Bar(rawValue: }} {{21-21=)}}
169169
}
170170

171+
class SR8150Box {
172+
var bar: Bar
173+
init(bar: Bar) { self.bar = bar }
174+
}
175+
// Bonus problem with mutable values being passed.
176+
func sr8150_mutable(obj: SR8150Box) {
177+
sr8150_helper1(obj.bar)
178+
// expected-error@-1 {{cannot convert value of type 'Bar' to expected argument type 'Double'}} {{18-18=}} {{25-25=.rawValue}}
179+
180+
var bar = obj.bar
181+
sr8150_helper1(bar)
182+
// expected-error@-1 {{cannot convert value of type 'Bar' to expected argument type 'Double'}} {{18-18=}} {{21-21=.rawValue}}
183+
}
171184

172185
struct NotEquatable { }
173186

0 commit comments

Comments
 (0)