-
Notifications
You must be signed in to change notification settings - Fork 439
[SwiftLexicalLookup][GSoC] Add switch, generic parameter and function scopes. #2782
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
Merged
DougGregor
merged 7 commits into
swiftlang:main
from
MAJKFL:generic-parameter-list-function-scope
Sep 11, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bbc9b6a
Add switch, generic parameter and function scopes.
MAJKFL d8cb895
Merge remote-tracking branch 'upstream/main' into generic-parameter-l…
MAJKFL adf11eb
Fix merge artifacts.
MAJKFL 4cac8a8
Fix primary associated type handling.
MAJKFL c05403a
Add CMakeLists to SwiftLexicalLookup.
MAJKFL 57183fc
Add suggested changes.
MAJKFL 5e8c6d4
Fix typo in a test case.
MAJKFL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# This source file is part of the Swift.org open source project | ||
# | ||
# Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
# Licensed under Apache License v2.0 with Runtime Library Exception | ||
# | ||
# See http://swift.org/LICENSE.txt for license information | ||
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors | ||
|
||
add_swift_syntax_library(SwiftLexicalLookup | ||
IdentifiableSyntax.swift | ||
LookupName.swift | ||
LookupResult.swift | ||
SimpleLookupQueries.swift | ||
|
||
Configurations/FileScopeHandlingConfig.swift | ||
Configurations/LookupConfig.swift | ||
|
||
Scopes/GenericParameterScopeSyntax.swift | ||
Scopes/IntroducingToSequentialParentScopeSyntax.swift | ||
Scopes/ScopeImplementations.swift | ||
Scopes/ScopeSyntax.swift | ||
Scopes/SequentialScopeSyntax.swift | ||
Scopes/TypeScopeSyntax.swift | ||
Scopes/WithGenericParametersScopeSyntax.swift | ||
) | ||
|
||
target_link_swift_syntax_libraries(SwiftLexicalLookup PUBLIC | ||
SwiftSyntax) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
Sources/SwiftLexicalLookup/Scopes/GenericParameterScopeSyntax.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2024 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import SwiftSyntax | ||
|
||
/// Scope that introduces generic parameter names and directs | ||
/// futher lookup to its `WithGenericParametersScopeSyntax` | ||
/// parent scope's parent scope (i.e. on return, bypasses names | ||
/// introduced by its parent). | ||
@_spi(Experimental) public protocol GenericParameterScopeSyntax: ScopeSyntax {} | ||
|
||
@_spi(Experimental) extension GenericParameterScopeSyntax { | ||
/// Returns names matching lookup and bypasses | ||
/// `WithGenericParametersScopeSyntax` parent scope in futher lookup. | ||
/// | ||
/// ### Example | ||
/// ```swift | ||
/// let a = 23 | ||
/// func foo<A>(a: A) { | ||
/// a // <-- start lookup here | ||
/// } | ||
/// ``` | ||
/// When starting lookup at the `a` reference, | ||
/// lookup first visits the code block scope associated | ||
/// with the function's body. Then, it's forwarded to the | ||
/// function declaration scope and then to generic parameter | ||
/// scope (`WithGenericParametersScopeSyntax`). | ||
/// Then, to ensure there is no infinite cycle, | ||
/// this method passes lookup to function scope's parent scope | ||
/// (in this case: file scope). | ||
@_spi(Experimental) public func lookup( | ||
_ identifier: Identifier?, | ||
at lookUpPosition: AbsolutePosition, | ||
with config: LookupConfig | ||
) -> [LookupResult] { | ||
return defaultLookupImplementation( | ||
identifier, | ||
at: lookUpPosition, | ||
with: config, | ||
propagateToParent: false | ||
) | ||
+ lookupBypassingParentResults( | ||
identifier, | ||
at: lookUpPosition, | ||
with: config | ||
) | ||
} | ||
|
||
/// Bypasses names introduced by `WithGenericParametersScopeSyntax` parent scope. | ||
/// | ||
/// ### Example | ||
/// ```swift | ||
/// let a = 23 | ||
/// func foo<A>(a: A) { | ||
/// a // <-- start lookup here | ||
/// } | ||
/// ``` | ||
/// When starting lookup at the `a` reference, | ||
/// lookup first visits the code block scope associated | ||
/// with the function's body. Then, it's forwarded to the | ||
/// function declaration scope and then to generic parameter | ||
/// scope (`WithGenericParametersScopeSyntax`). | ||
/// Then, to ensure there is no infinite cycle, | ||
/// we use this method instead of the standard `lookupInParent` | ||
/// to pass lookup to the function scope's parent scope (in this case: file scope) | ||
/// and effectively bypass names already looked up before. | ||
private func lookupBypassingParentResults( | ||
_ identifier: Identifier?, | ||
at lookUpPosition: AbsolutePosition, | ||
with config: LookupConfig | ||
) -> [LookupResult] { | ||
guard let parentScope else { return [] } | ||
|
||
if let parentScope = Syntax(parentScope).asProtocol(SyntaxProtocol.self) | ||
as? WithGenericParametersScopeSyntax | ||
{ | ||
return parentScope.lookupInParent(identifier, at: lookUpPosition, with: config) | ||
} else { | ||
return lookupInParent(identifier, at: lookUpPosition, with: config) | ||
} | ||
} | ||
} |
File renamed without changes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.