Forbid 'default' as a case constant or a pattern.#23629
Conversation
Has been reviewed by LDM and compat council. Fixes dotnet#23499
|
|
|
@alrz Yes, |
| bool wasExpression; | ||
| return BindConstantPattern(node, operandType, node.Expression, hasErrors, diagnostics, out wasExpression, wasSwitchCase); | ||
| var innerExpression = patternExpression.SkipParens(); | ||
| if (innerExpression.Kind() == SyntaxKind.DefaultLiteralExpression) |
There was a problem hiding this comment.
Any chance innerExpression could be null here? (case (): maybe?) #Resolved
There was a problem hiding this comment.
No. That is a positional pattern, but even if parsed as an expression the parser will insert a missing identifier. #Resolved
| } | ||
| namespace System | ||
| { | ||
| public struct ValueTuple<T1, T2> |
There was a problem hiding this comment.
Does this test actually need ValueTuple type? #Resolved
| // System.Console.Write($"{hello is default} {nullString is default} {two is default} {zero is default}"); | ||
| Diagnostic(ErrorCode.ERR_DefaultInPattern, "default").WithLocation(10, 101) | ||
| ); | ||
| //CompileAndVerify(comp, expectedOutput: "False True False True"); |
There was a problem hiding this comment.
Could remove the old code #Resolved
| Diagnostic(ErrorCode.ERR_DefaultInSwitch, "default").WithLocation(12, 18) | ||
| ); | ||
| CompileAndVerify(comp, expectedOutput: "default"); | ||
| //CompileAndVerify(comp, expectedOutput: "default"); |
There was a problem hiding this comment.
//CompileAndVerify(comp, expectedOutput: "default"); [](start = 12, length = 52)
Please remove commented out code. #Closed
| Diagnostic(ErrorCode.ERR_DefaultInSwitch, "default").WithLocation(12, 18) | ||
| ); | ||
| CompileAndVerify(comp, expectedOutput: "default"); | ||
| //CompileAndVerify(comp, expectedOutput: "default"); |
There was a problem hiding this comment.
//CompileAndVerify(comp, expectedOutput: "default"); [](start = 12, length = 52)
Please remove commented out code. #Closed
| // case (default): | ||
| Diagnostic(ErrorCode.ERR_DefaultInSwitch, "default").WithLocation(12, 19) | ||
| ); | ||
| //CompileAndVerify(comp, expectedOutput: "default"); |
There was a problem hiding this comment.
//CompileAndVerify(comp, expectedOutput: "default"); [](start = 12, length = 52)
Please remove commented out code. #Closed
| // case (default): | ||
| Diagnostic(ErrorCode.ERR_DefaultInSwitch, "default").WithLocation(12, 19) | ||
| ); | ||
| //CompileAndVerify(comp, expectedOutput: "default"); |
There was a problem hiding this comment.
//CompileAndVerify(comp, expectedOutput: "default"); [](start = 12, length = 52)
Please remove commented out code. #Closed
|
Done with review pass (iteration 3) #Closed |
|
This PR has been modified to remove some unneeded lines per review comments. Reviewers, (@jcouv and @AlekseyTs), do you have any other issues? #Resolved |
|
@MeiChin-Tsai for ask-mode approval for 15.6. Thanks |
|
Approved. Not all job is clean. |
|
Test windows_debug_unit64_prtest please |
FWIW, I don't believe we were seeing any warnings for this in corefx, where we did take a dependency on this in several places (and where we compile with warnings-as-errors). We just removed those as they broke the build when we upgraded to a newer compiler. |
|
@gafter From this PR, it indeed looks like we forgot to document the breaking change. |
|
Please see #25800 |
Has been reviewed by LDM and compat council.
Customer scenario
The C# language design team wants to forbid the use of a default expression
default(without the type between parens) as a constant pattern for pattern-matching, to make room for evolution of that feature in C# 8.0. We are treating this as a bug fix against thedefaultliteral feature that was introduced in C# 7.1, and as such we want to get the change in as soon as possible.Bugs this fixes
Fixes #23499
Workarounds, if any
N/A
Risk
Low, though there is a slight compat risk if people have already written code like
case default:. We think that is unlikely, as it produced a warning before.Performance impact
Only trivial additional complexity in the compiler.
Is this a regression from a previous update?
N/A
Root cause analysis
We did not predict the future well enough. 😉
How was the bug found?
We discovered this while designing recursive patterns.
Test documentation updated?
N/A
@dotnet/roslyn-compiler @jcouv Please review for 15.6