@@ -708,6 +708,26 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
708708 // Initializers come before VarDecls, e.g. PCMacro/didSet.swift 19
709709 auto patternEntry = getPatternEntry ();
710710
711+ // If the pattern type is for a named opaque result type, introduce the
712+ // generic type parameters based on the first variable we find.
713+ ASTScopeImpl *leaf = this ;
714+ auto pattern = patternEntry.getPattern ();
715+ if (auto typedPattern = dyn_cast<TypedPattern>(pattern)) {
716+ if (auto namedOpaque =
717+ dyn_cast_or_null<NamedOpaqueReturnTypeRepr>(
718+ typedPattern->getTypeRepr ())) {
719+ bool addedOpaqueResultTypeScope = false ;
720+ pattern->forEachVariable ([&](VarDecl *var) {
721+ if (addedOpaqueResultTypeScope)
722+ return ;
723+
724+ leaf = scopeCreator.addNestedGenericParamScopesToTree (
725+ var, namedOpaque->getGenericParams (), leaf);
726+ addedOpaqueResultTypeScope = true ;
727+ });
728+ }
729+ }
730+
711731 // Create a child for the initializer, if present.
712732 // Cannot trust the source range given in the ASTScopeImpl for the end of the
713733 // initializer (because of InterpolatedLiteralStrings and EditorPlaceHolders),
@@ -724,7 +744,7 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
724744 " Original inits are always after the '='" );
725745 scopeCreator
726746 .constructExpandAndInsert <PatternEntryInitializerScope>(
727- this , decl, patternEntryIndex);
747+ leaf , decl, patternEntryIndex);
728748 }
729749
730750 // If this pattern binding entry was created by the debugger, it will always
@@ -741,12 +761,12 @@ PatternEntryDeclScope::expandAScopeThatCreatesANewInsertionPoint(
741761 " inits are always after the '='" );
742762 scopeCreator
743763 .constructExpandAndInsert <PatternEntryInitializerScope>(
744- this , decl, patternEntryIndex);
764+ leaf , decl, patternEntryIndex);
745765 }
746766
747767 // Add accessors for the variables in this pattern.
748- patternEntry. getPattern () ->forEachVariable ([&](VarDecl *var) {
749- scopeCreator.addChildrenForParsedAccessors (var, this );
768+ pattern ->forEachVariable ([&](VarDecl *var) {
769+ scopeCreator.addChildrenForParsedAccessors (var, leaf );
750770 });
751771
752772 // In local context, the PatternEntryDeclScope becomes the insertion point, so
@@ -849,6 +869,20 @@ TopLevelCodeScope::expandAScopeThatCreatesANewInsertionPoint(ScopeCreator &
849869
850870// Create child scopes for every declaration in a body.
851871
872+ namespace {
873+ // / Retrieve the opaque generic parameter list if present, otherwise the normal generic parameter list.
874+ template <typename T>
875+ GenericParamList *getPotentiallyOpaqueGenericParams (T *decl) {
876+ if (auto opaqueRepr = decl->getOpaqueResultTypeRepr ()) {
877+ if (auto namedOpaque = dyn_cast<NamedOpaqueReturnTypeRepr>(opaqueRepr)) {
878+ return namedOpaque->getGenericParams ();
879+ }
880+ }
881+
882+ return decl->getGenericParams ();
883+ }
884+ }
885+
852886void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint (
853887 ScopeCreator &scopeCreator) {
854888 scopeCreator.addChildrenForKnownAttributes (decl, this );
@@ -860,7 +894,7 @@ void AbstractFunctionDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
860894
861895 if (!isa<AccessorDecl>(decl)) {
862896 leaf = scopeCreator.addNestedGenericParamScopesToTree (
863- decl, decl-> getGenericParams ( ), leaf);
897+ decl, getPotentiallyOpaqueGenericParams (decl ), leaf);
864898
865899 auto *params = decl->getParameters ();
866900 if (params->size () > 0 ) {
@@ -1008,7 +1042,7 @@ void SubscriptDeclScope::expandAScopeThatDoesNotCreateANewInsertionPoint(
10081042 ScopeCreator &scopeCreator) {
10091043 scopeCreator.addChildrenForKnownAttributes (decl, this );
10101044 auto *leaf = scopeCreator.addNestedGenericParamScopesToTree (
1011- decl, decl-> getGenericParams ( ), this );
1045+ decl, getPotentiallyOpaqueGenericParams (decl ), this );
10121046 scopeCreator.constructExpandAndInsert <ParameterListScope>(
10131047 leaf, decl->getIndices (), decl->getAccessor (AccessorKind::Get));
10141048 scopeCreator.addChildrenForParsedAccessors (decl, leaf);
0 commit comments