@@ -115,7 +115,7 @@ import SwiftSyntax
115115 switch config. fileScopeHandling {
116116 case . memberBlock:
117117 guard config. includeMembers else { return [ ] }
118-
118+
119119 let names = introducedNames ( using: . memberBlock)
120120 . filter { lookupName in
121121 checkIdentifier ( identifier, refersTo: lookupName, at: lookUpPosition)
@@ -150,7 +150,8 @@ import SwiftSyntax
150150 with: config
151151 )
152152
153- return ( members. isEmpty || !config. includeMembers ? [ ] : [ . fromFileScope( self , withNames: members) ] ) + sequentialNames
153+ return ( members. isEmpty || !config. includeMembers ? [ ] : [ . fromFileScope( self , withNames: members) ] )
154+ + sequentialNames
154155 }
155156 }
156157}
@@ -528,7 +529,7 @@ import SwiftSyntax
528529 @_spi ( Experimental) public var scopeDebugName : String {
529530 " AccessorDeclScope "
530531 }
531-
532+
532533 /// Returns result with matching names from
533534 /// this scope and passes result with implicit `self`
534535 /// to be introduced after the `subscript`
@@ -541,18 +542,24 @@ import SwiftSyntax
541542 guard let parentAccessorBlockScope = parentScope? . as ( AccessorBlockSyntax . self) else {
542543 return defaultLookupImplementation ( identifier, at: lookUpPosition, with: config)
543544 }
544-
545+
546+ let implicitSelf : [ LookupName ] = [ . implicit( . self ( self ) ) ]
547+ . filter { name in
548+ checkIdentifier ( identifier, refersTo: name, at: lookUpPosition)
549+ }
550+
545551 return defaultLookupImplementation (
546552 identifier,
547553 at: lookUpPosition,
548554 with: config,
549555 propagateToParent: false
550- ) + parentAccessorBlockScope. interleaveAccessorResultsAfterSubscriptLookup (
551- identifier,
552- at: lookUpPosition,
553- with: config,
554- resultsToInterleave: [ . fromScope( self , withNames: [ . implicit( . self ( self ) ) ] ) ]
555556 )
557+ + parentAccessorBlockScope. interleaveAccessorResultsAfterSubscriptLookup (
558+ identifier,
559+ at: lookUpPosition,
560+ with: config,
561+ resultsToInterleave: implicitSelf. isEmpty ? [ ] : [ . fromScope( self , withNames: implicitSelf) ]
562+ )
556563 }
557564}
558565
@@ -606,14 +613,18 @@ import SwiftSyntax
606613 with: config,
607614 propagateToParent: false
608615 ) + ( filteredNamesFromLabel. isEmpty ? [ ] : [ . fromScope( self , withNames: filteredNamesFromLabel) ] )
609- + ( config. finishInSequentialScope ? [ ] : lookupInParent ( identifier, at: lookUpPosition, with: config) )
616+ + ( config. finishInSequentialScope ? [ ] : lookupInParent ( identifier, at: lookUpPosition, with: config) )
610617 }
611618}
612619
613- @_spi ( Experimental) extension ProtocolDeclSyntax : ScopeSyntax {
620+ @_spi ( Experimental) extension ProtocolDeclSyntax : ScopeSyntax , LookInMembersScopeSyntax {
614621 /// Protocol declarations don't introduce names by themselves.
615622 @_spi ( Experimental) public var introducedNames : [ LookupName ] {
616- [ ]
623+ [ . implicit( . Self( self ) ) ]
624+ }
625+
626+ @_spi ( Experimental) public var lookupMembersPosition : AbsolutePosition {
627+ name. positionAfterSkippingLeadingTrivia
617628 }
618629
619630 @_spi ( Experimental) public var scopeDebugName : String {
@@ -655,7 +666,13 @@ import SwiftSyntax
655666 )
656667 }
657668
658- return results + defaultLookupImplementation( identifier, at: lookUpPosition, with: config)
669+ return results
670+ + defaultLookupImplementation(
671+ identifier,
672+ at: lookUpPosition,
673+ with: config,
674+ propagateToParent: false
675+ ) + [ . lookInMembers( self ) ] + lookupInParent( identifier, at: lookUpPosition, with: config)
659676 }
660677}
661678
@@ -672,42 +689,15 @@ import SwiftSyntax
672689 }
673690}
674691
675- @_spi ( Experimental) extension FunctionDeclSyntax : WithGenericParametersScopeSyntax {
676- /// Function parameters introduced by this function's signature.
677- @_spi ( Experimental) public var introducedNames : [ LookupName ] {
678- signature. parameterClause. parameters. flatMap { parameter in
679- LookupName . getNames ( from: parameter)
680- } + ( parentScope? . is ( MemberBlockSyntax . self) ?? false ? [ . implicit( . self ( self ) ) ] : [ ] )
681- }
682-
692+ @_spi ( Experimental) extension FunctionDeclSyntax : FunctionScopeSyntax {
683693 @_spi ( Experimental) public var scopeDebugName : String {
684694 " FunctionDeclScope "
685695 }
696+ }
686697
687- /// Lookup results from this function scope.
688- /// Routes to generic parameter clause scope if exists.
689- @_spi ( Experimental) public func lookup(
690- _ identifier: Identifier ? ,
691- at lookUpPosition: AbsolutePosition ,
692- with config: LookupConfig
693- ) -> [ LookupResult ] {
694- var thisScopeResults : [ LookupResult ] = [ ]
695-
696- if !signature. range. contains ( lookUpPosition) {
697- thisScopeResults = defaultLookupImplementation (
698- identifier,
699- at: position,
700- with: config,
701- propagateToParent: false
702- )
703- }
704-
705- return thisScopeResults
706- + lookupThroughGenericParameterScope(
707- identifier,
708- at: lookUpPosition,
709- with: config
710- )
698+ @_spi ( Experimental) extension InitializerDeclSyntax : FunctionScopeSyntax {
699+ @_spi ( Experimental) public var scopeDebugName : String {
700+ " InitializerDeclScope "
711701 }
712702}
713703
@@ -743,7 +733,7 @@ import SwiftSyntax
743733 resultsToInterleave: [ ]
744734 )
745735 }
746-
736+
747737 /// Lookup names in this scope and add `resultsToInterleave`
748738 /// after results from this scope.
749739 ///
@@ -822,7 +812,7 @@ import SwiftSyntax
822812 return lookupInParent ( identifier, at: lookUpPosition, with: config)
823813 }
824814 }
825-
815+
826816 /// Used by children accessors to interleave
827817 /// their results with parent `subscript` declaration scope.
828818 func interleaveAccessorResultsAfterSubscriptLookup(
@@ -834,7 +824,7 @@ import SwiftSyntax
834824 guard let parentSubscriptScope = parentScope? . as ( SubscriptDeclSyntax . self) else {
835825 return lookupInParent ( identifier, at: lookUpPosition, with: config)
836826 }
837-
827+
838828 return parentSubscriptScope. interleaveResultsAfterThisSubscriptLookup (
839829 identifier,
840830 at: lookUpPosition,
@@ -854,3 +844,39 @@ import SwiftSyntax
854844 " TypeAliasDeclScope "
855845 }
856846}
847+
848+ @_spi ( Experimental) extension VariableDeclSyntax : ScopeSyntax {
849+ /// Variable decl scope doesn't introduce any
850+ /// names unless it is a member and is looked
851+ /// up from inside it's accessor block.
852+ @_spi ( Experimental) public var introducedNames : [ LookupName ] {
853+ [ ]
854+ }
855+
856+ @_spi ( Experimental) public var scopeDebugName : String {
857+ " VariableDeclScope "
858+ }
859+
860+ /// If a member and looked up from inside
861+ /// it's accessor block, introduce implicit
862+ /// `self` and propagate the lookup further.
863+ @_spi ( Experimental) public func lookup(
864+ _ identifier: Identifier ? ,
865+ at lookUpPosition: AbsolutePosition ,
866+ with config: LookupConfig
867+ ) -> [ LookupResult ] {
868+ if let parentScope,
869+ parentScope. is ( MemberBlockSyntax . self) ,
870+ bindings. first? . accessorBlock? . range. contains ( lookUpPosition) ?? false
871+ {
872+ return defaultLookupImplementation (
873+ in: [ . implicit( . self ( self ) ) ] ,
874+ identifier,
875+ at: lookUpPosition,
876+ with: config
877+ )
878+ } else {
879+ return lookupInParent ( identifier, at: lookUpPosition, with: config)
880+ }
881+ }
882+ }
0 commit comments