Skip to content

Commit

Permalink
wip code quality and naming issues
Browse files Browse the repository at this point in the history
  • Loading branch information
msprotz committed Sep 18, 2024
1 parent 32f8435 commit 8dd4299
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
19 changes: 11 additions & 8 deletions lib/AstToMiniRust.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1083,15 +1083,15 @@ and translate_pat env (p: Ast.pattern): MiniRust.pat =
| PRecord fields ->
(* Records (a.k.a. "flat") translate to Rust structs whose name is simply
the name of the type. *)
let name = Helpers.assert_tlid p.typ in
let name = fst name @ [ snd name ] in
let name = lookup_type env (Helpers.assert_tlid p.typ) in
StructP (name, List.map (fun (f, p) -> f, translate_pat env p) fields)
| PCons (cons, pats) ->
(* Constructors (a.k.a. "variants"); need to mention
type_name::constructor, followed by fields (named) *)
let name = Helpers.assert_tlid p.typ in
let field_names = LidMap.find (fst name, snd name ^ "_" ^ cons) env.struct_fields in
let name = fst name @ [ snd name; cons ] in
let lid = Helpers.assert_tlid p.typ in
let name = lookup_type env lid in
let field_names = LidMap.find (fst lid, snd lid ^ "_" ^ cons) env.struct_fields in
let name = name @ [ cons ] in
StructP (name, List.map2 (fun f p ->
f.MiniRust.name, translate_pat env p
) field_names pats)
Expand Down Expand Up @@ -1194,10 +1194,13 @@ let bind_decl env (d: Ast.decl): env =
let lifetime = Idents.LidSet.mem lid env.pointer_holding_structs in
KPrint.bprintf "%a (VARIANT): lifetime=%b box=%b\n" PrintAst.Ops.plid lid lifetime box;
List.fold_left (fun env (cons, fields) ->
{ env with struct_fields = LidMap.add (fst lid, snd lid ^ "_" ^
cons) (List.map (fun (f, (t, _)) ->
(* TODO: change the type of keys to be either struct lid or variant lid * cons name *)
let cons_lid = fst lid, snd lid ^ "_" ^ cons in
let fields = List.map (fun (f, (t, _)) ->
{ MiniRust.name = f; visibility = Some Pub; typ = translate_type env t }
) fields) env.struct_fields }
) fields
in
{ env with struct_fields = LidMap.add cons_lid fields env.struct_fields }
) env branches
| _ ->
env
Expand Down
2 changes: 1 addition & 1 deletion lib/OptimizeMiniRust.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ let rec infer (env: env) (expected: typ) (known: known) (e: expr): known * expr
known, Call (Name n, targs, [ e1; e2 ])
) else (
KPrint.bprintf "[infer-mut,call] recursing on %s\n" (String.concat " :: " n);
failwith "TODO: recursion"
failwith "TODO: recursion or missing function"
)

| Call (Operator o, [], _) -> begin match o with
Expand Down
9 changes: 6 additions & 3 deletions lib/Simplify.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1426,15 +1426,17 @@ let rec fixup_return_pos e =
* switch nodes).
* *)
with_type e.typ (match e.node with
| ELet (_, ({ node = (EIfThenElse _ | ESwitch _); _ } as e), { node = EBound 0; _ }) ->
| ELet (_, ({ node = (EIfThenElse _ | ESwitch _ | EMatch _); _ } as e), { node = EBound 0; _ }) ->
(fixup_return_pos e).node
| ELet (_, ({ node = (EIfThenElse _ | ESwitch _); _ } as e),
| ELet (_, ({ node = (EIfThenElse _ | ESwitch _ | EMatch _); _ } as e),
{ node = ECast ({ node = EBound 0; _ }, t); _ }) ->
(nest_in_return_pos t (fun _ e -> with_type t (ECast (e, t))) (fixup_return_pos e)).node
| EIfThenElse (e1, e2, e3) ->
EIfThenElse (e1, fixup_return_pos e2, fixup_return_pos e3)
| ESwitch (e1, branches) ->
ESwitch (e1, List.map (fun (t, e) -> t, fixup_return_pos e) branches)
| EMatch (f, e1, branches) ->
EMatch (f, e1, List.map (fun (bs, pat, e) -> bs, pat, fixup_return_pos e) branches)
| ELet (b, e1, e2) ->
ELet (b, e1, fixup_return_pos e2)
| e ->
Expand Down Expand Up @@ -2074,7 +2076,8 @@ let simplify2 ifdefs (files: file list): file list =
let files = if Options.wasm () then files else fixup_while_tests#visit_files () files in
let files = hoist#visit_files [] files in
let files = if !Options.c89_scope then SimplifyC89.hoist_lets#visit_files (ref []) files else files in
let files = if Options.wasm () || Options.rust () then files else fixup_hoist#visit_files () files in
let files = if Options.wasm () then files else fixup_hoist#visit_files () files in
(* Disabled in Rust because this results in uninitialized variables *)
let files = if Options.wasm () || Options.rust () then files else let_if_to_assign#visit_files () files in
(* NB: could be disabled for Rust since the Rust checker will error out *)
let files = if Options.wasm () then files else hoist_bufcreate#visit_files ifdefs files in
Expand Down

0 comments on commit 8dd4299

Please sign in to comment.