mirror of
https://github.com/CatalaLang/catala.git
synced 2024-11-08 07:51:43 +03:00
Fix bug in Python backend
This commit is contained in:
parent
0f999fb285
commit
974c2b8d58
@ -20,6 +20,8 @@ module L = Lcalc.Ast
|
||||
module TopLevelName = Uid.Make (Uid.MarkedString) ()
|
||||
module LocalName = Uid.Make (Uid.MarkedString) ()
|
||||
|
||||
let dead_value = LocalName.fresh ("dead_value", Pos.no_pos)
|
||||
|
||||
type expr =
|
||||
| EVar of LocalName.t
|
||||
| EFunc of TopLevelName.t
|
||||
|
@ -242,7 +242,18 @@ and translate_statements (ctxt : ctxt) (block_expr : 'm L.marked_expr) : A.block
|
||||
let s_e_try = translate_statements ctxt e_try in
|
||||
let s_e_catch = translate_statements ctxt e_catch in
|
||||
[A.STryExcept (s_e_try, except, s_e_catch), D.pos block_expr]
|
||||
| L.ERaise except -> [A.SRaise except, D.pos block_expr]
|
||||
| L.ERaise except ->
|
||||
(* Before raising the exception, we still give a dummy definition to the
|
||||
current variable so that tools like mypy don't complain. *)
|
||||
(match ctxt.inside_definition_of with
|
||||
| None -> []
|
||||
| Some x ->
|
||||
[
|
||||
( A.SLocalDef
|
||||
((x, D.pos block_expr), (Ast.EVar Ast.dead_value, D.pos block_expr)),
|
||||
D.pos block_expr );
|
||||
])
|
||||
@ [A.SRaise except, D.pos block_expr]
|
||||
| _ -> (
|
||||
let e_stmts, new_e = translate_expr ctxt block_expr in
|
||||
e_stmts
|
||||
|
@ -614,6 +614,13 @@ def no_input() -> Callable[[Unit], Alpha]:
|
||||
raise EmptyError
|
||||
return closure
|
||||
|
||||
|
||||
# This value is used for the Python code generation to trump mypy and forcing
|
||||
# it to accept dead code. Indeed, when raising an exception during a variable
|
||||
# definition, mypy complains that the later dead code will not know what
|
||||
# this variable was. So we give this variable a dead value.
|
||||
dead_value: Any = 0
|
||||
|
||||
# =======
|
||||
# Logging
|
||||
# =======
|
||||
|
10
tests/test_scope/good/nothing.catala_en
Normal file
10
tests/test_scope/good/nothing.catala_en
Normal file
@ -0,0 +1,10 @@
|
||||
## Test
|
||||
|
||||
```catala
|
||||
declaration scope Foo2:
|
||||
output bar content integer
|
||||
```
|
||||
|
||||
```catala-test {id="foo_2_scalc"}
|
||||
catala Scalc -s Foo2 -O -t
|
||||
```
|
@ -0,0 +1,7 @@
|
||||
let Foo2 (Foo2_in : Foo2_in{}) =
|
||||
decl temp_bar : any;
|
||||
temp_bar = dead_value_1;
|
||||
raise NoValueProvided;
|
||||
decl bar : integer;
|
||||
bar = temp_bar_4;
|
||||
return Foo2_out {"bar_out": bar_3}
|
@ -1,8 +1,7 @@
|
||||
let Foo_29 : Foo_in{} → Foo_out{"bar_out": integer} =
|
||||
λ (Foo_in_30: Foo_in{}) →
|
||||
let bar_31 : integer =
|
||||
let Foo =
|
||||
λ (Foo_in_28: Foo_in{}) →
|
||||
let bar_29 : integer =
|
||||
try
|
||||
handle_default_0 [] (λ (__32: any) → true) (λ (__33: any) → 0)
|
||||
handle_default_0 [] (λ (__30: any) → true) (λ (__31: any) → 0)
|
||||
with EmptyError -> raise NoValueProvided in
|
||||
Foo_out {"bar_out": bar_31} in
|
||||
Foo_29
|
||||
Foo_out {"bar_out": bar_29}
|
||||
|
@ -9,5 +9,5 @@ scope Foo:
|
||||
```
|
||||
|
||||
```catala-test {id="Lcalc"}
|
||||
catala Lcalc
|
||||
catala Lcalc -s Foo
|
||||
```
|
||||
|
Loading…
Reference in New Issue
Block a user