提交 5d4ea622 编写于 作者: A AlekseyTs

Adjust scope of declarations within a Local Function Statement according to the latest design.

Related to #12597.
上级 28758406
......@@ -760,5 +760,22 @@ internal BoundStatement WrapWithVariablesIfAny(CSharpSyntaxNode scopeDesignator,
ImmutableArray.Create(statement))
{ WasCompilerGenerated = true };
}
/// <summary>
/// Should only be used with scopes that could declare local functions.
/// </summary>
internal BoundStatement WrapWithVariablesAndLocalFunctionsIfAny(CSharpSyntaxNode scopeDesignator, BoundStatement statement)
{
var locals = this.GetDeclaredLocalsForScope(scopeDesignator);
var localFunctions = this.GetDeclaredLocalFunctionsForScope(scopeDesignator);
if (locals.IsEmpty && localFunctions.IsEmpty)
{
return statement;
}
return new BoundBlock(statement.Syntax, locals, localFunctions,
ImmutableArray.Create(statement))
{ WasCompilerGenerated = true };
}
}
}
......@@ -272,6 +272,8 @@ internal virtual BoundStatement BindUsingStatementParts(DiagnosticBag diagnostic
internal BoundStatement BindPossibleEmbeddedStatement(StatementSyntax node, DiagnosticBag diagnostics)
{
Binder binder;
switch (node.Kind())
{
case SyntaxKind.ExpressionStatement:
......@@ -281,11 +283,16 @@ internal BoundStatement BindPossibleEmbeddedStatement(StatementSyntax node, Diag
case SyntaxKind.IfStatement:
case SyntaxKind.YieldReturnStatement:
case SyntaxKind.LocalDeclarationStatement:
case SyntaxKind.LabeledStatement:
Binder binder = this.GetBinder(node);
binder = this.GetBinder(node);
Debug.Assert(binder != null);
return binder.WrapWithVariablesIfAny(node, binder.BindStatement(node, diagnostics));
case SyntaxKind.LabeledStatement:
case SyntaxKind.LocalFunctionStatement:
binder = this.GetBinder(node);
Debug.Assert(binder != null);
return binder.WrapWithVariablesAndLocalFunctionsIfAny(node, binder.BindStatement(node, diagnostics));
case SyntaxKind.SwitchStatement:
var switchStatement = (SwitchStatementSyntax)node;
binder = this.GetBinder(switchStatement.Expression);
......
......@@ -3785,7 +3785,39 @@ static void Main(string[] args)
Diagnostic(ErrorCode.ERR_InvalidExprTerm, ")").WithArguments(")").WithLocation(6, 13),
// (7,9): error CS1023: Embedded statement cannot be a declaration or labeled statement
// int Add(int x, int y) => x + y;
Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "int Add(int x, int y) => x + y;").WithLocation(7, 9)
Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "int Add(int x, int y) => x + y;").WithLocation(7, 9),
// (7,13): warning CS0168: The variable 'Add' is declared but never used
// int Add(int x, int y) => x + y;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "Add").WithArguments("Add").WithLocation(7, 13)
);
}
[Fact, WorkItem(10521, "https://github.com/dotnet/roslyn/issues/10521")]
public void LabeledLocalFunctionInIf()
{
var source = @"
class Program
{
static void Main(string[] args)
{
if () // typing at this point
a: int Add(int x, int y) => x + y;
}
}
";
VerifyDiagnostics(source,
// (6,13): error CS1525: Invalid expression term ')'
// if () // typing at this point
Diagnostic(ErrorCode.ERR_InvalidExprTerm, ")").WithArguments(")").WithLocation(6, 13),
// (7,1): error CS1023: Embedded statement cannot be a declaration or labeled statement
// a: int Add(int x, int y) => x + y;
Diagnostic(ErrorCode.ERR_BadEmbeddedStmt, "a: int Add(int x, int y) => x + y;").WithLocation(7, 1),
// (7,1): warning CS0164: This label has not been referenced
// a: int Add(int x, int y) => x + y;
Diagnostic(ErrorCode.WRN_UnreferencedLabel, "a").WithLocation(7, 1),
// (7,13): warning CS0168: The variable 'Add' is declared but never used
// a: int Add(int x, int y) => x + y;
Diagnostic(ErrorCode.WRN_UnreferencedVar, "Add").WithArguments("Add").WithLocation(7, 13)
);
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册