diff options
Diffstat (limited to 'crates/ra_hir_ty/src/tests')
-rw-r--r-- | crates/ra_hir_ty/src/tests/coercion.rs | 86 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/macros.rs | 42 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/simple.rs | 55 | ||||
-rw-r--r-- | crates/ra_hir_ty/src/tests/traits.rs | 42 |
4 files changed, 220 insertions, 5 deletions
diff --git a/crates/ra_hir_ty/src/tests/coercion.rs b/crates/ra_hir_ty/src/tests/coercion.rs index ac9e3872a..7e99a42ed 100644 --- a/crates/ra_hir_ty/src/tests/coercion.rs +++ b/crates/ra_hir_ty/src/tests/coercion.rs | |||
@@ -370,6 +370,22 @@ fn test() { | |||
370 | } | 370 | } |
371 | 371 | ||
372 | #[test] | 372 | #[test] |
373 | fn return_coerce_unknown() { | ||
374 | assert_snapshot!( | ||
375 | infer_with_mismatches(r#" | ||
376 | fn foo() -> u32 { | ||
377 | return unknown; | ||
378 | } | ||
379 | "#, true), | ||
380 | @r###" | ||
381 | [17; 40) '{ ...own; }': ! | ||
382 | [23; 37) 'return unknown': ! | ||
383 | [30; 37) 'unknown': u32 | ||
384 | "### | ||
385 | ); | ||
386 | } | ||
387 | |||
388 | #[test] | ||
373 | fn coerce_autoderef() { | 389 | fn coerce_autoderef() { |
374 | assert_snapshot!( | 390 | assert_snapshot!( |
375 | infer_with_mismatches(r#" | 391 | infer_with_mismatches(r#" |
@@ -440,3 +456,73 @@ fn test() { | |||
440 | "### | 456 | "### |
441 | ); | 457 | ); |
442 | } | 458 | } |
459 | |||
460 | #[test] | ||
461 | fn closure_return_coerce() { | ||
462 | assert_snapshot!( | ||
463 | infer_with_mismatches(r#" | ||
464 | fn foo() { | ||
465 | let x = || { | ||
466 | if true { | ||
467 | return &1u32; | ||
468 | } | ||
469 | &&1u32 | ||
470 | }; | ||
471 | } | ||
472 | "#, true), | ||
473 | @r###" | ||
474 | [10; 106) '{ ... }; }': () | ||
475 | [20; 21) 'x': || -> &u32 | ||
476 | [24; 103) '|| { ... }': || -> &u32 | ||
477 | [27; 103) '{ ... }': &u32 | ||
478 | [37; 82) 'if tru... }': () | ||
479 | [40; 44) 'true': bool | ||
480 | [45; 82) '{ ... }': ! | ||
481 | [59; 71) 'return &1u32': ! | ||
482 | [66; 71) '&1u32': &u32 | ||
483 | [67; 71) '1u32': u32 | ||
484 | [91; 97) '&&1u32': &&u32 | ||
485 | [92; 97) '&1u32': &u32 | ||
486 | [93; 97) '1u32': u32 | ||
487 | "### | ||
488 | ); | ||
489 | } | ||
490 | |||
491 | #[test] | ||
492 | fn coerce_fn_item_to_fn_ptr() { | ||
493 | assert_snapshot!( | ||
494 | infer_with_mismatches(r#" | ||
495 | fn foo(x: u32) -> isize { 1 } | ||
496 | fn test() { | ||
497 | let f: fn(u32) -> isize = foo; | ||
498 | } | ||
499 | "#, true), | ||
500 | @r###" | ||
501 | [8; 9) 'x': u32 | ||
502 | [25; 30) '{ 1 }': isize | ||
503 | [27; 28) '1': isize | ||
504 | [41; 79) '{ ...foo; }': () | ||
505 | [51; 52) 'f': fn(u32) -> isize | ||
506 | [73; 76) 'foo': fn foo(u32) -> isize | ||
507 | "### | ||
508 | ); | ||
509 | } | ||
510 | |||
511 | #[test] | ||
512 | fn coerce_closure_to_fn_ptr() { | ||
513 | assert_snapshot!( | ||
514 | infer_with_mismatches(r#" | ||
515 | fn test() { | ||
516 | let f: fn(u32) -> isize = |x| { 1 }; | ||
517 | } | ||
518 | "#, true), | ||
519 | @r###" | ||
520 | [11; 55) '{ ...1 }; }': () | ||
521 | [21; 22) 'f': fn(u32) -> isize | ||
522 | [43; 52) '|x| { 1 }': |u32| -> isize | ||
523 | [44; 45) 'x': u32 | ||
524 | [47; 52) '{ 1 }': isize | ||
525 | [49; 50) '1': isize | ||
526 | "### | ||
527 | ); | ||
528 | } | ||
diff --git a/crates/ra_hir_ty/src/tests/macros.rs b/crates/ra_hir_ty/src/tests/macros.rs index 812f171db..69c695cc8 100644 --- a/crates/ra_hir_ty/src/tests/macros.rs +++ b/crates/ra_hir_ty/src/tests/macros.rs | |||
@@ -183,6 +183,48 @@ fn test() { S.foo()<|>; } | |||
183 | } | 183 | } |
184 | 184 | ||
185 | #[test] | 185 | #[test] |
186 | fn infer_impl_items_generated_by_macros() { | ||
187 | let t = type_at( | ||
188 | r#" | ||
189 | //- /main.rs | ||
190 | macro_rules! m { | ||
191 | () => (fn foo(&self) -> u128 {0}) | ||
192 | } | ||
193 | struct S; | ||
194 | impl S { | ||
195 | m!(); | ||
196 | } | ||
197 | |||
198 | fn test() { S.foo()<|>; } | ||
199 | "#, | ||
200 | ); | ||
201 | assert_eq!(t, "u128"); | ||
202 | } | ||
203 | |||
204 | #[test] | ||
205 | fn infer_impl_items_generated_by_macros_chain() { | ||
206 | let t = type_at( | ||
207 | r#" | ||
208 | //- /main.rs | ||
209 | macro_rules! m_inner { | ||
210 | () => {fn foo(&self) -> u128 {0}} | ||
211 | } | ||
212 | macro_rules! m { | ||
213 | () => {m_inner!();} | ||
214 | } | ||
215 | |||
216 | struct S; | ||
217 | impl S { | ||
218 | m!(); | ||
219 | } | ||
220 | |||
221 | fn test() { S.foo()<|>; } | ||
222 | "#, | ||
223 | ); | ||
224 | assert_eq!(t, "u128"); | ||
225 | } | ||
226 | |||
227 | #[test] | ||
186 | fn infer_macro_with_dollar_crate_is_correct_in_expr() { | 228 | fn infer_macro_with_dollar_crate_is_correct_in_expr() { |
187 | let (db, pos) = TestDB::with_position( | 229 | let (db, pos) = TestDB::with_position( |
188 | r#" | 230 | r#" |
diff --git a/crates/ra_hir_ty/src/tests/simple.rs b/crates/ra_hir_ty/src/tests/simple.rs index 18976c9ae..6fe647a5e 100644 --- a/crates/ra_hir_ty/src/tests/simple.rs +++ b/crates/ra_hir_ty/src/tests/simple.rs | |||
@@ -1606,3 +1606,58 @@ fn main() { | |||
1606 | ); | 1606 | ); |
1607 | assert_eq!(t, "u32"); | 1607 | assert_eq!(t, "u32"); |
1608 | } | 1608 | } |
1609 | |||
1610 | #[test] | ||
1611 | fn closure_return() { | ||
1612 | assert_snapshot!( | ||
1613 | infer(r#" | ||
1614 | fn foo() -> u32 { | ||
1615 | let x = || -> usize { return 1; }; | ||
1616 | } | ||
1617 | "#), | ||
1618 | @r###" | ||
1619 | [17; 59) '{ ...; }; }': () | ||
1620 | [27; 28) 'x': || -> usize | ||
1621 | [31; 56) '|| -> ...n 1; }': || -> usize | ||
1622 | [43; 56) '{ return 1; }': ! | ||
1623 | [45; 53) 'return 1': ! | ||
1624 | [52; 53) '1': usize | ||
1625 | "### | ||
1626 | ); | ||
1627 | } | ||
1628 | |||
1629 | #[test] | ||
1630 | fn closure_return_unit() { | ||
1631 | assert_snapshot!( | ||
1632 | infer(r#" | ||
1633 | fn foo() -> u32 { | ||
1634 | let x = || { return; }; | ||
1635 | } | ||
1636 | "#), | ||
1637 | @r###" | ||
1638 | [17; 48) '{ ...; }; }': () | ||
1639 | [27; 28) 'x': || -> () | ||
1640 | [31; 45) '|| { return; }': || -> () | ||
1641 | [34; 45) '{ return; }': ! | ||
1642 | [36; 42) 'return': ! | ||
1643 | "### | ||
1644 | ); | ||
1645 | } | ||
1646 | |||
1647 | #[test] | ||
1648 | fn closure_return_inferred() { | ||
1649 | assert_snapshot!( | ||
1650 | infer(r#" | ||
1651 | fn foo() -> u32 { | ||
1652 | let x = || { "test" }; | ||
1653 | } | ||
1654 | "#), | ||
1655 | @r###" | ||
1656 | [17; 47) '{ ..." }; }': () | ||
1657 | [27; 28) 'x': || -> &str | ||
1658 | [31; 44) '|| { "test" }': || -> &str | ||
1659 | [34; 44) '{ "test" }': &str | ||
1660 | [36; 42) '"test"': &str | ||
1661 | "### | ||
1662 | ); | ||
1663 | } | ||
diff --git a/crates/ra_hir_ty/src/tests/traits.rs b/crates/ra_hir_ty/src/tests/traits.rs index 802937cb0..76e2198b6 100644 --- a/crates/ra_hir_ty/src/tests/traits.rs +++ b/crates/ra_hir_ty/src/tests/traits.rs | |||
@@ -393,11 +393,11 @@ fn test() -> u64 { | |||
393 | [54; 55) 'a': S | 393 | [54; 55) 'a': S |
394 | [58; 59) 'S': S(fn(u32) -> u64) -> S | 394 | [58; 59) 'S': S(fn(u32) -> u64) -> S |
395 | [58; 68) 'S(|i| 2*i)': S | 395 | [58; 68) 'S(|i| 2*i)': S |
396 | [60; 67) '|i| 2*i': |i32| -> i32 | 396 | [60; 67) '|i| 2*i': |u32| -> u64 |
397 | [61; 62) 'i': i32 | 397 | [61; 62) 'i': u32 |
398 | [64; 65) '2': i32 | 398 | [64; 65) '2': u32 |
399 | [64; 67) '2*i': i32 | 399 | [64; 67) '2*i': u32 |
400 | [66; 67) 'i': i32 | 400 | [66; 67) 'i': u32 |
401 | [78; 79) 'b': u64 | 401 | [78; 79) 'b': u64 |
402 | [82; 83) 'a': S | 402 | [82; 83) 'a': S |
403 | [82; 85) 'a.0': fn(u32) -> u64 | 403 | [82; 85) 'a.0': fn(u32) -> u64 |
@@ -427,6 +427,38 @@ fn indexing_arrays() { | |||
427 | } | 427 | } |
428 | 428 | ||
429 | #[test] | 429 | #[test] |
430 | fn infer_ops_index() { | ||
431 | let (db, pos) = TestDB::with_position( | ||
432 | r#" | ||
433 | //- /main.rs crate:main deps:std | ||
434 | |||
435 | struct Bar; | ||
436 | struct Foo; | ||
437 | |||
438 | impl std::ops::Index<u32> for Bar { | ||
439 | type Output = Foo; | ||
440 | } | ||
441 | |||
442 | fn test() { | ||
443 | let a = Bar; | ||
444 | let b = a[1]; | ||
445 | b<|>; | ||
446 | } | ||
447 | |||
448 | //- /std.rs crate:std | ||
449 | |||
450 | #[prelude_import] use ops::*; | ||
451 | mod ops { | ||
452 | pub trait Index<Idx> { | ||
453 | type Output; | ||
454 | } | ||
455 | } | ||
456 | "#, | ||
457 | ); | ||
458 | assert_eq!("Foo", type_at_pos(&db, pos)); | ||
459 | } | ||
460 | |||
461 | #[test] | ||
430 | fn deref_trait() { | 462 | fn deref_trait() { |
431 | let t = type_at( | 463 | let t = type_at( |
432 | r#" | 464 | r#" |