Skip to content

Commit

Permalink
[move-2024] Fix issue with namd block cfg lowering (MystenLabs#15791)
Browse files Browse the repository at this point in the history
## Description 

This fixes a bug for named blocks.

## Test Plan 

New tests that work 
---
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 Jan 18, 2024
1 parent 1c46227 commit cdbe6d8
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
processed 3 tasks
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//# init --edition 2024.alpha

//# publish
module 0x42::m {

public fun t0(): u64 {
if ('block: { true }) {
10
} else {
20
}
}

public fun t1(): u64 {
if ('block: { true }) {
'block: { 10 }
} else {
20
}
}

public fun t2(): u64 {
if ('block: { false }) {
20
} else {
'block: { 10 }
}
}

public fun t3(): u64 {
if ('block: { false }) {
20
} else {
while (false) { 'block: { 20 }; };
'block: { 10 }
}
}

public fun t4(): u64 {
let mut count = 0;
let mut x = 0;
while (x < 10) {
'inner: {
count = count + 1;
};
x = x + 1;
};
count
}

public fun t5(): u64 {
let mut count = 0;
let (start, stop) = (0, 10);
let mut i = start;
while (i < stop) {
let x = i;
count = count + x * x;
i = i + 1;
};
count
}

public fun t6(): u64 {
let mut count = 0u64;
{
let (start, stop) = (0, 10);
'macro: {
'lambdabreak: {
{
let mut i = start;
while (i < stop) 'loop: {
{
let x = i;
'lambdareturn: {
count = count + x * x;
}
};
i = i + 1;
}
}
}
}
};
count
}
}

//# run
module 0x42::main {
use 0x42::m;
fun main() {
assert!(m::t0() == 10, 0);
assert!(m::t1() == 10, 1);
assert!(m::t2() == 10, 2);
assert!(m::t3() == 10, 3);
assert!(m::t4() == 10, 4);
assert!(m::t5() == 285, 5);
assert!(m::t6() == 285, 6);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ fn statement(

let (body_entry_block, body_blocks) = block_(
context,
with_last(body, make_jump(sloc, start_label, false)),
with_last(body, make_jump(sloc, end_label, false)),
);

context.exit_named_block(&name);
Expand Down
3 changes: 2 additions & 1 deletion external-crates/move/crates/move-compiler/src/hlir/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1130,12 +1130,13 @@ impl AstDebug for Statement_ {
if *has_break {
w.write("#has_break");
}
w.write(" ");
name.ast_debug(w);
w.write(": ");
w.block(|w| block.ast_debug(w))
}
S::NamedBlock { name, block } => {
w.write("loop");
w.write("named-block ");
name.ast_debug(w);
w.write(": ");
w.block(|w| block.ast_debug(w))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

module 0x42::m {
fun t0() {
let mut count = 0;
let (start, stop) = (0, 10);
let mut i = start;
while (i < stop) {
let x = i;
count = count + x * x;
i = i + 1;
};
assert!(count == 285, 0);
}

fun t1() {
let mut count = 0u64;
{
let (start, stop) = (0, 10);
'macro: {
'lambdabreak: {
{
let mut i = start;
while (i < stop) 'loop: {
{
let x = i;
'lambdareturn: {
count = count + x * x;
}
};
i = i + 1;
}
}
}
}
};
assert!(count == 285, 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
module 0x42::a {

fun t0(): u64 {
if ('block: { true }) {
10
} else {
20
}
}

fun t1(): u64 {
if ('block: { true }) {
'block: { 10 }
} else {
20
}
}

fun t2(): u64 {
if ('block: { false }) {
20
} else {
'block: { 10 }
}
}

fun t3(): u64 {
if ('block: { false }) {
20
} else {
while (false) { 'block: { 20 }; };
'block: { 10 }
}
}

fun t4(): u64 {
let mut count = 0;
let mut x = 0;
while (x < 10) {
'inner: {
count = count + 1;
};
x = x + 1;
};
count
}

}

0 comments on commit cdbe6d8

Please sign in to comment.