1+ #if NET8_0_OR_GREATER
2+ using System ;
3+ using System . Reflection . Emit ;
4+ using System . Text . Json ;
5+
6+ #if LIGHT_EXPRESSION
7+ using FastExpressionCompiler . LightExpression . ImTools ;
8+ using static FastExpressionCompiler . LightExpression . Expression ;
9+ namespace FastExpressionCompiler . LightExpression . IssueTests ;
10+ #else
11+ using System . Linq . Expressions ;
12+ using static System . Linq . Expressions . Expression ;
13+ namespace FastExpressionCompiler . IssueTests ;
14+ #endif
15+
16+ public struct Issue495_Incomplete_pattern_detection_for_NotSupported_1007_Return_goto_from_TryCatch_with_Assign_generates_invalid_IL : ITestX
17+ {
18+ public void Run ( TestRun t )
19+ {
20+ ReturnGotoFromTryCatchWithAssign_ShouldBeDetectedAsError1007 ( t ) ;
21+ }
22+
23+ public void ReturnGotoFromTryCatchWithAssign_ShouldBeDetectedAsError1007 ( TestContext t )
24+ {
25+ // Arrange: Build expression with Return(label, Assign(...)) inside TryCatch
26+ var variable = Variable ( typeof ( object ) , "var" ) ;
27+ var finalResult = Variable ( typeof ( object ) , "finalResult" ) ;
28+ var returnLabel = Label ( typeof ( object ) , "return" ) ;
29+ var exceptionParam = Parameter ( typeof ( Exception ) , "ex" ) ;
30+
31+ var block = Block (
32+ new [ ] { variable , finalResult } ,
33+ TryCatch (
34+ Block (
35+ typeof ( void ) ,
36+ Assign ( variable , Constant ( "hello" , typeof ( object ) ) ) ,
37+ IfThen (
38+ NotEqual ( variable , Constant ( null , typeof ( object ) ) ) ,
39+ // FEC should detect this as error 1007 and reject it
40+ Return ( returnLabel , Assign ( finalResult , variable ) , typeof ( object ) )
41+ // @wip other patters:
42+ // - Return(label, Block(Assign(var, value), value))
43+ // - Return(label, Call(MethodThatAssigns, ref var, value))
44+ // - Return(label, Coalesce(value, Assign(var, default)))
45+ ) ,
46+ Assign ( finalResult , Constant ( "default" , typeof ( object ) ) ) ,
47+ Label ( returnLabel , Constant ( "fallback" , typeof ( object ) ) )
48+ ) ,
49+ Catch ( exceptionParam , Empty ( ) )
50+ ) ,
51+ finalResult
52+ ) ;
53+
54+ var expr = Lambda < Func < object > > ( block ) ;
55+
56+ expr . PrintCSharp ( ) ;
57+ var @cs = ( Func < object > ) ( ( ) => //object
58+ {
59+ object var = null ;
60+ object finalResult = null ;
61+ try
62+ {
63+ var = ( object ) "hello" ;
64+ if ( var != null )
65+ {
66+ return finalResult = var ;
67+ } ; // todo: @wip remove ;
68+ finalResult = ( object ) "default" ;
69+ // return:; // todo: @wip remove or comment or rename but make it a valid c#
70+ }
71+ catch ( Exception ex ) // no need for ex
72+ {
73+ ; // todo: @wip remove ;
74+ }
75+ return finalResult ;
76+ } ) ;
77+
78+
79+ var fs = expr . CompileSys ( ) ;
80+ fs . PrintIL ( format : ILDecoder . ILFormat . AssertOpCodes ) ;
81+ fs ( ) ;
82+
83+ // Act: CompileFast should throw NotSupportedExpressionException or return null
84+ // var ff = expr.CompileFast(ifFastFailedReturnNull: true);
85+ // ff.PrintIL(format: ILDecoder.ILFormat.AssertOpCodes);
86+
87+ // // Expected: compiled should be null (pattern detected as unsupported)
88+ // t.IsNull(ff);
89+ }
90+ }
91+ #endif
0 commit comments