From 4ccc9916f126dada672f606cd088f9b747bd149d Mon Sep 17 00:00:00 2001 From: Wolfgang Grieskamp Date: Mon, 7 Oct 2024 23:02:01 -0700 Subject: [PATCH] Implement positional layouts in sourcifier --- .../bug_14300_variant_select_autoref.exp | 2 +- .../matching_refutable_err.exp | 2 +- .../tests/checking/dotdot/dotdot_valid.exp | 72 +++++++++---------- .../tests/checking/dotdot/extra_dotdot.exp | 6 +- .../positional_fields/assign_field.exp | 10 +-- .../bind_anonymous_field.exp | 8 +-- .../checking/positional_fields/decl_ok.exp | 2 +- .../named_tuple_construct_ok.exp | 10 +-- third_party/move/move-model/src/model.rs | 11 +++ third_party/move/move-model/src/sourcifier.rs | 16 ++++- 10 files changed, 80 insertions(+), 59 deletions(-) diff --git a/third_party/move/move-compiler-v2/tests/bytecode-generator/bug_14300_variant_select_autoref.exp b/third_party/move/move-compiler-v2/tests/bytecode-generator/bug_14300_variant_select_autoref.exp index 65b950ae473bf6..0d74991c8f9b01 100644 --- a/third_party/move/move-compiler-v2/tests/bytecode-generator/bug_14300_variant_select_autoref.exp +++ b/third_party/move/move-compiler-v2/tests/bytecode-generator/bug_14300_variant_select_autoref.exp @@ -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 } diff --git a/third_party/move/move-compiler-v2/tests/bytecode-generator/matching_refutable_err.exp b/third_party/move/move-compiler-v2/tests/bytecode-generator/matching_refutable_err.exp index c7e4acb12ac001..24100f7e3de487 100644 --- a/third_party/move/move-compiler-v2/tests/bytecode-generator/matching_refutable_err.exp +++ b/third_party/move/move-compiler-v2/tests/bytecode-generator/matching_refutable_err.exp @@ -23,7 +23,7 @@ module 0x815::m { } } fun t(self: E): u64 { - let E::Some{0: x} = self; + let E::Some(x) = self; x } } diff --git a/third_party/move/move-compiler-v2/tests/checking/dotdot/dotdot_valid.exp b/third_party/move/move-compiler-v2/tests/checking/dotdot/dotdot_valid.exp index 38c97b75a97a88..71f94e6f025b8a 100644 --- a/third_party/move/move-compiler-v2/tests/checking/dotdot/dotdot_valid.exp +++ b/third_party/move/move-compiler-v2/tests/checking/dotdot/dotdot_valid.exp @@ -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) { @@ -369,22 +369,22 @@ module 0x42::test { let S4{x: _x2,y: S3{x: _x1,y: _}} = x; } fun nested2(x: S5) { - let S5{0: _,1: S1{0: _}} = x; + let S5(_,S1(_)) = x; } fun nested2_ref(x: &S5) { - let S5{0: _,1: S1{0: _}} = x; + let S5(_,S1(_)) = x; } fun nested3(x: S5>) { - let S5>{0: _,1: S4{x: _,y: _}} = x; + let S5>(_,S4{x: _,y: _}) = x; } fun nested3_ref(x: &S5>) { - let S5>{0: _,1: S4{x: _,y: _}} = x; + let S5>(_,S4{x: _,y: _}) = x; } fun nested4(x: S4) { - let S4{x: S1{0: _},y: _} = x; + let S4{x: S1(_),y: _} = x; } fun nested4_ref(x: &S4) { - let S4{x: S1{0: _},y: _} = x; + let S4{x: S1(_),y: _} = x; } fun simple_0(x: S0) { let S0{} = x; @@ -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; @@ -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 } } diff --git a/third_party/move/move-compiler-v2/tests/checking/dotdot/extra_dotdot.exp b/third_party/move/move-compiler-v2/tests/checking/dotdot/extra_dotdot.exp index 5628d8db32cbb1..14221b9623b211 100644 --- a/third_party/move/move-compiler-v2/tests/checking/dotdot/extra_dotdot.exp +++ b/third_party/move/move-compiler-v2/tests/checking/dotdot/extra_dotdot.exp @@ -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; } } diff --git a/third_party/move/move-compiler-v2/tests/checking/positional_fields/assign_field.exp b/third_party/move/move-compiler-v2/tests/checking/positional_fields/assign_field.exp index 6f447fc7cb719b..66c17ae08d807a 100644 --- a/third_party/move/move-compiler-v2/tests/checking/positional_fields/assign_field.exp +++ b/third_party/move/move-compiler-v2/tests/checking/positional_fields/assign_field.exp @@ -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 @@ -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; diff --git a/third_party/move/move-compiler-v2/tests/checking/positional_fields/bind_anonymous_field.exp b/third_party/move/move-compiler-v2/tests/checking/positional_fields/bind_anonymous_field.exp index 2d803a8468b740..3cad8094244fec 100644 --- a/third_party/move/move-compiler-v2/tests/checking/positional_fields/bind_anonymous_field.exp +++ b/third_party/move/move-compiler-v2/tests/checking/positional_fields/bind_anonymous_field.exp @@ -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; } } diff --git a/third_party/move/move-compiler-v2/tests/checking/positional_fields/decl_ok.exp b/third_party/move/move-compiler-v2/tests/checking/positional_fields/decl_ok.exp index 292ece45c67f2e..d5eadf5b7a3a83 100644 --- a/third_party/move/move-compiler-v2/tests/checking/positional_fields/decl_ok.exp +++ b/third_party/move/move-compiler-v2/tests/checking/positional_fields/decl_ok.exp @@ -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; diff --git a/third_party/move/move-compiler-v2/tests/checking/positional_fields/named_tuple_construct_ok.exp b/third_party/move/move-compiler-v2/tests/checking/positional_fields/named_tuple_construct_ok.exp index fdfa85f4155ca0..855a685508fa35 100644 --- a/third_party/move/move-compiler-v2/tests/checking/positional_fields/named_tuple_construct_ok.exp +++ b/third_party/move/move-compiler-v2/tests/checking/positional_fields/named_tuple_construct_ok.exp @@ -88,16 +88,16 @@ 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(x: T): S3 { - S3{0: x,1: 0u8} + S3(x,0u8) } fun nested_0(): S3 { - S3{0: S4{},1: 0u8} + S3(S4{},0u8) } fun nested_1(): S5 { S5{x: S0{},y: 0u8} @@ -105,6 +105,6 @@ module 0x42::test { fun test_variant() { E1::V1{}; E1::V2{}; - E1::V3{0: 42u8,1: true}; + E1::V3(42u8,true); } } diff --git a/third_party/move/move-model/src/model.rs b/third_party/move/move-model/src/model.rs index 2a052430518e1c..693c6aebe3b760 100644 --- a/third_party/move/move-model/src/model.rs +++ b/third_party/move/move-model/src/model.rs @@ -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::() + .is_ok() + } + /// Gets the location of the field declaration. pub fn get_loc(&self) -> &Loc { &self.data.loc diff --git a/third_party/move/move-model/src/sourcifier.rs b/third_party/move/move-model/src/sourcifier.rs index bd1efd112e77f5..e1e6b936cb7389 100644 --- a/third_party/move/move-model/src/sourcifier.rs +++ b/third_party/move/move-model/src/sourcifier.rs @@ -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) }, );