Skip to content

Commit

Permalink
Implement positional layouts in sourcifier
Browse files Browse the repository at this point in the history
  • Loading branch information
wrwg committed Oct 8, 2024
1 parent d470c70 commit 4ccc991
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module 0x815::m {
}
}
fun test_common_access(): u8 {
let x = Positional::A{0: 42u8};
let x = Positional::A(42u8);
x.A.0 = 19u8;
20u8
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module 0x815::m {
}
}
fun t(self: E): u64 {
let E::Some{0: x} = self;
let E::Some(x) = self;
x
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ module 0x42::test {
3: u64,
}
inline fun lambda_param(f: |S2|bool): bool {
let x = S2{0: true,1: S0{}};
let x = S2(true,S0{});
f(x)
}
fun nested1(x: S4<bool>) {
Expand All @@ -369,22 +369,22 @@ module 0x42::test {
let S4<bool>{x: _x2,y: S3{x: _x1,y: _}} = x;
}
fun nested2(x: S5<bool, S1>) {
let S5<bool,S1>{0: _,1: S1{0: _}} = x;
let S5<bool,S1>(_,S1(_)) = x;
}
fun nested2_ref(x: &S5<bool, S1>) {
let S5<bool,S1>{0: _,1: S1{0: _}} = x;
let S5<bool,S1>(_,S1(_)) = x;
}
fun nested3(x: S5<bool, S4<bool>>) {
let S5<bool,S4<bool>>{0: _,1: S4<bool>{x: _,y: _}} = x;
let S5<bool,S4<bool>>(_,S4<bool>{x: _,y: _}) = x;
}
fun nested3_ref(x: &S5<bool, S4<bool>>) {
let S5<bool,S4<bool>>{0: _,1: S4<bool>{x: _,y: _}} = x;
let S5<bool,S4<bool>>(_,S4<bool>{x: _,y: _}) = x;
}
fun nested4(x: S4<S1>) {
let S4<S1>{x: S1{0: _},y: _} = x;
let S4<S1>{x: S1(_),y: _} = x;
}
fun nested4_ref(x: &S4<S1>) {
let S4<S1>{x: S1{0: _},y: _} = x;
let S4<S1>{x: S1(_),y: _} = x;
}
fun simple_0(x: S0) {
let S0{} = x;
Expand All @@ -393,30 +393,30 @@ module 0x42::test {
let S0{} = x;
}
fun simple_1(x: S1) {
let S1{0: _} = x;
let S1(_) = x;
}
fun simple_1_ref(x: &mut S1) {
let S1{0: _} = x;
let S1(_) = x;
}
fun simple_2(x: S2) {
let S2{0: _,1: _} = x;
let S2{0: _x,1: _} = x;
let S2{0: _,1: _x} = x;
let S2{0: _,1: _} = x;
let S2{0: _,1: _} = x;
let S2{0: _x,1: _y} = x;
let S2{0: _x,1: _y} = x;
let S2{0: _x,1: _y} = x;
let S2(_,_) = x;
let S2(_x,_) = x;
let S2(_,_x) = x;
let S2(_,_) = x;
let S2(_,_) = x;
let S2(_x,_y) = x;
let S2(_x,_y) = x;
let S2(_x,_y) = x;
}
fun simple_2_ref(x: &S2) {
let S2{0: _,1: _} = x;
let S2{0: _x,1: _} = x;
let S2{0: _,1: _x} = x;
let S2{0: _,1: _} = x;
let S2{0: _,1: _} = x;
let S2{0: _x,1: _y} = x;
let S2{0: _x,1: _y} = x;
let S2{0: _x,1: _y} = x;
let S2(_,_) = x;
let S2(_x,_) = x;
let S2(_,_x) = x;
let S2(_,_) = x;
let S2(_,_) = x;
let S2(_x,_y) = x;
let S2(_x,_y) = x;
let S2(_x,_y) = x;
}
fun simple_3(x: S3) {
let S3{x: _,y: _} = x;
Expand All @@ -430,31 +430,31 @@ module 0x42::test {
}
fun simple_4(x: E1): u8 {
match (x) {
E1::A{0: x,1: _} => x,
E1::B{0: x} => x,
E1::A(x,_) => x,
E1::B(x) => x,
E1::C{x: x,y: _} => x,
}
}
fun simple_4_ref(x: &E1): &u8 {
match (x) {
E1::A{0: x,1: _} => x,
E1::B{0: x} => x,
E1::A(x,_) => x,
E1::B(x) => x,
}
}
fun simple_5(x: E1): u8 {
match (x) {
E1::A{0: _,1: y} => if (y) 1u8 else 0u8,
E1::B{0: x} => x,
E1::C{x: _,y: S1{0: x}} => x,
E1::A(_,y) => if (y) 1u8 else 0u8,
E1::B(x) => x,
E1::C{x: _,y: S1(x)} => x,
}
}
fun simple_6(x: &S7) {
let S7{0: _w,1: _,2: _,3: _z} = x;
let S7{0: _w,1: _x,2: _y,3: _z} = x;
let S7(_w,_,_,_z) = x;
let S7(_w,_x,_y,_z) = x;
}
fun test_lambda_param(): bool {
let x = S2{0: true,1: S0{}};
let (S2{0: x,1: _}) = (x);
let x = S2(true,S0{});
let (S2(x,_)) = (x);
x
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ module 0x42::test {
2: address,
}
fun extra_dotdot(x: S, y: T) {
let S{0: _x,1: _,2: _} = x;
let S{0: _,1: _,2: _} = x;
let S{0: _,1: _,2: _} = x;
let S(_x,_,_) = x;
let S(_,_,_) = x;
let S(_,_,_) = x;
let T{x: _,y: _,z: _} = y;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,16 @@ module 0x42::test {
2: S2,
}
fun assign0(a: u64, b: bool) {
let x = S1{0: a,1: b};
let x = S1(a,b);
while (x.1) {
x = S1{0: x.0 - 1,1: x.0 >= 1};
x = S1(x.0 - 1,x.0 >= 1);
}
}
fun assign1(x: S1): u64 {
let count = 0;
while (x.1) {
let y = if (x.0 > 0) x.0 - 1 else 0;
x = S1{0: y,1: y >= 1};
x = S1(y,y >= 1);
count = count + 1;
};
count
Expand All @@ -145,11 +145,11 @@ module 0x42::test {
}
fun assign_enum(x: &mut E) {
match (x) {
E::V1{0: x,1: y} => {
E::V1(x,y) => {
*x = 42u8;
*y = true;
},
E::V2{0: x} => {
E::V2(x) => {
x.0.0.x = 0u8;
x.1.x = 1u8;
x.2.0.x = 2u8;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ module 0x42::test {
}
fun match(x: E1) {
match (x) {
E1::V1{0: S0{0: _x}} => (),
E1::V2{0: S1{0: _x,1: S0{0: _y}}} => (),
E1::V1(S0(_x)) => (),
E1::V2(S1(_x,S0(_y))) => (),
}
}
fun nested(x: S1) {
let S1{0: _x,1: S0{0: _y}} = x;
let S1(_x,S0(_y)) = x;
}
fun simple(x: S0) {
let S0{0: _x} = x;
let S0(_x) = x;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ module 0x42::test {
fun baz() {
E1::V1{};
E1::V2{};
E1::V3{0: 42u8,1: true};
E1::V3(42u8,true);
}
fun foo(x: S2) {
x.0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,23 +88,23 @@ module 0x42::test {
S0{}
}
fun S1_inhabited(): S1 {
S1{0: 0u8}
S1(0u8)
}
fun S2_inhabited(): S2 {
S2{0: 0u8,1: false}
S2(0u8,false)
}
fun S3_test<T>(x: T): S3<T> {
S3<T>{0: x,1: 0u8}
S3<T>(x,0u8)
}
fun nested_0(): S3<S4> {
S3<S4>{0: S4{},1: 0u8}
S3<S4>(S4{},0u8)
}
fun nested_1(): S5<S0> {
S5<S0>{x: S0{},y: 0u8}
}
fun test_variant() {
E1::V1{};
E1::V2{};
E1::V3{0: 42u8,1: true};
E1::V3(42u8,true);
}
}
11 changes: 11 additions & 0 deletions third_party/move/move-model/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3778,6 +3778,17 @@ impl<'env> FieldEnv<'env> {
FieldId(self.data.name)
}

/// Returns true if this is a positional field. Identified by that the name
/// of the field is a number.
pub fn is_positional(&self) -> bool {
self.data
.name
.display(self.struct_env.symbol_pool())
.to_string()
.parse::<u64>()
.is_ok()
}

/// Gets the location of the field declaration.
pub fn get_loc(&self) -> &Loc {
&self.data.loc
Expand Down
16 changes: 13 additions & 3 deletions third_party/move/move-model/src/sourcifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,16 +944,26 @@ impl<'a> ExpSourcifier<'a> {
emit!(self.wr(), "::{}", self.sym(*v))
}
self.print_inst(&qid.inst);
let (open, close) = if struct_env
.get_fields_optional_variant(*variant)
.any(|f| f.is_positional())
{
("(", ")")
} else {
("{", "}")
};
self.parent.print_list(
"{",
open,
",",
"}",
close,
struct_env
.get_fields_optional_variant(*variant)
.zip(items)
.filter(|(f, _)| !self.parent.is_dummy_field(f)),
|(f, i)| {
emit!(self.wr(), "{}: ", self.sym(f.get_name()));
if !f.is_positional() {
emit!(self.wr(), "{}: ", self.sym(f.get_name()));
}
printer(i)
},
);
Expand Down

0 comments on commit 4ccc991

Please sign in to comment.