// REQUIRES: swift_swift_parser // RUN: %empty-directory(%t) // RUN: %host-build-swift -swift-version 5 -emit-library -o %t/%target-library-name(MacroDefinition) -module-name=MacroDefinition %S/Inputs/syntax_macro_definitions.swift -g -no-toolchain-stdlib-rpath // RUN: %target-typecheck-verify-swift -swift-version 5 -load-plugin-library %t/%target-library-name(MacroDefinition) %s -enable-bare-slash-regex public typealias Stringified = (T, String) @freestanding(expression) macro stringify(_ value: T) -> Stringified = #externalMacro( module: "MacroDefinition", type: "StringifyMacro" ) struct MyType { } typealias MyInt = Int struct MyLiteral: ExpressibleByStringLiteral, _ExpressibleByColorLiteral, _ExpressibleByImageLiteral, _ExpressibleByFileReferenceLiteral { init(stringLiteral value: StringLiteralType) {} init(_colorLiteralRed: Float, green: Float, blue: Float, alpha: Float) {} init(imageLiteralResourceName: String) {} init(fileReferenceLiteralResourceName: String) {} } // MARK: literals func testNil(nil: Stringified = #stringify(nil)) {} func testBool(true: Stringified = #stringify(true), false: Stringified = #stringify(false)) {} func testInt(positive: Stringified = #stringify(1_000_001), zero: Stringified = #stringify(0x0), negative: Stringified = #stringify(-0o21)) {} func testFloat(double: Stringified = #stringify(-0xC.3p0), float: Stringified = #stringify(00003.14159)) {} func testString(literal: Stringified = #stringify("🐨")) {} func testMagic(fileID: Stringified = #stringify(#fileID), filePath: Stringified = #stringify(#filePath), file: Stringified = #stringify(#file), function: Stringified = #stringify(#function), line: Stringified = #stringify(#line), column: Stringified = #stringify(#column), dso: Stringified = #stringify(#dsohandle)) {} @available(SwiftStdlib 5.7, *) func testRegex(literal: Stringified> = #stringify(/foo/)) {} func testObject(color: Stringified = #stringify(#colorLiteral(red: 0.4, green: 0.8, blue: 1, alpha: 1)), image: Stringified = #stringify(#imageLiteral(resourceName: "swift.png")), file: Stringified = #stringify(#fileLiteral(resourceName: "main.swift"))) {} // MARK: not literal let myString = "oops" // expected-error@+1{{only literals are permitted}} func testIdentifier(notOkay: Stringified = #stringify(myString)) {} // expected-error@+1{{only literals are permitted}} func testString(interpolated: Stringified = #stringify("Hello \(0b10001)")) {} // expected-error@+1{{default argument value of type '(Int, String)' cannot be converted to type 'Int'}} func testReturn(wrongType: Int = #stringify(0)) {}