Skip to content

Commit ebdef55

Browse files
slavapestovrjmccall
authored andcommitted
SILGen: Use emitOpenExistentialLValue() when lowering OpaqueValueExprs as lvalues
This is part of allowing lvalue access on class existential payloads.
1 parent 4aa2878 commit ebdef55

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

lib/SILGen/SILGenLValue.cpp

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -591,10 +591,26 @@ namespace {
591591
"base for open existential component must be an existential");
592592
assert(base.getType().isAddress() &&
593593
"base value of open-existential component was not an address?");
594+
SILValue addr;
594595

595-
SILValue addr = SGF.B.createOpenExistentialAddr(
596+
auto rep = base.getType().getPreferredExistentialRepresentation(SGF.SGM.M);
597+
switch (rep) {
598+
case ExistentialRepresentation::Opaque:
599+
addr = SGF.B.createOpenExistentialAddr(
596600
loc, base.getValue(), getTypeOfRValue().getAddressType(),
597601
getOpenedExistentialAccessFor(accessKind));
602+
break;
603+
case ExistentialRepresentation::Boxed: {
604+
auto &TL = SGF.getTypeLowering(base.getType());
605+
auto error = SGF.emitLoad(loc, base.getValue(), TL,
606+
SGFContext(), IsNotTake);
607+
addr = SGF.B.createOpenExistentialBox(
608+
loc, error.getValue(), getTypeOfRValue().getAddressType());
609+
break;
610+
}
611+
default:
612+
llvm_unreachable("Bad existential representation for address-only type");
613+
}
598614

599615
SGF.setArchetypeOpeningSite(cast<ArchetypeType>(getSubstFormalType()),
600616
addr);
@@ -1999,9 +2015,11 @@ LValue SILGenLValue::visitOpaqueValueExpr(OpaqueValueExpr *e,
19992015
openedExistentials.erase(known);
20002016

20012017
// Do formal evaluation of the underlying existential lvalue.
2002-
LValue lv = visitRec(opened->getExistentialValue(), accessKind);
2003-
lv.add<OpenOpaqueExistentialComponent>(
2004-
cast<ArchetypeType>(opened->getOpenedArchetype()->getCanonicalType()));
2018+
auto lv = visitRec(opened->getExistentialValue(), accessKind);
2019+
lv = SGF.emitOpenExistentialLValue(
2020+
opened, std::move(lv),
2021+
CanArchetypeType(opened->getOpenedArchetype()),
2022+
accessKind);
20052023
return lv;
20062024
}
20072025

@@ -2907,7 +2925,8 @@ SILGenFunction::emitOpenExistentialLValue(SILLocation loc,
29072925
auto rep = lv.getTypeOfRValue()
29082926
.getPreferredExistentialRepresentation(SGM.M);
29092927
switch (rep) {
2910-
case ExistentialRepresentation::Opaque: {
2928+
case ExistentialRepresentation::Opaque:
2929+
case ExistentialRepresentation::Boxed: {
29112930
lv.add<OpenOpaqueExistentialComponent>(openedArchetype);
29122931
break;
29132932
}

0 commit comments

Comments
 (0)