Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disallow 'var' as an explicit lambda expression return type #56911

Merged
merged 9 commits into from
Oct 4, 2021

Conversation

cston
Copy link
Member

@cston cston commented Oct 1, 2021

See LDM-2021-06-02 notes.

Relates to #52192

@cston cston requested a review from a team as a code owner October 1, 2021 17:43
@jcouv jcouv self-assigned this Oct 1, 2021
@jcouv jcouv added this to the 17.0 milestone Oct 1, 2021
var resetPoint = this.GetResetPoint();
try
{
returnType = ParseReturnType();
if (CurrentToken.Kind != SyntaxKind.OpenParenToken)
if (!IsVarType())
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 1, 2021

Choose a reason for hiding this comment

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

if (!IsVarType())

Should we instead parse this as a type and report an error that "var" is not allowed as a return type of a lambda? #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps even in binding.

@AlekseyTs
Copy link
Contributor

Done with review pass (commit 2)

@cston cston requested a review from a team October 1, 2021 18:11
@@ -12787,6 +12787,12 @@ LambdaExpressionSyntax parseLambdaExpressionWorker()
arrow = CheckFeatureAvailability(arrow, MessageID.IDS_FeatureLambda);
var (block, expression) = ParseLambdaBody();

// Disallow 'var' as explicit return type.
if ((returnType as IdentifierNameSyntax)?.Identifier.ContextualKind == SyntaxKind.VarKeyword)
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 1, 2021

Choose a reason for hiding this comment

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

if ((returnType as IdentifierNameSyntax)?.Identifier.ContextualKind == SyntaxKind.VarKeyword)

Should we also disallow ref var? #Closed

void verify(string source, ParseOptions? parseOptions = null)
{
UsingDeclaration(source, parseOptions,
// (1,9): error CS1031: Type expected
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 1, 2021

Choose a reason for hiding this comment

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

error CS1031: Type expected

It feels like this generic error might confuse a user. #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps error CS0825: The contextual keyword 'var' may only appear within a local variable declaration or in script code is suitable. SharpLab

@jcouv jcouv modified the milestones: 17.0, 17.1 Oct 1, 2021
@jcouv jcouv changed the base branch from release/dev17.0 to main October 1, 2021 18:52
@jcouv
Copy link
Member

jcouv commented Oct 1, 2021

Moved PR to target main/17.1


// Breaking change from C#9 which binds calls to F(Func<Func<object>>, int).
//
// The calls such as F(() => () => 1, 2) should be considered ambiguous in C#9 as per the C# spec.
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 1, 2021

Choose a reason for hiding this comment

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

// The calls such as F(() => () => 1, 2) should be considered ambiguous in C#9 as per the C# spec.

The change doesn't look related to the goal of the PR. Is it intentional? #Closed

@@ -248,6 +248,11 @@ static void checkAttributes(AnonymousFunctionExpressionSyntax syntax, SyntaxList
MessageID.IDS_FeatureLambdaReturnType.CheckFeatureAvailability(diagnostics, syntax);

syntax = syntax.SkipRef(out RefKind refKind);
if ((syntax as IdentifierNameSyntax)?.Identifier.ContextualKind() == SyntaxKind.VarKeyword)
{
diagnostics.Add(ErrorCode.ERR_LambdaExplicitReturnTypeVar, syntax.Location);
Copy link
Member

@jcouv jcouv Oct 1, 2021

Choose a reason for hiding this comment

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

Probably should document as breaking change in 17.1 #Pending

Copy link
Member

Choose a reason for hiding this comment

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

Does the spec capture that design decision?

Copy link
Member Author

Choose a reason for hiding this comment

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

I've added an item to the breaking changes document. I'll update the spec in a subsequent PR.

Copy link
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 (iteration 7) modulo documentation questions

{
static void Main()
{
F(var (var v) => v);
Copy link
Contributor

@AlekseyTs AlekseyTs Oct 1, 2021

Choose a reason for hiding this comment

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

var

Why this var is not reported? #Closed

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps there is a mismatch in source for VerifyDiagnostics below.

Copy link
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 (commit 9)

@cston cston merged commit 7950309 into dotnet:main Oct 4, 2021
@cston cston deleted the var-return-type branch October 4, 2021 16:11
@ghost ghost modified the milestones: 17.1, Next Oct 4, 2021
@cston cston modified the milestones: Next, 17.1 Oct 14, 2021
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.

4 participants