-
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
[SwiftLexicalLookup][GSoC] Add switch, generic parameter and function scopes. #2782
Conversation
Would it be possible to do the changes that address my review comments from #2748 in a separate PR again? It just makes it easier to review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have a few comments, but in general this looks great. There are a few constructs that probably still need to be covered here, but they can be in a follow-up PR:
- subscripts
- type aliases
- where clauses and inheritance clauses of types/extensions to make sure they're seeing the right things
|
||
import SwiftSyntax | ||
|
||
@_spi(Experimental) public protocol WithGenericParametersScopeSyntax: ScopeSyntax { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like something we should define via CodeGeneration so it's declarative. (Doesn't have to be now)
let 7️⃣x: 3️⃣T1 = v | ||
let y: 4️⃣T2 = v | ||
|
||
class B<5️⃣T1> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am curious what the compiler (and this implementation) would do for the second T2
in class B<T1, T2: T2>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least when it comes to this implementation, the result would be empty.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, it seems that there's no ordering amongst the various generic parameters. Given this:
class A<X> {
class B<T2: T1, T1> { }
}
unqualified lookup is finding T1
and the type checker is rejecting it:
1 | class A<X> {
2 | class B<T1, T2: T1> { }
| `- error: type 'T2' constrained to non-protocol, non-class type 'T1'
3 | }
4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FWIW, here's a case where it's also semantically well-formed to refer to generic parameters out-of-order:
struct X<C: Collection<T>, T> { }
@swift-ci please test |
@swift-ci please test Windows |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice. I’m really curious how close we are to being feature-equivalent with the C++ name lookup and how many more edge cases we might hit.
Sources/SwiftLexicalLookup/Scopes/GenericParameterScopeSyntax.swift
Outdated
Show resolved
Hide resolved
Sources/SwiftLexicalLookup/Scopes/GenericParameterScopeSyntax.swift
Outdated
Show resolved
Hide resolved
Sources/SwiftLexicalLookup/Scopes/GenericParameterScopeSyntax.swift
Outdated
Show resolved
Hide resolved
@swift-ci please test |
func testGenericParameterOrdering() { | ||
assertLexicalNameLookup( | ||
source: """ | ||
class Foo<1️⃣A: 2️⃣A, B: 3️⃣A, 4️⃣C: 5️⃣D, D: 6️⃣C> {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Based on my comment above, I think the results from the lookup at "2" should find "1", and the results from the lookup at "5" should find the generic parameter D.
@swift-ci please test Windows |
@swift-ci please test |
This PR introduces
switch case
, generic parameter/primary associated types and function scopes.WithGenericParametersOrAssociatedTypesScopeSyntax
andGenericParameterOrAssociatedTypeScopeSyntax
are used to properly handle generic parameters/primary associated types by providing an alternative route for the lookup method before propagating it to parent scope.switch
now introduces names in it'scase
bodies