提交 5794403b 编写于 作者: A AlekseyTs

Merge pull request #9265 from AlekseyTs/Issue8810

Test/fix scoping of pattern variables in expression-bodied functions and properties.
......@@ -457,7 +457,7 @@ private BoundStatement BindLocalFunctionStatement(LocalFunctionStatementSyntax n
}
else if (node.ExpressionBody != null)
{
block = binder.BindExpressionBodyAsBlock(node.ExpressionBody, diagnostics);
block = binder.GetBinder(node.ExpressionBody).BindExpressionBodyAsBlock(node.ExpressionBody, diagnostics);
}
else
{
......@@ -3575,9 +3575,7 @@ internal BoundBlock CreateBlockFromExpression(CSharpSyntaxNode node, ImmutableAr
DiagnosticBag diagnostics)
{
RefKind refKind = expressionBody.RefKeyword.Kind().GetRefKind();
PatternVariableBinder patternBinder = new PatternVariableBinder(expressionBody, expressionBody.Expression, this);
BoundExpression expression = patternBinder.BindValue(expressionBody.Expression, diagnostics, refKind != RefKind.None ? BindValueKind.RefReturn : BindValueKind.RValue);
expression = patternBinder.WrapWithPatternVariables(expression);
BoundExpression expression = BindValue(expressionBody.Expression, diagnostics, refKind != RefKind.None ? BindValueKind.RefReturn : BindValueKind.RValue);
return CreateBlockFromExpression(expressionBody, this.Locals, refKind, expression, expressionBody.Expression, diagnostics);
}
......
......@@ -187,9 +187,8 @@ public override void VisitLocalFunctionStatement(LocalFunctionStatementSyntax no
public override void VisitArrowExpressionClause(ArrowExpressionClauseSyntax node)
{
// TODO: Pattern variables in the => expression are local
//var arrowBinder = new PatternVariableBinder(node, node.Expression, _enclosing);
//AddToMap(node, arrowBinder);
var arrowBinder = new PatternVariableBinder(node, node.Expression, _enclosing);
AddToMap(node, arrowBinder);
}
public override void VisitAnonymousMethodExpression(AnonymousMethodExpressionSyntax node)
......
......@@ -43,6 +43,12 @@ protected MemberSemanticModel(CSharpCompilation compilation, CSharpSyntaxNode ro
_compilation = compilation;
_root = root;
_memberSymbol = memberSymbol;
if (root.Kind() == SyntaxKind.ArrowExpressionClause)
{
rootBinder = rootBinder.WithPatternVariablesIfAny(((ArrowExpressionClauseSyntax)root).Expression);
}
this.RootBinder = rootBinder.WithAdditionalFlags(GetSemanticModelBinderFlags());
_parentSemanticModelOpt = parentSemanticModelOpt;
_speculatedPosition = speculatedPosition;
......@@ -215,11 +221,16 @@ private Binder GetEnclosingBinder(CSharpSyntaxNode node, int position)
binder = RootBinder.GetBinder(current);
}
}
else if (current.Kind() == SyntaxKind.ArrowExpressionClause && current.Parent?.Kind() == SyntaxKind.LocalFunctionStatement)
{
binder = RootBinder.GetBinder(current);
}
else
{
// If this ever breaks, make sure that all callers of
// CanHaveAssociatedLocalBinder are in sync.
Debug.Assert(!current.CanHaveAssociatedLocalBinder());
Debug.Assert(!current.CanHaveAssociatedLocalBinder() ||
(current == _root && current.Kind() == SyntaxKind.ArrowExpressionClause));
}
if (current == _root)
......
......@@ -1549,7 +1549,7 @@ private static BoundBlock BindMethodBody(MethodSymbol method, TypeCompilationSta
binder = new ExecutableCodeBinder(arrowExpression, sourceMethod, binder);
importChain = binder.ImportChain;
// Add locals
return binder.BindExpressionBodyAsBlock(arrowExpression, diagnostics);
return binder.WithPatternVariablesIfAny(arrowExpression.Expression).BindExpressionBodyAsBlock(arrowExpression, diagnostics);
}
else
{
......
......@@ -60,11 +60,13 @@ public static bool IsQuery(this CSharpSyntaxNode syntax)
/// </summary>
internal static bool CanHaveAssociatedLocalBinder(this CSharpSyntaxNode syntax)
{
SyntaxKind kind;
return syntax.IsAnonymousFunction() ||
syntax is StatementSyntax ||
syntax.Kind() == SyntaxKind.CatchClause ||
syntax.Kind() == SyntaxKind.CatchFilterClause ||
syntax.Kind() == SyntaxKind.SwitchSection;
(kind = syntax.Kind()) == SyntaxKind.CatchClause ||
kind == SyntaxKind.CatchFilterClause ||
kind == SyntaxKind.SwitchSection ||
kind == SyntaxKind.ArrowExpressionClause;
}
/// <summary>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册