Skip to content

SILGen: When transforming to AnyHashable materialize the value in a temporary #17928

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,8 +598,10 @@ ManagedValue Transform::transform(ManagedValue v,
KnownProtocolKind::Hashable);
auto conformance = SGF.SGM.M.getSwiftModule()->lookupConformance(
inputSubstType, protocol);
auto result = SGF.emitAnyHashableErasure(Loc, v, inputSubstType,
*conformance, ctxt);
auto addr = v.getType().isAddress() ? v : v.materialize(SGF, Loc);
auto result = SGF.emitAnyHashableErasure(Loc, addr,
inputSubstType, *conformance,
ctxt);
if (result.isInContext())
return ManagedValue::forInContext();
return std::move(result).getAsSingleValue(SGF, Loc);
Expand Down
18 changes: 18 additions & 0 deletions test/SILGen/function_conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -665,3 +665,21 @@ struct FunctionConversionParameterSubstToOrigReabstractionTest {
return Foo<Klass>.enum1Func(SelfTy.bar)
}
}

// CHECK: sil {{.*}} @$SS4SIgggoo_S2Ss11AnyHashableVyps5Error_pIegggrrzo_TR
// CHECK: [[TUPLE:%.*]] = apply %4(%2, %3) : $@noescape @callee_guaranteed (@guaranteed String, @guaranteed String) -> (@owned String, @owned String)
// CHECK: [[BORROW:%.*]] = begin_borrow [[TUPLE]] : $(String, String)
// CHECK: [[FRST:%.*]] = tuple_extract [[BORROW]] : $(String, String), 0
// CHECK: [[COPY1:%.*]] = copy_value [[FRST]] : $String
// CHECK: [[SND:%.*]] = tuple_extract [[BORROW]] : $(String, String), 1
// CHECK: [[COPY2:%.*]] = copy_value [[SND]] : $String
// CHECK: end_borrow [[BORROW]] from [[TUPLE]] : $(String, String), $(String, String)
// CHECK: destroy_value [[TUPLE]] : $(String, String)
// CHECK: [[ADDR:%.*]] = alloc_stack $String
// CHECK: store [[COPY1]] to [init] [[ADDR]] : $*String
// CHECK: [[CVT:%.*]] = function_ref @$Ss21_convertToAnyHashableys0cD0VxSHRzlF : $@convention(thin) <τ_0_0 where τ_0_0 : Hashable> (@in_guaranteed τ_0_0) -> @out AnyHashable
// CHECK: apply [[CVT]]<String>(%0, [[ADDR]])
func dontCrash() {
let userInfo = ["hello": "world"]
let d = [AnyHashable: Any](uniqueKeysWithValues: userInfo.map { ($0.key, $0.value) })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aschwaighofer I would prefer an explicit function conversion test case, like the rest of the file. It's kind of odd that the type checker picks a function conversion here, instead of converting $0.key and $0.value, and if we fix that the test will no longer be testing the problematic code path.

}