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 declaration of variables within constructor/field initializers. #13510

Merged
merged 1 commit into from
Sep 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,11 @@ private BoundExpression BindArgumentValue(DiagnosticBag diagnostics, ArgumentSyn
var declaration = (TypedVariableComponentSyntax)declarationExpression.VariableComponent;
var typeSyntax = declaration.Type;

if (InConstructorInitializer || InFieldInitializer)
{
Error(diagnostics, ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, declarationExpression);
}

bool isConst = false;
bool isVar;
AliasSymbol alias;
Expand Down
6 changes: 6 additions & 0 deletions src/Compilers/CSharp/Portable/Binder/Binder_Patterns.cs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,12 @@ private BoundPattern BindDeclarationPattern(
DiagnosticBag diagnostics)
{
Debug.Assert(operand != null || operandType != (object)null);

if (InConstructorInitializer || InFieldInitializer)
{
Error(diagnostics, ErrorCode.ERR_ExpressionVariableInConstructorOrFieldInitializer, node);
}

var typeSyntax = node.Type;
var identifier = node.Identifier;

Expand Down
9 changes: 9 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions src/Compilers/CSharp/Portable/CSharpResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -4947,4 +4947,7 @@ To remove the warning, you can use /reference instead (set the Embed Interop Typ
<data name="ERR_OutVarDeconstructionIsNotSupported" xml:space="preserve">
<value>Deconstruction is not supported for an 'out' argument.</value>
</data>
<data name="ERR_ExpressionVariableInConstructorOrFieldInitializer" xml:space="preserve">
<value>Out variable or pattern variable declarations are not allowed within constructor/field/auto-implemented property initializers.</value>
Copy link
Member

Choose a reason for hiding this comment

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

Consider separating cases with comma for consistency with other errors. And remove auto-implemented if not necessary. Perhaps: ... not allowed within constructors, field initializers, or property initializers.

Copy link
Member

Choose a reason for hiding this comment

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

Also, I believe the first "or" should be "and" (they are both forbidden), so I suggest

Out variable and pattern variable declarations are not allowed within constructor initializers, field initializers, or property initializers.

This is long-winded enough that you could consider specializing it.


In reply to: 77212251 [](ancestors = 77212251)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Opened #13528.

</data>
</root>
1 change: 1 addition & 0 deletions src/Compilers/CSharp/Portable/Errors/ErrorCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1432,6 +1432,7 @@ internal enum ErrorCode
ERR_TypeInferenceFailedForImplicitlyTypedOutVariable = 8197,
ERR_ExpressionTreeContainsOutVariable = 8198,
ERR_OutVarDeconstructionIsNotSupported = 8199,
ERR_ExpressionVariableInConstructorOrFieldInitializer = 8200,
#endregion diagnostics for out var
}
}
200 changes: 190 additions & 10 deletions src/Compilers/CSharp/Test/Semantic/Semantics/OutVarTests.cs

Large diffs are not rendered by default.

196 changes: 194 additions & 2 deletions src/Compilers/CSharp/Test/Semantic/Semantics/PatternMatchingTests.cs

Large diffs are not rendered by default.