Skip to content

TryCatch - Bad label content in ILGenerator #430

@snakex64

Description

@snakex64

Hi!

I keep getting "Bad label content in ILGenerator" when I try to compile a small try..catch expression, either I get it directly when I call "CompileFast" or I get it when I try to create the type if I use it with "CompileFastToIL".

The issue seems to be around the "return 10" inside the "Catch". It does the same behavior if I "return" inside the "try".

I have been trying to compile an expression that represent represent something like this:

try{
    Call();
}
catch(Exception)
{
    return 10;
}
Call();
return 1;

I wrote this tiny demo Expression to replicate the issue:

var returnLabelTarget = Expression.Label(typeof(int), "ReturnLabel");

var tryBody = Expression.Call(typeof(Console).GetMethod("Clear")!);
var catchBody = Expression.Return(returnLabelTarget, Expression.Constant(10));

var tryCatch = Expression.TryCatch(tryBody, Expression.Catch(typeof(Exception), catchBody));

var afterCatch = Expression.Block(
	Expression.Call(typeof(Console).GetMethod("Clear")!),
	Expression.Return(returnLabelTarget, Expression.Constant(5)),
	Expression.Label(returnLabelTarget, Expression.Constant(0)));

var body = Expression.Block(tryCatch, afterCatch);

var expression = Expression.Lambda(body);

var v = expression.CompileFast();

Any idea what I'm doing wrong?

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions