Skip to content

Commit 999f2b0

Browse files
committed
[OpaqueValues] AnyHashable src gets new context.
When storing an instance of some type that conforms to Hashable into an lvalue of type AnyHashable, the source rvalue needs to be emitted within a new SGFContext. Otherwise, the LocalVarInitialization for the var of type AnyHashable would be used and an attempt would be made to store the instance of the conforming type into the projected box.
1 parent 1791108 commit 999f2b0

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

lib/SILGen/SILGenExpr.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2059,10 +2059,10 @@ RValue RValueEmitter::visitAnyHashableErasureExpr(AnyHashableErasureExpr *E,
20592059
auto sourceOrigType = AbstractionPattern::getOpaque();
20602060
auto subExpr = E->getSubExpr();
20612061
auto &sourceOrigTL = SGF.getTypeLowering(sourceOrigType, subExpr->getType());
2062-
auto source =
2063-
SGF.silConv.useLoweredAddresses()
2064-
? SGF.emitMaterializedRValueAsOrig(subExpr, sourceOrigType)
2065-
: SGF.emitRValueAsOrig(subExpr, sourceOrigType, sourceOrigTL, C);
2062+
auto source = SGF.silConv.useLoweredAddresses()
2063+
? SGF.emitMaterializedRValueAsOrig(subExpr, sourceOrigType)
2064+
: SGF.emitRValueAsOrig(subExpr, sourceOrigType,
2065+
sourceOrigTL, SGFContext());
20662066

20672067
return SGF.emitAnyHashableErasure(E, source, subExpr->getType(),
20682068
E->getConformance(), C);

test/SILGen/opaque_values_silgen.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,3 +760,25 @@ func callMutatingFooOnInoutExistential(_ i: inout any MutatingFooable) {
760760
struct WeakBox<T : AnyObject> {
761761
weak var t: T?
762762
}
763+
764+
// CHECK-LABEL: sil {{.*}}[ossa] @intIntoAnyHashableVar : {{.*}} {
765+
// CHECK: [[VAR_BOX_ADDR:%[^,]+]] = project_box
766+
// CHECK: [[INT:%[^,]+]] = apply
767+
// CHECK: [[CONVERT:%[^,]+]] = function_ref @$ss21_convertToAnyHashableys0cD0VxSHRzlF
768+
// CHECK: [[INSTANCE:%[^,]+]] = apply [[CONVERT]]<Int>([[INT]])
769+
// CHECK: store [[INSTANCE]] to [init] [[VAR_BOX_ADDR]]
770+
// CHECK-LABEL: } // end sil function 'intIntoAnyHashableVar'
771+
@_silgen_name("intIntoAnyHashableVar")
772+
func intIntoAnyHashableVar() {
773+
var anyHashable: AnyHashable = 0
774+
}
775+
776+
// CHECK-LABEL: sil {{.*}}[ossa] @intIntoAnyHashableLet : {{.*}} {
777+
// CHECK: [[INT:%[^,]+]] = apply
778+
// CHECK: [[CONVERT:%[^,]+]] = function_ref @$ss21_convertToAnyHashableys0cD0VxSHRzlF
779+
// CHECK: [[INSTANCE:%[^,]+]] = apply [[CONVERT]]<Int>([[INT]])
780+
// CHECK-LABEL: } // end sil function 'intIntoAnyHashableLet'
781+
@_silgen_name("intIntoAnyHashableLet")
782+
func intIntoAnyHashableLet() {
783+
let anyHashable: AnyHashable = 0
784+
}

0 commit comments

Comments
 (0)