Skip to content

Commit ac1443a

Browse files
committed
[Macros] Adjust insertion position of peer macros for var decls
When the target decl is a VarDecl, peer macro should expand at the end of the pattern binding decl, instead of the end of the VarDecl which only covers the name. rdar://117098598
1 parent dab1b27 commit ac1443a

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

lib/Sema/TypeCheckMacros.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -926,8 +926,12 @@ static CharSourceRange getExpansionInsertionRange(MacroRole role,
926926
return CharSourceRange(rightBraceLoc, 0);
927927
}
928928
case MacroRole::Peer: {
929-
SourceLoc afterDeclLoc =
930-
Lexer::getLocForEndOfToken(sourceMgr, target.getEndLoc());
929+
SourceLoc endLoc = target.getEndLoc();
930+
if (auto var = dyn_cast<VarDecl>(target.get<Decl *>())) {
931+
if (auto binding = var->getParentPatternBinding())
932+
endLoc = binding->getEndLoc();
933+
}
934+
SourceLoc afterDeclLoc = Lexer::getLocForEndOfToken(sourceMgr, endLoc);
931935
return CharSourceRange(afterDeclLoc, 0);
932936
break;
933937
}

test/SourceKit/Macros/macro_basic.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,3 +293,7 @@ struct S5 {
293293
// RUN: %sourcekitd-test -req=cursor -pos=67:7 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=CURSOR_ON_DECL_WITH_PEER %s
294294
// CURSOR_ON_DECL_WITH_PEER: <decl.var.instance><syntaxtype.keyword>var</syntaxtype.keyword> <decl.name>test</decl.name>: <decl.var.type><ref.struct usr="s:Si">Int</ref.struct></decl.var.type></decl.var.instance>
295295
// CURSOR_ON_DECL_WITH_PEER-NOT: _foo
296+
297+
//##-- Expansion on the peer macro attached to pattern binding decl
298+
// RUN: %sourcekitd-test -req=refactoring.expand.macro -pos=66:4 %s -- ${COMPILER_ARGS[@]} | %FileCheck -check-prefix=EXPAND_PEER_ON_VAR %s
299+
// EXPAND_PEER_ON_VAR: 67:21-67:21 (@__swiftmacro_9MacroUser2S5V4test21AddPeerStoredPropertyfMp_.swift) "public var _foo: Int = 100"

test/SourceKit/Macros/syntactic_expansion.swift.expected

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ source.edit.kind.active:
2020
}
2121
}"
2222
source.edit.kind.active:
23-
7:12-7:12 "var _value: MyWrapperThingy<Int>"
23+
7:17-7:17 "var _value: MyWrapperThingy<Int>"
2424
source.edit.kind.active:
2525
2:1-2:19 ""
2626
source.edit.kind.active:
@@ -96,7 +96,7 @@ source.edit.kind.active:
9696
}
9797
}"
9898
source.edit.kind.active:
99-
7:12-7:12 "var _value: MyWrapperThingy<Int>"
99+
7:17-7:17 "var _value: MyWrapperThingy<Int>"
100100
source.edit.kind.active:
101101
1:1-1:22 ""
102102
source.edit.kind.active:

0 commit comments

Comments
 (0)