-
Notifications
You must be signed in to change notification settings - Fork 104
Implement let mutable
#3964
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement let mutable
#3964
Conversation
Parser Change ChecklistThis PR modifies the parser. Please check that the following tests are updated:
This test should have examples of every new bit of syntax you are adding. Feel free to just check the box if your PR does not actually change the syntax (because it is refactoring the parser, say). |
I've been leaving myself comments in the diff (all start with |
I think there are some tricky questions to be asked and answered wrt modes - let's discuss in other channels. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a quick pass over some of the places where you had questions - we'll chat more tomorrow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK - I've now read everything except the modes bits we're going to discuss with @riaqn.
I pushed some change to mode checking, more needs to be done. For example, type checking |
Feature request: not mutating a mutable variable should result in a warning Implemented here: #4089 |
jane/doc/extensions/_08-miscellaneous-extensions/let-mutable.md
Outdated
Show resolved
Hide resolved
jane/doc/extensions/_08-miscellaneous-extensions/let-mutable.md
Outdated
Show resolved
Hide resolved
jane/doc/extensions/_08-miscellaneous-extensions/let-mutable.md
Outdated
Show resolved
Hide resolved
@ccasin 's comment that
From some local testing, I think the patch currently gets the first two right and fails with an internal compiler failure on the third, but all three should have tests. |
Don't forget monadic operators: let (let*) x f = f x in
let mutable x = 42 in
let* y = 0 in
x + y (This example should be forbidden as |
@stedolan @lthls Tests have been added: 4c24f74 Every test has the behavior described except one: Locally defined functors is not giving me a compiler error like you said. The code you gave has syntax errors, so I fixed it up like this: module type T = sig module F () : sig val x : int end end
let m =
let mutable x = 42 in
(module (struct module F () = struct let x = x end end) : T) Though this might not be what you intended. Anyway, this compiles just fine for me, and it seems that module type T = sig module F () : sig val x : int end end
let m =
let mutable x = 42 in
let module M = (struct module F () = struct let x = x end end) in
x <- 10;
(module M : T)
let x =
let module M = (val m : T) in
let module M = M.F() in
M.x
let () = print_int x This program prints |
Removing this test of unboxed products because it is not supported in lambda. let triangle_i64_i32_f64 n =
let mutable sum = #(#0L, #(#0l, #0.)) in
for i = 1 to n do
let #(a, #(b, c)) = sum in
sum <- #(Int64_u.add a (Int64_u.of_int i),
#(Int32_u.add b (Int32_u.of_int i),
Float_u.add c (Float_u.of_int i)))
done;
sum
let () =
let #(a, #(b, c)) = triangle_i64_i32_f64 10 in
Printf.printf "%d %d %.2f\n" (Int64_u.to_int a)
(Int32_u.to_int b)
(Float_u.to_float c) |
Continuing on this thread, I've updated |
I'm confused, it fails for me with:
|
@stedolan It is disallowed now: e94b66a#diff-748f86aa68fdeea50b75e3d9c839c6f365d55d0f2f49f141cbaab409ffa12247 It was actually an existing bug, fixed by Zesen's |
jane/doc/extensions/_11-miscellaneous-extensions/let-mutable.md
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to merge once remaining comments are addressed. Thanks!
The unboxed product support is now in #4184 (which will merge into this PR). |
* Middle end support for mutable vars of non-singleton layouts * Add and fix tests --------- Co-authored-by: James Rayman <james@jamesrayman.com>
Implement
let mutable
.This feature makes explicit an existing optimization which puts
ref
s in registers or the call stack (which eliminates an allocation). Seejane/doc/extensions/_08-miscellaneous-extensions/let-mutable.md
for more.Most relevant functionality is tested in
testsuite/tests/typing-local/let_mutable.ml
, and other tests have been modified (e.g.typing-local/alloc.ml
now makes surelet mutable
does not allocate). Additional testing with unboxed types is done intestsuite/tests/typing-layouts/let_mutable.ml
. Parse error tests have been added totestsuite/tests/parse-errors
.