Skip to content

Commit 024f630

Browse files
committed
Add a basic SwiftLexicalLookup validation test. Add caching of ConfiguredRegions to unqualified lookup validation.
1 parent bda3c7f commit 024f630

File tree

2 files changed

+106
-8
lines changed

2 files changed

+106
-8
lines changed

lib/ASTGen/Sources/ASTGen/LexicalLookup.swift

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ private let rowCharWidth: Int = 30
3838
/// Additionally, when matching fails, the function prints console output with the two results compared.
3939
@_cdecl("swift_ASTGen_validateUnqualifiedLookup")
4040
public func unqualifiedLookup(
41-
sourceFilePtr: UnsafeRawPointer,
41+
sourceFilePtr: UnsafeMutableRawPointer,
4242
astContext: BridgedASTContext,
4343
lookupAt: BridgedSourceLoc,
4444
finishInSequentialScope: Bool,
@@ -51,10 +51,7 @@ public func unqualifiedLookup(
5151
return false
5252
}
5353
let sourceLocationConverter = sourceFile.pointee.sourceLocationConverter
54-
let buildConfiguration = CompilerBuildConfiguration(
55-
ctx: astContext,
56-
sourceBuffer: sourceFile.pointee.buffer
57-
)
54+
let configuredRegions = sourceFile.pointee.configuredRegions(astContext: astContext)
5855

5956
guard let lookupPosition = sourceFile.pointee.position(of: lookupAt),
6057
let lookupToken = sourceFileSyntax.token(at: lookupPosition)
@@ -73,7 +70,7 @@ public func unqualifiedLookup(
7370
let sllResults = sllConsumedResults(
7471
lookupToken: lookupToken,
7572
finishInSequentialScope: finishInSequentialScope,
76-
configuredRegions: sourceFileSyntax.configuredRegions(in: buildConfiguration)
73+
configuredRegions: configuredRegions
7774
)
7875

7976
// Add header to the output
@@ -201,11 +198,39 @@ private func sllConsumedResults(
201198
finishInSequentialScope: Bool,
202199
configuredRegions: ConfiguredRegions
203200
) -> [ConsumedLookupResult] {
204-
lookupToken.lookup(
201+
let resultsWithoutMacroReordering = lookupToken.lookup(
205202
nil,
206203
with: LookupConfig(finishInSequentialScope: finishInSequentialScope, configuredRegions: configuredRegions)
207204
)
208-
.flatMap { result in
205+
206+
// Early reordering of macro declaration parameters with its generic parameters.
207+
var results: [LookupResult] = []
208+
var previousMacroResult: LookupResult?
209+
210+
for result in resultsWithoutMacroReordering {
211+
if let unwrappedMacroResult = previousMacroResult,
212+
result.scope.is(GenericParameterClauseSyntax.self)
213+
{
214+
results += [result, unwrappedMacroResult]
215+
previousMacroResult = nil
216+
continue
217+
} else if let unwrappedMacroResult = previousMacroResult {
218+
results.append(unwrappedMacroResult)
219+
previousMacroResult = nil
220+
}
221+
222+
if result.scope.is(MacroDeclSyntax.self) {
223+
previousMacroResult = result
224+
} else {
225+
results.append(result)
226+
}
227+
}
228+
229+
if let previousMacroResult {
230+
results.append(previousMacroResult)
231+
}
232+
233+
return results.flatMap { result in
209234
switch result {
210235
case .lookInMembers(let lookInMembers):
211236
return [
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// RUN: %target-typecheck-verify-swift -enable-experimental-feature UnqualifiedLookupValidation
2+
3+
protocol P1 {
4+
associatedtype A
5+
func f() -> A
6+
}
7+
8+
protocol P2 {
9+
associatedtype A: P2
10+
associatedtype B: P2 where Self.A.A == Self.B.A
11+
}
12+
13+
protocol P3 {
14+
associatedtype A: P3
15+
}
16+
17+
struct Basic: P1 {
18+
typealias A = Int
19+
func f() -> Int { fatalError() }
20+
}
21+
22+
struct Recur: P2 {
23+
typealias A = Recur
24+
typealias B = Recur
25+
}
26+
27+
struct NonRecur: P2 {
28+
typealias A = Recur
29+
typealias B = Recur
30+
}
31+
32+
struct Generic<T> {}
33+
34+
class Super<T, U> {}
35+
36+
extension Super: P2 where T: P2, U: P2 {
37+
typealias A = T
38+
typealias B = T
39+
40+
func foo() -> Int { fatalError() }
41+
}
42+
43+
class Sub: Super<NonRecur, Recur> {}
44+
45+
struct RecurGeneric<T: P3>: P3 {
46+
typealias A = RecurGeneric<T>
47+
}
48+
49+
struct Specialize: P3 {
50+
typealias A = RecurGeneric<Specialize>
51+
}
52+
53+
protocol P48a { associatedtype A = Int }
54+
protocol P48b { associatedtype B }
55+
protocol P48c: P48a, P48b where A == B {}
56+
57+
public extension Array where Element == Int {
58+
mutating func foo(
59+
at index: Int,
60+
byCalling closure: (inout Element) -> Void
61+
) where Element: Differentiable { // expected-error{{cannot find type 'Differentiable' in scope}}
62+
closure(&self[index])
63+
}
64+
}
65+
66+
public extension Array {
67+
mutating func bar(
68+
at index: Int,
69+
byCalling closure:(inout Element) -> Void
70+
) where Element: Differentiable { // expected-error{{cannot find type 'Differentiable' in scope}}
71+
closure(&self[index])
72+
}
73+
}

0 commit comments

Comments
 (0)