Skip to content

Commit d5fd738

Browse files
Merge pull request #17929 from aschwaighofer/silgen_emit_any_hashable_erasure_4.2
[4.2] SILGen: When transforming to AnyHashable materialize the value in a temporary
2 parents e4ddba2 + bd64ea6 commit d5fd738

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/SILGen/SILGenPoly.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -599,8 +599,10 @@ ManagedValue Transform::transform(ManagedValue v,
599599
KnownProtocolKind::Hashable);
600600
auto conformance = SGF.SGM.M.getSwiftModule()->lookupConformance(
601601
inputSubstType, protocol);
602-
auto result = SGF.emitAnyHashableErasure(Loc, v, inputSubstType,
603-
*conformance, ctxt);
602+
auto addr = v.getType().isAddress() ? v : v.materialize(SGF, Loc);
603+
auto result = SGF.emitAnyHashableErasure(Loc, addr,
604+
inputSubstType, *conformance,
605+
ctxt);
604606
if (result.isInContext())
605607
return ManagedValue::forInContext();
606608
return std::move(result).getAsSingleValue(SGF, Loc);

test/SILGen/function_conversion.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,3 +665,21 @@ struct FunctionConversionParameterSubstToOrigReabstractionTest {
665665
return Foo<Klass>.enum1Func(SelfTy.bar)
666666
}
667667
}
668+
669+
// CHECK: sil {{.*}} @$SS4SIgggoo_S2Ss11AnyHashableVyps5Error_pIegggrrzo_TR
670+
// CHECK: [[TUPLE:%.*]] = apply %4(%2, %3) : $@noescape @callee_guaranteed (@guaranteed String, @guaranteed String) -> (@owned String, @owned String)
671+
// CHECK: [[BORROW:%.*]] = begin_borrow [[TUPLE]] : $(String, String)
672+
// CHECK: [[FRST:%.*]] = tuple_extract [[BORROW]] : $(String, String), 0
673+
// CHECK: [[COPY1:%.*]] = copy_value [[FRST]] : $String
674+
// CHECK: [[SND:%.*]] = tuple_extract [[BORROW]] : $(String, String), 1
675+
// CHECK: [[COPY2:%.*]] = copy_value [[SND]] : $String
676+
// CHECK: end_borrow [[BORROW]] from [[TUPLE]] : $(String, String), $(String, String)
677+
// CHECK: destroy_value [[TUPLE]] : $(String, String)
678+
// CHECK: [[ADDR:%.*]] = alloc_stack $String
679+
// CHECK: store [[COPY1]] to [init] [[ADDR]] : $*String
680+
// CHECK: [[CVT:%.*]] = function_ref @$Ss21_convertToAnyHashableys0cD0VxSHRzlF : $@convention(thin) <τ_0_0 where τ_0_0 : Hashable> (@in_guaranteed τ_0_0) -> @out AnyHashable
681+
// CHECK: apply [[CVT]]<String>(%0, [[ADDR]])
682+
func dontCrash() {
683+
let userInfo = ["hello": "world"]
684+
let d = [AnyHashable: Any](uniqueKeysWithValues: userInfo.map { ($0.key, $0.value) })
685+
}

0 commit comments

Comments
 (0)