mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This started out as a crash, where an expression macro could not be defined in terms of one of the builtin macros (e.g., `#line`), because we were expecting a macro expansion expression but didn't get one. Easy fix. However, this uncovered a second bug, which is that we couldn't handle an expression macro expansino to `#line`. This is because we were parsing the macro expansion buffer as "top level items", which treats `#line` at the start of a line as a deprecated alias of `#sourceLocation`. Switch over to parsing a single expression in these contexts, and fix up an issue where `#isolation` didn't even have that expression. Fixes rdar://139372780.
45 lines
1.6 KiB
Swift
45 lines
1.6 KiB
Swift
// Expanding macros that are defined in terms of other macros.
|
|
|
|
// RUN: %empty-directory(%t)
|
|
// RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -parse-as-library -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift
|
|
|
|
// Diagnostics testing
|
|
// RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) -module-name MacroUser -DTEST_DIAGNOSTICS
|
|
|
|
// Execution testing
|
|
// RUN: %target-build-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -o %t/main -module-name MacroUser
|
|
// RUN: %target-codesign %t/main
|
|
// RUN: %target-run %t/main | %FileCheck %s
|
|
|
|
// REQUIRES: swift_swift_parser, executable_test
|
|
|
|
@freestanding(expression) macro stringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
|
|
|
|
@freestanding(expression) macro stringifySeven() -> (Int, String) = #stringify(7)
|
|
|
|
@freestanding(expression) macro recurse(_: Bool) = #externalMacro(module: "MacroDefinition", type: "RecursiveMacro")
|
|
|
|
@freestanding(expression) macro recurseThrough(_ value: Bool) = #recurse(value)
|
|
|
|
func testFreestandingExpansionOfOther() {
|
|
// CHECK: ---testFreestandingExpansionOfOther
|
|
print("---testFreestandingExpansionOfOther")
|
|
|
|
// CHECK-NEXT: (7, "7")
|
|
print(#stringifySeven)
|
|
|
|
#recurseThrough(false)
|
|
|
|
#if TEST_DIAGNOSTICS
|
|
#recurseThrough(true)
|
|
// expected-note@-1 {{in expansion of macro 'recurseThrough' here}}
|
|
#endif
|
|
}
|
|
|
|
@freestanding(expression)
|
|
macro myLineMacro() -> Int = #line
|
|
|
|
var thisLine = #myLineMacro
|
|
|
|
testFreestandingExpansionOfOther()
|