Skip to content

Eliminate non-generic dependent types in ASD::getValueInterfaceType() #21518

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

Closed
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
18 changes: 17 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5185,7 +5185,23 @@ Type SubscriptDecl::getElementInterfaceType() const {
auto elementTy = getInterfaceType();
if (elementTy->is<ErrorType>())
return elementTy;
return elementTy->castTo<AnyFunctionType>()->getResult();
auto fnTy = elementTy->castTo<AnyFunctionType>();
auto resultTy = fnTy->getResult();

// Make sure we don't return a dependent type if the context is not
// actually generic. This can only happen with subscripts because we're
// apparently somewhat eager to leave dependent types as dependent
// because of generic subscripts.
if (resultTy->hasTypeParameter()) {
auto genericFnTy = cast<GenericFunctionType>(fnTy);
auto sig = genericFnTy->getGenericSignature();
if (sig->areAllParamsConcrete()) {
// It's a bit unfortunate to canonicalize here.
return sig->getCanonicalTypeInContext(resultTy);
}
}

return resultTy;
}

void SubscriptDecl::computeType() {
Expand Down
13 changes: 13 additions & 0 deletions test/SILGen/subscript_accessor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@ func testXRead<T>(x: X<T>) -> T {
return x[]!
}
// CHECK: $s18subscript_accessor1XVxSgycisTf4dn_n

// Don't crash dealing with T? in a non-generic context.
// rdar://44762116
struct WillBeConcretelyConstrained<T> {}
extension WillBeConcretelyConstrained where T == Int {
subscript(key: Int) -> T? {
get { return nil }
set {}
}
}

// CHECK-LABEL: sil hidden [transparent] @$s18subscript_accessor27WillBeConcretelyConstrainedVAASiRszlEySiSgSiciM
// CHECK-SAME: $@yield_once @convention(method) (Int, @inout WillBeConcretelyConstrained<Int>) -> @yields @inout Optional<Int>