Skip to content
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

[compiler v2][type checker] Make arithmetic operators generic with constraints #9792

Merged
merged 3 commits into from
Sep 1, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module 0x42::assign {
}
private fun assign_pattern(s: assign::S,f: u64,h: u64) {
assign::S{ f: f: u64, g: assign::T{ h: h: u64 } } = s;
Add(f, h)
Add<u64>(f, h)
}
private fun assign_struct(s: &mut assign::S) {
s = pack assign::S(42, pack assign::T(42));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module 0x42::borrow {
}
private fun mut_expr(x: u64) {
{
let r: &mut u64 = Borrow(Mutable)(Add(x, 1));
let r: &mut u64 = Borrow(Mutable)(Add<u64>(x, 1));
r = 22;
Deref(r)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
module 0x42::if_else {
private fun if_else(cond: bool,x: u64) {
if cond {
Add(x, 1)
Add<u64>(x, 1)
} else {
Sub(x, 1)
Sub<u64>(x, 1)
}
}
private fun if_else_nested(cond: bool,x: u64) {
if Gt(if cond {
Add(x, 1)
if Gt<u64>(if cond {
Add<u64>(x, 1)
} else {
Sub(x, 1)
Sub<u64>(x, 1)
}, 10) {
Mul(x, 2)
Mul<u64>(x, 2)
} else {
Div(x, 2)
Div<u64>(x, 2)
}
}
} // end 0x42::if_else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
module 0x42::loops {
private fun nested_loop(x: u64) {
loop {
if Gt(x, 0) {
if Gt<u64>(x, 0) {
loop {
if Gt(x, 10) {
x: u64 = Sub(x, 1);
if Gt<u64>(x, 10) {
x: u64 = Sub<u64>(x, 1);
break;
Tuple()
} else {
break
}
};
x: u64 = Sub(x, 1);
x: u64 = Sub<u64>(x, 1);
continue;
Tuple()
} else {
Expand All @@ -23,8 +23,8 @@ module 0x42::loops {
}
private fun while_loop(x: u64) {
loop {
if Gt(x, 0) {
x: u64 = Sub(x, 1);
if Gt<u64>(x, 0) {
x: u64 = Sub<u64>(x, 1);
Tuple()
} else {
break
Expand All @@ -34,7 +34,7 @@ module 0x42::loops {
}
private fun while_loop_with_break_and_continue(x: u64) {
loop {
if Gt(x, 0) {
if Gt<u64>(x, 0) {
if Eq<u64>(x, 42) {
break
} else {
Expand All @@ -45,7 +45,7 @@ module 0x42::loops {
} else {
Tuple()
};
x: u64 = Sub(x, 1);
x: u64 = Sub<u64>(x, 1);
Tuple()
} else {
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
module 0x42::loop_invalid {
private fun misplaced_break(x: u64) {
loop {
if Gt(x, 0) {
if Gt<u64>(x, 0) {
break
} else {
break
Expand All @@ -14,7 +14,7 @@ module 0x42::loop_invalid {
private fun misplaced_continue(x: u64) {
continue;
loop {
if Gt(x, 0) {
if Gt<u64>(x, 0) {
continue
} else {
break
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// ---- Model Dump
module 0x42::operators {
private fun arithm(x: u64,y: u64) {
Add(x, Mod(Mul(Div(y, Sub(x, y)), y), x))
Add<u64>(x, Mod<u64>(Mul<u64>(Div<u64>(y, Sub<u64>(x, y)), y), x))
}
private fun bits(x: u64,y: u8) {
BitAnd(Shl(x, y), x)
BitAnd<u64>(Shl<u64>(x, y), x)
}
private fun bools(x: bool,y: bool) {
Or(Or(Or(And(x, y), And(x, Not(y))), And(Not(x), y)), And(Not(x), Not(y)))
Expand All @@ -16,7 +16,7 @@ module 0x42::operators {
Neq<T>(x, y)
}
private fun order(x: u64,y: u64) {
And(And(And(Lt(x, y), Le(x, y)), Not(Gt(x, y))), Not(Ge(x, y)))
And(And(And(Lt<u64>(x, y), Le<u64>(x, y)), Not(Gt<u64>(x, y))), Not(Ge<u64>(x, y)))
}
} // end 0x42::operators

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ module 0x42::tuple {
f: u64,
}
private fun tuple(x: u64) {
Tuple(x, pack tuple::S(Add(x, 1)))
Tuple(x, pack tuple::S(Add<u64>(x, 1)))
}
private fun use_tuple(x: u64) {
{
let (x: u64, tuple::S{ f: y: u64 }) = tuple::tuple(x);
Add(x, y)
Add<u64>(x, y)
}
}
} // end 0x42::tuple
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module 0x42::tuple_invalid {
f: u64,
}
private fun tuple(x: u64) {
Tuple(x, pack tuple_invalid::S(Add(x, 1)))
Tuple(x, pack tuple_invalid::S(Add<u64>(x, 1)))
}
private fun use_tuple1(x: u64) {
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ error: expected `S` but found `M::S`
│ ^^^^

error: invalid call of `M::bar`: expected `M::S` but found `S` for argument 1
┌─ tests/checking/naming/generics_shadowing_invalid.move:9:9
┌─ tests/checking/naming/generics_shadowing_invalid.move:9:13
9 │ bar(s1);
^^^^^^^
^^

error: expected `S` but found `M::S`
┌─ tests/checking/naming/generics_shadowing_invalid.move:10:9
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// ---- Model Dump
module 0x42::M {
private fun bar(x: u64) {
if Gt(x, 0) {
if Gt<u64>(x, 0) {
Tuple()
} else {
Abort(1)
};
Sub(x, 1)
Sub<u64>(x, 1)
}
} // end 0x42::M
Original file line number Diff line number Diff line change
@@ -1,36 +1,36 @@

Diagnostics:
error: expected `&u64` but found `integer`
┌─ tests/checking/typing/assign_unpack_references_invalid.move:9:13
error: expected `&u64` but found `integer` (from assignment or declaration context)
┌─ tests/checking/typing/assign_unpack_references_invalid.move:9:9
9 │ f = 0;
^
│ ^

error: expected `&M::S` but found `M::S`
error: expected `&M::S` but found `M::S` (from assignment or declaration context)
┌─ tests/checking/typing/assign_unpack_references_invalid.move:10:9
10 │ s2 = S { f: 0 }
│ ^^

error: expected `&mut u64` but found `integer`
┌─ tests/checking/typing/assign_unpack_references_invalid.move:17:13
error: expected `&mut u64` but found `integer` (from assignment or declaration context)
┌─ tests/checking/typing/assign_unpack_references_invalid.move:17:9
17 │ f = 0;
^
│ ^

error: expected `&mut M::S` but found `M::S`
error: expected `&mut M::S` but found `M::S` (from assignment or declaration context)
┌─ tests/checking/typing/assign_unpack_references_invalid.move:18:9
18 │ s2 = S { f: 0 }
│ ^^

error: mutability mismatch (&mut != &)
error: mutability mismatch (&mut != &) (from assignment or declaration context)
┌─ tests/checking/typing/assign_unpack_references_invalid.move:26:9
26 │ f = &0;
│ ^

error: mutability mismatch (&mut != &)
error: mutability mismatch (&mut != &) (from assignment or declaration context)
┌─ tests/checking/typing/assign_unpack_references_invalid.move:27:9
27 │ s2 = &S { f: 0 }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@

Diagnostics:
error: tuples have different arity (0 != 3)
error: tuples have different arity (0 != 3) (from assignment or declaration context)
┌─ tests/checking/typing/assign_wrong_arity.move:7:9
7 │ x = (0, 1, 2);
│ ^

error: expected `()` but found `integer`
┌─ tests/checking/typing/assign_wrong_arity.move:8:14
error: expected `()` but found `integer` (from assignment or declaration context)
┌─ tests/checking/typing/assign_wrong_arity.move:8:9
8 │ () = 0;
^
^^

error: expected 4 item(s), found 3
┌─ tests/checking/typing/assign_wrong_arity.move:11:9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ error: expected `M::S` but found `M::R`
9 │ (S { g }, R { f }) = (R{ f: 0 }, R{ f: 1 });
│ ^^^^^^^

error: expected `()` but found `integer`
┌─ tests/checking/typing/assign_wrong_type.move:17:14
error: expected `()` but found `integer` (from assignment or declaration context)
┌─ tests/checking/typing/assign_wrong_type.move:17:9
17 │ () = 0;
^
^^

error: expected 4 item(s), found 3
┌─ tests/checking/typing/assign_wrong_type.move:18:9
Expand All @@ -30,25 +30,25 @@ error: expected 2 item(s), found 3
19 │ (x, b, R{f}) = (0, false);
│ ^^^^^^^^^^^^

error: expected `bool` but found `integer`
┌─ tests/checking/typing/assign_wrong_type.move:27:28
error: expected `bool` but found `integer` (from assignment or declaration context)
┌─ tests/checking/typing/assign_wrong_type.move:27:10
27 │ (x, b, R{f}, r) = (0, false, R{f: 0}, R{f: 0});
^
│ ^

error: expected `bool` but found `integer`
┌─ tests/checking/typing/assign_wrong_type.move:24:17
error: expected `integer` but found `bool` (from assignment or declaration context)
┌─ tests/checking/typing/assign_wrong_type.move:27:13
24let b = 0;
^
27(x, b, R{f}, r) = (0, false, R{f: 0}, R{f: 0});
│ ^

error: expected `address` but found `u64`
error: expected `address` but found `u64` (from assignment or declaration context)
┌─ tests/checking/typing/assign_wrong_type.move:27:18
27 │ (x, b, R{f}, r) = (0, false, R{f: 0}, R{f: 0});
│ ^

error: expected `M::S` but found `M::R`
error: expected `M::S` but found `M::R` (from assignment or declaration context)
┌─ tests/checking/typing/assign_wrong_type.move:27:22
27 │ (x, b, R{f}, r) = (0, false, R{f: 0}, R{f: 0});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ module 0x8675309::M {
f: u64,
}
private fun t0(x: u64,r: M::R) {
Add(0, 0);
Add(1, 0);
Add(0, 1);
Add(0, 1);
Add(0, 1);
Add(0, 1);
Add(0, 1);
Add(0, 1);
Add(x, x);
Add(select M::R.f(r), select M::R.f(r));
Add(Add(Add(1, select M::R.f(r)), select M::R.f(r)), 0);
Add<u64>(0, 0);
Add<u64>(1, 0);
Add<u64>(0, 1);
Add<u8>(0, 1);
Add<u8>(0, 1);
Add<u128>(0, 1);
Add<u128>(0, 1);
Add<u64>(0, 1);
Add<u64>(x, x);
Add<u64>(select M::R.f(r), select M::R.f(r));
Add<u64>(Add<u64>(Add<u64>(1, select M::R.f(r)), select M::R.f(r)), 0);
{
let M::R{ f: _ } = r;
Tuple()
Expand Down
Loading