Browse Source

Fix `throw switch-expr;` by specifying the typeHint for `throw`.

pull/4065/head
Daniel Grunwald 3 weeks ago
parent
commit
1a784e2fa9
  1. 9
      ICSharpCode.Decompiler.Tests/TestCases/Pretty/SwitchExpressions.cs
  2. 3
      ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
  3. 3
      ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

9
ICSharpCode.Decompiler.Tests/TestCases/Pretty/SwitchExpressions.cs

@ -244,6 +244,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty @@ -244,6 +244,15 @@ namespace ICSharpCode.Decompiler.Tests.TestCases.Pretty
}).ToValidated();
}
public static void ThrowDifferentExceptions(int i)
{
throw i switch {
0 => (Exception)new ArgumentException("Invalid argument"),
1 => new InvalidOperationException("Invalid operation"),
_ => new NotSupportedException(),
};
}
public static int SwitchOnStringImplicitDefault(string s)
{
return s switch {

3
ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs

@ -1272,7 +1272,8 @@ namespace ICSharpCode.Decompiler.CSharp @@ -1272,7 +1272,8 @@ namespace ICSharpCode.Decompiler.CSharp
protected internal override TranslatedExpression VisitThrow(Throw inst, TranslationContext context)
{
return new ThrowExpression(Translate(inst.Argument))
var ex = Translate(inst.Argument, typeHint: compilation.FindType(KnownTypeCode.Exception));
return new ThrowExpression(ex)
.WithILInstruction(inst)
.WithRR(new ThrowResolveResult());
}

3
ICSharpCode.Decompiler/CSharp/StatementBuilder.cs

@ -434,7 +434,8 @@ namespace ICSharpCode.Decompiler.CSharp @@ -434,7 +434,8 @@ namespace ICSharpCode.Decompiler.CSharp
protected internal override TranslatedStatement VisitThrow(Throw inst)
{
return new ThrowStatement(exprBuilder.Translate(inst.Argument)).WithILInstruction(inst);
var ex = exprBuilder.Translate(inst.Argument, typeHint: typeSystem.FindType(KnownTypeCode.Exception));
return new ThrowStatement(ex).WithILInstruction(inst);
}
protected internal override TranslatedStatement VisitRethrow(Rethrow inst)

Loading…
Cancel
Save