Skip to content

Commit a5a8908

Browse files
committed
Fixes bug where input arguments could be shadowed by mem variables.
1 parent caeb92d commit a5a8908

File tree

8 files changed

+38
-3
lines changed

8 files changed

+38
-3
lines changed

dune-project

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
(package
1616
(name vult)
1717
(synopsis "Transcompiler for DSP audio")
18-
(depends ocaml dune containers pla js_of_ocaml js_of_ocaml-ppx ounit yojson)
18+
(depends ocaml dune containers pla js_of_ocaml js_of_ocaml-ppx ounit yojson (ocamlformat (= "0.27.0")))
1919
)
2020

2121
; removes the Dune__exe prefix

runtime/vultin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ fix16_t fix_random() {
152152
return (fix16_t)temp;
153153
}
154154

155-
int irandom() { return (int)rand(); }
155+
int int_random() { return (int)rand(); }
156156

157157
void float_print(float value) { printf("%f\n", value); }
158158
void fix_print(fix16_t value) { printf("%f\n", fix_to_float(value)); }

runtime/vultin.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ static_inline int16_t *int16_wrap_array(const int16_t x[]) { return (int16_t *)x
205205
/* Random numbers */
206206
float float_random();
207207
fix16_t fix_random();
208-
int irandom();
208+
int int_random();
209209

210210
/* Print values */
211211
void float_print(float value);

src/core/env.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,26 @@ let checkDuplicatedVal (locals : var Map.t list) (name : string) (loc : Loc.t) :
454454
locals
455455

456456

457+
(* Helper: Check if any argument names conflict with mem variables in the context *)
458+
let checkArgumentsAgainstContext (context : context) (args : Typed.arg list) : unit =
459+
match context with
460+
| Some (_, { descr = Record members; _ }) ->
461+
CCList.iter
462+
(fun ({ name; loc; _ } : Typed.arg) ->
463+
match Map.find name members with
464+
| None -> ()
465+
| Some found ->
466+
Error.raiseError
467+
("Function parameter '"
468+
^ name
469+
^ "' shadows a mem variable declared at "
470+
^ Loc.to_string_readable found.loc
471+
^ ". Rename the parameter or the mem variable to avoid this conflict.")
472+
loc)
473+
args
474+
| _ -> ()
475+
476+
457477
(* Helper: Create a reporter for mem variable updates that handles type unification *)
458478
let makeMemReporter (unify : Typed.type_ -> Typed.type_ -> bool) (t : Typed.type_) (found : var) (value : var) : var =
459479
if unify found.t t then
@@ -689,6 +709,7 @@ let enterFunction (env : env) (name : string) (args : Typed.arg list) (ret : Typ
689709
env * path * (Typed.type_ list * Typed.type_) =
690710
let m = getCurrentModule env in
691711
let context = getCurrentContext env in
712+
let () = checkArgumentsAgainstContext context args in
692713
let report (found : f) =
693714
Error.raiseError ("A function with the name '" ^ found.path.id ^ "' has already been declared.") loc
694715
in

test/errors/error46.base

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Function parameter 'value' shadows a mem variable declared at line 2, columns 7-12. Rename the parameter or the mem variable to avoid this conflict.
2+
}
3+
and helper(value:bool) : real {
4+
^^^^^

test/errors/error46.vult

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
fun process(input:real) {
2+
mem value;
3+
value = input;
4+
}
5+
and helper(value:bool) : real {
6+
if (value) return 1.0;
7+
else return 0.0;
8+
}

test/test.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ let errors_files =
149149
; "error43.vult"
150150
; "error44.vult"
151151
; "error45.vult"
152+
; "error46.vult"
152153
]
153154

154155

vult.opam

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ depends: [
1515
"js_of_ocaml-ppx"
1616
"ounit"
1717
"yojson"
18+
"ocamlformat" {= "0.27.0"}
1819
"odoc" {with-doc}
1920
]
2021
build: [

0 commit comments

Comments
 (0)