提交 46e87249 编写于 作者: C CyrusNajmabadi 提交者: GitHub

Merge pull request #17059 from CyrusNajmabadi/typeParameterResolution

Fix crash when trying to resolve a generic method that doesn't exist in the later compilation.
Fixes #17056
......@@ -144,8 +144,13 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
{
// We didn't find any candidates. We still need to stream through this
// method signature so the reader is in a proper position.
// Push an null-method to our stack so that any method-type-parameters
// can at least be read (if not resolved) properly.
reader.PushMethod(methodOpt: null);
var parameterTypeResolutions = reader.ReadSymbolKeyArray();
var returnType = GetFirstSymbol<ITypeSymbol>(reader.ReadSymbolKey());
reader.PopMethod(methodOpt: null);
}
return CreateSymbolInfo(result);
......
......@@ -360,13 +360,13 @@ public override void Dispose()
return true;
}
public void PushMethod(IMethodSymbol method)
=> _methodSymbolStack.Add(method);
public void PushMethod(IMethodSymbol methodOpt)
=> _methodSymbolStack.Add(methodOpt);
public void PopMethod(IMethodSymbol method)
public void PopMethod(IMethodSymbol methodOpt)
{
Contract.ThrowIfTrue(_methodSymbolStack.Count == 0);
Contract.ThrowIfFalse(method.Equals(_methodSymbolStack.Last()));
Contract.ThrowIfFalse(Equals(methodOpt, _methodSymbolStack.Last()));
_methodSymbolStack.RemoveAt(_methodSymbolStack.Count - 1);
}
......
......@@ -20,8 +20,11 @@ public static SymbolKeyResolution Resolve(SymbolKeyReader reader)
{
var methodIndex = reader.ReadInteger();
var ordinal = reader.ReadInteger();
var typeParameter = reader.ResolveMethod(methodIndex).TypeParameters[ordinal];
return new SymbolKeyResolution(typeParameter);
var method = reader.ResolveMethod(methodIndex);
var typeParameter = method?.TypeParameters[ordinal];
return typeParameter == null
? default(SymbolKeyResolution)
: new SymbolKeyResolution(typeParameter);
}
}
}
......
......@@ -3,6 +3,7 @@
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using Microsoft.CodeAnalysis.Shared.Utilities;
......@@ -105,7 +106,9 @@ public static IEqualityComparer<SymbolKey> GetComparer(bool ignoreCase, bool ign
{
using (var reader = SymbolKeyReader.GetReader(symbolKey, compilation, ignoreAssemblyKey, cancellationToken))
{
return reader.ReadFirstSymbolKey();
var result = reader.ReadFirstSymbolKey();
Debug.Assert(reader.Position == symbolKey.Length);
return result;
}
}
......
......@@ -579,6 +579,33 @@ void foo()
Assert.NotNull(SymbolKey.Create(xSymbol));
}
[Fact]
public void TestGenericMethodTypeParameterMissing1()
{
var source1 = @"
public class C
{
void M<T>(T t) { }
}
";
var source2 = @"
public class C
{
}
";
var compilation1 = GetCompilation(source1, LanguageNames.CSharp);
var compilation2 = GetCompilation(source2, LanguageNames.CSharp);
var methods = GetDeclaredSymbols(compilation1).OfType<IMethodSymbol>();
foreach (var method in methods)
{
var key = SymbolKey.Create(method);
key.Resolve(compilation2);
}
}
[Fact, WorkItem(377839, "https://devdiv.visualstudio.com/DevDiv/_workitems?id=377839")]
public void TestConstructedMethodInsideLocalFunctionWithTypeParameters()
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册