@@ -587,12 +587,26 @@ namespace {
587
587
" base for open existential component must be an existential" );
588
588
assert (base.getType ().isAddress () &&
589
589
" base value of open-existential component was not an address?" );
590
- assert (base.getType ().getPreferredExistentialRepresentation (SGF.SGM .M )
591
- == ExistentialRepresentation::Opaque);
590
+ SILValue addr;
592
591
593
- SILValue addr = SGF.B .createOpenExistentialAddr (
592
+ auto rep = base.getType ().getPreferredExistentialRepresentation (SGF.SGM .M );
593
+ switch (rep) {
594
+ case ExistentialRepresentation::Opaque:
595
+ addr = SGF.B .createOpenExistentialAddr (
594
596
loc, base.getValue (), getTypeOfRValue ().getAddressType (),
595
597
getOpenedExistentialAccessFor (accessKind));
598
+ break ;
599
+ case ExistentialRepresentation::Boxed: {
600
+ auto &TL = SGF.getTypeLowering (base.getType ());
601
+ auto error = SGF.emitLoad (loc, base.getValue (), TL,
602
+ SGFContext (), IsNotTake);
603
+ addr = SGF.B .createOpenExistentialBox (
604
+ loc, error.getValue (), getTypeOfRValue ().getAddressType ());
605
+ break ;
606
+ }
607
+ default :
608
+ llvm_unreachable (" Bad existential representation for address-only type" );
609
+ }
596
610
597
611
SGF.setArchetypeOpeningSite (cast<ArchetypeType>(getSubstFormalType ()),
598
612
addr);
@@ -1997,9 +2011,11 @@ LValue SILGenLValue::visitOpaqueValueExpr(OpaqueValueExpr *e,
1997
2011
SGF.OpaqueValueExprs .erase (known);
1998
2012
1999
2013
// Do formal evaluation of the underlying existential lvalue.
2000
- LValue lv = visitRec (opened->getExistentialValue (), accessKind);
2001
- lv.add <OpenOpaqueExistentialComponent>(
2002
- cast<ArchetypeType>(opened->getOpenedArchetype ()->getCanonicalType ()));
2014
+ auto lv = visitRec (opened->getExistentialValue (), accessKind);
2015
+ lv = SGF.emitOpenExistentialLValue (
2016
+ opened, std::move (lv),
2017
+ CanArchetypeType (opened->getOpenedArchetype ()),
2018
+ accessKind);
2003
2019
return lv;
2004
2020
}
2005
2021
@@ -2909,7 +2925,8 @@ SILGenFunction::emitOpenExistentialLValue(SILLocation loc,
2909
2925
auto rep = lv.getTypeOfRValue ()
2910
2926
.getPreferredExistentialRepresentation (SGM.M );
2911
2927
switch (rep) {
2912
- case ExistentialRepresentation::Opaque: {
2928
+ case ExistentialRepresentation::Opaque:
2929
+ case ExistentialRepresentation::Boxed: {
2913
2930
lv.add <OpenOpaqueExistentialComponent>(openedArchetype);
2914
2931
break ;
2915
2932
}
0 commit comments