Skip to content

Commit

Permalink
[move] Fix subtyping bug in typing (#16841)
Browse files Browse the repository at this point in the history
## Description 

Fixes a small bug in subtyping

## Test Plan 

Added a new test, plus everythong else still works

---
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 and leecchh committed Mar 27, 2024
1 parent 3710e52 commit e524bd4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
14 changes: 10 additions & 4 deletions external-crates/move/crates/move-compiler/src/typing/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1019,10 +1019,16 @@ fn subtype_no_report(
let subst = std::mem::replace(&mut context.subst, Subst::empty());
let lhs = core::ready_tvars(&subst, pre_lhs);
let rhs = core::ready_tvars(&subst, pre_rhs);
core::subtype(subst, &lhs, &rhs).map(|(next_subst, ty)| {
context.subst = next_subst;
ty
})
match core::subtype(subst.clone(), &lhs, &rhs) {
Ok((next_subst, ty)) => {
context.subst = next_subst;
Ok(ty)
}
Err(err) => {
context.subst = subst;
Err(err)
}
}
}

fn subtype_impl<T: ToString, F: FnOnce() -> T>(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
error[E04007]: incompatible types
┌─ tests/move_2024/typing/num_infer.move:27:34
5 │ native public fun vborrow<Element>(v: &vector<Element>, i: u64): &Element;
│ --- Expected: 'u64'
·
26 │ let idx = rnd % (matches.length() as u256);
│ ---- Given: 'u256'
27 │ let game = orders.remove(matches[idx]);
│ ^^^^^^^^^^^^ Invalid call of 'std::vector::vborrow'. Invalid argument for parameter 'i'

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

#[defines_primitive(vector)]
module std::vector {
#[syntax(index)]
native public fun vborrow<Element>(v: &vector<Element>, i: u64): &Element;
#[syntax(index)]
native public fun vborrow_mut<Element>(v: &mut vector<Element>, i: u64): &mut Element;
native public fun remove<Element>(v: &mut vector<Element>, i: u64): Element;
native public fun length<Element>(v: &vector<Element>): u64;
}

module a::pool {
public struct Order has store, drop { value: u8 }

public fun find_match(
orders: &mut vector<Order>,
): Option<Order> {
let (mut i, len) = (0, orders.length());
let mut matches = vector<u64>[];

while (i < len) {
i = i + 1;
};

let rnd = 100u256;
let idx = rnd % (matches.length() as u256);
let game = orders.remove(matches[idx]);
option::some(game)
}
}

0 comments on commit e524bd4

Please sign in to comment.