Skip to content

Macro expansion scopes are inserted at the last character in their insertion range #70740

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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/AST/ASTScopeCreation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,13 @@ ASTSourceFileScope::ASTSourceFileScope(SourceFile *SF,
case MacroRole::Extension:
case MacroRole::Member:
case MacroRole::Peer:
case MacroRole::Preamble:
parentLoc = SF->getMacroInsertionRange().End;;
case MacroRole::Preamble: {
auto insertionRange = SF->getMacroInsertionRange();
parentLoc = insertionRange.End;
if (insertionRange.Start != insertionRange.End)
parentLoc = parentLoc.getAdvancedLoc(-1);
break;
}
case MacroRole::Body: {
// Use the end location of the function decl itself as the parentLoc
// for the new function body scope. This is different from the end
Expand Down
13 changes: 13 additions & 0 deletions test/Macros/macro_expand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ testFileID(a: 1, b: 2)
@freestanding(expression) macro stringifyAndTry<T>(_ value: T) -> (T, String) =
#externalMacro(module: "MacroDefinition", type: "StringifyAndTryMacro")

enum Angle {
case degrees(Double)
case radians(Double)
}

func testStringify(a: Int, b: Int) {
let s = #stringify(a + b)
print(s)
Expand All @@ -196,6 +201,14 @@ func testStringify(a: Int, b: Int) {
// CHECK-AST: tuple_expr type='(Double, String)' location=Macro expansion of #stringify

_ = (b, b2, s2, s3)

let angle = Angle.degrees(17)
switch angle {
case .degrees(let value):
_ = #stringify(value)
case .radians(let value):
_ = #stringify(value)
}
}

func testAssert(a: Int, b: Int) {
Expand Down