提交 be5deed6 编写于 作者: J Julien Couvreur 提交者: GitHub

Infer Exception type when typing throw expression (#19828)

上级 99a1a34a
......@@ -536,6 +536,44 @@ class Variable
End Using
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.Completion)>
<WorkItem(17256, "https://github.com/dotnet/roslyn/issues/17256")>
Public Async Function TestThrowExpression() As Task
Using state = TestState.CreateCSharpTestState(
<Document><![CDATA[
using System;
class C
{
public object Foo()
{
return null ?? throw new$$
}
}]]></Document>)
state.SendTypeChars(" ")
Await state.AssertSelectedCompletionItem(displayText:="Exception", isHardSelected:=True)
End Using
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.Completion)>
<WorkItem(17256, "https://github.com/dotnet/roslyn/issues/17256")>
Public Async Function TestThrowStatement() As Task
Using state = TestState.CreateCSharpTestState(
<Document><![CDATA[
using System;
class C
{
public object Foo()
{
throw new$$
}
}]]></Document>)
state.SendTypeChars(" ")
Await state.AssertSelectedCompletionItem(displayText:="Exception", isHardSelected:=True)
End Using
End Function
<WpfFact, Trait(Traits.Feature, Traits.Features.Completion)>
<WorkItem(13527, "https://github.com/dotnet/roslyn/issues/13527")>
Public Async Function TestSymbolInTupleLiteral() As Task
......@@ -3338,4 +3376,4 @@ class C
End Function
End Class
End Class
End Namespace
\ No newline at end of file
End Namespace
......@@ -163,6 +163,7 @@ private IEnumerable<TypeInferenceInfo> GetTypesSimple(ExpressionSyntax expressio
case SimpleLambdaExpressionSyntax simpleLambdaExpression: return InferTypeInSimpleLambdaExpression(simpleLambdaExpression);
case SwitchLabelSyntax switchLabel: return InferTypeInSwitchLabel(switchLabel);
case SwitchStatementSyntax switchStatement: return InferTypeInSwitchStatement(switchStatement);
case ThrowExpressionSyntax throwExpression: return InferTypeInThrowExpression(throwExpression);
case ThrowStatementSyntax throwStatement: return InferTypeInThrowStatement(throwStatement);
case UsingStatementSyntax usingStatement: return InferTypeInUsingStatement(usingStatement);
case WhileStatementSyntax whileStatement: return InferTypeInWhileStatement(whileStatement);
......@@ -1971,6 +1972,17 @@ private ISymbol GetDeclaredMemberSymbolFromOriginalSemanticModel(SemanticModel c
return SpecializedCollections.SingletonEnumerable(new TypeInferenceInfo(this.Compilation.GetSpecialType(SpecialType.System_Int32)));
}
private IEnumerable<TypeInferenceInfo> InferTypeInThrowExpression(ThrowExpressionSyntax throwExpression, SyntaxToken? previousToken = null)
{
// If we have a position, it has to be after the 'throw' keyword.
if (previousToken.HasValue && previousToken.Value != throwExpression.ThrowKeyword)
{
return SpecializedCollections.EmptyEnumerable<TypeInferenceInfo>();
}
return SpecializedCollections.SingletonEnumerable(new TypeInferenceInfo(this.Compilation.ExceptionType()));
}
private IEnumerable<TypeInferenceInfo> InferTypeInThrowStatement(ThrowStatementSyntax throwStatement, SyntaxToken? previousToken = null)
{
// If we have a position, it has to be after the 'throw' keyword.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册