Skip to content

Commit 8c9eeb2

Browse files
committed
[ASTScope] In the isBeforeInSource predicate, use the end location of the
original source location of a macro expansion when walking up the ancestor list. The original source range of a macro expansion buffer contains the entire range of the code that is replaced by the expansion. In some cases, the original code is type checked, so ASTScope needs to represent both the original code and the macro-expanded code in the scope tree. For accessor macros, the original code -- the initializer expression of the property -- is type checked, but not before macro expansion, so unqualified lookup must deal with the scope tree for the initializer and the expanded accessors. The accessors are inserted in the tree after the initializer scope, but the `isBeforeInSource` was using the start location of the original source range for macro expansions, so the accessor scopes were incorrectly considered to be ordered before a source location in the initializer expression, which caused unqualified lookup of local variables within the initializer to fail.
1 parent 85cced7 commit 8c9eeb2

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

lib/Basic/SourceLoc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -797,11 +797,11 @@ static bool isBeforeInSource(
797797
SourceLoc firstLocInLCA = firstMismatch == firstAncestors.end()
798798
? firstLoc
799799
: sourceMgr.getGeneratedSourceInfo(*firstMismatch)
800-
->originalSourceRange.getStart();
800+
->originalSourceRange.getEnd();
801801
SourceLoc secondLocInLCA = secondMismatch == secondAncestors.end()
802802
? secondLoc
803803
: sourceMgr.getGeneratedSourceInfo(*secondMismatch)
804-
->originalSourceRange.getStart();
804+
->originalSourceRange.getEnd();
805805
return sourceMgr.isBeforeInBuffer(firstLocInLCA, secondLocInLCA) ||
806806
(allowEqual && firstLocInLCA == secondLocInLCA);
807807
}

test/stdlib/Observation/Observable.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,16 @@ class RecursiveOuter {
215215
class GuardedAvailability {
216216
}
217217

218+
@Observable class TestASTScopeLCA {
219+
// Make sure ASTScope unqualified lookup can find local variables
220+
// inside initial values with closures when accessor macros are
221+
// involved.
222+
var state : Bool = {
223+
let value = true
224+
return value
225+
}()
226+
}
227+
218228
@main
219229
struct Validator {
220230
@MainActor

0 commit comments

Comments
 (0)