diff options
Diffstat (limited to 'crates/hir_ty/src/diagnostics/match_check.rs')
-rw-r--r-- | crates/hir_ty/src/diagnostics/match_check.rs | 957 |
1 files changed, 0 insertions, 957 deletions
diff --git a/crates/hir_ty/src/diagnostics/match_check.rs b/crates/hir_ty/src/diagnostics/match_check.rs index c8e1b23de..a30e42699 100644 --- a/crates/hir_ty/src/diagnostics/match_check.rs +++ b/crates/hir_ty/src/diagnostics/match_check.rs | |||
@@ -364,960 +364,3 @@ impl PatternFoldable for PatKind { | |||
364 | } | 364 | } |
365 | } | 365 | } |
366 | } | 366 | } |
367 | |||
368 | #[cfg(test)] | ||
369 | pub(super) mod tests { | ||
370 | mod report { | ||
371 | use std::any::Any; | ||
372 | |||
373 | use hir_def::{expr::PatId, DefWithBodyId}; | ||
374 | use hir_expand::{HirFileId, InFile}; | ||
375 | use syntax::SyntaxNodePtr; | ||
376 | |||
377 | use crate::{ | ||
378 | db::HirDatabase, | ||
379 | diagnostics_sink::{Diagnostic, DiagnosticCode, DiagnosticSink}, | ||
380 | }; | ||
381 | |||
382 | /// In tests, match check bails out loudly. | ||
383 | /// This helps to catch incorrect tests that pass due to false negatives. | ||
384 | pub(crate) fn report_bail_out( | ||
385 | db: &dyn HirDatabase, | ||
386 | def: DefWithBodyId, | ||
387 | pat: PatId, | ||
388 | sink: &mut DiagnosticSink, | ||
389 | ) { | ||
390 | let (_, source_map) = db.body_with_source_map(def); | ||
391 | if let Ok(source_ptr) = source_map.pat_syntax(pat) { | ||
392 | let pat_syntax_ptr = source_ptr.value.either(Into::into, Into::into); | ||
393 | sink.push(BailedOut { file: source_ptr.file_id, pat_syntax_ptr }); | ||
394 | } | ||
395 | } | ||
396 | |||
397 | #[derive(Debug)] | ||
398 | struct BailedOut { | ||
399 | file: HirFileId, | ||
400 | pat_syntax_ptr: SyntaxNodePtr, | ||
401 | } | ||
402 | |||
403 | impl Diagnostic for BailedOut { | ||
404 | fn code(&self) -> DiagnosticCode { | ||
405 | DiagnosticCode("internal:match-check-bailed-out") | ||
406 | } | ||
407 | fn message(&self) -> String { | ||
408 | format!("Internal: match check bailed out") | ||
409 | } | ||
410 | fn display_source(&self) -> InFile<SyntaxNodePtr> { | ||
411 | InFile { file_id: self.file, value: self.pat_syntax_ptr.clone() } | ||
412 | } | ||
413 | fn as_any(&self) -> &(dyn Any + Send + 'static) { | ||
414 | self | ||
415 | } | ||
416 | } | ||
417 | } | ||
418 | |||
419 | use crate::diagnostics::tests::check_diagnostics; | ||
420 | |||
421 | pub(crate) use self::report::report_bail_out; | ||
422 | |||
423 | #[test] | ||
424 | fn empty_tuple() { | ||
425 | check_diagnostics( | ||
426 | r#" | ||
427 | fn main() { | ||
428 | match () { } | ||
429 | //^^ Missing match arm | ||
430 | match (()) { } | ||
431 | //^^^^ Missing match arm | ||
432 | |||
433 | match () { _ => (), } | ||
434 | match () { () => (), } | ||
435 | match (()) { (()) => (), } | ||
436 | } | ||
437 | "#, | ||
438 | ); | ||
439 | } | ||
440 | |||
441 | #[test] | ||
442 | fn tuple_of_two_empty_tuple() { | ||
443 | check_diagnostics( | ||
444 | r#" | ||
445 | fn main() { | ||
446 | match ((), ()) { } | ||
447 | //^^^^^^^^ Missing match arm | ||
448 | |||
449 | match ((), ()) { ((), ()) => (), } | ||
450 | } | ||
451 | "#, | ||
452 | ); | ||
453 | } | ||
454 | |||
455 | #[test] | ||
456 | fn boolean() { | ||
457 | check_diagnostics( | ||
458 | r#" | ||
459 | fn test_main() { | ||
460 | match false { } | ||
461 | //^^^^^ Missing match arm | ||
462 | match false { true => (), } | ||
463 | //^^^^^ Missing match arm | ||
464 | match (false, true) {} | ||
465 | //^^^^^^^^^^^^^ Missing match arm | ||
466 | match (false, true) { (true, true) => (), } | ||
467 | //^^^^^^^^^^^^^ Missing match arm | ||
468 | match (false, true) { | ||
469 | //^^^^^^^^^^^^^ Missing match arm | ||
470 | (false, true) => (), | ||
471 | (false, false) => (), | ||
472 | (true, false) => (), | ||
473 | } | ||
474 | match (false, true) { (true, _x) => (), } | ||
475 | //^^^^^^^^^^^^^ Missing match arm | ||
476 | |||
477 | match false { true => (), false => (), } | ||
478 | match (false, true) { | ||
479 | (false, _) => (), | ||
480 | (true, false) => (), | ||
481 | (_, true) => (), | ||
482 | } | ||
483 | match (false, true) { | ||
484 | (true, true) => (), | ||
485 | (true, false) => (), | ||
486 | (false, true) => (), | ||
487 | (false, false) => (), | ||
488 | } | ||
489 | match (false, true) { | ||
490 | (true, _x) => (), | ||
491 | (false, true) => (), | ||
492 | (false, false) => (), | ||
493 | } | ||
494 | match (false, true, false) { | ||
495 | (false, ..) => (), | ||
496 | (true, ..) => (), | ||
497 | } | ||
498 | match (false, true, false) { | ||
499 | (.., false) => (), | ||
500 | (.., true) => (), | ||
501 | } | ||
502 | match (false, true, false) { (..) => (), } | ||
503 | } | ||
504 | "#, | ||
505 | ); | ||
506 | } | ||
507 | |||
508 | #[test] | ||
509 | fn tuple_of_tuple_and_bools() { | ||
510 | check_diagnostics( | ||
511 | r#" | ||
512 | fn main() { | ||
513 | match (false, ((), false)) {} | ||
514 | //^^^^^^^^^^^^^^^^^^^^ Missing match arm | ||
515 | match (false, ((), false)) { (true, ((), true)) => (), } | ||
516 | //^^^^^^^^^^^^^^^^^^^^ Missing match arm | ||
517 | match (false, ((), false)) { (true, _) => (), } | ||
518 | //^^^^^^^^^^^^^^^^^^^^ Missing match arm | ||
519 | |||
520 | match (false, ((), false)) { | ||
521 | (true, ((), true)) => (), | ||
522 | (true, ((), false)) => (), | ||
523 | (false, ((), true)) => (), | ||
524 | (false, ((), false)) => (), | ||
525 | } | ||
526 | match (false, ((), false)) { | ||
527 | (true, ((), true)) => (), | ||
528 | (true, ((), false)) => (), | ||
529 | (false, _) => (), | ||
530 | } | ||
531 | } | ||
532 | "#, | ||
533 | ); | ||
534 | } | ||
535 | |||
536 | #[test] | ||
537 | fn enums() { | ||
538 | check_diagnostics( | ||
539 | r#" | ||
540 | enum Either { A, B, } | ||
541 | |||
542 | fn main() { | ||
543 | match Either::A { } | ||
544 | //^^^^^^^^^ Missing match arm | ||
545 | match Either::B { Either::A => (), } | ||
546 | //^^^^^^^^^ Missing match arm | ||
547 | |||
548 | match &Either::B { | ||
549 | //^^^^^^^^^^ Missing match arm | ||
550 | Either::A => (), | ||
551 | } | ||
552 | |||
553 | match Either::B { | ||
554 | Either::A => (), Either::B => (), | ||
555 | } | ||
556 | match &Either::B { | ||
557 | Either::A => (), Either::B => (), | ||
558 | } | ||
559 | } | ||
560 | "#, | ||
561 | ); | ||
562 | } | ||
563 | |||
564 | #[test] | ||
565 | fn enum_containing_bool() { | ||
566 | check_diagnostics( | ||
567 | r#" | ||
568 | enum Either { A(bool), B } | ||
569 | |||
570 | fn main() { | ||
571 | match Either::B { } | ||
572 | //^^^^^^^^^ Missing match arm | ||
573 | match Either::B { | ||
574 | //^^^^^^^^^ Missing match arm | ||
575 | Either::A(true) => (), Either::B => () | ||
576 | } | ||
577 | |||
578 | match Either::B { | ||
579 | Either::A(true) => (), | ||
580 | Either::A(false) => (), | ||
581 | Either::B => (), | ||
582 | } | ||
583 | match Either::B { | ||
584 | Either::B => (), | ||
585 | _ => (), | ||
586 | } | ||
587 | match Either::B { | ||
588 | Either::A(_) => (), | ||
589 | Either::B => (), | ||
590 | } | ||
591 | |||
592 | } | ||
593 | "#, | ||
594 | ); | ||
595 | } | ||
596 | |||
597 | #[test] | ||
598 | fn enum_different_sizes() { | ||
599 | check_diagnostics( | ||
600 | r#" | ||
601 | enum Either { A(bool), B(bool, bool) } | ||
602 | |||
603 | fn main() { | ||
604 | match Either::A(false) { | ||
605 | //^^^^^^^^^^^^^^^^ Missing match arm | ||
606 | Either::A(_) => (), | ||
607 | Either::B(false, _) => (), | ||
608 | } | ||
609 | |||
610 | match Either::A(false) { | ||
611 | Either::A(_) => (), | ||
612 | Either::B(true, _) => (), | ||
613 | Either::B(false, _) => (), | ||
614 | } | ||
615 | match Either::A(false) { | ||
616 | Either::A(true) | Either::A(false) => (), | ||
617 | Either::B(true, _) => (), | ||
618 | Either::B(false, _) => (), | ||
619 | } | ||
620 | } | ||
621 | "#, | ||
622 | ); | ||
623 | } | ||
624 | |||
625 | #[test] | ||
626 | fn tuple_of_enum_no_diagnostic() { | ||
627 | check_diagnostics( | ||
628 | r#" | ||
629 | enum Either { A(bool), B(bool, bool) } | ||
630 | enum Either2 { C, D } | ||
631 | |||
632 | fn main() { | ||
633 | match (Either::A(false), Either2::C) { | ||
634 | (Either::A(true), _) | (Either::A(false), _) => (), | ||
635 | (Either::B(true, _), Either2::C) => (), | ||
636 | (Either::B(false, _), Either2::C) => (), | ||
637 | (Either::B(_, _), Either2::D) => (), | ||
638 | } | ||
639 | } | ||
640 | "#, | ||
641 | ); | ||
642 | } | ||
643 | |||
644 | #[test] | ||
645 | fn or_pattern_no_diagnostic() { | ||
646 | check_diagnostics( | ||
647 | r#" | ||
648 | enum Either {A, B} | ||
649 | |||
650 | fn main() { | ||
651 | match (Either::A, Either::B) { | ||
652 | (Either::A | Either::B, _) => (), | ||
653 | } | ||
654 | }"#, | ||
655 | ) | ||
656 | } | ||
657 | |||
658 | #[test] | ||
659 | fn mismatched_types() { | ||
660 | // Match statements with arms that don't match the | ||
661 | // expression pattern do not fire this diagnostic. | ||
662 | check_diagnostics( | ||
663 | r#" | ||
664 | enum Either { A, B } | ||
665 | enum Either2 { C, D } | ||
666 | |||
667 | fn main() { | ||
668 | match Either::A { | ||
669 | Either2::C => (), | ||
670 | // ^^^^^^^^^^ Internal: match check bailed out | ||
671 | Either2::D => (), | ||
672 | } | ||
673 | match (true, false) { | ||
674 | (true, false, true) => (), | ||
675 | // ^^^^^^^^^^^^^^^^^^^ Internal: match check bailed out | ||
676 | (true) => (), | ||
677 | } | ||
678 | match (true, false) { (true,) => {} } | ||
679 | // ^^^^^^^ Internal: match check bailed out | ||
680 | match (0) { () => () } | ||
681 | // ^^ Internal: match check bailed out | ||
682 | match Unresolved::Bar { Unresolved::Baz => () } | ||
683 | } | ||
684 | "#, | ||
685 | ); | ||
686 | } | ||
687 | |||
688 | #[test] | ||
689 | fn mismatched_types_in_or_patterns() { | ||
690 | check_diagnostics( | ||
691 | r#" | ||
692 | fn main() { | ||
693 | match false { true | () => {} } | ||
694 | // ^^^^^^^^^ Internal: match check bailed out | ||
695 | match (false,) { (true | (),) => {} } | ||
696 | // ^^^^^^^^^^^^ Internal: match check bailed out | ||
697 | } | ||
698 | "#, | ||
699 | ); | ||
700 | } | ||
701 | |||
702 | #[test] | ||
703 | fn malformed_match_arm_tuple_enum_missing_pattern() { | ||
704 | // We are testing to be sure we don't panic here when the match | ||
705 | // arm `Either::B` is missing its pattern. | ||
706 | check_diagnostics( | ||
707 | r#" | ||
708 | enum Either { A, B(u32) } | ||
709 | |||
710 | fn main() { | ||
711 | match Either::A { | ||
712 | Either::A => (), | ||
713 | Either::B() => (), | ||
714 | } | ||
715 | } | ||
716 | "#, | ||
717 | ); | ||
718 | } | ||
719 | |||
720 | #[test] | ||
721 | fn malformed_match_arm_extra_fields() { | ||
722 | check_diagnostics( | ||
723 | r#" | ||
724 | enum A { B(isize, isize), C } | ||
725 | fn main() { | ||
726 | match A::B(1, 2) { | ||
727 | A::B(_, _, _) => (), | ||
728 | // ^^^^^^^^^^^^^ Internal: match check bailed out | ||
729 | } | ||
730 | match A::B(1, 2) { | ||
731 | A::C(_) => (), | ||
732 | // ^^^^^^^ Internal: match check bailed out | ||
733 | } | ||
734 | } | ||
735 | "#, | ||
736 | ); | ||
737 | } | ||
738 | |||
739 | #[test] | ||
740 | fn expr_diverges() { | ||
741 | check_diagnostics( | ||
742 | r#" | ||
743 | enum Either { A, B } | ||
744 | |||
745 | fn main() { | ||
746 | match loop {} { | ||
747 | Either::A => (), | ||
748 | // ^^^^^^^^^ Internal: match check bailed out | ||
749 | Either::B => (), | ||
750 | } | ||
751 | match loop {} { | ||
752 | Either::A => (), | ||
753 | // ^^^^^^^^^ Internal: match check bailed out | ||
754 | } | ||
755 | match loop { break Foo::A } { | ||
756 | //^^^^^^^^^^^^^^^^^^^^^ Missing match arm | ||
757 | Either::A => (), | ||
758 | } | ||
759 | match loop { break Foo::A } { | ||
760 | Either::A => (), | ||
761 | Either::B => (), | ||
762 | } | ||
763 | } | ||
764 | "#, | ||
765 | ); | ||
766 | } | ||
767 | |||
768 | #[test] | ||
769 | fn expr_partially_diverges() { | ||
770 | check_diagnostics( | ||
771 | r#" | ||
772 | enum Either<T> { A(T), B } | ||
773 | |||
774 | fn foo() -> Either<!> { Either::B } | ||
775 | fn main() -> u32 { | ||
776 | match foo() { | ||
777 | Either::A(val) => val, | ||
778 | Either::B => 0, | ||
779 | } | ||
780 | } | ||
781 | "#, | ||
782 | ); | ||
783 | } | ||
784 | |||
785 | #[test] | ||
786 | fn enum_record() { | ||
787 | check_diagnostics( | ||
788 | r#" | ||
789 | enum Either { A { foo: bool }, B } | ||
790 | |||
791 | fn main() { | ||
792 | let a = Either::A { foo: true }; | ||
793 | match a { } | ||
794 | //^ Missing match arm | ||
795 | match a { Either::A { foo: true } => () } | ||
796 | //^ Missing match arm | ||
797 | match a { | ||
798 | Either::A { } => (), | ||
799 | //^^^^^^^^^ Missing structure fields: | ||
800 | // | - foo | ||
801 | Either::B => (), | ||
802 | } | ||
803 | match a { | ||
804 | //^ Missing match arm | ||
805 | Either::A { } => (), | ||
806 | } //^^^^^^^^^ Missing structure fields: | ||
807 | // | - foo | ||
808 | |||
809 | match a { | ||
810 | Either::A { foo: true } => (), | ||
811 | Either::A { foo: false } => (), | ||
812 | Either::B => (), | ||
813 | } | ||
814 | match a { | ||
815 | Either::A { foo: _ } => (), | ||
816 | Either::B => (), | ||
817 | } | ||
818 | } | ||
819 | "#, | ||
820 | ); | ||
821 | } | ||
822 | |||
823 | #[test] | ||
824 | fn enum_record_fields_out_of_order() { | ||
825 | check_diagnostics( | ||
826 | r#" | ||
827 | enum Either { | ||
828 | A { foo: bool, bar: () }, | ||
829 | B, | ||
830 | } | ||
831 | |||
832 | fn main() { | ||
833 | let a = Either::A { foo: true, bar: () }; | ||
834 | match a { | ||
835 | //^ Missing match arm | ||
836 | Either::A { bar: (), foo: false } => (), | ||
837 | Either::A { foo: true, bar: () } => (), | ||
838 | } | ||
839 | |||
840 | match a { | ||
841 | Either::A { bar: (), foo: false } => (), | ||
842 | Either::A { foo: true, bar: () } => (), | ||
843 | Either::B => (), | ||
844 | } | ||
845 | } | ||
846 | "#, | ||
847 | ); | ||
848 | } | ||
849 | |||
850 | #[test] | ||
851 | fn enum_record_ellipsis() { | ||
852 | check_diagnostics( | ||
853 | r#" | ||
854 | enum Either { | ||
855 | A { foo: bool, bar: bool }, | ||
856 | B, | ||
857 | } | ||
858 | |||
859 | fn main() { | ||
860 | let a = Either::B; | ||
861 | match a { | ||
862 | //^ Missing match arm | ||
863 | Either::A { foo: true, .. } => (), | ||
864 | Either::B => (), | ||
865 | } | ||
866 | match a { | ||
867 | //^ Missing match arm | ||
868 | Either::A { .. } => (), | ||
869 | } | ||
870 | |||
871 | match a { | ||
872 | Either::A { foo: true, .. } => (), | ||
873 | Either::A { foo: false, .. } => (), | ||
874 | Either::B => (), | ||
875 | } | ||
876 | |||
877 | match a { | ||
878 | Either::A { .. } => (), | ||
879 | Either::B => (), | ||
880 | } | ||
881 | } | ||
882 | "#, | ||
883 | ); | ||
884 | } | ||
885 | |||
886 | #[test] | ||
887 | fn enum_tuple_partial_ellipsis() { | ||
888 | check_diagnostics( | ||
889 | r#" | ||
890 | enum Either { | ||
891 | A(bool, bool, bool, bool), | ||
892 | B, | ||
893 | } | ||
894 | |||
895 | fn main() { | ||
896 | match Either::B { | ||
897 | //^^^^^^^^^ Missing match arm | ||
898 | Either::A(true, .., true) => (), | ||
899 | Either::A(true, .., false) => (), | ||
900 | Either::A(false, .., false) => (), | ||
901 | Either::B => (), | ||
902 | } | ||
903 | match Either::B { | ||
904 | //^^^^^^^^^ Missing match arm | ||
905 | Either::A(true, .., true) => (), | ||
906 | Either::A(true, .., false) => (), | ||
907 | Either::A(.., true) => (), | ||
908 | Either::B => (), | ||
909 | } | ||
910 | |||
911 | match Either::B { | ||
912 | Either::A(true, .., true) => (), | ||
913 | Either::A(true, .., false) => (), | ||
914 | Either::A(false, .., true) => (), | ||
915 | Either::A(false, .., false) => (), | ||
916 | Either::B => (), | ||
917 | } | ||
918 | match Either::B { | ||
919 | Either::A(true, .., true) => (), | ||
920 | Either::A(true, .., false) => (), | ||
921 | Either::A(.., true) => (), | ||
922 | Either::A(.., false) => (), | ||
923 | Either::B => (), | ||
924 | } | ||
925 | } | ||
926 | "#, | ||
927 | ); | ||
928 | } | ||
929 | |||
930 | #[test] | ||
931 | fn never() { | ||
932 | check_diagnostics( | ||
933 | r#" | ||
934 | enum Never {} | ||
935 | |||
936 | fn enum_(never: Never) { | ||
937 | match never {} | ||
938 | } | ||
939 | fn enum_ref(never: &Never) { | ||
940 | match never {} | ||
941 | //^^^^^ Missing match arm | ||
942 | } | ||
943 | fn bang(never: !) { | ||
944 | match never {} | ||
945 | } | ||
946 | "#, | ||
947 | ); | ||
948 | } | ||
949 | |||
950 | #[test] | ||
951 | fn unknown_type() { | ||
952 | check_diagnostics( | ||
953 | r#" | ||
954 | enum Option<T> { Some(T), None } | ||
955 | |||
956 | fn main() { | ||
957 | // `Never` is deliberately not defined so that it's an uninferred type. | ||
958 | match Option::<Never>::None { | ||
959 | None => (), | ||
960 | Some(never) => match never {}, | ||
961 | // ^^^^^^^^^^^ Internal: match check bailed out | ||
962 | } | ||
963 | match Option::<Never>::None { | ||
964 | //^^^^^^^^^^^^^^^^^^^^^ Missing match arm | ||
965 | Option::Some(_never) => {}, | ||
966 | } | ||
967 | } | ||
968 | "#, | ||
969 | ); | ||
970 | } | ||
971 | |||
972 | #[test] | ||
973 | fn tuple_of_bools_with_ellipsis_at_end_missing_arm() { | ||
974 | check_diagnostics( | ||
975 | r#" | ||
976 | fn main() { | ||
977 | match (false, true, false) { | ||
978 | //^^^^^^^^^^^^^^^^^^^^ Missing match arm | ||
979 | (false, ..) => (), | ||
980 | } | ||
981 | }"#, | ||
982 | ); | ||
983 | } | ||
984 | |||
985 | #[test] | ||
986 | fn tuple_of_bools_with_ellipsis_at_beginning_missing_arm() { | ||
987 | check_diagnostics( | ||
988 | r#" | ||
989 | fn main() { | ||
990 | match (false, true, false) { | ||
991 | //^^^^^^^^^^^^^^^^^^^^ Missing match arm | ||
992 | (.., false) => (), | ||
993 | } | ||
994 | }"#, | ||
995 | ); | ||
996 | } | ||
997 | |||
998 | #[test] | ||
999 | fn tuple_of_bools_with_ellipsis_in_middle_missing_arm() { | ||
1000 | check_diagnostics( | ||
1001 | r#" | ||
1002 | fn main() { | ||
1003 | match (false, true, false) { | ||
1004 | //^^^^^^^^^^^^^^^^^^^^ Missing match arm | ||
1005 | (true, .., false) => (), | ||
1006 | } | ||
1007 | }"#, | ||
1008 | ); | ||
1009 | } | ||
1010 | |||
1011 | #[test] | ||
1012 | fn record_struct() { | ||
1013 | check_diagnostics( | ||
1014 | r#"struct Foo { a: bool } | ||
1015 | fn main(f: Foo) { | ||
1016 | match f {} | ||
1017 | //^ Missing match arm | ||
1018 | match f { Foo { a: true } => () } | ||
1019 | //^ Missing match arm | ||
1020 | match &f { Foo { a: true } => () } | ||
1021 | //^^ Missing match arm | ||
1022 | match f { Foo { a: _ } => () } | ||
1023 | match f { | ||
1024 | Foo { a: true } => (), | ||
1025 | Foo { a: false } => (), | ||
1026 | } | ||
1027 | match &f { | ||
1028 | Foo { a: true } => (), | ||
1029 | Foo { a: false } => (), | ||
1030 | } | ||
1031 | } | ||
1032 | "#, | ||
1033 | ); | ||
1034 | } | ||
1035 | |||
1036 | #[test] | ||
1037 | fn tuple_struct() { | ||
1038 | check_diagnostics( | ||
1039 | r#"struct Foo(bool); | ||
1040 | fn main(f: Foo) { | ||
1041 | match f {} | ||
1042 | //^ Missing match arm | ||
1043 | match f { Foo(true) => () } | ||
1044 | //^ Missing match arm | ||
1045 | match f { | ||
1046 | Foo(true) => (), | ||
1047 | Foo(false) => (), | ||
1048 | } | ||
1049 | } | ||
1050 | "#, | ||
1051 | ); | ||
1052 | } | ||
1053 | |||
1054 | #[test] | ||
1055 | fn unit_struct() { | ||
1056 | check_diagnostics( | ||
1057 | r#"struct Foo; | ||
1058 | fn main(f: Foo) { | ||
1059 | match f {} | ||
1060 | //^ Missing match arm | ||
1061 | match f { Foo => () } | ||
1062 | } | ||
1063 | "#, | ||
1064 | ); | ||
1065 | } | ||
1066 | |||
1067 | #[test] | ||
1068 | fn record_struct_ellipsis() { | ||
1069 | check_diagnostics( | ||
1070 | r#"struct Foo { foo: bool, bar: bool } | ||
1071 | fn main(f: Foo) { | ||
1072 | match f { Foo { foo: true, .. } => () } | ||
1073 | //^ Missing match arm | ||
1074 | match f { | ||
1075 | //^ Missing match arm | ||
1076 | Foo { foo: true, .. } => (), | ||
1077 | Foo { bar: false, .. } => () | ||
1078 | } | ||
1079 | match f { Foo { .. } => () } | ||
1080 | match f { | ||
1081 | Foo { foo: true, .. } => (), | ||
1082 | Foo { foo: false, .. } => () | ||
1083 | } | ||
1084 | } | ||
1085 | "#, | ||
1086 | ); | ||
1087 | } | ||
1088 | |||
1089 | #[test] | ||
1090 | fn internal_or() { | ||
1091 | check_diagnostics( | ||
1092 | r#" | ||
1093 | fn main() { | ||
1094 | enum Either { A(bool), B } | ||
1095 | match Either::B { | ||
1096 | //^^^^^^^^^ Missing match arm | ||
1097 | Either::A(true | false) => (), | ||
1098 | } | ||
1099 | } | ||
1100 | "#, | ||
1101 | ); | ||
1102 | } | ||
1103 | |||
1104 | #[test] | ||
1105 | fn no_panic_at_unimplemented_subpattern_type() { | ||
1106 | check_diagnostics( | ||
1107 | r#" | ||
1108 | struct S { a: char} | ||
1109 | fn main(v: S) { | ||
1110 | match v { S{ a } => {} } | ||
1111 | match v { S{ a: _x } => {} } | ||
1112 | match v { S{ a: 'a' } => {} } | ||
1113 | //^^^^^^^^^^^ Internal: match check bailed out | ||
1114 | match v { S{..} => {} } | ||
1115 | match v { _ => {} } | ||
1116 | match v { } | ||
1117 | //^ Missing match arm | ||
1118 | } | ||
1119 | "#, | ||
1120 | ); | ||
1121 | } | ||
1122 | |||
1123 | #[test] | ||
1124 | fn binding() { | ||
1125 | check_diagnostics( | ||
1126 | r#" | ||
1127 | fn main() { | ||
1128 | match true { | ||
1129 | _x @ true => {} | ||
1130 | false => {} | ||
1131 | } | ||
1132 | match true { _x @ true => {} } | ||
1133 | //^^^^ Missing match arm | ||
1134 | } | ||
1135 | "#, | ||
1136 | ); | ||
1137 | } | ||
1138 | |||
1139 | #[test] | ||
1140 | fn binding_ref_has_correct_type() { | ||
1141 | // Asserts `PatKind::Binding(ref _x): bool`, not &bool. | ||
1142 | // If that's not true match checking will panic with "incompatible constructors" | ||
1143 | // FIXME: make facilities to test this directly like `tests::check_infer(..)` | ||
1144 | check_diagnostics( | ||
1145 | r#" | ||
1146 | enum Foo { A } | ||
1147 | fn main() { | ||
1148 | // FIXME: this should not bail out but current behavior is such as the old algorithm. | ||
1149 | // ExprValidator::validate_match(..) checks types of top level patterns incorrecly. | ||
1150 | match Foo::A { | ||
1151 | ref _x => {} | ||
1152 | // ^^^^^^ Internal: match check bailed out | ||
1153 | Foo::A => {} | ||
1154 | } | ||
1155 | match (true,) { | ||
1156 | (ref _x,) => {} | ||
1157 | (true,) => {} | ||
1158 | } | ||
1159 | } | ||
1160 | "#, | ||
1161 | ); | ||
1162 | } | ||
1163 | |||
1164 | #[test] | ||
1165 | fn enum_non_exhaustive() { | ||
1166 | check_diagnostics( | ||
1167 | r#" | ||
1168 | //- /lib.rs crate:lib | ||
1169 | #[non_exhaustive] | ||
1170 | pub enum E { A, B } | ||
1171 | fn _local() { | ||
1172 | match E::A { _ => {} } | ||
1173 | match E::A { | ||
1174 | E::A => {} | ||
1175 | E::B => {} | ||
1176 | } | ||
1177 | match E::A { | ||
1178 | E::A | E::B => {} | ||
1179 | } | ||
1180 | } | ||
1181 | |||
1182 | //- /main.rs crate:main deps:lib | ||
1183 | use lib::E; | ||
1184 | fn main() { | ||
1185 | match E::A { _ => {} } | ||
1186 | match E::A { | ||
1187 | //^^^^ Missing match arm | ||
1188 | E::A => {} | ||
1189 | E::B => {} | ||
1190 | } | ||
1191 | match E::A { | ||
1192 | //^^^^ Missing match arm | ||
1193 | E::A | E::B => {} | ||
1194 | } | ||
1195 | } | ||
1196 | "#, | ||
1197 | ); | ||
1198 | } | ||
1199 | |||
1200 | #[test] | ||
1201 | fn match_guard() { | ||
1202 | check_diagnostics( | ||
1203 | r#" | ||
1204 | fn main() { | ||
1205 | match true { | ||
1206 | true if false => {} | ||
1207 | true => {} | ||
1208 | false => {} | ||
1209 | } | ||
1210 | match true { | ||
1211 | //^^^^ Missing match arm | ||
1212 | true if false => {} | ||
1213 | false => {} | ||
1214 | } | ||
1215 | "#, | ||
1216 | ); | ||
1217 | } | ||
1218 | |||
1219 | #[test] | ||
1220 | fn pattern_type_is_of_substitution() { | ||
1221 | cov_mark::check!(match_check_wildcard_expanded_to_substitutions); | ||
1222 | check_diagnostics( | ||
1223 | r#" | ||
1224 | struct Foo<T>(T); | ||
1225 | struct Bar; | ||
1226 | fn main() { | ||
1227 | match Foo(Bar) { | ||
1228 | _ | Foo(Bar) => {} | ||
1229 | } | ||
1230 | } | ||
1231 | "#, | ||
1232 | ); | ||
1233 | } | ||
1234 | |||
1235 | #[test] | ||
1236 | fn record_struct_no_such_field() { | ||
1237 | check_diagnostics( | ||
1238 | r#" | ||
1239 | struct Foo { } | ||
1240 | fn main(f: Foo) { | ||
1241 | match f { Foo { bar } => () } | ||
1242 | // ^^^^^^^^^^^ Internal: match check bailed out | ||
1243 | } | ||
1244 | "#, | ||
1245 | ); | ||
1246 | } | ||
1247 | |||
1248 | #[test] | ||
1249 | fn match_ergonomics_issue_9095() { | ||
1250 | check_diagnostics( | ||
1251 | r#" | ||
1252 | enum Foo<T> { A(T) } | ||
1253 | fn main() { | ||
1254 | match &Foo::A(true) { | ||
1255 | _ => {} | ||
1256 | Foo::A(_) => {} | ||
1257 | } | ||
1258 | } | ||
1259 | "#, | ||
1260 | ); | ||
1261 | } | ||
1262 | |||
1263 | mod false_negatives { | ||
1264 | //! The implementation of match checking here is a work in progress. As we roll this out, we | ||
1265 | //! prefer false negatives to false positives (ideally there would be no false positives). This | ||
1266 | //! test module should document known false negatives. Eventually we will have a complete | ||
1267 | //! implementation of match checking and this module will be empty. | ||
1268 | //! | ||
1269 | //! The reasons for documenting known false negatives: | ||
1270 | //! | ||
1271 | //! 1. It acts as a backlog of work that can be done to improve the behavior of the system. | ||
1272 | //! 2. It ensures the code doesn't panic when handling these cases. | ||
1273 | use super::*; | ||
1274 | |||
1275 | #[test] | ||
1276 | fn integers() { | ||
1277 | // We don't currently check integer exhaustiveness. | ||
1278 | check_diagnostics( | ||
1279 | r#" | ||
1280 | fn main() { | ||
1281 | match 5 { | ||
1282 | 10 => (), | ||
1283 | // ^^ Internal: match check bailed out | ||
1284 | 11..20 => (), | ||
1285 | } | ||
1286 | } | ||
1287 | "#, | ||
1288 | ); | ||
1289 | } | ||
1290 | |||
1291 | #[test] | ||
1292 | fn reference_patterns_at_top_level() { | ||
1293 | check_diagnostics( | ||
1294 | r#" | ||
1295 | fn main() { | ||
1296 | match &false { | ||
1297 | &true => {} | ||
1298 | // ^^^^^ Internal: match check bailed out | ||
1299 | } | ||
1300 | } | ||
1301 | "#, | ||
1302 | ); | ||
1303 | } | ||
1304 | |||
1305 | #[test] | ||
1306 | fn reference_patterns_in_fields() { | ||
1307 | check_diagnostics( | ||
1308 | r#" | ||
1309 | fn main() { | ||
1310 | match (&false,) { | ||
1311 | (true,) => {} | ||
1312 | // ^^^^^^^ Internal: match check bailed out | ||
1313 | } | ||
1314 | match (&false,) { | ||
1315 | (&true,) => {} | ||
1316 | // ^^^^^^^^ Internal: match check bailed out | ||
1317 | } | ||
1318 | } | ||
1319 | "#, | ||
1320 | ); | ||
1321 | } | ||
1322 | } | ||
1323 | } | ||