提交 09c7d0bd 编写于 作者: A AlekseyTs

Ensure that SemanicModel doesn’t dereference null binder.

***NO_CI***
 (changeset 1392909)
上级 d66cab23
......@@ -2757,7 +2757,21 @@ public ILocalSymbol GetDeclaredSymbol(ForEachStatementSyntax forEachStatement, C
using (Logger.LogBlock(FunctionId.CSharp_SemanticModel_GetDeclaredSymbol, message: this.SyntaxTree.FilePath, cancellationToken: cancellationToken))
{
Binder enclosingBinder = this.GetEnclosingBinder(GetAdjustedNodePosition(forEachStatement));
Binder foreachBinder = enclosingBinder.GetBinder(forEachStatement).WithAdditionalFlags(BinderFlags.SemanticModel);
if (enclosingBinder == null)
{
return null;
}
Binder foreachBinder = enclosingBinder.GetBinder(forEachStatement);
// Binder.GetBinder can fail in presence of syntax errors.
if (foreachBinder == null)
{
return null;
}
foreachBinder = foreachBinder.WithAdditionalFlags(BinderFlags.SemanticModel);
LocalSymbol local = foreachBinder.Locals.FirstOrDefault();
return ((object)local != null && local.DeclarationKind == LocalDeclarationKind.ForEachIterationVariable)
? local
......@@ -2777,7 +2791,21 @@ public ILocalSymbol GetDeclaredSymbol(CatchDeclarationSyntax catchDeclaration, C
CSharpSyntaxNode catchClause = catchDeclaration.Parent; //Syntax->Binder map is keyed on clause, not decl
Debug.Assert(catchClause.Kind() == SyntaxKind.CatchClause);
Binder enclosingBinder = this.GetEnclosingBinder(GetAdjustedNodePosition(catchClause));
Binder catchBinder = enclosingBinder.GetBinder(catchClause).WithAdditionalFlags(BinderFlags.SemanticModel);
if (enclosingBinder == null)
{
return null;
}
Binder catchBinder = enclosingBinder.GetBinder(catchClause);
// Binder.GetBinder can fail in presence of syntax errors.
if (catchBinder == null)
{
return null;
}
catchBinder = catchBinder.WithAdditionalFlags(BinderFlags.SemanticModel);
LocalSymbol local = catchBinder.Locals.FirstOrDefault();
return ((object)local != null && local.DeclarationKind == LocalDeclarationKind.CatchVariable)
? local
......
......@@ -2907,5 +2907,65 @@ private static BoundForEachStatement GetBoundForEachStatement(string text, param
return boundNode;
}
[WorkItem(1100741, "DevDiv")]
[Fact]
public void Bug1100741()
{
var source = @"
namespace ImmutableObjectGraph
{
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IdentityFieldType = System.UInt32;
public static class RecursiveTypeExtensions
{
/// <summary>Gets the recursive parent of the specified value, or <c>null</c> if none could be found.</summary>
internal ParentedRecursiveType<<#= templateType.RecursiveParent.TypeName #>, <#= templateType.RecursiveTypeFromFamily.TypeName #>> GetParentedNode(<#= templateType.RequiredIdentityField.TypeName #> identity) {
if (this.Identity == identity) {
return new ParentedRecursiveType<<#= templateType.RecursiveParent.TypeName #>, <#= templateType.RecursiveTypeFromFamily.TypeName #>>(this, null);
}
if (this.LookupTable != null) {
System.Collections.Generic.KeyValuePair<<#= templateType.RecursiveType.TypeName #>, <#= templateType.RequiredIdentityField.TypeName #>> lookupValue;
if (this.LookupTable.TryGetValue(identity, out lookupValue)) {
var parentIdentity = lookupValue.Value;
return new ParentedRecursiveType<<#= templateType.RecursiveParent.TypeName #>, <#= templateType.RecursiveTypeFromFamily.TypeName #>>(this.LookupTable[identity].Key, (<#= templateType.RecursiveParent.TypeName #>)this.Find(parentIdentity));
}
} else {
// No lookup table means we have to aggressively search each child.
foreach (var child in this.Children) {
if (child.Identity.Equals(identity)) {
return new ParentedRecursiveType<<#= templateType.RecursiveParent.TypeName #>, <#= templateType.RecursiveTypeFromFamily.TypeName #>>(child, this);
}
var recursiveChild = child as <#= templateType.RecursiveParent.TypeName #>;
if (recursiveChild != null) {
var childResult = recursiveChild.GetParentedNode(identity);
if (childResult.Value != null) {
return childResult;
}
}
}
}
return default(ParentedRecursiveType<<#= templateType.RecursiveParent.TypeName #>, <#= templateType.RecursiveTypeFromFamily.TypeName #>>);
}
}
}
";
var compilation = CreateCompilationWithMscorlib(source);
var tree = compilation.SyntaxTrees.Single();
var node = tree.GetRoot().DescendantNodes().Where(n => n.Kind() == SyntaxKind.ForEachStatement).OfType<ForEachStatementSyntax>().Single();
var model = compilation.GetSemanticModel(tree);
Assert.Null(model.GetDeclaredSymbol(node));
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册