JEP 406 states:
A pattern variable introduced by a switch label is definitely matched in the associated switch rule expression, switch rule block or switch rule throw statement.
Does this mean pattern matched switch needs to be compulsorily exhaustive regardless of whether it's used as a statement or an expression?
You have asked two different questions. Your question’s title asks
The answer is yes. This is given in §14.11.2:
The definition of “enhanced switch statement” is given shortly before that statement
which implies that switch with pattern matching is always an enhanced switch statement.
But the body of your question is a different, very specific question:
The answer to this question is, no, this cited sentence does not have that meaning. The subject of this sentence is “pattern variable” and its scope is “the associated switch rule expression, switch rule block or switch rule throw statement”. So it’s not even remotely talking about the exhaustiveness of the entire
switch
statement.When you have, e.g.
The “pattern variable” is
p
and the part to the right of the->
is “the associated switch rule expression” and the cited sentence says that within the latter, the variablep
is “definitely matched”:This is formally necessary for §6.3 to establish that the variable will be in scope and initialized to the right of the
->
(or:
if you’re still using it).