From 6299ccd350c190003c51aa68f48b1edfb1a497b1 Mon Sep 17 00:00:00 2001 From: Michael Killough Date: Sun, 17 Mar 2019 19:05:10 +0000 Subject: Split test case and use tested_by!. --- crates/ra_hir/src/marks.rs | 1 + crates/ra_hir/src/ty/infer.rs | 3 + crates/ra_hir/src/ty/tests.rs | 175 ++++++++++++++++++++++++------------------ 3 files changed, 103 insertions(+), 76 deletions(-) (limited to 'crates/ra_hir') diff --git a/crates/ra_hir/src/marks.rs b/crates/ra_hir/src/marks.rs index 16852a6a1..6f3e5f09d 100644 --- a/crates/ra_hir/src/marks.rs +++ b/crates/ra_hir/src/marks.rs @@ -7,4 +7,5 @@ test_utils::marks!( glob_enum glob_across_crates std_prelude + match_ergonomics_ref ); diff --git a/crates/ra_hir/src/ty/infer.rs b/crates/ra_hir/src/ty/infer.rs index 3d0b7a827..0a698988c 100644 --- a/crates/ra_hir/src/ty/infer.rs +++ b/crates/ra_hir/src/ty/infer.rs @@ -619,6 +619,9 @@ impl<'a, D: HirDatabase> InferenceContext<'a, D> { } } } else if let Pat::Ref { .. } = &body[pat] { + tested_by!(match_ergonomics_ref); + // When you encounter a `&pat` pattern, reset to Move. + // This is so that `w` is by value: `let (_, &w) = &(1, &2);` default_bm = BindingMode::Move; } diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index b12ea4334..0f8551a9d 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -756,8 +756,6 @@ fn test(x: &str, y: isize) { fn infer_pattern() { assert_snapshot_matches!( infer(r#" -struct A(T); - fn test(x: &i32) { let y = x; let &z = x; @@ -774,12 +772,6 @@ fn test(x: &i32) { let lambda = |a: u64, b, c: i32| { a + b; c }; - let A(n) = &A(1); - let A(n) = &mut A(1); - - let v = &(1, &2); - let (_, &w) = v; - let ref ref_to_x = x; let mut mut_x = x; let ref mut mut_ref_to_x = x; @@ -787,76 +779,107 @@ fn test(x: &i32) { } "#), @r###" -[26; 27) 'x': &i32 -[35; 479) '{ ...o_x; }': () -[45; 46) 'y': &i32 -[49; 50) 'x': &i32 -[60; 62) '&z': &i32 -[61; 62) 'z': i32 -[65; 66) 'x': &i32 -[76; 77) 'a': i32 -[80; 81) 'z': i32 -[91; 97) '(c, d)': (i32, &str) -[92; 93) 'c': i32 -[95; 96) 'd': &str -[100; 112) '(1, "hello")': (i32, &str) -[101; 102) '1': i32 -[104; 111) '"hello"': &str -[119; 169) 'for (e... }': () -[123; 129) '(e, f)': ({unknown}, {unknown}) -[124; 125) 'e': {unknown} -[127; 128) 'f': {unknown} -[133; 142) 'some_iter': {unknown} -[143; 169) '{ ... }': () -[157; 158) 'g': {unknown} -[161; 162) 'e': {unknown} -[175; 222) 'if let... }': () -[182; 187) '[val]': {unknown} -[190; 193) 'opt': {unknown} -[194; 222) '{ ... }': () -[208; 209) 'h': {unknown} -[212; 215) 'val': {unknown} -[232; 238) 'lambda': {unknown} -[241; 273) '|a: u6...b; c }': {unknown} -[242; 243) 'a': u64 +[9; 10) 'x': &i32 +[18; 369) '{ ...o_x; }': () +[28; 29) 'y': &i32 +[32; 33) 'x': &i32 +[43; 45) '&z': &i32 +[44; 45) 'z': i32 +[48; 49) 'x': &i32 +[59; 60) 'a': i32 +[63; 64) 'z': i32 +[74; 80) '(c, d)': (i32, &str) +[75; 76) 'c': i32 +[78; 79) 'd': &str +[83; 95) '(1, "hello")': (i32, &str) +[84; 85) '1': i32 +[87; 94) '"hello"': &str +[102; 152) 'for (e... }': () +[106; 112) '(e, f)': ({unknown}, {unknown}) +[107; 108) 'e': {unknown} +[110; 111) 'f': {unknown} +[116; 125) 'some_iter': {unknown} +[126; 152) '{ ... }': () +[140; 141) 'g': {unknown} +[144; 145) 'e': {unknown} +[158; 205) 'if let... }': () +[165; 170) '[val]': {unknown} +[173; 176) 'opt': {unknown} +[177; 205) '{ ... }': () +[191; 192) 'h': {unknown} +[195; 198) 'val': {unknown} +[215; 221) 'lambda': {unknown} +[224; 256) '|a: u6...b; c }': {unknown} +[225; 226) 'a': u64 +[233; 234) 'b': u64 +[236; 237) 'c': i32 +[244; 256) '{ a + b; c }': i32 +[246; 247) 'a': u64 +[246; 251) 'a + b': u64 [250; 251) 'b': u64 [253; 254) 'c': i32 -[261; 273) '{ a + b; c }': i32 -[263; 264) 'a': u64 -[263; 268) 'a + b': u64 -[267; 268) 'b': u64 -[270; 271) 'c': i32 -[284; 288) 'A(n)': A -[286; 287) 'n': &i32 -[291; 296) '&A(1)': &A -[292; 293) 'A': A(T) -> A -[292; 296) 'A(1)': A -[294; 295) '1': i32 -[306; 310) 'A(n)': A -[308; 309) 'n': &mut i32 -[313; 322) '&mut A(1)': &mut A -[318; 319) 'A': A(T) -> A -[318; 322) 'A(1)': A -[320; 321) '1': i32 -[333; 334) 'v': &(i32, &i32) -[337; 345) '&(1, &2)': &(i32, &i32) -[338; 345) '(1, &2)': (i32, &i32) -[339; 340) '1': i32 -[342; 344) '&2': &i32 -[343; 344) '2': i32 -[355; 362) '(_, &w)': (i32, &i32) -[356; 357) '_': i32 -[359; 361) '&w': &i32 -[360; 361) 'w': i32 -[365; 366) 'v': &(i32, &i32) -[377; 389) 'ref ref_to_x': &&i32 -[392; 393) 'x': &i32 -[403; 412) 'mut mut_x': &i32 -[415; 416) 'x': &i32 -[426; 446) 'ref mu...f_to_x': &mut &i32 -[449; 450) 'x': &i32 -[460; 461) 'k': &mut &i32 -[464; 476) 'mut_ref_to_x': &mut &i32"### +[267; 279) 'ref ref_to_x': &&i32 +[282; 283) 'x': &i32 +[293; 302) 'mut mut_x': &i32 +[305; 306) 'x': &i32 +[316; 336) 'ref mu...f_to_x': &mut &i32 +[339; 340) 'x': &i32 +[350; 351) 'k': &mut &i32 +[354; 366) 'mut_ref_to_x': &mut &i32"### + ); +} + +#[test] +fn infer_pattern_match_ergonomics() { + assert_snapshot_matches!( + infer(r#" +struct A(T); + +fn test() { + let A(n) = &A(1); + let A(n) = &mut A(1); +} +"#), + @r###" +[28; 79) '{ ...(1); }': () +[38; 42) 'A(n)': A +[40; 41) 'n': &i32 +[45; 50) '&A(1)': &A +[46; 47) 'A': A(T) -> A +[46; 50) 'A(1)': A +[48; 49) '1': i32 +[60; 64) 'A(n)': A +[62; 63) 'n': &mut i32 +[67; 76) '&mut A(1)': &mut A +[72; 73) 'A': A(T) -> A +[72; 76) 'A(1)': A +[74; 75) '1': i32"### + ); +} + +#[test] +fn infer_pattern_match_ergonomics_ref() { + covers!(match_ergonomics_ref); + assert_snapshot_matches!( + infer(r#" +fn test() { + let v = &(1, &2); + let (_, &w) = v; +} +"#), + @r###" +[11; 57) '{ ...= v; }': () +[21; 22) 'v': &(i32, &i32) +[25; 33) '&(1, &2)': &(i32, &i32) +[26; 33) '(1, &2)': (i32, &i32) +[27; 28) '1': i32 +[30; 32) '&2': &i32 +[31; 32) '2': i32 +[43; 50) '(_, &w)': (i32, &i32) +[44; 45) '_': i32 +[47; 49) '&w': &i32 +[48; 49) 'w': i32 +[53; 54) 'v': &(i32, &i32)"### ); } -- cgit v1.2.3