Skip to content

Commit 41b0104

Browse files
committed
Macro expansion scopes are inserted at the last character in their insertion range
Macro expansion scope ranges are in terms of characters, so the end of the insertion range is one past the last character of the insertion range. If the last character in the insertion range is also the last character in the enclosing scope, we would build the scope tree incorrectly. Adjust the position for scope creation to the last character in the insertion range, not one past it. Fixes rdar://120559184.
1 parent dfe1e69 commit 41b0104

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

lib/AST/ASTScopeCreation.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,13 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
284284
case MacroRole::Extension:
285285
case MacroRole::Member:
286286
case MacroRole::Peer:
287-
case MacroRole::Preamble:
288-
parentLoc = SF->getMacroInsertionRange().End;;
287+
case MacroRole::Preamble: {
288+
auto insertionRange = SF->getMacroInsertionRange();
289+
parentLoc = insertionRange.End;
290+
if (insertionRange.Start != insertionRange.End)
291+
parentLoc = parentLoc.getAdvancedLoc(-1);
289292
break;
293+
}
290294
case MacroRole::Body: {
291295
// Use the end location of the function decl itself as the parentLoc
292296
// for the new function body scope. This is different from the end

test/Macros/macro_expand.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ testFileID(a: 1, b: 2)
177177
@freestanding(expression) macro stringifyAndTry<T>(_ value: T) -> (T, String) =
178178
#externalMacro(module: "MacroDefinition", type: "StringifyAndTryMacro")
179179

180+
enum Angle {
181+
case degrees(Double)
182+
case radians(Double)
183+
}
184+
180185
func testStringify(a: Int, b: Int) {
181186
let s = #stringify(a + b)
182187
print(s)
@@ -196,6 +201,14 @@ func testStringify(a: Int, b: Int) {
196201
// CHECK-AST: tuple_expr type='(Double, String)' location=Macro expansion of #stringify
197202

198203
_ = (b, b2, s2, s3)
204+
205+
let angle = Angle.degrees(17)
206+
switch angle {
207+
case .degrees(let value):
208+
_ = #stringify(value)
209+
case .radians(let value):
210+
_ = #stringify(value)
211+
}
199212
}
200213

201214
func testAssert(a: Int, b: Int) {

0 commit comments

Comments
 (0)