aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/hir_def/src/find_path.rs587
1 files changed, 327 insertions, 260 deletions
diff --git a/crates/hir_def/src/find_path.rs b/crates/hir_def/src/find_path.rs
index 109d3552f..317ca86ec 100644
--- a/crates/hir_def/src/find_path.rs
+++ b/crates/hir_def/src/find_path.rs
@@ -425,106 +425,145 @@ mod tests {
425 425
426 #[test] 426 #[test]
427 fn same_module() { 427 fn same_module() {
428 let code = r#" 428 check_found_path(
429 //- /main.rs 429 r#"
430 struct S; 430//- /main.rs
431 $0 431struct S;
432 "#; 432$0
433 check_found_path(code, "S", "S", "crate::S", "self::S"); 433 "#,
434 "S",
435 "S",
436 "crate::S",
437 "self::S",
438 );
434 } 439 }
435 440
436 #[test] 441 #[test]
437 fn enum_variant() { 442 fn enum_variant() {
438 let code = r#" 443 check_found_path(
439 //- /main.rs 444 r#"
440 enum E { A } 445//- /main.rs
441 $0 446enum E { A }
442 "#; 447$0
443 check_found_path(code, "E::A", "E::A", "E::A", "E::A"); 448 "#,
449 "E::A",
450 "E::A",
451 "E::A",
452 "E::A",
453 );
444 } 454 }
445 455
446 #[test] 456 #[test]
447 fn sub_module() { 457 fn sub_module() {
448 let code = r#" 458 check_found_path(
449 //- /main.rs 459 r#"
450 mod foo { 460//- /main.rs
451 pub struct S; 461mod foo {
452 } 462 pub struct S;
453 $0 463}
454 "#; 464$0
455 check_found_path(code, "foo::S", "foo::S", "crate::foo::S", "self::foo::S"); 465 "#,
466 "foo::S",
467 "foo::S",
468 "crate::foo::S",
469 "self::foo::S",
470 );
456 } 471 }
457 472
458 #[test] 473 #[test]
459 fn super_module() { 474 fn super_module() {
460 let code = r#" 475 check_found_path(
461 //- /main.rs 476 r#"
462 mod foo; 477//- /main.rs
463 //- /foo.rs 478mod foo;
464 mod bar; 479//- /foo.rs
465 struct S; 480mod bar;
466 //- /foo/bar.rs 481struct S;
467 $0 482//- /foo/bar.rs
468 "#; 483$0
469 check_found_path(code, "super::S", "super::S", "crate::foo::S", "super::S"); 484 "#,
485 "super::S",
486 "super::S",
487 "crate::foo::S",
488 "super::S",
489 );
470 } 490 }
471 491
472 #[test] 492 #[test]
473 fn self_module() { 493 fn self_module() {
474 let code = r#" 494 check_found_path(
475 //- /main.rs 495 r#"
476 mod foo; 496//- /main.rs
477 //- /foo.rs 497mod foo;
478 $0 498//- /foo.rs
479 "#; 499$0
480 check_found_path(code, "self", "self", "crate::foo", "self"); 500 "#,
501 "self",
502 "self",
503 "crate::foo",
504 "self",
505 );
481 } 506 }
482 507
483 #[test] 508 #[test]
484 fn crate_root() { 509 fn crate_root() {
485 let code = r#" 510 check_found_path(
486 //- /main.rs 511 r#"
487 mod foo; 512//- /main.rs
488 //- /foo.rs 513mod foo;
489 $0 514//- /foo.rs
490 "#; 515$0
491 check_found_path(code, "crate", "crate", "crate", "crate"); 516 "#,
517 "crate",
518 "crate",
519 "crate",
520 "crate",
521 );
492 } 522 }
493 523
494 #[test] 524 #[test]
495 fn same_crate() { 525 fn same_crate() {
496 let code = r#" 526 check_found_path(
497 //- /main.rs 527 r#"
498 mod foo; 528//- /main.rs
499 struct S; 529mod foo;
500 //- /foo.rs 530struct S;
501 $0 531//- /foo.rs
502 "#; 532$0
503 check_found_path(code, "crate::S", "crate::S", "crate::S", "crate::S"); 533 "#,
534 "crate::S",
535 "crate::S",
536 "crate::S",
537 "crate::S",
538 );
504 } 539 }
505 540
506 #[test] 541 #[test]
507 fn different_crate() { 542 fn different_crate() {
508 let code = r#" 543 check_found_path(
509 //- /main.rs crate:main deps:std 544 r#"
510 $0 545//- /main.rs crate:main deps:std
511 //- /std.rs crate:std 546$0
512 pub struct S; 547//- /std.rs crate:std
513 "#; 548pub struct S;
514 check_found_path(code, "std::S", "std::S", "std::S", "std::S"); 549 "#,
550 "std::S",
551 "std::S",
552 "std::S",
553 "std::S",
554 );
515 } 555 }
516 556
517 #[test] 557 #[test]
518 fn different_crate_renamed() { 558 fn different_crate_renamed() {
519 let code = r#"
520 //- /main.rs crate:main deps:std
521 extern crate std as std_renamed;
522 $0
523 //- /std.rs crate:std
524 pub struct S;
525 "#;
526 check_found_path( 559 check_found_path(
527 code, 560 r#"
561//- /main.rs crate:main deps:std
562extern crate std as std_renamed;
563$0
564//- /std.rs crate:std
565pub struct S;
566 "#,
528 "std_renamed::S", 567 "std_renamed::S",
529 "std_renamed::S", 568 "std_renamed::S",
530 "std_renamed::S", 569 "std_renamed::S",
@@ -537,41 +576,38 @@ mod tests {
537 cov_mark::check!(partially_imported); 576 cov_mark::check!(partially_imported);
538 // Tests that short paths are used even for external items, when parts of the path are 577 // Tests that short paths are used even for external items, when parts of the path are
539 // already in scope. 578 // already in scope.
540 let code = r#" 579 check_found_path(
541 //- /main.rs crate:main deps:syntax 580 r#"
581//- /main.rs crate:main deps:syntax
542 582
543 use syntax::ast; 583use syntax::ast;
544 $0 584$0
545 585
546 //- /lib.rs crate:syntax 586//- /lib.rs crate:syntax
547 pub mod ast { 587pub mod ast {
548 pub enum ModuleItem { 588 pub enum ModuleItem {
549 A, B, C, 589 A, B, C,
550 } 590 }
551 } 591}
552 "#; 592 "#,
553 check_found_path(
554 code,
555 "ast::ModuleItem", 593 "ast::ModuleItem",
556 "syntax::ast::ModuleItem", 594 "syntax::ast::ModuleItem",
557 "syntax::ast::ModuleItem", 595 "syntax::ast::ModuleItem",
558 "syntax::ast::ModuleItem", 596 "syntax::ast::ModuleItem",
559 ); 597 );
560 598
561 let code = r#"
562 //- /main.rs crate:main deps:syntax
563
564 $0
565
566 //- /lib.rs crate:syntax
567 pub mod ast {
568 pub enum ModuleItem {
569 A, B, C,
570 }
571 }
572 "#;
573 check_found_path( 599 check_found_path(
574 code, 600 r#"
601//- /main.rs crate:main deps:syntax
602$0
603
604//- /lib.rs crate:syntax
605pub mod ast {
606 pub enum ModuleItem {
607 A, B, C,
608 }
609}
610 "#,
575 "syntax::ast::ModuleItem", 611 "syntax::ast::ModuleItem",
576 "syntax::ast::ModuleItem", 612 "syntax::ast::ModuleItem",
577 "syntax::ast::ModuleItem", 613 "syntax::ast::ModuleItem",
@@ -581,68 +617,88 @@ mod tests {
581 617
582 #[test] 618 #[test]
583 fn same_crate_reexport() { 619 fn same_crate_reexport() {
584 let code = r#" 620 check_found_path(
585 //- /main.rs 621 r#"
586 mod bar { 622//- /main.rs
587 mod foo { pub(super) struct S; } 623mod bar {
588 pub(crate) use foo::*; 624 mod foo { pub(super) struct S; }
589 } 625 pub(crate) use foo::*;
590 $0 626}
591 "#; 627$0
592 check_found_path(code, "bar::S", "bar::S", "crate::bar::S", "self::bar::S"); 628 "#,
629 "bar::S",
630 "bar::S",
631 "crate::bar::S",
632 "self::bar::S",
633 );
593 } 634 }
594 635
595 #[test] 636 #[test]
596 fn same_crate_reexport_rename() { 637 fn same_crate_reexport_rename() {
597 let code = r#" 638 check_found_path(
598 //- /main.rs 639 r#"
599 mod bar { 640//- /main.rs
600 mod foo { pub(super) struct S; } 641mod bar {
601 pub(crate) use foo::S as U; 642 mod foo { pub(super) struct S; }
602 } 643 pub(crate) use foo::S as U;
603 $0 644}
604 "#; 645$0
605 check_found_path(code, "bar::U", "bar::U", "crate::bar::U", "self::bar::U"); 646 "#,
647 "bar::U",
648 "bar::U",
649 "crate::bar::U",
650 "self::bar::U",
651 );
606 } 652 }
607 653
608 #[test] 654 #[test]
609 fn different_crate_reexport() { 655 fn different_crate_reexport() {
610 let code = r#" 656 check_found_path(
611 //- /main.rs crate:main deps:std 657 r#"
612 $0 658//- /main.rs crate:main deps:std
613 //- /std.rs crate:std deps:core 659$0
614 pub use core::S; 660//- /std.rs crate:std deps:core
615 //- /core.rs crate:core 661pub use core::S;
616 pub struct S; 662//- /core.rs crate:core
617 "#; 663pub struct S;
618 check_found_path(code, "std::S", "std::S", "std::S", "std::S"); 664 "#,
665 "std::S",
666 "std::S",
667 "std::S",
668 "std::S",
669 );
619 } 670 }
620 671
621 #[test] 672 #[test]
622 fn prelude() { 673 fn prelude() {
623 let code = r#" 674 check_found_path(
624 //- /main.rs crate:main deps:std 675 r#"
625 $0 676//- /main.rs crate:main deps:std
626 //- /std.rs crate:std 677$0
627 pub mod prelude { pub struct S; } 678//- /std.rs crate:std
628 #[prelude_import] 679pub mod prelude { pub struct S; }
629 pub use prelude::*; 680#[prelude_import]
630 "#; 681pub use prelude::*;
631 check_found_path(code, "S", "S", "S", "S"); 682 "#,
683 "S",
684 "S",
685 "S",
686 "S",
687 );
632 } 688 }
633 689
634 #[test] 690 #[test]
635 fn enum_variant_from_prelude() { 691 fn enum_variant_from_prelude() {
636 let code = r#" 692 let code = r#"
637 //- /main.rs crate:main deps:std 693//- /main.rs crate:main deps:std
638 $0 694$0
639 //- /std.rs crate:std 695//- /std.rs crate:std
640 pub mod prelude { 696pub mod prelude {
641 pub enum Option<T> { Some(T), None } 697 pub enum Option<T> { Some(T), None }
642 pub use Option::*; 698 pub use Option::*;
643 } 699}
644 #[prelude_import] 700#[prelude_import]
645 pub use prelude::*; 701pub use prelude::*;
646 "#; 702 "#;
647 check_found_path(code, "None", "None", "None", "None"); 703 check_found_path(code, "None", "None", "None", "None");
648 check_found_path(code, "Some", "Some", "Some", "Some"); 704 check_found_path(code, "Some", "Some", "Some", "Some");
@@ -650,71 +706,85 @@ mod tests {
650 706
651 #[test] 707 #[test]
652 fn shortest_path() { 708 fn shortest_path() {
653 let code = r#" 709 check_found_path(
654 //- /main.rs 710 r#"
655 pub mod foo; 711//- /main.rs
656 pub mod baz; 712pub mod foo;
657 struct S; 713pub mod baz;
658 $0 714struct S;
659 //- /foo.rs 715$0
660 pub mod bar { pub struct S; } 716//- /foo.rs
661 //- /baz.rs 717pub mod bar { pub struct S; }
662 pub use crate::foo::bar::S; 718//- /baz.rs
663 "#; 719pub use crate::foo::bar::S;
664 check_found_path(code, "baz::S", "baz::S", "crate::baz::S", "self::baz::S"); 720 "#,
721 "baz::S",
722 "baz::S",
723 "crate::baz::S",
724 "self::baz::S",
725 );
665 } 726 }
666 727
667 #[test] 728 #[test]
668 fn discount_private_imports() { 729 fn discount_private_imports() {
669 let code = r#" 730 check_found_path(
670 //- /main.rs 731 r#"
671 mod foo; 732//- /main.rs
672 pub mod bar { pub struct S; } 733mod foo;
673 use bar::S; 734pub mod bar { pub struct S; }
674 //- /foo.rs 735use bar::S;
675 $0 736//- /foo.rs
676 "#; 737$0
677 // crate::S would be shorter, but using private imports seems wrong 738 "#,
678 check_found_path(code, "crate::bar::S", "crate::bar::S", "crate::bar::S", "crate::bar::S"); 739 // crate::S would be shorter, but using private imports seems wrong
740 "crate::bar::S",
741 "crate::bar::S",
742 "crate::bar::S",
743 "crate::bar::S",
744 );
679 } 745 }
680 746
681 #[test] 747 #[test]
682 fn import_cycle() { 748 fn import_cycle() {
683 let code = r#" 749 check_found_path(
684 //- /main.rs 750 r#"
685 pub mod foo; 751//- /main.rs
686 pub mod bar; 752pub mod foo;
687 pub mod baz; 753pub mod bar;
688 //- /bar.rs 754pub mod baz;
689 $0 755//- /bar.rs
690 //- /foo.rs 756$0
691 pub use super::baz; 757//- /foo.rs
692 pub struct S; 758pub use super::baz;
693 //- /baz.rs 759pub struct S;
694 pub use super::foo; 760//- /baz.rs
695 "#; 761pub use super::foo;
696 check_found_path(code, "crate::foo::S", "crate::foo::S", "crate::foo::S", "crate::foo::S"); 762 "#,
763 "crate::foo::S",
764 "crate::foo::S",
765 "crate::foo::S",
766 "crate::foo::S",
767 );
697 } 768 }
698 769
699 #[test] 770 #[test]
700 fn prefer_std_paths_over_alloc() { 771 fn prefer_std_paths_over_alloc() {
701 cov_mark::check!(prefer_std_paths); 772 cov_mark::check!(prefer_std_paths);
702 let code = r#" 773 check_found_path(
703 //- /main.rs crate:main deps:alloc,std 774 r#"
704 $0 775//- /main.rs crate:main deps:alloc,std
776$0
705 777
706 //- /std.rs crate:std deps:alloc 778//- /std.rs crate:std deps:alloc
707 pub mod sync { 779pub mod sync {
708 pub use alloc::sync::Arc; 780 pub use alloc::sync::Arc;
709 } 781}
710 782
711 //- /zzz.rs crate:alloc 783//- /zzz.rs crate:alloc
712 pub mod sync { 784pub mod sync {
713 pub struct Arc; 785 pub struct Arc;
714 } 786}
715 "#; 787 "#,
716 check_found_path(
717 code,
718 "std::sync::Arc", 788 "std::sync::Arc",
719 "std::sync::Arc", 789 "std::sync::Arc",
720 "std::sync::Arc", 790 "std::sync::Arc",
@@ -725,26 +795,25 @@ mod tests {
725 #[test] 795 #[test]
726 fn prefer_core_paths_over_std() { 796 fn prefer_core_paths_over_std() {
727 cov_mark::check!(prefer_no_std_paths); 797 cov_mark::check!(prefer_no_std_paths);
728 let code = r#" 798 check_found_path(
729 //- /main.rs crate:main deps:core,std 799 r#"
730 #![no_std] 800//- /main.rs crate:main deps:core,std
801#![no_std]
731 802
732 $0 803$0
733 804
734 //- /std.rs crate:std deps:core 805//- /std.rs crate:std deps:core
735 806
736 pub mod fmt { 807pub mod fmt {
737 pub use core::fmt::Error; 808 pub use core::fmt::Error;
738 } 809}
739 810
740 //- /zzz.rs crate:core 811//- /zzz.rs crate:core
741 812
742 pub mod fmt { 813pub mod fmt {
743 pub struct Error; 814 pub struct Error;
744 } 815}
745 "#; 816 "#,
746 check_found_path(
747 code,
748 "core::fmt::Error", 817 "core::fmt::Error",
749 "core::fmt::Error", 818 "core::fmt::Error",
750 "core::fmt::Error", 819 "core::fmt::Error",
@@ -754,26 +823,25 @@ mod tests {
754 823
755 #[test] 824 #[test]
756 fn prefer_alloc_paths_over_std() { 825 fn prefer_alloc_paths_over_std() {
757 let code = r#" 826 check_found_path(
758 //- /main.rs crate:main deps:alloc,std 827 r#"
759 #![no_std] 828//- /main.rs crate:main deps:alloc,std
829#![no_std]
760 830
761 $0 831$0
762 832
763 //- /std.rs crate:std deps:alloc 833//- /std.rs crate:std deps:alloc
764 834
765 pub mod sync { 835pub mod sync {
766 pub use alloc::sync::Arc; 836 pub use alloc::sync::Arc;
767 } 837}
768 838
769 //- /zzz.rs crate:alloc 839//- /zzz.rs crate:alloc
770 840
771 pub mod sync { 841pub mod sync {
772 pub struct Arc; 842 pub struct Arc;
773 } 843}
774 "#; 844 "#,
775 check_found_path(
776 code,
777 "alloc::sync::Arc", 845 "alloc::sync::Arc",
778 "alloc::sync::Arc", 846 "alloc::sync::Arc",
779 "alloc::sync::Arc", 847 "alloc::sync::Arc",
@@ -783,20 +851,19 @@ mod tests {
783 851
784 #[test] 852 #[test]
785 fn prefer_shorter_paths_if_not_alloc() { 853 fn prefer_shorter_paths_if_not_alloc() {
786 let code = r#" 854 check_found_path(
787 //- /main.rs crate:main deps:megaalloc,std 855 r#"
788 $0 856//- /main.rs crate:main deps:megaalloc,std
857$0
789 858
790 //- /std.rs crate:std deps:megaalloc 859//- /std.rs crate:std deps:megaalloc
791 pub mod sync { 860pub mod sync {
792 pub use megaalloc::sync::Arc; 861 pub use megaalloc::sync::Arc;
793 } 862}
794 863
795 //- /zzz.rs crate:megaalloc 864//- /zzz.rs crate:megaalloc
796 pub struct Arc; 865pub struct Arc;
797 "#; 866 "#,
798 check_found_path(
799 code,
800 "megaalloc::Arc", 867 "megaalloc::Arc",
801 "megaalloc::Arc", 868 "megaalloc::Arc",
802 "megaalloc::Arc", 869 "megaalloc::Arc",
@@ -807,12 +874,12 @@ mod tests {
807 #[test] 874 #[test]
808 fn builtins_are_in_scope() { 875 fn builtins_are_in_scope() {
809 let code = r#" 876 let code = r#"
810 //- /main.rs 877//- /main.rs
811 $0 878$0
812 879
813 pub mod primitive { 880pub mod primitive {
814 pub use u8; 881 pub use u8;
815 } 882}
816 "#; 883 "#;
817 check_found_path(code, "u8", "u8", "u8", "u8"); 884 check_found_path(code, "u8", "u8", "u8", "u8");
818 check_found_path(code, "u16", "u16", "u16", "u16"); 885 check_found_path(code, "u16", "u16", "u16", "u16");
@@ -822,10 +889,10 @@ mod tests {
822 fn inner_items() { 889 fn inner_items() {
823 check_found_path( 890 check_found_path(
824 r#" 891 r#"
825 fn main() { 892fn main() {
826 struct Inner {} 893 struct Inner {}
827 $0 894 $0
828 } 895}
829 "#, 896 "#,
830 "Inner", 897 "Inner",
831 "Inner", 898 "Inner",
@@ -838,12 +905,12 @@ mod tests {
838 fn inner_items_from_outer_scope() { 905 fn inner_items_from_outer_scope() {
839 check_found_path( 906 check_found_path(
840 r#" 907 r#"
841 fn main() { 908fn main() {
842 struct Struct {} 909 struct Struct {}
843 { 910 {
844 $0 911 $0
845 } 912 }
846 } 913}
847 "#, 914 "#,
848 "Struct", 915 "Struct",
849 "Struct", 916 "Struct",
@@ -857,14 +924,14 @@ mod tests {
857 cov_mark::check!(prefixed_in_block_expression); 924 cov_mark::check!(prefixed_in_block_expression);
858 check_found_path( 925 check_found_path(
859 r#" 926 r#"
860 fn main() { 927fn main() {
861 mod module { 928 mod module {
862 struct Struct {} 929 struct Struct {}
863 } 930 }
864 { 931 {
865 $0 932 $0
866 } 933 }
867 } 934}
868 "#, 935 "#,
869 "module::Struct", 936 "module::Struct",
870 "module::Struct", 937 "module::Struct",
@@ -877,14 +944,14 @@ mod tests {
877 fn outer_items_with_inner_items_present() { 944 fn outer_items_with_inner_items_present() {
878 check_found_path( 945 check_found_path(
879 r#" 946 r#"
880 mod module { 947mod module {
881 pub struct CompleteMe; 948 pub struct CompleteMe;
882 } 949}
883 950
884 fn main() { 951fn main() {
885 fn inner() {} 952 fn inner() {}
886 $0 953 $0
887 } 954}
888 "#, 955 "#,
889 "module::CompleteMe", 956 "module::CompleteMe",
890 "module::CompleteMe", 957 "module::CompleteMe",