Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/codegen.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4030,15 +4030,21 @@ let gen_decl (ctx : context) (decl : top_level) : context result =
Silent on missing modules / unresolved items / loader errors: the
resolver runs before codegen and would have already errored. *)
(** #561: every name a program's bodies mention (function bodies, impl
methods, const initialisers). Over-approximate — parameters and locals
land in it too — which only matters as a filter: `use Mod;` imports the
methods, const initialisers), less each function's parameters (#815).
Locals are already excluded by {!free_names}. It is a filter: `use Mod;` imports the
public items of [Mod] that appear here, not all of them, so the host
is not asked to supply a `Dom.*` import for each of Dom's 38 functions
when the program calls one. *)
let program_free_names (prog : program) : (string, unit) Hashtbl.t =
let h = Hashtbl.create 64 in
let add = List.iter (fun n -> Hashtbl.replace h n ()) in
let of_fn (fd : fn_decl) = match fd.fd_body with
(* #815: a parameter is not an import reference. Resolve refuses a
qualified `Mod.f` that a local `f` would capture, so every free
occurrence of a parameter name in the body is the parameter. *)
let of_fn (fd : fn_decl) =
let params = List.map (fun (p : param) -> p.p_name.name) fd.fd_params in
let add ns = add (List.filter (fun n -> not (List.mem n params)) ns) in
match fd.fd_body with
| FnBlock b -> add (free_names_block b)
| FnExpr e -> add (free_names e)
| FnExtern -> ()
Expand Down
160 changes: 95 additions & 65 deletions lib/resolve.ml
Original file line number Diff line number Diff line change
Expand Up @@ -954,97 +954,127 @@ let import_qualifiers (imports : import_decl list) : (string, unit) Hashtbl.t =
let rec strip_span (e : expr) : expr =
match e with ExprSpan (e', _) -> strip_span e' | e -> e

let rec lower_expr quals (e : expr) : expr =
(* #815: [q] is the import qualifiers plus the local names in scope. A
qualified `Mod.fn` lowers to a bare `fn`, so a local `fn` in scope would
capture it; refuse that loudly instead of mis-binding the call. *)
let with_bound (quals, bound) names = (quals, names @ bound)

let rec lower_expr q (e : expr) : expr =
let (quals, bound) = q in
match e with
| ExprField (base, fld) ->
(match strip_span base with
| ExprVar m when Hashtbl.mem quals m.name -> ExprVar fld
| _ -> ExprField (lower_expr quals base, fld))
| ExprSpan (e', sp) -> ExprSpan (lower_expr quals e', sp)
| ExprVar m when Hashtbl.mem quals m.name ->
if List.mem fld.name bound then
raise (Parse_driver.Parse_error
(Printf.sprintf
"`%s.%s` is shadowed by a local `%s` in scope; rename the local"
m.name fld.name fld.name, fld.span));
ExprVar fld
| _ -> ExprField (lower_expr q base, fld))
| ExprSpan (e', sp) -> ExprSpan (lower_expr q e', sp)
| ExprLit _ | ExprVar _ | ExprVariant _ -> e
| ExprLet r ->
ExprLet { r with el_value = lower_expr quals r.el_value;
el_body = Option.map (lower_expr quals) r.el_body }
ExprLet { r with el_value = lower_expr q r.el_value;
el_body = Option.map
(lower_expr (with_bound q (pattern_binders r.el_pat)))
r.el_body }
| ExprIf r ->
ExprIf { ei_cond = lower_expr quals r.ei_cond;
ei_then = lower_expr quals r.ei_then;
ei_else = Option.map (lower_expr quals) r.ei_else }
ExprIf { ei_cond = lower_expr q r.ei_cond;
ei_then = lower_expr q r.ei_then;
ei_else = Option.map (lower_expr q) r.ei_else }
| ExprMatch r ->
ExprMatch { em_scrutinee = lower_expr quals r.em_scrutinee;
em_arms = List.map (lower_arm quals) r.em_arms }
| ExprLambda r -> ExprLambda { r with elam_body = lower_expr quals r.elam_body }
ExprMatch { em_scrutinee = lower_expr q r.em_scrutinee;
em_arms = List.map (lower_arm q) r.em_arms }
| ExprLambda r ->
let ps = List.map (fun (p : param) -> p.p_name.name) r.elam_params in
ExprLambda { r with elam_body = lower_expr (with_bound q ps) r.elam_body }
| ExprApp (f, args) ->
ExprApp (lower_expr quals f, List.map (lower_expr quals) args)
| ExprTupleIndex (e1, i) -> ExprTupleIndex (lower_expr quals e1, i)
| ExprIndex (a, i) -> ExprIndex (lower_expr quals a, lower_expr quals i)
| ExprTuple es -> ExprTuple (List.map (lower_expr quals) es)
| ExprArray es -> ExprArray (List.map (lower_expr quals) es)
ExprApp (lower_expr q f, List.map (lower_expr q) args)
| ExprTupleIndex (e1, i) -> ExprTupleIndex (lower_expr q e1, i)
| ExprIndex (a, i) -> ExprIndex (lower_expr q a, lower_expr q i)
| ExprTuple es -> ExprTuple (List.map (lower_expr q) es)
| ExprArray es -> ExprArray (List.map (lower_expr q) es)
| ExprRecord r ->
ExprRecord
{ er_fields =
List.map (fun (id, eo) -> (id, Option.map (lower_expr quals) eo))
List.map (fun (id, eo) -> (id, Option.map (lower_expr q) eo))
r.er_fields;
er_spread = Option.map (lower_expr quals) r.er_spread }
| ExprRowRestrict (e1, id) -> ExprRowRestrict (lower_expr quals e1, id)
| ExprBinary (l, op, r) -> ExprBinary (lower_expr quals l, op, lower_expr quals r)
| ExprUnary (op, e1) -> ExprUnary (op, lower_expr quals e1)
| ExprBlock b -> ExprBlock (lower_block quals b)
| ExprReturn eo -> ExprReturn (Option.map (lower_expr quals) eo)
er_spread = Option.map (lower_expr q) r.er_spread }
| ExprRowRestrict (e1, id) -> ExprRowRestrict (lower_expr q e1, id)
| ExprBinary (l, op, r) -> ExprBinary (lower_expr q l, op, lower_expr q r)
| ExprUnary (op, e1) -> ExprUnary (op, lower_expr q e1)
| ExprBlock b -> ExprBlock (lower_block q b)
| ExprReturn eo -> ExprReturn (Option.map (lower_expr q) eo)
| ExprBreak sp -> ExprBreak sp
| ExprContinue sp -> ExprContinue sp
| ExprTry r ->
ExprTry { et_body = lower_block quals r.et_body;
et_catch = Option.map (List.map (lower_arm quals)) r.et_catch;
et_finally = Option.map (lower_block quals) r.et_finally }
ExprTry { et_body = lower_block q r.et_body;
et_catch = Option.map (List.map (lower_arm q)) r.et_catch;
et_finally = Option.map (lower_block q) r.et_finally }
| ExprHandle r ->
ExprHandle { eh_body = lower_expr quals r.eh_body;
eh_handlers = List.map (lower_handler quals) r.eh_handlers }
| ExprResume eo -> ExprResume (Option.map (lower_expr quals) eo)
| ExprUnsafe ops -> ExprUnsafe (List.map (lower_unsafe quals) ops)

and lower_arm quals a =
{ a with ma_guard = Option.map (lower_expr quals) a.ma_guard;
ma_body = lower_expr quals a.ma_body }

and lower_handler quals = function
| HandlerReturn (p, e) -> HandlerReturn (p, lower_expr quals e)
| HandlerOp (id, ps, e) -> HandlerOp (id, ps, lower_expr quals e)

and lower_unsafe quals = function
| UnsafeRead e -> UnsafeRead (lower_expr quals e)
| UnsafeWrite (a, b) -> UnsafeWrite (lower_expr quals a, lower_expr quals b)
| UnsafeOffset (a, b) -> UnsafeOffset (lower_expr quals a, lower_expr quals b)
| UnsafeTransmute (t1, t2, e) -> UnsafeTransmute (t1, t2, lower_expr quals e)
| UnsafeForget e -> UnsafeForget (lower_expr quals e)

and lower_block quals b =
{ blk_stmts = List.map (lower_stmt quals) b.blk_stmts;
blk_expr = Option.map (lower_expr quals) b.blk_expr }

and lower_stmt quals = function
| StmtLet r -> StmtLet { r with sl_value = lower_expr quals r.sl_value }
| StmtExpr e -> StmtExpr (lower_expr quals e)
| StmtAssign (a, op, b) ->
StmtAssign (lower_expr quals a, op, lower_expr quals b)
| StmtWhile (e, b) -> StmtWhile (lower_expr quals e, lower_block quals b)
| StmtFor (p, e, b) -> StmtFor (p, lower_expr quals e, lower_block quals b)
ExprHandle { eh_body = lower_expr q r.eh_body;
eh_handlers = List.map (lower_handler q) r.eh_handlers }
| ExprResume eo -> ExprResume (Option.map (lower_expr q) eo)
| ExprUnsafe ops -> ExprUnsafe (List.map (lower_unsafe q) ops)

and lower_arm q a =
let q = with_bound q (pattern_binders a.ma_pat) in
{ a with ma_guard = Option.map (lower_expr q) a.ma_guard;
ma_body = lower_expr q a.ma_body }

and lower_handler q = function
| HandlerReturn (p, e) ->
HandlerReturn (p, lower_expr (with_bound q (pattern_binders p)) e)
| HandlerOp (id, ps, e) ->
HandlerOp (id, ps,
lower_expr (with_bound q (List.concat_map pattern_binders ps)) e)

and lower_unsafe q = function
| UnsafeRead e -> UnsafeRead (lower_expr q e)
| UnsafeWrite (a, b) -> UnsafeWrite (lower_expr q a, lower_expr q b)
| UnsafeOffset (a, b) -> UnsafeOffset (lower_expr q a, lower_expr q b)
| UnsafeTransmute (t1, t2, e) -> UnsafeTransmute (t1, t2, lower_expr q e)
| UnsafeForget e -> UnsafeForget (lower_expr q e)

and lower_block q b =
let rec go q acc = function
| [] -> (List.rev acc, q)
| (StmtLet r) :: rest ->
let s = StmtLet { r with sl_value = lower_expr q r.sl_value } in
go (with_bound q (pattern_binders r.sl_pat)) (s :: acc) rest
| st :: rest -> go q (lower_stmt q st :: acc) rest
in
let stmts, q_after = go q [] b.blk_stmts in
{ blk_stmts = stmts; blk_expr = Option.map (lower_expr q_after) b.blk_expr }

let lower_fn_body quals = function
| FnBlock b -> FnBlock (lower_block quals b)
| FnExpr e -> FnExpr (lower_expr quals e)
and lower_stmt q = function
| StmtLet r -> StmtLet { r with sl_value = lower_expr q r.sl_value }
| StmtExpr e -> StmtExpr (lower_expr q e)
| StmtAssign (a, op, b) ->
StmtAssign (lower_expr q a, op, lower_expr q b)
| StmtWhile (e, b) -> StmtWhile (lower_expr q e, lower_block q b)
| StmtFor (p, e, b) ->
StmtFor (p, lower_expr q e, lower_block (with_bound q (pattern_binders p)) b)

let lower_fn_body quals (fd : fn_decl) =
let q = (quals, List.map (fun (p : param) -> p.p_name.name) fd.fd_params) in
match fd.fd_body with
| FnBlock b -> FnBlock (lower_block q b)
| FnExpr e -> FnExpr (lower_expr q e)
| FnExtern -> FnExtern

let lower_top quals = function
| TopFn fd -> TopFn { fd with fd_body = lower_fn_body quals fd.fd_body }
| TopConst r -> TopConst { r with tc_value = lower_expr quals r.tc_value }
| TopFn fd -> TopFn { fd with fd_body = lower_fn_body quals fd }
| TopConst r -> TopConst { r with tc_value = lower_expr (quals, []) r.tc_value }
| TopImpl ib ->
TopImpl { ib with ib_items = List.map (function
| ImplFn fd -> ImplFn { fd with fd_body = lower_fn_body quals fd.fd_body }
| ImplFn fd -> ImplFn { fd with fd_body = lower_fn_body quals fd }
| ImplType _ as it -> it) ib.ib_items }
| TopTrait td ->
TopTrait { td with trd_items = List.map (function
| TraitFnDefault fd ->
TraitFnDefault { fd with fd_body = lower_fn_body quals fd.fd_body }
TraitFnDefault { fd with fd_body = lower_fn_body quals fd }
| other -> other) td.trd_items }
| (TopType _ | TopEffect _ | TopExternType _ | TopExternFn _) as t -> t

Expand Down
37 changes: 37 additions & 0 deletions test/test_e2e.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4825,9 +4825,46 @@ let test_822_wasm_crypto_unused_import_ok () =
| Error msg -> Alcotest.failf "an unused Crypto import should compile: %s" msg
| Ok () -> ()

(* #815: `use Mod;` imports only names that bind to Mod, not parameters. *)
let imports815 (src : string) : string list =
let prog = Resolve.lower_qualified_value_paths
(Parse_driver.parse_string ~file:"<t815>" src) in
let stdlib_dir = List.find Sys.file_exists [
"stdlib"; "../../../stdlib"; "../../../../stdlib" ] in
let loader = Module_loader.create {
Module_loader.stdlib_path = stdlib_dir; search_paths = [];
current_dir = stdlib_dir } in
match Codegen.generate_module ~loader prog with
| Error e -> Alcotest.failf "codegen failed: %s" (Codegen.show_codegen_error e)
| Ok m -> List.map (fun (i : Wasm.import) -> i.i_module ^ "." ^ i.i_name) m.imports

let test_815_param_is_not_import () =
Alcotest.(check bool) "no Console.log import" false
(List.mem "Console.log"
(imports815 "use Console;\npub fn main(log: Int) -> Int { log }"))

let test_815_qualified_call_imports () =
Alcotest.(check bool) "Console.log imported" true
(List.mem "Console.log"
(imports815 "use Console;\npub fn main(n: Int) -> Int { Console.log(\"x\"); n }"))

let test_815_captured_qualified_call_rejected () =
match Resolve.lower_qualified_value_paths
(Parse_driver.parse_string ~file:"<t815>"
"use Console;\npub fn main(log: Int) -> Int { Console.log(\"x\"); log }") with
| _ -> Alcotest.fail "expected a shadowing error"
| exception Parse_driver.Parse_error (msg, _) ->
Alcotest.(check bool) "names the shadowing local" true
(String.length msg > 0
&& (try ignore (Str.search_forward (Str.regexp_string "shadowed by a local `log`") msg 0); true
with Not_found -> false))

let modmut561_tests = [
Alcotest.test_case "#822: a Crypto extern use on core WASM is rejected by name" `Quick test_822_wasm_crypto_use_rejected;
Alcotest.test_case "#822: an unused Crypto import still compiles" `Quick test_822_wasm_crypto_unused_import_ok;
Alcotest.test_case "#815: a parameter named like an import is not imported" `Quick test_815_param_is_not_import;
Alcotest.test_case "#815: a qualified Mod.f call keeps its import" `Quick test_815_qualified_call_imports;
Alcotest.test_case "#815: a local that captures Mod.f is refused" `Quick test_815_captured_qualified_call_rejected;
Alcotest.test_case "wasm: same-name let of a Float const is f64" `Quick test_modmut561_float_global_same_name_let;
Alcotest.test_case "wasm: imported Float const is an f64 global" `Quick test_modmut561_imported_float_const;
Alcotest.test_case "wasm: record const field loads at its offset" `Quick test_modmut561_record_const_field_offset;
Expand Down
Loading