Skip to content

SILGen: Relax assertion in tuple-to-optional function result conversion #7018

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
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
1 change: 0 additions & 1 deletion lib/SILGen/SILGenPoly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1918,7 +1918,6 @@ ResultPlanner::planTupleIntoDirectResult(AbstractionPattern innerOrigType,
PlanData &planData,
SILResultInfo outerResult) {
assert(innerOrigType.isTuple());
assert(!outerOrigType.isTuple());

CanTupleType outerSubstTupleType = dyn_cast<TupleType>(outerSubstType);

Expand Down
42 changes: 42 additions & 0 deletions test/SILGen/function_conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,48 @@ func convTupleScalarOpaque<T>(_ f: @escaping (T...) -> ()) -> ((_ args: T...) ->
return f
}

// CHECK-LABEL: sil hidden @_TF19function_conversion25convTupleToOptionalDirectFFSiTSiSi_FSiGSqTSiSi__ : $@convention(thin) (@owned @callee_owned (Int) -> (Int, Int)) -> @owned @callee_owned (Int) -> Optional<(Int, Int)>
// CHECK: bb0(%0 : $@callee_owned (Int) -> (Int, Int)):
// CHECK: [[FN:%.*]] = copy_value %0
// CHECK: [[THUNK_FN:%.*]] = function_ref @_TTRXFo_dSi_dSidSi_XFo_dSi_dGSqTSiSi___
// CHECK-NEXT: [[THUNK:%.*]] = partial_apply [[THUNK_FN]]([[FN]])
// CHECK-NEXT: destroy_value %0
// CHECK-NEXT: return [[THUNK]]

// CHECK-LABEL: sil shared [transparent] [reabstraction_thunk] @_TTRXFo_dSi_dSidSi_XFo_dSi_dGSqTSiSi___ : $@convention(thin) (Int, @owned @callee_owned (Int) -> (Int, Int)) -> Optional<(Int, Int)>
// CHECK: bb0(%0 : $Int, %1 : $@callee_owned (Int) -> (Int, Int)):
// CHECK: [[RESULT:%.*]] = apply %1(%0)
// CHECK-NEXT: [[LEFT:%.*]] = tuple_extract [[RESULT]]
// CHECK-NEXT: [[RIGHT:%.*]] = tuple_extract [[RESULT]]
// CHECK-NEXT: [[RESULT:%.*]] = tuple ([[LEFT]] : $Int, [[RIGHT]] : $Int)
// CHECK-NEXT: [[OPTIONAL:%.*]] = enum $Optional<(Int, Int)>, #Optional.some!enumelt.1, [[RESULT]]
// CHECK-NEXT: return [[OPTIONAL]]

func convTupleToOptionalDirect(_ f: @escaping (Int) -> (Int, Int)) -> (Int) -> (Int, Int)? {
return f
}

// CHECK-LABEL: sil hidden @_TF19function_conversion27convTupleToOptionalIndirecturFFxTxx_FxGSqTxx__ : $@convention(thin) <T> (@owned @callee_owned (@in T) -> (@out T, @out T)) -> @owned @callee_owned (@in T) -> @out Optional<(T, T)>
// CHECK: bb0(%0 : $@callee_owned (@in T) -> (@out T, @out T)):
// CHECK: [[FN:%.*]] = copy_value %0
// CHECK: [[THUNK_FN:%.*]] = function_ref @_TTRGrXFo_ix_ixix_XFo_ix_iGSqTxx___
// CHECK-NEXT: [[THUNK:%.*]] = partial_apply [[THUNK_FN]]<T>([[FN]])
// CHECK-NEXT: destroy_value %0
// CHECK-NEXT: return [[THUNK]]

// CHECK: sil shared [transparent] [reabstraction_thunk] @_TTRGrXFo_ix_ixix_XFo_ix_iGSqTxx___ : $@convention(thin) <T> (@in T, @owned @callee_owned (@in T) -> (@out T, @out T)) -> @out Optional<(T, T)>
// CHECK: bb0(%0 : $*Optional<(T, T)>, %1 : $*T, %2 : $@callee_owned (@in T) -> (@out T, @out T)):
// CHECK: [[OPTIONAL:%.*]] = init_enum_data_addr %0 : $*Optional<(T, T)>, #Optional.some!enumelt.1
// CHECK-NEXT: [[LEFT:%.*]] = tuple_element_addr %3 : $*(T, T), 0
// CHECK-NEXT: [[RIGHT:%.*]] = tuple_element_addr %3 : $*(T, T), 1
// CHECK-NEXT: apply %2([[LEFT]], [[RIGHT]], %1)
// CHECK-NEXT: inject_enum_addr %0 : $*Optional<(T, T)>, #Optional.some!enumelt.1
// CHECK: return

func convTupleToOptionalIndirect<T>(_ f: @escaping (T) -> (T, T)) -> (T) -> (T, T)? {
return f
}

// ==== Make sure we support AnyHashable erasure

// CHECK-LABEL: sil hidden @_TF19function_conversion15convAnyHashableuRxs8HashablerFT1tx_T_
Expand Down
6 changes: 6 additions & 0 deletions validation-test/compiler_crashers_2_fixed/0065-sr3706.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// RUN: %target-swift-frontend %s -emit-ir

let occurrences: [String] = []
let results: [(String, Int)] = occurrences.flatMap({ (match: String) -> (String, Int) in
return ("", 0)
})