And it seems very strange to me, how to express something like [Int].self or Int?.self in this grammar?
It looks like it can be done with some hacks, via literals, identifiers and optional-chaining-expression, for example, [Int] is primary -> literal -> array-literal -> expression -> postfix -> primary -> identifier, but that seems really weird.
Isn't the grammar just suppose to have option for type?
At a glance I'd say I agree with you here, maybe it's a bug/omission from the grammar, since the syntax outline right above it in that section has essentially your example as well.
I think it actually makes sense, though it's a bit obscure.
For historical (Objective-C**) reasons, you can add .self to pretty much any expression, and the resulting value is that same expression's value. In the informal lexical grammar of the document, postfix-expression basically represents the concept of "pretty much any expression".
([Int] is a postfix-expression because it's a primary-expression because it's a literal-expression because it's a literal type name. It isn't an array-literal. A literal array of 1 type would be [Int.self], not [Int].)
In most cases, there's no reason to add .self, but a type name literal alone isn't treated as a value. Adding .self forces it to be treated as a value, so that you can (for example) pass the type as a parameter to a function.
**In Obj-C, KVC always requires an object and a non-empty key-path to access a value. To reference the object itself, you don't have a key-path, so NSObject defined a self method, which works as key-path "self" in KVC. It looks like this works in Swift too:
The operand of .self doesn't have to be a type. Ambiguities are resolved in favor of a type, though.
Your example is ambiguous: it could also parse as an array literal with a single element that's a reference to the Int type. Since this is also the sugar syntax for Array<Int>, Swift prefers the latter.
So, because grammar needs to be completely unambiguous, and every type is reachable via postfix expression (even though in a weird way), that's why there is no postfix-self-expression → type . self rule?
Yes, I'm not saying that it have to be a type, all I'm saying that it can be a type, and that not represented in grammar.
I've discovered another example: (() -> ()).self, and function-type () -> () is not expressible as postfix expression, or I'm missing something.
That's not correct. There is no such thing as "literal type name" in grammar, there is a type, but that's not literal. Secondly, you can reach [Int].self through array literal, as I tried to show above shortly, but here is full version:
However, I think it's correct to describe it under the primary-expression . self rule and not a (potential) type . self rule.
Something like [Int].self or Int?.self is already shaped like a primary-expression followed by .self, and as @John_McCall said it's reinterpreted as the correct thing by a later compiler decision. These things don't need a new rule.
If the type . self rule existed as you proposed, then something like () -> ().self would be allowed under the rule, since () -> () is a type.
(() -> ()).self works because the parenthesized part is shaped like a primary-expression: it's a binary-expression made primary by the parentheses. Just like the other cases, it's reinterpreted as the correct thing later.