docs
docs copied to clipboard
Problem with switch_label grammar rule, and possibly ambiguity
I'm working on the spec for adding patterns as at V7 to the Ecma standard.
We have the following grammar:
switch_label
: 'case' complex_pattern case_guard? ':'
| 'case' constant_expression case_guard? ':'
| 'default' ':'
;
This refers to the rule complex_pattern, which is NEVER defined or referred to in any other proposals from V7-V10! So that name has to be wrong. I'm thinking it should simply be pattern instead.
If we assume that is the correct fix,
: 'case' pattern case_guard? ':'
would be satisfied by
: 'case' constant_pattern case_guard? ':'
which is equivalent to
: 'case' constant_expression case_guard? ':'
making both paths equivalent, and so ambiguous.
If the second case really is a subset of the first, we don't need, and can't have, the second one.
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 2e3f3f52-86a5-4cc9-ab1f-874cad72b821
- Version Independent ID: f69dc2c8-79bc-300c-5804-e1f7aa5c8352
- Content: Pattern matching - C# 7.0 draft specifications
- Content Source: proposals/csharp-7.0/pattern-matching.md
- Product: dotnet-csharp
- Technology: csharp-spec
- GitHub Login: @dotnet-bot
- Microsoft Alias: wiwagn
ping @333fred @jcouv
Who should I route this one to?
(If Rex's analysis is correct, I can submit the PR)
I would ask @gafter what he meant to write there.
FWIW, we have three cases in the implementation (see antlr snippet below) and the parser deals with some expression-or-pattern ambiguity (see ParseExpressionOrPatternForSwitchStatementCore):
switch_label
: case_pattern_switch_label
| case_switch_label
| default_switch_label
;
case_pattern_switch_label
: 'case' pattern when_clause? ':'
;
case_switch_label
: 'case' expression ':'
;
Might be some backwards compatibility concern?
But the main compatibility concern mentioned in the speclet and a quick scan of LDM notes regards binding of identifier X as type vs. constant (which interpretation do we prefer), and making that work across is, case and patterns in general. But I think that's a different issue than one above.
I would suggest digging through LDM notes more and starting a broader email with LDM and compiler teams.
I think complex_pattern is supposed to be any pattern that isn't a constant pattern.
complex_pattern
: declaration_pattern
| var_pattern
;
That's because, as a pattern, a constant pattern only accepts a shift_expression, but as a case label any expression will do.