Skip to content

Commit

Permalink
[move-2024] Add support for migrating keywords (#16598)
Browse files Browse the repository at this point in the history
## Description 

This sets up a (mildly hacky) solution to migrate newly-reserved
keywords when they appear in existing code by maing them restricted
identifiers (e.g., "type" -> "`type`").

## Test Plan 

New test case for migration, plus attempting to run it against the sui
framework locally which does similar things.

---
If your changes are not user-facing and do not break anything, you can
skip the following section. Otherwise, please briefly describe what has
changed under the Release Notes section.

### Type of Change (Check all that apply)

- [ ] protocol change
- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
cgswords authored Mar 12, 2024
1 parent defd2a8 commit 5eddd50
Show file tree
Hide file tree
Showing 24 changed files with 509 additions and 47 deletions.
13 changes: 13 additions & 0 deletions crates/move-cli/tests/build_tests/migration_names/Move.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# TOML FILE

[package]
name = "A"

# this is a comment
[addresses]
A = "0x42"
std = "0x1"

[dependencies]
MoveStdlib = { local = "../../../../move-stdlib" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# TOML FILE

[package]
name = "A"
edition = "2024.beta"

# this is a comment
[addresses]
A = "0x42"
std = "0x1"

[dependencies]
MoveStdlib = { local = "../../../../move-stdlib" }
129 changes: 129 additions & 0 deletions crates/move-cli/tests/build_tests/migration_names/args.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
Command `migrate`:
Package toml does not specify an edition. As of 2024, Move requires all packages to define a language edition.

Please select one of the following editions:

1) 2024.beta
2) legacy

Selection (default=1): Recorded edition in 'Move.toml'

Would you like the Move compiler to migrate your code to Move 2024? (Y/n)
Generated changes . . .
INCLUDING DEPENDENCY MoveStdlib
BUILDING A

The following changes will be made.
============================================================

--- sources/mod0.move
+++ sources/mod0.move
@@ -3,1 +3,1 @@
- struct S { n: u64 }
+ public struct S { n: u64 }
@@ -5,1 +5,1 @@
- fun t0(type: u64, enum: S, mut: bool, match: u64): u64 {
+ fun t0(`type`: u64, `enum`: S, `mut`: bool, `match`: u64): u64 {
@@ -6,1 +6,1 @@
- if (type == match) {
+ if (`type` == `match`) {
@@ -7,1 +7,1 @@
- type
+ `type`
@@ -8,1 +8,1 @@
- } else if (mut) {
+ } else if (`mut`) {
@@ -9,1 +9,1 @@
- match
+ `match`
@@ -11,1 +11,1 @@
- enum.n
+ `enum`.n
--- sources/mod_intermodule_errors.move
+++ sources/mod_intermodule_errors.move
@@ -3,1 +3,1 @@
- struct S { n: u64 }
+ public struct S { n: u64 }
@@ -5,1 +5,1 @@
- public fun t0(type: u64, enum: S, mut: bool, match: u64): u64 {
+ public fun t0(`type`: u64, `enum`: S, `mut`: bool, `match`: u64): u64 {
@@ -6,1 +6,1 @@
- if (type == match) {
+ if (`type` == `match`) {
@@ -7,1 +7,1 @@
- type
+ `type`
@@ -8,1 +8,1 @@
- } else if (mut) {
+ } else if (`mut`) {
@@ -9,1 +9,1 @@
- match
+ `match`
@@ -11,1 +11,1 @@
- enum.n
+ `enum`.n
--- sources/mod_let.move
+++ sources/mod_let.move
@@ -3,1 +3,1 @@
- struct S has drop { n: u64 }
+ public struct S has drop { n: u64 }
@@ -5,1 +5,1 @@
- fun t0(type: u64, enum: S, mut: bool, match: u64): u64 {
+ fun t0(`type`: u64, `enum`: S, `mut`: bool, `match`: u64): u64 {
@@ -6,1 +6,1 @@
- let type = 0;
+ let `type` = 0;
@@ -7,1 +7,1 @@
- let enum = 1;
+ let `enum` = 1;
@@ -8,1 +8,1 @@
- let mut = 2;
+ let `mut` = 2;
@@ -9,1 +9,1 @@
- let match = 3;
+ let `match` = 3;
@@ -10,1 +10,1 @@
- type + enum + mut + match
+ `type` + `enum` + `mut` + `match`
--- sources/mod_let_errors.move
+++ sources/mod_let_errors.move
@@ -3,1 +3,1 @@
- struct S { n: u64 }
+ public struct S { n: u64 }
@@ -5,1 +5,1 @@
- fun t0(type: u64, enum: S, mut: bool, match: u64): u64 {
+ fun t0(`type`: u64, `enum`: S, `mut`: bool, `match`: u64): u64 {
@@ -6,1 +6,1 @@
- let type = 0;
+ let `type` = 0;
@@ -7,1 +7,1 @@
- let enum = 1;
+ let `enum` = 1;
@@ -8,1 +8,1 @@
- let mut = 2;
+ let `mut` = 2;
@@ -9,1 +9,1 @@
- let match = 3;
+ let `match` = 3;
@@ -10,1 +10,1 @@
- type + enum + mut + match
+ `type` + `enum` + `mut` + `match`


============================================================
Apply changes? (Y/n)
Updating "sources/mod0.move" . . .
Updating "sources/mod_intermodule_errors.move" . . .
Updating "sources/mod_let.move" . . .
Updating "sources/mod_let_errors.move" . . .

Changes complete
Wrote patchfile out to: ./migration.patch

External Command `diff -r -s sources migration_sources`:
Files sources/mod0.move and migration_sources/mod0.move are identical
Files sources/mod_intermodule_errors.move and migration_sources/mod_intermodule_errors.move are identical
Files sources/mod_let.move and migration_sources/mod_let.move are identical
Files sources/mod_let_errors.move and migration_sources/mod_let_errors.move are identical
External Command `diff -s Move.toml Move.toml.expected`:
Files Move.toml and Move.toml.expected are identical
3 changes: 3 additions & 0 deletions crates/move-cli/tests/build_tests/migration_names/args.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
migrate
> diff -r -s sources migration_sources
> diff -s Move.toml Move.toml.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module A::mod0 {

public struct S { n: u64 }

fun t0(`type`: u64, `enum`: S, `mut`: bool, `match`: u64): u64 {
if (`type` == `match`) {
`type`
} else if (`mut`) {
`match`
} else {
`enum`.n
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module A::mod1 {

public struct S { n: u64 }

public fun t0(`type`: u64, `enum`: S, `mut`: bool, `match`: u64): u64 {
if (`type` == `match`) {
`type`
} else if (`mut`) {
`match`
} else {
`enum`.n
}
}

}

module A::mod2 {

use A::mod1::t0;
use A::mod1::S;

public fun t1(t: u64, e: S, m: bool, m2: u64): u64 { t0(t, e, m, m2) }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module A::mod_let {

public struct S has drop { n: u64 }

fun t0(`type`: u64, `enum`: S, `mut`: bool, `match`: u64): u64 {
let `type` = 0;
let `enum` = 1;
let `mut` = 2;
let `match` = 3;
`type` + `enum` + `mut` + `match`
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module A::mod_let_errors {

public struct S { n: u64 }

fun t0(`type`: u64, `enum`: S, `mut`: bool, `match`: u64): u64 {
let `type` = 0;
let `enum` = 1;
let `mut` = 2;
let `match` = 3;
`type` + `enum` + `mut` + `match`
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module A::mod0 {

struct S { n: u64 }

fun t0(type: u64, enum: S, mut: bool, match: u64): u64 {
if (type == match) {
type
} else if (mut) {
match
} else {
enum.n
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module A::mod1 {

struct S { n: u64 }

public fun t0(type: u64, enum: S, mut: bool, match: u64): u64 {
if (type == match) {
type
} else if (mut) {
match
} else {
enum.n
}
}

}

module A::mod2 {

use A::mod1::t0;
use A::mod1::S;

public fun t1(t: u64, e: S, m: bool, m2: u64): u64 { t0(t, e, m, m2) }

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module A::mod_let {

struct S has drop { n: u64 }

fun t0(type: u64, enum: S, mut: bool, match: u64): u64 {
let type = 0;
let enum = 1;
let mut = 2;
let match = 3;
type + enum + mut + match
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module A::mod_let_errors {

struct S { n: u64 }

fun t0(type: u64, enum: S, mut: bool, match: u64): u64 {
let type = 0;
let enum = 1;
let mut = 2;
let match = 3;
type + enum + mut + match
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

# TOML FILE

[package]
name = "A"

# this is a comment
[addresses]
A = "0x42"
std = "0x1"

[dependencies]
MoveStdlib = { local = "../../../../move-stdlib" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

# TOML FILE

[package]
name = "A"
edition = "2024.beta"

# this is a comment
[addresses]
A = "0x42"
std = "0x1"

[dependencies]
MoveStdlib = { local = "../../../../move-stdlib" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
Command `migrate`:
Package toml does not specify an edition. As of 2024, Move requires all packages to define a language edition.

Please select one of the following editions:

1) 2024.beta
2) legacy

Selection (default=1): Recorded edition in 'Move.toml'

Would you like the Move compiler to migrate your code to Move 2024? (Y/n)
Generated changes . . .
INCLUDING DEPENDENCY MoveStdlib
BUILDING A

The following changes will be made.
============================================================

--- sources/mut_name.move
+++ sources/mut_name.move
@@ -3,1 +3,1 @@
- fun dumpster_fire(mut x: u64, mut: u64): u64 {
+ fun dumpster_fire(mut x: u64, `mut`: u64): u64 {
@@ -5,1 +5,1 @@
- let mut: u64 = mut + 2;
+ let `mut`: u64 = `mut` + 2;
@@ -6,1 +6,1 @@
- mut + y + x
+ `mut` + y + x


============================================================
Apply changes? (Y/n)
Updating "sources/mut_name.move" . . .

Changes complete
Wrote patchfile out to: ./migration.patch

External Command `diff -r -s sources migration_sources`:
Files sources/mut_name.move and migration_sources/mut_name.move are identical
External Command `diff -s Move.toml Move.toml.expected`:
Files Move.toml and Move.toml.expected are identical
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
migrate
> diff -r -s sources migration_sources
> diff -s Move.toml Move.toml.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module 0x42::m {

fun dumpster_fire(mut x: u64, `mut`: u64): u64 {
let mut y: u64 = 10;
let `mut`: u64 = `mut` + 2;
`mut` + y + x
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module 0x42::m {

fun dumpster_fire(mut x: u64, mut: u64): u64 {
let mut y: u64 = 10;
let mut: u64 = mut + 2;
mut + y + x
}

}
Loading

0 comments on commit 5eddd50

Please sign in to comment.