Skip to content

Commit 4a219db

Browse files
committed
Eliminate non-generic dependent types in ASD::getValueInterfaceType().
This feels like a hack. rdar://44762116
1 parent 36c0ebf commit 4a219db

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

lib/AST/Decl.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5185,7 +5185,23 @@ Type SubscriptDecl::getElementInterfaceType() const {
51855185
auto elementTy = getInterfaceType();
51865186
if (elementTy->is<ErrorType>())
51875187
return elementTy;
5188-
return elementTy->castTo<AnyFunctionType>()->getResult();
5188+
auto fnTy = elementTy->castTo<AnyFunctionType>();
5189+
auto resultTy = fnTy->getResult();
5190+
5191+
// Make sure we don't return a dependent type if the context is not
5192+
// actually generic. This can only happen with subscripts because we're
5193+
// apparently somewhat eager to leave dependent types as dependent
5194+
// because of generic subscripts.
5195+
if (resultTy->hasTypeParameter()) {
5196+
auto genericFnTy = cast<GenericFunctionType>(fnTy);
5197+
auto sig = genericFnTy->getGenericSignature();
5198+
if (sig->areAllParamsConcrete()) {
5199+
// It's a bit unfortunate to canonicalize here.
5200+
return sig->getCanonicalTypeInContext(resultTy);
5201+
}
5202+
}
5203+
5204+
return resultTy;
51895205
}
51905206

51915207
void SubscriptDecl::computeType() {

test/SILGen/subscript_accessor.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,16 @@ func testXRead<T>(x: X<T>) -> T {
1818
return x[]!
1919
}
2020
// CHECK: $s18subscript_accessor1XVxSgycisTf4dn_n
21+
22+
// Don't crash dealing with T? in a non-generic context.
23+
// rdar://44762116
24+
struct WillBeConcretelyConstrained<T> {}
25+
extension WillBeConcretelyConstrained where T == Int {
26+
subscript(key: Int) -> T? {
27+
get { return nil }
28+
set {}
29+
}
30+
}
31+
32+
// CHECK-LABEL: sil hidden [transparent] @$s18subscript_accessor27WillBeConcretelyConstrainedVAASiRszlEySiSgSiciM
33+
// CHECK-SAME: $@yield_once @convention(method) (Int, @inout WillBeConcretelyConstrained<Int>) -> @yields @inout Optional<Int>

0 commit comments

Comments
 (0)