Skip to content

Commit c38ef86

Browse files
committed
Disable lexing #assert unless experimental static assertions are enabled
`#assert` should be usable as a macro name, but the experimental feature was getting in the way. Fixes rdar://107894668.
1 parent 79e2697 commit c38ef86

File tree

3 files changed

+23
-0
lines changed

3 files changed

+23
-0
lines changed

lib/Parse/Lexer.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -694,6 +694,11 @@ void Lexer::lexHash() {
694694
#include "swift/AST/TokenKinds.def"
695695
.Default(tok::pound);
696696

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

test/Macros/Inputs/syntax_macro_definitions.swift

+13
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

+5
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)