Skip to content

Commit 63b9415

Browse files
authored
Merge pull request #65299 from DougGregor/move-over-static-assert-5.9
2 parents d955b16 + 67f6532 commit 63b9415

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/Parse/Lexer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,11 @@ void Lexer::lexHash() {
693693
#include "swift/AST/TokenKinds.def"
694694
.Default(tok::pound);
695695

696+
// If we found '#assert' but that experimental feature is not enabled,
697+
// treat it as '#'.
698+
if (Kind == tok::pound_assert && !LangOpts.hasFeature(Feature::StaticAssert))
699+
Kind = tok::pound;
700+
696701
// If we didn't find a match, then just return tok::pound. This is highly
697702
// dubious in terms of error recovery, but is useful for code completion and
698703
// SIL parsing.

test/Macros/Inputs/syntax_macro_definitions.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ public struct FileIDMacro: ExpressionMacro {
4949
}
5050
}
5151

52+
public struct AssertMacro: ExpressionMacro {
53+
public static func expansion(
54+
of macro: some FreestandingMacroExpansionSyntax,
55+
in context: some MacroExpansionContext
56+
) -> ExprSyntax {
57+
guard let argument = macro.argumentList.first?.expression else {
58+
fatalError("boom")
59+
}
60+
61+
return "assert(\(argument))"
62+
}
63+
}
64+
5265
public struct StringifyMacro: ExpressionMacro {
5366
public static func expansion(
5467
of macro: some FreestandingMacroExpansionSyntax,

test/Macros/macro_expand.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ struct Bad {}
9494
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
9595
@freestanding(expression) macro fileID<T: ExpressibleByStringLiteral>() -> T = #externalMacro(module: "MacroDefinition", type: "FileIDMacro")
9696
@freestanding(expression) macro recurse(_: Bool) = #externalMacro(module: "MacroDefinition", type: "RecursiveMacro")
97+
@freestanding(expression) macro assert(_: Bool) = #externalMacro(module: "MacroDefinition", type: "AssertMacro")
9798

9899
func testFileID(a: Int, b: Int) {
99100
// CHECK: MacroUser/macro_expand.swift
@@ -143,6 +144,10 @@ func testStringify(a: Int, b: Int) {
143144
_ = (b, b2, s2, s3)
144145
}
145146

147+
func testAssert(a: Int, b: Int) {
148+
#assert(a == b)
149+
}
150+
146151
public struct Outer {
147152
var value: Int = 0
148153
public func test() {

0 commit comments

Comments
 (0)