未验证 提交 9046f1ed 编写于 作者: F Fred Silberberg 提交者: GitHub

Merge pull request #30721 from 333fred/incremental-bind-embbeded

Correctly return cached blocks in the IncrementalBinder for things th…
......@@ -1599,7 +1599,7 @@ protected virtual LocalFunctionSymbol LookupLocalFunction(SyntaxToken nameToken)
/// </summary>
internal virtual uint LocalScopeDepth => Next.LocalScopeDepth;
internal BoundBlock BindEmbeddedBlock(BlockSyntax node, DiagnosticBag diagnostics)
internal virtual BoundBlock BindEmbeddedBlock(BlockSyntax node, DiagnosticBag diagnostics)
{
return BindBlock(node, diagnostics);
}
......
......@@ -2091,6 +2091,13 @@ public override BoundStatement BindStatement(StatementSyntax node, DiagnosticBag
return (BoundStatement)boundNode;
}
internal override BoundBlock BindEmbeddedBlock(BlockSyntax node, DiagnosticBag diagnostics)
{
BoundBlock block = (BoundBlock)TryGetBoundNodeFromMap(node) ?? base.BindEmbeddedBlock(node, diagnostics);
Debug.Assert(!block.WasCompilerGenerated);
return block;
}
private BoundNode TryGetBoundNodeFromMap(CSharpSyntaxNode node)
{
ImmutableArray<BoundNode> boundNodes = _semanticModel.GuardedGetBoundNodesFromMap(node);
......
......@@ -493,6 +493,39 @@ void M(int x)
");
}
[Fact, WorkItem(26649, "https://github.com/dotnet/roslyn/issues/26649")]
public void IncrementalBindingReusesBlock()
{
var source = @"
class C
{
void M()
{
try
{
}
catch (Exception e)
{
throw new Exception();
}
}
}";
var compilation = CreateCompilation(source);
var syntaxTree = compilation.SyntaxTrees[0];
var semanticModel = compilation.GetSemanticModel(syntaxTree);
// We want to get the IOperation for the { throw new Exception(); } first, and then for the containing catch block, to
// force the semantic model to bind the inner first. It should reuse that inner block when binding the outer catch.
var catchBlock = syntaxTree.GetRoot().DescendantNodes().OfType<CatchClauseSyntax>().Single();
var exceptionBlock = catchBlock.Block;
var blockOperation = semanticModel.GetOperation(exceptionBlock);
var catchOperation = (ICatchClauseOperation)semanticModel.GetOperation(catchBlock);
Assert.Same(blockOperation, catchOperation.Handler);
}
private static void VerifyRootAndModelForOperationAncestors(
IOperation operation,
SemanticModel model,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册