Skip to content

Commit 8d3cf17

Browse files
authored
Merge pull request #21519 from slavapestov/subscript-element-canonical
SIL: Canonicalize subscript element type with respect to generic signature when lowering accessor type
2 parents 04ce7ab + b9e0a94 commit 8d3cf17

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

lib/SIL/SILFunctionType.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -865,15 +865,17 @@ static void destructureYieldsForCoroutine(SILModule &M,
865865
.getReferenceStorageReferentType();
866866

867867
auto storage = accessor->getStorage();
868-
auto valueType = storage->getValueInterfaceType()
869-
->getReferenceStorageReferent();
868+
auto valueType = storage->getValueInterfaceType();
870869
if (reqtSubs) {
871870
valueType = valueType.subst(*reqtSubs);
872871
}
873872

873+
auto canValueType = valueType->getCanonicalType(
874+
accessor->getGenericSignature());
875+
874876
// 'modify' yields an inout of the target type.
875877
if (accessor->getAccessorKind() == AccessorKind::Modify) {
876-
auto loweredValueTy = M.Types.getLoweredType(origType, valueType);
878+
auto loweredValueTy = M.Types.getLoweredType(origType, canValueType);
877879
yields.push_back(SILYieldInfo(loweredValueTy.getASTType(),
878880
ParameterConvention::Indirect_Inout));
879881
return;
@@ -882,8 +884,7 @@ static void destructureYieldsForCoroutine(SILModule &M,
882884
// 'read' yields a borrowed value of the target type, destructuring
883885
// tuples as necessary.
884886
assert(accessor->getAccessorKind() == AccessorKind::Read);
885-
destructureYieldsForReadAccessor(M, origType, valueType->getCanonicalType(),
886-
yields);
887+
destructureYieldsForReadAccessor(M, origType, canValueType, yields);
887888
}
888889

889890
/// Create the appropriate SIL function type for the given formal type

test/SILGen/subscript_accessor.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,20 @@ struct X<T> {
1212
}
1313
}
1414

15-
// CHECK: sil{{.*}}s18subscript_accessor9testXRead1xxAA1XVyxG_tlF
15+
// Don't crash dealing with T? in a non-generic context.
16+
// rdar://44762116
17+
struct WillBeConcretelyConstrained<T> {}
18+
extension WillBeConcretelyConstrained where T == Int {
19+
subscript(key: Int) -> T? {
20+
get { return nil }
21+
set {}
22+
}
23+
}
24+
25+
// CHECK-LABEL: sil hidden [transparent] @$s18subscript_accessor27WillBeConcretelyConstrainedVAASiRszlEySiSgSiciM
26+
// CHECK-SAME: $@yield_once @convention(method) (Int, @inout WillBeConcretelyConstrained<Int>) -> @yields @inout Optional<Int>
27+
28+
// CHECK: sil{{.*}}s18subscript_accessor9testXRead1xxAA1XVyxG_tlF
1629
@_specialize(where T == (Int, Int))
1730
func testXRead<T>(x: X<T>) -> T {
1831
return x[]!

0 commit comments

Comments
 (0)