From 4ae4d9c311084e3092eb4c2d35e98f6c2c70315b Mon Sep 17 00:00:00 2001 From: Florian Diebold Date: Wed, 4 Sep 2019 22:32:02 +0200 Subject: Add some more tests --- crates/ra_hir/src/ty/tests.rs | 205 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) (limited to 'crates/ra_hir/src/ty') diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index d92d4659b..cb5540cb6 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs @@ -3643,6 +3643,211 @@ fn test>(x: T) { "### ); } + +#[test] +fn where_clause_trait_in_scope_for_method_resolution() { + let t = type_at( + r#" +//- /main.rs +mod foo { + trait Trait { + fn foo(&self) -> u32 {} + } +} + +fn test(x: T) { + x.foo()<|>; +} +"#, + ); + // FIXME should be u32 + assert_eq!(t, "{unknown}"); +} + +#[test] +fn super_trait_method_resolution() { + assert_snapshot!( + infer(r#" +mod foo { + trait SuperTrait { + fn foo(&self) -> u32 {} + } +} +trait Trait1: SuperTrait {} +trait Trait2 where Self: SuperTrait {} + +fn test(x: T, y: U) { + x.foo(); + y.foo(); +} +"#), + @r###" + [50; 54) 'self': &Self + [63; 65) '{}': () + [172; 173) 'x': T + [178; 179) 'y': U + [184; 213) '{ ...o(); }': () + [190; 191) 'x': T + [190; 197) 'x.foo()': {unknown} + [203; 204) 'y': U + [203; 210) 'y.foo()': {unknown} + "### + ); +} + +#[test] +fn super_trait_assoc_type_bounds() { + assert_snapshot!( + infer(r#" +trait SuperTrait { type Type; } +trait Trait where Self: SuperTrait {} + +fn get2>(t: T) -> U {} +fn set>(t: T) -> T {t} + +struct S; +impl SuperTrait for S { type Type = T; } +impl Trait for S {} + +fn test() { + get2(set(S)); +} +"#), + @r###" + [103; 104) 't': T + [114; 116) '{}': () + [146; 147) 't': T + [157; 160) '{t}': T + [158; 159) 't': T + [259; 280) '{ ...S)); }': () + [265; 269) 'get2': fn get2<{unknown}, S<{unknown}>>(T) -> U + [265; 277) 'get2(set(S))': {unknown} + [270; 273) 'set': fn set>(T) -> T + [270; 276) 'set(S)': S<{unknown}> + [274; 275) 'S': S<{unknown}> + "### + ); +} + +#[test] +fn fn_trait() { + assert_snapshot!( + infer(r#" +trait FnOnce { + type Output; + + fn call_once(self, args: Args) -> Self::Output; +} + +fn test u128>(f: F) { + f.call_once((1, 2)); +} +"#), + @r###" + [57; 61) 'self': Self + [63; 67) 'args': Args + [132; 133) 'f': F + [138; 166) '{ ...2)); }': () + [144; 145) 'f': F + [144; 163) 'f.call...1, 2))': {unknown} + [156; 162) '(1, 2)': (i32, i32) + [157; 158) '1': i32 + [160; 161) '2': i32 + "### + ); +} + +#[test] +fn closure_1() { + assert_snapshot!( + infer(r#" +trait FnOnce { + type Output; +} + +enum Option { Some(T), None } +impl Option { + fn map U>(self, f: F) -> U {} +} + +fn test() { + let x = Option::Some(1i32); + x.map(|v| v + 1); + x.map(|_v| 1u64); + let y: Option = x.map(|_v| 1); +} +"#), + @r###" + [128; 132) 'self': Option + [134; 135) 'f': F + [145; 147) '{}': () + [161; 280) '{ ... 1); }': () + [171; 172) 'x': Option + [175; 187) 'Option::Some': Some(T) -> Option + [175; 193) 'Option...(1i32)': Option + [188; 192) '1i32': i32 + [199; 200) 'x': Option + [199; 215) 'x.map(...v + 1)': {unknown} + [205; 214) '|v| v + 1': {unknown} + [206; 207) 'v': {unknown} + [209; 210) 'v': {unknown} + [209; 214) 'v + 1': i32 + [213; 214) '1': i32 + [221; 222) 'x': Option + [221; 237) 'x.map(... 1u64)': {unknown} + [227; 236) '|_v| 1u64': {unknown} + [228; 230) '_v': {unknown} + [232; 236) '1u64': u64 + [247; 248) 'y': Option + [264; 265) 'x': Option + [264; 277) 'x.map(|_v| 1)': Option + [270; 276) '|_v| 1': {unknown} + [271; 273) '_v': {unknown} + [275; 276) '1': i32 + "### + ); +} + +#[test] +fn closure_2() { + assert_snapshot!( + infer(r#" +trait FnOnce { + type Output; +} + +fn test u64>(f: F) { + f(1); + let g = |v| v + 1; + g(1u64); + let h = |v| 1u128 + v; +} +"#), + @r###" + [73; 74) 'f': F + [79; 155) '{ ...+ v; }': () + [85; 86) 'f': F + [85; 89) 'f(1)': {unknown} + [87; 88) '1': i32 + [99; 100) 'g': {unknown} + [103; 112) '|v| v + 1': {unknown} + [104; 105) 'v': {unknown} + [107; 108) 'v': {unknown} + [107; 112) 'v + 1': i32 + [111; 112) '1': i32 + [118; 119) 'g': {unknown} + [118; 125) 'g(1u64)': {unknown} + [120; 124) '1u64': u64 + [135; 136) 'h': {unknown} + [139; 152) '|v| 1u128 + v': {unknown} + [140; 141) 'v': u128 + [143; 148) '1u128': u128 + [143; 152) '1u128 + v': u128 + [151; 152) 'v': u128 + "### + ); +} + fn type_at_pos(db: &MockDatabase, pos: FilePosition) -> String { let file = db.parse(pos.file_id).ok().unwrap(); let expr = algo::find_node_at_offset::(file.syntax(), pos.offset).unwrap(); -- cgit v1.2.3