aboutsummaryrefslogtreecommitdiff
path: root/crates/hir_ty/src/tests
diff options
context:
space:
mode:
Diffstat (limited to 'crates/hir_ty/src/tests')
-rw-r--r--crates/hir_ty/src/tests/coercion.rs43
-rw-r--r--crates/hir_ty/src/tests/patterns.rs57
-rw-r--r--crates/hir_ty/src/tests/traits.rs67
3 files changed, 115 insertions, 52 deletions
diff --git a/crates/hir_ty/src/tests/coercion.rs b/crates/hir_ty/src/tests/coercion.rs
index 6dac7e103..71047703d 100644
--- a/crates/hir_ty/src/tests/coercion.rs
+++ b/crates/hir_ty/src/tests/coercion.rs
@@ -1,6 +1,6 @@
1use expect_test::expect; 1use expect_test::expect;
2 2
3use super::{check_infer, check_infer_with_mismatches, check_types}; 3use super::{check_infer, check_infer_with_mismatches, check_no_mismatches, check_types};
4 4
5#[test] 5#[test]
6fn infer_block_expr_type_mismatch() { 6fn infer_block_expr_type_mismatch() {
@@ -963,7 +963,7 @@ fn test() -> i32 {
963 963
964#[test] 964#[test]
965fn panic_macro() { 965fn panic_macro() {
966 check_infer_with_mismatches( 966 check_no_mismatches(
967 r#" 967 r#"
968mod panic { 968mod panic {
969 #[macro_export] 969 #[macro_export]
@@ -991,15 +991,34 @@ fn main() {
991 panic!() 991 panic!()
992} 992}
993 "#, 993 "#,
994 expect![[r#" 994 );
995 174..185 '{ loop {} }': ! 995}
996 176..183 'loop {}': ! 996
997 181..183 '{}': () 997#[test]
998 !0..24 '$crate...:panic': fn panic() -> ! 998fn coerce_unsize_expected_type() {
999 !0..26 '$crate...anic()': ! 999 check_no_mismatches(
1000 !0..26 '$crate...anic()': ! 1000 r#"
1001 !0..28 '$crate...015!()': ! 1001#[lang = "sized"]
1002 454..470 '{ ...c!() }': () 1002pub trait Sized {}
1003 "#]], 1003#[lang = "unsize"]
1004pub trait Unsize<T> {}
1005#[lang = "coerce_unsized"]
1006pub trait CoerceUnsized<T> {}
1007
1008impl<T: Unsize<U>, U> CoerceUnsized<&U> for &T {}
1009
1010fn main() {
1011 let foo: &[u32] = &[1, 2];
1012 let foo: &[u32] = match true {
1013 true => &[1, 2],
1014 false => &[1, 2, 3],
1015 };
1016 let foo: &[u32] = if true {
1017 &[1, 2]
1018 } else {
1019 &[1, 2, 3]
1020 };
1021}
1022 "#,
1004 ); 1023 );
1005} 1024}
diff --git a/crates/hir_ty/src/tests/patterns.rs b/crates/hir_ty/src/tests/patterns.rs
index 7d00cee9b..aa513c56d 100644
--- a/crates/hir_ty/src/tests/patterns.rs
+++ b/crates/hir_ty/src/tests/patterns.rs
@@ -1,6 +1,6 @@
1use expect_test::expect; 1use expect_test::expect;
2 2
3use super::{check_infer, check_infer_with_mismatches, check_types}; 3use super::{check_infer, check_infer_with_mismatches, check_mismatches, check_types};
4 4
5#[test] 5#[test]
6fn infer_pattern() { 6fn infer_pattern() {
@@ -518,47 +518,24 @@ fn infer_generics_in_patterns() {
518 518
519#[test] 519#[test]
520fn infer_const_pattern() { 520fn infer_const_pattern() {
521 check_infer_with_mismatches( 521 check_mismatches(
522 r#" 522 r#"
523 enum Option<T> { None } 523enum Option<T> { None }
524 use Option::None; 524use Option::None;
525 struct Foo; 525struct Foo;
526 const Bar: usize = 1; 526const Bar: usize = 1;
527 527
528 fn test() { 528fn test() {
529 let a: Option<u32> = None; 529 let a: Option<u32> = None;
530 let b: Option<i64> = match a { 530 let b: Option<i64> = match a {
531 None => None, 531 None => None,
532 }; 532 };
533 let _: () = match () { Foo => Foo }; // Expected mismatch 533 let _: () = match () { Foo => () };
534 let _: () = match () { Bar => Bar }; // Expected mismatch 534 // ^^^ expected (), got Foo
535 } 535 let _: () = match () { Bar => () };
536 // ^^^ expected (), got usize
537}
536 "#, 538 "#,
537 expect![[r#"
538 73..74 '1': usize
539 87..309 '{ ...atch }': ()
540 97..98 'a': Option<u32>
541 114..118 'None': Option<u32>
542 128..129 'b': Option<i64>
543 145..182 'match ... }': Option<i64>
544 151..152 'a': Option<u32>
545 163..167 'None': Option<u32>
546 171..175 'None': Option<i64>
547 192..193 '_': ()
548 200..223 'match ... Foo }': Foo
549 206..208 '()': ()
550 211..214 'Foo': Foo
551 218..221 'Foo': Foo
552 254..255 '_': ()
553 262..285 'match ... Bar }': usize
554 268..270 '()': ()
555 273..276 'Bar': usize
556 280..283 'Bar': usize
557 200..223: expected (), got Foo
558 211..214: expected (), got Foo
559 262..285: expected (), got usize
560 273..276: expected (), got usize
561 "#]],
562 ); 539 );
563} 540}
564 541
diff --git a/crates/hir_ty/src/tests/traits.rs b/crates/hir_ty/src/tests/traits.rs
index 588f0d1d4..6bcede4c4 100644
--- a/crates/hir_ty/src/tests/traits.rs
+++ b/crates/hir_ty/src/tests/traits.rs
@@ -3740,3 +3740,70 @@ mod future {
3740"#, 3740"#,
3741 ); 3741 );
3742} 3742}
3743
3744#[test]
3745fn local_impl_1() {
3746 check_types(
3747 r#"
3748trait Trait<T> {
3749 fn foo(&self) -> T;
3750}
3751
3752fn test() {
3753 struct S;
3754 impl Trait<u32> for S {
3755 fn foo(&self) { 0 }
3756 }
3757
3758 S.foo();
3759 // ^^^^^^^ u32
3760}
3761"#,
3762 );
3763}
3764
3765#[test]
3766fn local_impl_2() {
3767 check_types(
3768 r#"
3769struct S;
3770
3771fn test() {
3772 trait Trait<T> {
3773 fn foo(&self) -> T;
3774 }
3775 impl Trait<u32> for S {
3776 fn foo(&self) { 0 }
3777 }
3778
3779 S.foo();
3780 // ^^^^^^^ u32
3781}
3782"#,
3783 );
3784}
3785
3786#[test]
3787fn local_impl_3() {
3788 check_types(
3789 r#"
3790trait Trait<T> {
3791 fn foo(&self) -> T;
3792}
3793
3794fn test() {
3795 struct S1;
3796 {
3797 struct S2;
3798
3799 impl Trait<S1> for S2 {
3800 fn foo(&self) { S1 }
3801 }
3802
3803 S2.foo();
3804 // ^^^^^^^^ S1
3805 }
3806}
3807"#,
3808 );
3809}