Skip to content

Commit ed79bab

Browse files
committed
Implement name lookup into named opaque result types of subscripts
1 parent 791c9c7 commit ed79bab

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

lib/AST/ASTScopeCreation.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -849,6 +849,20 @@ TopLevelCodeScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
849849

850850
// Create child scopes for every declaration in a body.
851851

852+
namespace {
853+
/// Retrieve the opaque generic parameter list if present, otherwise the normal generic parameter list.
854+
template<typename T>
855+
GenericParamList *getPotentiallyOpaqueGenericParams(T *decl) {
856+
if (auto opaqueRepr = decl->getOpaqueResultTypeRepr()) {
857+
if (auto namedOpaque = dyn_cast<NamedOpaqueReturnTypeRepr>(opaqueRepr)) {
858+
return namedOpaque->getGenericParams();
859+
}
860+
}
861+
862+
return decl->getGenericParams();
863+
}
864+
}
865+
852866
void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
853867
ScopeCreator &scopeCreator) {
854868
scopeCreator.addChildrenForKnownAttributes(decl, this);
@@ -859,15 +873,8 @@ void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
859873
ASTScopeImpl *leaf = this;
860874

861875
if (!isa<AccessorDecl>(decl)) {
862-
if (auto opaqueRepr = decl->getOpaqueResultTypeRepr()) {
863-
if (auto namedOpaque = dyn_cast<NamedOpaqueReturnTypeRepr>(opaqueRepr)) {
864-
leaf = scopeCreator.addNestedGenericParamScopesToTree(
865-
decl, namedOpaque->getGenericParams(), leaf);
866-
}
867-
}
868-
869876
leaf = scopeCreator.addNestedGenericParamScopesToTree(
870-
decl, decl->getGenericParams(), leaf);
877+
decl, getPotentiallyOpaqueGenericParams(decl), leaf);
871878

872879
auto *params = decl->getParameters();
873880
if (params->size() > 0) {
@@ -1015,7 +1022,7 @@ void SubscriptDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
10151022
ScopeCreator &scopeCreator) {
10161023
scopeCreator.addChildrenForKnownAttributes(decl, this);
10171024
auto *leaf = scopeCreator.addNestedGenericParamScopesToTree(
1018-
decl, decl->getGenericParams(), this);
1025+
decl, getPotentiallyOpaqueGenericParams(decl), this);
10191026
scopeCreator.constructExpandAndInsert<ParameterListScope>(
10201027
leaf, decl->getIndices(), decl->getAccessor(AccessorKind::Get));
10211028
scopeCreator.addChildrenForParsedAccessors(decl, leaf);

test/type/opaque_return_named.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,20 @@ struct Generic<T: P1 & Equatable> {
106106
func f() -> <U: Q1, V: P1 where U: Equatable, V: Equatable> (U, V) {
107107
return ("hello", value)
108108
}
109+
110+
subscript(index: Int) -> <U: Q1, V: P1 where U: Equatable, V: Equatable> (U, V) {
111+
return ("hello", value)
112+
}
109113
}
110114

111-
func testGeneric(gs: Generic<String>, gi: Generic<Int>) {
115+
func testGeneric(i: Int, gs: Generic<String>, gi: Generic<Int>) {
112116
let gs1 = gs.f()
113117
let gs2 = gs.f()
114118
_ = (gs1 == gs2)
115119

116120
let gi1 = gi.f()
117121
// FIXME: Diagnostic below is correct, but a bit misleading because these are different Us and Vs.
118122
_ = (gs1 == gi1) // expected-error{{binary operator '==' cannot be applied to operands of type '(U, V)' and '(U, V)'}}
123+
124+
_ = gs[i]
119125
}

0 commit comments

Comments
 (0)