Skip to content

Commit b273ba2

Browse files
authored
Merge pull request #29614 from xedin/rdar-58972627
[TypeChecker/BuilderTransform] Make sure implicit load expression upd…
2 parents aa5eb87 + 7453d53 commit b273ba2

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

lib/Sema/BuilderTransform.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,9 +693,9 @@ class BuilderClosureRewriter
693693
declRef->setType(LValueType::get(temporaryVar->getType()));
694694

695695
// Load the right-hand side if needed.
696-
if (finalCapturedExpr->getType()->is<LValueType>()) {
697-
auto &cs = solution.getConstraintSystem();
698-
finalCapturedExpr = cs.addImplicitLoadExpr(finalCapturedExpr);
696+
if (finalCapturedExpr->getType()->hasLValueType()) {
697+
finalCapturedExpr =
698+
TypeChecker::addImplicitLoadExpr(ctx, finalCapturedExpr);
699699
}
700700

701701
auto assign = new (ctx) AssignExpr(
@@ -839,9 +839,8 @@ class BuilderClosureRewriter
839839
auto finalCondExpr = rewriteExpr(condExpr);
840840

841841
// Load the condition if needed.
842-
if (finalCondExpr->getType()->is<LValueType>()) {
843-
auto &cs = solution.getConstraintSystem();
844-
finalCondExpr = cs.addImplicitLoadExpr(finalCondExpr);
842+
if (finalCondExpr->getType()->hasLValueType()) {
843+
finalCondExpr = TypeChecker::addImplicitLoadExpr(ctx, finalCondExpr);
845844
}
846845

847846
condElement.setBoolean(finalCondExpr);

lib/Sema/TypeChecker.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,10 +1074,13 @@ class TypeChecker final {
10741074
/// more complicated than simplify wrapping given root in newly created
10751075
/// `LoadExpr`, because `ForceValueExpr` and `ParenExpr` supposed to appear
10761076
/// only at certain positions in AST.
1077-
static Expr *
1078-
addImplicitLoadExpr(ASTContext &Context, Expr *expr,
1079-
std::function<Type(Expr *)> getType,
1080-
std::function<void(Expr *, Type)> setType);
1077+
static Expr *addImplicitLoadExpr(ASTContext &Context, Expr *expr,
1078+
std::function<Type(Expr *)> getType =
1079+
[](Expr *E) { return E->getType(); },
1080+
std::function<void(Expr *, Type)> setType =
1081+
[](Expr *E, Type type) {
1082+
E->setType(type);
1083+
});
10811084

10821085
/// Determine whether the given type contains the given protocol.
10831086
///
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// RUN: %target-swift-frontend %s -target x86_64-apple-macosx10.15 -emit-sil -verify
2+
// REQUIRES: objc_interop
3+
// REQUIRES: OS=macosx
4+
5+
import SwiftUI
6+
import Combine
7+
8+
struct A: View {
9+
var body: some View {
10+
Spacer()
11+
}
12+
}
13+
14+
struct B: View {
15+
var body: some View {
16+
Spacer()
17+
}
18+
}
19+
20+
class Context: ObservableObject {
21+
@State var flag: Bool
22+
23+
init() {
24+
self.flag = false
25+
}
26+
}
27+
28+
struct S : View {
29+
@EnvironmentObject var context: Context
30+
31+
var body: some View {
32+
VStack {
33+
if (context.flag) { // Ok (shouldn't trip SIL Verifier)
34+
A()
35+
} else {
36+
B()
37+
}
38+
}
39+
}
40+
}

0 commit comments

Comments
 (0)