use super::*;
#[test]
fn parameter_type_moves() {
let a = "fn f(x: i32) { save(x); }\n";
let b = "fn f(x: i64) { save(x); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn let_annotation_type_moves() {
let a = "fn f() { let s: HashSet<u32> = collect(); save(s); }\n";
let b = "fn f() { let s: BTreeSet<u32> = collect(); save(s); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn return_type_moves() {
let a = "fn f() -> u8 { parse() }\n";
let b = "fn f() -> u16 { parse() }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn closure_parameter_type_moves() {
let a = "fn f() { save(|x: u8| x); }\n";
let b = "fn f() { save(|x: u16| x); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn local_rename_beside_a_kept_type_stays() {
let a = "fn f(x: Order) { let total: Money = x.sum(); save(total); }\n";
let b = "fn f(o: Order) { let t: Money = o.sum(); save(t); }\n";
assert_eq!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn arg_plus_one_moves() {
let a = "fn f(x: i32) { save(x); }\n";
let b = "fn f(x: i32) { save(x + 1); }\n";
let pa = pair(&RUST, a, 1);
let pb = pair(&RUST, b, 1);
assert_ne!(pa.complete, pb.complete);
assert_ne!(pa.shape, pb.shape);
}
#[test]
fn call_order_moves() {
let a = "fn f() { debit(); credit(); }\n";
let b = "fn f() { credit(); debit(); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn local_rename_stays() {
let a = "fn f(x: i32) { save(x); }\n";
let b = "fn f(y: i32) { save(y); }\n";
assert_eq!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn callee_rename_moves() {
let a = "fn f(x: i32) { save(x); }\n";
let b = "fn f(x: i32) { log(x); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn field_access_moves() {
let a = "fn f(obj: S) { save(obj.a); }\n";
let b = "fn f(obj: S) { save(obj.b); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn constructor_qualifier_moves() {
let a = "fn f() { Foo::new(); }\n";
let b = "fn f() { Bar::new(); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn swapped_arguments_move() {
let a = "fn f(payer: A, payee: B) { move_funds(payer, payee); }\n";
let b = "fn f(payer: A, payee: B) { move_funds(payee, payer); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn swapped_operands_move() {
let a = "fn f() { let a = x(); let amount = y(); pay(a - amount); }\n";
let b = "fn f() { let a = x(); let amount = y(); pay(amount - a); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn wrong_field_source_moves() {
let a = "fn f(src: &A, dst: &A) { save(src.balance); }\n";
let b = "fn f(src: &A, dst: &A) { save(dst.balance); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn enum_variant_moves() {
let a = "fn f() { grant(Role::Admin); }\n";
let b = "fn f() { grant(Role::Guest); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn pattern_variant_moves() {
let a = "fn f(r: R) { match r { Some(x) => use_it(x), None => {} } }\n";
let b = "fn f(r: R) { match r { Ok(x) => use_it(x), None => {} } }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn struct_name_moves() {
let a = "fn f() { send(Grant { level: 1 }); }\n";
let b = "fn f() { send(Revoke { level: 1 }); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn comparison_moves() {
let a = "fn f(amount: u64) { if amount < LIMIT { pay(); } }\n";
let b = "fn f(amount: u64) { if amount > LIMIT { pay(); } }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn free_constant_moves() {
let a = "fn f(amount: u64) { if amount < LIMIT { pay(); } }\n";
let b = "fn f(amount: u64) { if amount < CEILING { pay(); } }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn unary_and_assignment_operators_move() {
let a = "fn f(mut n: i64) { n += 1; save(-n); }\n";
let b = "fn f(mut n: i64) { n -= 1; save(!n); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn cast_operand_moves() {
let a = "fn f() { save(count_rows() as i64); }\n";
let b = "fn f() { save(drop_all_tables() as i64); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn cast_type_moves() {
let a = "fn f(n: u64) { save(n as i64); }\n";
let b = "fn f(n: u64) { save(n as i8); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn turbofish_moves() {
let a = "fn f(s: &str) { save(s.parse::<i64>()); }\n";
let b = "fn f(s: &str) { save(s.parse::<u8>()); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn path_turbofish_moves() {
let a = "fn f() { save(Vec::<u8>::new()); }\n";
let b = "fn f() { save(Vec::<u16>::new()); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn cfg_on_a_statement_moves() {
let a = "fn f() {\n drop_all();\n}\n";
let b = "fn f() {\n #[cfg(any())]\n drop_all();\n}\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn cfg_on_the_span_item_moves() {
let a = "#[inline]\nfn f() {\n drop_all();\n}\n";
let b = "#[cfg(any())]\nfn f() {\n drop_all();\n}\n";
let c = "#[cfg_attr(test, inline)]\nfn f() {\n drop_all();\n}\n";
assert_ne!(pair(&RUST, a, 2).shape, pair(&RUST, b, 2).shape);
assert_ne!(pair(&RUST, a, 2).shape, pair(&RUST, c, 2).shape);
}
#[test]
fn cfg_on_an_enclosing_item_moves() {
let a = "mod m {\n fn f() {\n // pin\n drop_all();\n }\n}\n";
let b = "#[cfg(any())]\nmod m {\n fn f() {\n // pin\n drop_all();\n }\n}\n";
assert_ne!(pair(&RUST, a, 3).shape, pair(&RUST, b, 4).shape);
}
#[test]
fn other_attributes_stay() {
let a = "fn f() {\n drop_all();\n}\n";
let b = "#[inline]\nfn f() {\n #[allow(unused)]\n drop_all();\n}\n";
assert_eq!(pair(&RUST, a, 1).shape, pair(&RUST, b, 2).shape);
}
#[test]
fn let_rename_stays() {
let a = "fn f() { let a = load(); let b = a.trim(); save(b, a); }\n";
let b = "fn f() { let x = load(); let y = x.trim(); save(y, x); }\n";
assert_eq!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn shadowing_reads_the_outer_binding() {
let a = "fn f() { let s = load(); let s = s.trim(); save(s); }\n";
let b = "fn f() { let t = load(); let s = t.trim(); save(s); }\n";
let c = "fn f() { let t = load(); let s = s.trim(); save(s); }\n";
assert_eq!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, c, 1).shape);
}
#[test]
fn shadowed_inner_binding_is_not_the_outer() {
let a = "fn f() { let a = 1; { let a = 2; } save(a); }\n";
let b = "fn f() { let z = 1; { let a = 2; } save(a); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn pattern_and_closure_rename_stays() {
let a = "fn f(r: R) { match r { Some(v) if v > 0 => use_it(v), _ => {} } for i in xs() { take(i); } run(|k| k + 1); }\n";
let b = "fn f(r: R) { match r { Some(w) if w > 0 => use_it(w), _ => {} } for j in xs() { take(j); } run(|m| m + 1); }\n";
assert_eq!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn local_closure_call_stays_under_rename() {
let a = "fn f() { let pay = |n| n; pay(1); }\n";
let b = "fn f() { let settle = |n| n; settle(1); }\n";
assert_eq!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn shorthand_field_keeps_its_name() {
let a = "fn f() { let x = 1; send(S { x }); }\n";
let b = "fn f() { let y = 1; send(S { y }); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn parse_error_has_no_digest() {
assert!(mint_span_digests(&RUST, "fn f() { save(; }\n", 1, &[]).is_none());
assert!(mint_span_digests(&RUST, "fn f() { let x = 1 }\n", 1, &[]).is_none());
}
#[test]
fn macro_comparison_moves() {
let a = "fn f(amount: u64) { assert!(amount < LIMIT); }\n";
let b = "fn f(amount: u64) { assert!(amount > LIMIT); }\n";
assert_ne!(pair(&RUST, a, 1).shape, pair(&RUST, b, 1).shape);
}
#[test]
fn block_span_reads_parameters_as_free() {
let a = "fn f(payer: A, payee: B) {\n // pin\n move_funds(payer, payee);\n}\n";
let b = "fn f(payer: A, payee: B) {\n // pin\n move_funds(payee, payer);\n}\n";
assert_ne!(pair(&RUST, a, 2).shape, pair(&RUST, b, 2).shape);
}
fn body(src: &str) -> String {
pair(&RUST, src, src.lines().count() - 1).shape
}
#[test]
fn body_marker_reads_the_parameter_type() {
let a = "fn f(days: u32) {\n renew(days);\n}\n";
let b = "fn f(days: u8) {\n renew(days);\n}\n";
assert_ne!(body(a), body(b));
}
#[test]
fn body_marker_reads_the_return_type() {
let a = "fn f() -> bool {\n check()\n}\n";
let b = "fn f() -> u8 {\n check()\n}\n";
assert_ne!(body(a), body(b));
}
#[test]
fn body_marker_reads_a_behavioural_attribute_and_not_a_lint() {
let a = "#[cached(size = 10)]\nfn f() {\n check();\n}\n";
let b = "#[cached(size = 20)]\nfn f() {\n check();\n}\n";
assert_ne!(body(a), body(b));
let plain = "fn f() {\n check();\n}\n";
let linted = "#[allow(dead_code)]\n#[inline]\n/// Doc.\nfn g() {\n check();\n}\n";
assert_eq!(body(plain), body(linted));
}
#[test]
fn body_marker_reads_track_caller() {
let plain = "fn f() -> u32 {\n Location::caller().line()\n}\n";
let tracked = "#[track_caller]\nfn f() -> u32 {\n Location::caller().line()\n}\n";
assert_ne!(body(plain), body(tracked));
let renamed = "#[track_caller]\nfn g() -> u32 {\n Location::caller().line()\n}\n";
assert_eq!(body(tracked), body(renamed));
}
#[test]
fn body_marker_ignores_the_name_and_the_visibility() {
let a = "fn f() {\n check();\n}\n";
let b = "pub(crate) fn g() {\n check();\n}\n";
assert_eq!(body(a), body(b));
}
#[test]
fn body_marker_reads_async() {
let a = "fn f() {\n check();\n}\n";
let b = "async fn f() {\n check();\n}\n";
assert_ne!(body(a), body(b));
}
#[test]
fn every_rust_type_reaches_the_shape() {
let item = |ty: &str| {
format!("fn g() {{\n struct T {{ f: {ty} }}\n type U = {ty};\n const N: {ty} = 1;\n run(T {{ f: 1 }});\n}}\n")
};
moves(&RUST, &item("u8"), &item("u16"), 1);
}
#[test]
fn a_lifetime_rename_stays() {
let f = |l: &str| format!("fn g<{l}>(x: &{l} str) {{\n // pin\n run(x);\n}}\n");
stays(&RUST, &f("'a"), &f("'b"), 2);
}
#[test]
fn a_behavioural_attribute_in_a_span_moves() {
let f = |name: &str| {
format!("fn g() {{\n #[derive(Serialize)]\n struct T {{ #[serde(rename = \"{name}\")] f: u8 }}\n run();\n}}\n")
};
moves(&RUST, &f("a"), &f("b"), 1);
let doc =
|text: &str| format!("fn g() {{\n #[doc = \"{text}\"]\n struct T;\n run();\n}}\n");
stays(&RUST, &doc("a"), &doc("b"), 1);
}
#[test]
fn the_impl_header_reaches_a_method_body() {
let imp = |head: &str| {
format!("{head} {{\n fn g(&self) {{\n // pin\n run(self);\n }}\n}}\n")
};
moves(&RUST, &imp("impl Foo for A"), &imp("impl Foo for B"), 3);
moves(&RUST, &imp("impl Foo for A"), &imp("impl Bar for A"), 3);
}
#[test]
fn an_inner_cfg_reaches_the_items_of_its_file() {
let file = |feature: &str| {
format!("#![cfg(feature = \"{feature}\")]\nfn g() {{\n // pin\n run();\n}}\n")
};
moves(&RUST, &file("a"), &file("b"), 3);
}
#[test]
fn whitespace_in_a_string_moves() {
let f = |s: &str| format!("fn g(v: &[&str]) {{\n run(v.join(\"{s}\"));\n}}\n");
moves(&RUST, &f(", "), &f(","), 1);
stays(
&RUST,
"fn g() {\n run(0xFF);\n}\n",
"fn g() {\n run(0xff);\n}\n",
1,
);
}
#[test]
fn a_parameter_rename_stays_for_a_span_in_the_body() {
let f = |p: &str| format!("fn g({p}: u8) {{\n // pin\n run({p});\n}}\n");
stays(&RUST, &f("x"), &f("y"), 2);
}
#[test]
fn a_closure_block_that_only_holds_its_tail_stays() {
let f = |body: &str| format!("fn g(v: Vec<u8>) {{\n run(v.iter().map(|p| {body}));\n}}\n");
stays(&RUST, &f("p + 1"), &f("{ p + 1 }"), 1);
stays(
&RUST,
&f("p + 1"),
&f("{\n // why\n p + 1\n }"),
1,
);
moves(&RUST, &f("p + 1"), &f("{ p + 1; }"), 1);
moves(&RUST, &f("p + 1"), &f("{ let q = p; q + 1 }"), 1);
moves(&RUST, &f("p + 1"), &f("'a: { p + 1 }"), 1);
moves(&RUST, &f("p + 1"), &f("{ #![allow(unused)] p + 1 }"), 1);
}
#[test]
fn the_control_headers_around_a_span_reach_it() {
let body = |head: &str| {
format!(
"fn f(u: bool, n: u32) {{\n {head} {{\n // pin\n act();\n }}\n}}\n"
)
};
moves(&RUST, &body("if u"), &body("if !u"), 3);
moves(&RUST, &body("for i in 0..n"), &body("for i in 0..n + 1"), 3);
moves(&RUST, &body("while u"), &body("while !u"), 3);
let arm = |pat: &str| {
format!("fn f(r: R) {{\n match r {{\n {pat} => {{\n // pin\n act();\n }}\n _ => {{}}\n }}\n}}\n")
};
moves(&RUST, &arm("Some(1)"), &arm("Some(2)"), 4);
moves(&RUST, &arm("Some(x) if x > 1"), &arm("Some(x) if x > 2"), 4);
let other = |then: &str| {
format!("fn f(u: bool) {{\n if u {{\n // pin\n act();\n }} else {{\n {then}\n }}\n}}\n")
};
stays(&RUST, &other("one();"), &other("two();"), 3);
let tail = |cond: &str| {
format!("fn f(u: bool, v: bool) {{\n if u {{\n one();\n }} else if {cond} {{\n // pin\n act();\n }}\n}}\n")
};
moves(&RUST, &tail("v"), &tail("!v"), 5);
}
#[test]
fn the_call_that_receives_a_closure_reaches_its_span() {
let call = |head: &str| {
format!("fn f(xs: &[u32]) -> Vec<u32> {{\n {head}(|x| {{\n // pin\n *x > 3\n }}).collect()\n}}\n")
};
moves(
&RUST,
&call("xs.iter().copied().filter"),
&call("xs.iter().copied().take_while"),
3,
);
moves(
&RUST,
&call("xs.iter().filter"),
&call("ys.iter().filter"),
3,
);
let tail = |head: &str| {
format!("fn f(xs: &[u32]) -> bool {{\n xs.iter().{head}(|x|\n // pin\n *x > 3)\n}}\n")
};
moves(&RUST, &tail("any"), &tail("all"), 3);
let args = |n: &str, other: &str| {
format!("fn f(v: u32) {{\n run({n}, |x| {{\n // pin\n go(x)\n }}, |y| {{\n {other}\n }});\n}}\n")
};
moves(&RUST, &args("1", "one(y)"), &args("2", "one(y)"), 3);
moves(&RUST, &args("1", "one(y)"), &args("1", "two(y)"), 3);
let nested = |outer: &str| {
format!("fn f(v: u32) {{\n {outer}(move || {{\n spawn(|| {{\n // pin\n go(v)\n }});\n }});\n}}\n")
};
moves(&RUST, &nested("defer"), &nested("now"), 4);
}
#[test]
fn an_async_block_a_call_receives_reads_that_call() {
let call = |head: &str| {
format!("fn f(v: u32) {{\n {head}(async move {{\n // pin\n go(v).await;\n }});\n}}\n")
};
moves(&RUST, &call("spawn"), &call("spawn_later"), 3);
let timed = |secs: &str| {
format!("fn f(v: u32) {{\n timeout(Duration::from_secs({secs}), async {{\n // pin\n go(v).await;\n }});\n}}\n")
};
moves(&RUST, &timed("5"), &timed("9"), 3);
let unsafe_arg = |head: &str| {
format!(
"fn f(v: u32) {{\n {head}(unsafe {{\n // pin\n go(v)\n }});\n}}\n"
)
};
moves(&RUST, &unsafe_arg("keep"), &unsafe_arg("drop"), 3);
}
#[test]
fn every_statement_before_the_span_in_its_function_reaches_it() {
let body = |guard: &str| {
format!("fn f(a: u32, n: u32) -> bool {{\n {guard}\n if a > 5 {{\n // pin\n return n >= 1;\n }}\n true\n}}\n")
};
let guarded = |a: &str, b: &str| moves(&RUST, &body(a), &body(b), 4);
guarded(
"if a > 10000 { return true; }",
"if a > 600 { return true; }",
);
guarded("if a > 1 { return true; }", "if a > 1 { log(); }");
guarded(
"if a > 1 { return true; } else { log(); }",
"if a > 1 { log(); } else { return true; }",
);
guarded("let v = load(a)?;", "let v = load(a + 1)?;");
guarded(
"let Some(v) = get(a) else { return false };",
"let Some(v) = get(n) else { return false };",
);
guarded("assert!(a > 1);", "assert!(a > 2);");
guarded("ensure!(a > 1, \"low\");", "ensure!(a > 2, \"low\");");
guarded("log!(\"{}\", load(a)?);", "log!(\"{}\", load(n)?);");
guarded(
"match a { 0 => return false, _ => {} }",
"match a { 1 => return false, _ => {} }",
);
guarded(
"if a > 1 { std::process::exit(1); }",
"if a > 2 { std::process::exit(1); }",
);
guarded(
"if a > 1 { one(); return true; }",
"if a > 1 { two(); return true; }",
);
guarded(
"for x in 0..a { if x > 1 { break; } }",
"for x in 0..a + 1 { if x > 1 { break; } }",
);
guarded("let g = || { return 1; };", "let g = || { return 2; };");
guarded("let v = load(a);", "let v = load(a + 1);");
guarded(
"assert!(conv::<i32, i64>(a) > 0, \"neg\");",
"assert!(conv::<i32, i64>(a) < 0, \"neg\");",
);
guarded("p.expect(\"(\")?;", "p.expect(\"[\")?;");
guarded(
"if a > 10 { return Err(\"neg\".to_string()); }",
"if a > 10 { return { if a == 11 { continue; } Err(\"neg\".to_string()) }; }",
);
guarded(
"let v = find(a).ok_or(\"no value\")?;",
"let v = find(a).ok_or(format!(\"{}\", { std::process::exit(1); 0 }))?;",
);
guarded("if a > 100 { log(1); }", "if a > 100 { leave(1); }");
guarded(
"if a > 10 { bail!(\"big\"); }",
"if a > 10 { bail!(\"huge\"); }",
);
let outer = |cond: &str| {
format!("fn f(a: u32) {{\n if {cond} {{\n return;\n }}\n loop {{\n if a > 1 {{\n // pin\n go();\n }}\n }}\n}}\n")
};
moves(&RUST, &outer("a > 9"), &outer("a > 8"), 7);
let looped = |cond: &str| {
format!("fn f(xs: &[u32]) {{\n for x in xs {{\n if {cond} {{\n continue;\n }}\n if *x > 1 {{\n // pin\n go();\n }}\n }}\n}}\n")
};
moves(&RUST, &looped("*x > 9"), &looped("*x > 8"), 7);
let received = |cond: &str| {
format!("fn f(a: u32) {{\n if {cond} {{\n return;\n }}\n spawn(move || {{\n // pin\n go(a);\n }});\n}}\n")
};
moves(&RUST, &received("a > 9"), &received("a > 8"), 6);
let nested = |cond: &str| {
format!("fn f(a: u32) {{\n if {cond} {{\n return;\n }}\n fn inner(b: u32) {{\n // pin\n go(b);\n }}\n}}\n")
};
moves(&RUST, &nested("a > 9"), &nested("a > 8"), 6);
}
#[test]
fn a_closure_in_a_struct_or_array_argument_reads_the_call() {
let call = |flag: &str| {
format!("fn f(flag: bool) {{\n run(Opts {{\n on: {flag},\n f: || {{\n // pin\n go();\n }},\n }});\n}}\n")
};
moves(&RUST, &call("flag"), &call("!flag"), 5);
let array = |head: &str| {
format!("fn f() {{\n {head}([|| {{\n // pin\n go();\n }}]);\n}}\n")
};
moves(&RUST, &array("run"), &array("skip"), 3);
}
#[test]
fn a_local_bound_above_the_span_is_numbered_over_the_prefix() {
let body = |a: &str, b: &str, read: &str| {
format!("fn f(p: u32, q: u32) -> u32 {{\n let {a} = p * 2;\n let {b} = q * 3;\n if p > 3 {{\n // pin\n return go({read});\n }}\n 0\n}}\n")
};
stays(
&RUST,
&body("lim", "cap", "lim, cap"),
&body("limit", "ceiling", "limit, ceiling"),
5,
);
moves(
&RUST,
&body("lim", "cap", "lim, cap"),
&body("cap", "lim", "lim, cap"),
5,
);
moves(
&RUST,
&body("lim", "cap", "lim, cap"),
&body("lim", "cap", "cap, lim"),
5,
);
moves(
&RUST,
&body("lim", "cap", "lim"),
&body("lim", "cap", "cap"),
5,
);
moves(
&RUST,
&body("lim", "cap", "lim - cap"),
&body("lim", "cap", "cap - p"),
5,
);
let traced = |extra: &str| {
format!("fn f(p: u32) -> u32 {{\n let lim = p * 2;\n {extra}\n if p > 3 {{\n // pin\n return lim + 1;\n }}\n 0\n}}\n")
};
moves(&RUST, &traced(""), &traced("let trace = p;"), 5);
let ordered = |first: &str, second: &str, read: &str| {
format!("fn f(p: u32) -> bool {{\n {first}\n {second}\n if p > 0 {{\n // pin\n return p <= {read};\n }}\n false\n}}\n")
};
let (limit, floor) = ("let limit = p * 2;", "let floor = p / 2;");
moves(
&RUST,
&ordered(limit, floor, "limit"),
&ordered(limit, floor, "floor"),
5,
);
moves(
&RUST,
&ordered(limit, floor, "limit"),
&ordered(floor, limit, "limit"),
5,
);
let closure = |name: &str| {
format!("fn f(p: u32) {{\n let {name} = p;\n spawn(move || {{\n // pin\n go({name});\n }});\n}}\n")
};
stays(&RUST, &closure("n"), &closure("count"), 4);
}
#[test]
fn the_pattern_of_a_let_else_reaches_its_else() {
let body = |head: &str| {
format!("fn f(o: Option<u32>) -> u32 {{\n {head} else {{\n // pin\n return 0;\n }};\n v\n}}\n")
};
moves(
&RUST,
&body("let Some(v) = o.filter(|v| *v > 4)"),
&body("let Some(v) = o.filter(|v| *v > 5)"),
3,
);
moves(&RUST, &body("let Some(v) = o"), &body("let None = o"), 3);
}
#[test]
fn a_borrow_of_a_name_raw_parses() {
let body = |stmt: &str| format!("fn f(raw: &str, n: u8) {{\n // pin\n {stmt}\n}}\n");
for stmt in [
"open(Path::new(&raw));",
"open(&raw, 1);",
"let p = &raw;",
"let p = &mut raw;",
] {
let src = body(stmt);
let tree = parse(&RUST, &src).unwrap();
assert!(!tree.root_node().has_error(), "{stmt}");
every_byte_reaches_the_walk(&RUST, &src, 2);
}
moves(&RUST, &body("open(&raw, 1);"), &body("open(&raw, 2);"), 2);
moves(&RUST, &body("open(&raw);"), &body("open(&n);"), 2);
let real = body("let p = &raw const n;");
assert!(!parse(&RUST, &real).unwrap().root_node().has_error());
moves(&RUST, &real, &body("let p = &raw mut n;"), 2);
}
#[test]
fn a_match_arm_wrapped_in_a_block_stays() {
let arm = |value: &str| {
format!("fn f(r: u8) {{\n match r {{\n 1 => {value}\n _ => stop(),\n }}\n}}\n")
};
stays(
&RUST,
&arm("go(1),"),
&arm("{\n go(1)\n }"),
1,
);
moves(
&RUST,
&arm("go(1),"),
&arm("{\n go(1);\n }"),
1,
);
}