Skip to content

Forbid 'default' as a case constant or a pattern.#23629

Merged
gafter merged 4 commits into
dotnet:masterfrom
gafter:master-23499
Dec 10, 2017
Merged

Forbid 'default' as a case constant or a pattern.#23629
gafter merged 4 commits into
dotnet:masterfrom
gafter:master-23499

Conversation

@gafter
Copy link
Copy Markdown
Member

@gafter gafter commented Dec 7, 2017

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 the default literal 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

@gafter gafter added this to the 15.6 milestone Dec 7, 2017
@gafter gafter requested review from a team and jcouv December 7, 2017 01:31
@gafter gafter self-assigned this Dec 7, 2017
Has been reviewed by LDM and compat council.
Fixes dotnet#23499
@alrz
Copy link
Copy Markdown
Member

alrz commented Dec 7, 2017

(int)default should be valid? #Resolved

@gafter
Copy link
Copy Markdown
Member Author

gafter commented Dec 7, 2017

@alrz Yes, (int)default is fine. #Resolved

bool wasExpression;
return BindConstantPattern(node, operandType, node.Expression, hasErrors, diagnostics, out wasExpression, wasSwitchCase);
var innerExpression = patternExpression.SkipParens();
if (innerExpression.Kind() == SyntaxKind.DefaultLiteralExpression)
Copy link
Copy Markdown
Member

@jcouv jcouv Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance innerExpression could be null here? (case (): maybe?) #Resolved

Copy link
Copy Markdown
Member Author

@gafter gafter Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
Copy link
Copy Markdown
Member

@jcouv jcouv Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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");
Copy link
Copy Markdown
Member

@jcouv jcouv Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could remove the old code #Resolved

Diagnostic(ErrorCode.ERR_DefaultInSwitch, "default").WithLocation(12, 18)
);
CompileAndVerify(comp, expectedOutput: "default");
//CompileAndVerify(comp, expectedOutput: "default");
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Dec 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//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");
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Dec 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//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");
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Dec 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//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");
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs Dec 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

//CompileAndVerify(comp, expectedOutput: "default"); [](start = 12, length = 52)

Please remove commented out code. #Closed

@AlekseyTs
Copy link
Copy Markdown
Contributor

AlekseyTs commented Dec 8, 2017

Done with review pass (iteration 3) #Closed

@gafter gafter requested a review from a team as a code owner December 9, 2017 03:11
@gafter
Copy link
Copy Markdown
Member Author

gafter commented Dec 9, 2017

This PR has been modified to remove some unneeded lines per review comments. Reviewers, (@jcouv and @AlekseyTs), do you have any other issues? #Resolved

@gafter gafter removed the request for review from a team December 9, 2017 03:18
Copy link
Copy Markdown
Contributor

@AlekseyTs AlekseyTs left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (iteration 4)

Copy link
Copy Markdown
Member

@jcouv jcouv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM Thanks

@jcouv
Copy link
Copy Markdown
Member

jcouv commented Dec 9, 2017

@MeiChin-Tsai for ask-mode approval for 15.6. Thanks

@jcouv jcouv removed the Feature - Default Interface Impl Default Interface Implementation label Dec 9, 2017
@MeiChin-Tsai
Copy link
Copy Markdown

Approved. Not all job is clean.

@gafter
Copy link
Copy Markdown
Member Author

gafter commented Dec 10, 2017

Test windows_debug_unit64_prtest please

@stephentoub
Copy link
Copy Markdown
Member

stephentoub commented Mar 21, 2018

We think that is unlikely, as it produced a warning before.

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.
dotnet/corefx#28284

@jcouv
Copy link
Copy Markdown
Member

jcouv commented Mar 28, 2018

@gafter From this PR, it indeed looks like we forgot to document the breaking change.

@gafter
Copy link
Copy Markdown
Member Author

gafter commented Mar 28, 2018

Please see #25800

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Disallow default as a constant pattern.

6 participants