Files
swift-mirror/test/Macros/macro_self.swift
Alex Hoppen fbbbd0d08a [Macros] Allow keywords after # in freestanding macro expansions
Allow keywords after `#` in freestanding macro expansions

There is no reason why we shouldn’t allow keywords here.

I also thought about allowing keywords after `@` but things become tricky here for two reasons:
- In the parser, we parse a type after the `@`, which could start with a keyword itself (e.g. `any`). If we want to keep the parser logic to parse a type after `@` (which I think we should), then it becomes unclear what `@any T` should parse as.
- We allow a space between `@` and the type name. This makes it very hard for recovery to tell whether `@ struct` refers to an attribute with name `struct` or if the user forgot to write the attribute name after `@`.

Since almost all keywords are lowercase and attached member macros are usually spelled with an uppercase name, there are a lot fewer chances for clashes here, so I don’t think it’s worth allowing keywords after `@`.

https://github.com/apple/swift/issues/66444
rdar://110472060
2023-06-14 22:02:37 -07:00

28 lines
684 B
Swift

// RUN: %target-swift-frontend -parse %s -verify
@freestanding(expression) // expected-error {{expected expression}}
macro self() = #externalMacro(module: "MacroDefinition", type: "InvalidMacro")
func sync() {}
@freestanding(expression) // expected-error {{expected expression}}
macro Self() = #externalMacro(module: "MacroDefinition", type: "InvalidMacro")
func testSelfAsFreestandingMacro() {
_ = #self
}
func testCapitalSelfAsFreestandingMacro() {
_ = #Self
}
func testSelfAsAttachedMacro() {
@self // expected-error {{expected expression}}
struct Foo {}
}
func testCapitalSelfAsAttachedMacro() {
@Self // expected-error {{expected expression}}
struct Foo {}
}