Skip to content

Commit

Permalink
Merge pull request #13510 from AlekseyTs/NewScopingRules_10
Browse files Browse the repository at this point in the history
Disallow declaration of variables within constructor/field initializers.
  • Loading branch information
AlekseyTs authored Sep 1, 2016
2 parents 88ac8e7 + cc05b66 commit 60362d8
Show file tree
Hide file tree
Showing 7 changed files with 408 additions and 12 deletions.
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 @@ -4950,4 +4950,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>
</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 @@ -1433,6 +1433,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.

0 comments on commit 60362d8

Please sign in to comment.