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

This commit is contained in:
Daniel Grunwald
2026-08-28 09:19:11 +02:00
parent ca24b8f3af
commit 1a784e2fa9
3 changed files with 13 additions and 2 deletions
@@ -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 {
@@ -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());
}
@@ -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)