Skip to content

Commit

Permalink
working (without type-checking)
Browse files Browse the repository at this point in the history
  • Loading branch information
crusso committed Dec 18, 2024
1 parent bb81d3f commit e3621d6
Show file tree
Hide file tree
Showing 7 changed files with 56 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/lowering/desugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,8 @@ and build_actor at ts exp_opt self_id es obj_typ =
let vi = fresh_var ("v_"^i) (T.as_immut t) in
(i, switch_optE (dotE (varE v) i (T.Opt (T.as_immut t)))
(primE (Ir.OtherPrim "trap")
[textE ("stable variable " ^ i ^ " expected but not found")])
[textE (Printf.sprintf
"stable variable `%s` of type `%s` expected but not found" i (T.string_of_typ t))])
(varP vi) (varE vi)
(T.as_immut t))) dom_fields) dom_fields)
(letE v_rng (callE e [] (varE v_dom))
Expand Down
20 changes: 20 additions & 0 deletions test/run-drun/ok/upgrade-migration.drun.ok
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
ingress Completed: Reply: 0x4449444c016c01b3c4b1f204680100010a00000000000000000101
debug.print: Version 0
ingress Completed: Reply: 0x4449444c0000
ingress Completed: Reply: 0x4449444c0000
debug.print: {migration = {new = {three = [var (1, "1"), (2, "2"), (3, "3"), (4, "4")]}; old = {one = [var 1, 2, 3, 4]; two = [var "1", "2", "3", "4"]}}}
debug.print: Version 1
ingress Completed: Reply: 0x4449444c0000
ingress Completed: Reply: 0x4449444c0000
debug.print: {three = [var (1, "1"), (2, "2"), (3, "3"), (4, "4")]}
ingress Completed: Reply: 0x4449444c0000
debug.print: {migration = {new = {four = [var (1, "1"), (2, "2"), (3, "3"), (4, "4")]}; old = {three = [var (1, "1"), (2, "2"), (3, "3"), (4, "4")]}}}
debug.print: Version 2
ingress Completed: Reply: 0x4449444c0000
ingress Completed: Reply: 0x4449444c0000
debug.print: {four = [var (1, "1"), (2, "2"), (3, "3"), (4, "4")]}
ingress Completed: Reply: 0x4449444c0000
ingress Err: IC0503: Error from Canister rwlgt-iiaaa-aaaaa-aaaaa-cai: Canister called `ic0.trap` with message: stable variable `three` of type `var [var (Nat, Text)]` expected but not found.
Consider gracefully handling failures from this canister or altering the canister to handle exceptions. See documentation: http://internetcomputer.org/docs/current/references/execution-errors#trapped-explicitly
debug.print: {four = [var (1, "1"), (2, "2"), (3, "3"), (4, "4")]}
ingress Completed: Reply: 0x4449444c0000
27 changes: 21 additions & 6 deletions test/run-drun/upgrade-migration/Migration1.mo
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
import Prim "mo:prim";
module {

public func run( o: { var one : [var Nat];
var two : [var Nat];
} ) :
{ var three : [var (Nat,Nat)] } {
func zip<T,U>(ts : [var T], us: [var U]) : [var (T, U)] {
let size = if (ts.size() <= us.size()) ts.size() else us.size();
if (size == 0) return [var];
let res = Prim.Array_init<(T,U)>(size, (ts[0], us[0]));
for (i in res.keys()) {
res[i] := (ts[i], us[i]);
};
res
};

public func run( old : {
var one : [var Nat];
var two : [var Text];
} ) :
{ var three : [var (Nat, Text)] } {
let new =
{
var three = [var (o.one[0], o.two[0])]
}
var three = zip(old.one, old.two);
};
Prim.debugPrint(debug_show{migration={old;new}});
new
}

}
13 changes: 7 additions & 6 deletions test/run-drun/upgrade-migration/Migration2.mo
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import Prim "mo:prim";

module {

public func run( pre : { var three : [var (Nat,Nat)] } ) :
{ var four : [var (Nat,Nat)] } {
let post =
public func run( old : { var three : [var (Nat, Text)] } ) :
{ var four : [var (Nat, Text)] } {
let new =
{
var four = pre.three;
var four = old.three;
};
Prim.debugPrint(debug_show{pre;post});
post
Prim.debugPrint(debug_show {migration = {old; new}});
new
}

}
4 changes: 2 additions & 2 deletions test/run-drun/upgrade-migration/version0.mo
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import Prim "mo:prim";
actor {
Prim.debugPrint("Version 0");

stable var one : [var Nat] = [var 1,2];
stable var two : [var Nat] = [var 1,2];
stable var one : [var Nat] = [var 1, 2, 3, 4];
stable var two : [var Text] = [var "1", "2", "3", "4"];

public func check(): async() {
};
Expand Down
3 changes: 2 additions & 1 deletion test/run-drun/upgrade-migration/version1.mo
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ actor [Migration.run] {

stable var version = 0;

stable var three : [var (Nat,Nat)] = [var ];
stable var three : [var (Nat, Text)] = [var];

public func check(): async() {
Prim.debugPrint (debug_show {three});
}
};
3 changes: 2 additions & 1 deletion test/run-drun/upgrade-migration/version2.mo
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ actor [Migration.run] {
Prim.debugPrint("Version 2");

stable var version = 2;
stable var four : [var (Nat,Nat)] = [var];
stable var four : [var (Nat, Text)] = [var];

public func check(): async() {
Prim.debugPrint(debug_show{four});
}
};

0 comments on commit e3621d6

Please sign in to comment.