提交 7003dfec 编写于 作者: Š Šimon Koníček

Fixing TypeInferrer crash with `return` or `yield return` as a global statement

上级 638f14d8
......@@ -1096,6 +1096,13 @@ IEnumerable<int> F()
await TestAsync(markup, "global::System.Int32");
}
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestYieldReturnAsGlobalStatement()
{
await TestAsync(
@"yield return [|abc|]", "global::System.Object", sourceCodeKind: SourceCodeKind.Script);
}
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestReturnInSimpleLambda()
{
......@@ -1219,6 +1226,13 @@ public async Task TestReturnInAsyncVoidAnonymousMethod()
};", "void");
}
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestReturnAsGlobalStatement()
{
await TestAsync(
@"return [|Goo()|];", "global::System.Object", sourceCodeKind: SourceCodeKind.Script);
}
[Fact, Trait(Traits.Feature, Traits.Features.TypeInferenceService)]
public async Task TestSimpleLambda()
{
......
......@@ -37,18 +37,19 @@ private static async Task<bool> CanUseSpeculativeSemanticModelAsync(Document doc
return !service.GetMemberBodySpanForSpeculativeBinding(node).IsEmpty;
}
protected async Task TestAsync(string text, string expectedType, bool testNode = true, bool testPosition = true)
protected async Task TestAsync(string text, string expectedType, bool testNode = true, bool testPosition = true,
SourceCodeKind sourceCodeKind = SourceCodeKind.Regular)
{
MarkupTestFile.GetSpan(text.NormalizeLineEndings(), out text, out var textSpan);
if (testNode)
{
await TestWithAndWithoutSpeculativeSemanticModelAsync(text, textSpan, expectedType, useNodeStartPosition: false);
await TestWithAndWithoutSpeculativeSemanticModelAsync(text, textSpan, expectedType, useNodeStartPosition: false, sourceCodeKind);
}
if (testPosition)
{
await TestWithAndWithoutSpeculativeSemanticModelAsync(text, textSpan, expectedType, useNodeStartPosition: true);
await TestWithAndWithoutSpeculativeSemanticModelAsync(text, textSpan, expectedType, useNodeStartPosition: true, sourceCodeKind);
}
}
......@@ -56,14 +57,15 @@ protected async Task TestAsync(string text, string expectedType, bool testNode =
string text,
TextSpan textSpan,
string expectedType,
bool useNodeStartPosition)
bool useNodeStartPosition,
SourceCodeKind sourceCodeKind)
{
var document = fixture.UpdateDocument(text, SourceCodeKind.Regular);
var document = fixture.UpdateDocument(text, sourceCodeKind);
await TestWorkerAsync(document, textSpan, expectedType, useNodeStartPosition);
if (await CanUseSpeculativeSemanticModelAsync(document, textSpan.Start))
{
var document2 = fixture.UpdateDocument(text, SourceCodeKind.Regular, cleanBeforeUpdate: false);
var document2 = fixture.UpdateDocument(text, sourceCodeKind, cleanBeforeUpdate: false);
await TestWorkerAsync(document2, textSpan, expectedType, useNodeStartPosition);
}
}
......
......@@ -1869,7 +1869,9 @@ private ISymbol GetDeclaredMemberSymbolFromOriginalSemanticModel(SyntaxNode decl
return originalSemanticModel.GetDeclaredSymbol(declaration, CancellationToken);
}
return currentSemanticModel.GetDeclaredSymbol(declarationInCurrentTree, CancellationToken);
return declarationInCurrentTree != null
? currentSemanticModel.GetDeclaredSymbol(declarationInCurrentTree, CancellationToken)
: null;
}
private IEnumerable<TypeInferenceInfo> InferTypeInSwitchLabel(
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册