@@ -290,6 +290,24 @@ import SwiftSyntax
290290 LookupName . getNames ( from: member. decl)
291291 }
292292 }
293+
294+ /// Creates a result from associated type declarations
295+ /// made by it's members.
296+ func lookupAssociatedTypeDeclarations(
297+ _ identifier: Identifier ? ,
298+ at lookUpPosition: AbsolutePosition ,
299+ with config: LookupConfig
300+ ) -> [ LookupResult ] {
301+ let filteredNames = members. flatMap { member in
302+ guard member. decl. kind == . associatedTypeDecl else { return [ LookupName] ( ) }
303+
304+ return LookupName . getNames ( from: member. decl)
305+ } . filter { name in
306+ checkIdentifier ( identifier, refersTo: name, at: lookUpPosition)
307+ }
308+
309+ return filteredNames. isEmpty ? [ ] : [ . fromScope( self , withNames: filteredNames) ]
310+ }
293311}
294312
295313@_spi ( Experimental) extension GuardStmtSyntax : IntroducingToSequentialParentScopeSyntax {
@@ -330,10 +348,10 @@ import SwiftSyntax
330348 }
331349}
332350
333- @_spi ( Experimental) extension ActorDeclSyntax : TypeScopeSyntax , WithGenericParametersOrAssociatedTypesScopeSyntax { }
334- @_spi ( Experimental) extension ClassDeclSyntax : TypeScopeSyntax , WithGenericParametersOrAssociatedTypesScopeSyntax { }
335- @_spi ( Experimental) extension StructDeclSyntax : TypeScopeSyntax , WithGenericParametersOrAssociatedTypesScopeSyntax { }
336- @_spi ( Experimental) extension EnumDeclSyntax : TypeScopeSyntax , WithGenericParametersOrAssociatedTypesScopeSyntax { }
351+ @_spi ( Experimental) extension ActorDeclSyntax : TypeScopeSyntax , WithGenericParametersScopeSyntax { }
352+ @_spi ( Experimental) extension ClassDeclSyntax : TypeScopeSyntax , WithGenericParametersScopeSyntax { }
353+ @_spi ( Experimental) extension StructDeclSyntax : TypeScopeSyntax , WithGenericParametersScopeSyntax { }
354+ @_spi ( Experimental) extension EnumDeclSyntax : TypeScopeSyntax , WithGenericParametersScopeSyntax { }
337355@_spi ( Experimental) extension ExtensionDeclSyntax : TypeScopeSyntax { }
338356
339357@_spi ( Experimental) extension AccessorDeclSyntax : ScopeSyntax {
@@ -370,32 +388,61 @@ import SwiftSyntax
370388 }
371389}
372390
373- @_spi ( Experimental) extension GenericParameterClauseSyntax : GenericParameterOrAssociatedTypeScopeSyntax {
374- /// Generic parameter names introduced by this clause .
391+ @_spi ( Experimental) extension ProtocolDeclSyntax : ScopeSyntax {
392+ /// Protocol declarations don't introduce names by themselves .
375393 @_spi ( Experimental) public var introducedNames : [ LookupName ] {
376- parameters. children ( viewMode: . sourceAccurate) . flatMap { child in
377- LookupName . getNames ( from: child, accessibleAfter: child. endPosition)
378- }
394+ [ ]
379395 }
380- }
381396
382- @_spi ( Experimental) extension PrimaryAssociatedTypeClauseSyntax : GenericParameterOrAssociatedTypeScopeSyntax {
383- /// Primary associated type names introduced by this clause.
384- @_spi ( Experimental) public var introducedNames : [ LookupName ] {
385- primaryAssociatedTypes. children ( viewMode: . sourceAccurate) . flatMap { child in
386- LookupName . getNames ( from: child, accessibleAfter: child. endPosition)
397+ /// For the lookup initiated from inside primary
398+ /// associated type clause, this function also finds
399+ /// all associated type declarations made inside the
400+ /// protocol member block.
401+ ///
402+ /// example:
403+ /// ```swift
404+ /// class A {}
405+ ///
406+ /// protocol Foo<A/*<-- lookup here>*/> {
407+ /// associatedtype A
408+ /// class A {}
409+ /// }
410+ /// ```
411+ /// For the lookup started at the primary associated type `A`,
412+ /// the function returns exactly two results. First associated with the member
413+ /// block that consists of the `associatedtype A` declaration and
414+ /// the latter one from the file scope and `class A` exactly in this order.
415+ public func lookup(
416+ _ identifier: Identifier ? ,
417+ at lookUpPosition: AbsolutePosition ,
418+ with config: LookupConfig
419+ ) -> [ LookupResult ] {
420+ var results : [ LookupResult ] = [ ]
421+
422+ if let primaryAssociatedTypeClause,
423+ primaryAssociatedTypeClause. range. contains ( lookUpPosition)
424+ {
425+ results = memberBlock. lookupAssociatedTypeDeclarations (
426+ identifier,
427+ at: lookUpPosition,
428+ with: config
429+ )
387430 }
431+
432+ return results + defaultLookupImplementation( identifier, at: lookUpPosition, with: config)
388433 }
389434}
390435
391- @_spi ( Experimental) extension ProtocolDeclSyntax : WithGenericParametersOrAssociatedTypesScopeSyntax {
392- /// Protocol declarations don't introduce names by themselves .
436+ @_spi ( Experimental) extension GenericParameterClauseSyntax : GenericParameterScopeSyntax {
437+ /// Generic parameter names introduced by this clause .
393438 @_spi ( Experimental) public var introducedNames : [ LookupName ] {
394- [ ]
439+ parameters. children ( viewMode: . sourceAccurate) . flatMap { child in
440+ LookupName . getNames ( from: child, accessibleAfter: child. endPosition)
441+ }
395442 }
396443}
397444
398- @_spi ( Experimental) extension FunctionDeclSyntax : WithGenericParametersOrAssociatedTypesScopeSyntax {
445+ @_spi ( Experimental) extension FunctionDeclSyntax : WithGenericParametersScopeSyntax {
399446 /// Function parameters introduced by this function's signature.
400447 @_spi ( Experimental) public var introducedNames : [ LookupName ] {
401448 signature. parameterClause. parameters. flatMap { parameter in
0 commit comments