diff options
-rw-r--r-- | Cargo.lock | 1 | ||||
-rw-r--r-- | crates/ra_hir/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_hir/src/code_model.rs | 36 | ||||
-rw-r--r-- | crates/ra_hir/src/ty/tests.rs | 21 |
4 files changed, 39 insertions, 20 deletions
diff --git a/Cargo.lock b/Cargo.lock index 7f7de3bf4..f93f11a82 100644 --- a/Cargo.lock +++ b/Cargo.lock | |||
@@ -1007,7 +1007,6 @@ dependencies = [ | |||
1007 | "chalk-solve 0.1.0 (git+https://github.com/rust-lang/chalk.git)", | 1007 | "chalk-solve 0.1.0 (git+https://github.com/rust-lang/chalk.git)", |
1008 | "ena 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1008 | "ena 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1009 | "insta 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", | 1009 | "insta 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", |
1010 | "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", | ||
1011 | "lalrpop-intern 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", | 1010 | "lalrpop-intern 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", |
1012 | "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", | 1011 | "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)", |
1013 | "once_cell 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", | 1012 | "once_cell 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", |
diff --git a/crates/ra_hir/Cargo.toml b/crates/ra_hir/Cargo.toml index e89b97daf..d9bed4dda 100644 --- a/crates/ra_hir/Cargo.toml +++ b/crates/ra_hir/Cargo.toml | |||
@@ -12,7 +12,6 @@ rustc-hash = "1.0" | |||
12 | parking_lot = "0.9.0" | 12 | parking_lot = "0.9.0" |
13 | ena = "0.13" | 13 | ena = "0.13" |
14 | once_cell = "1.0.1" | 14 | once_cell = "1.0.1" |
15 | itertools = "0.8.0" | ||
16 | 15 | ||
17 | ra_syntax = { path = "../ra_syntax" } | 16 | ra_syntax = { path = "../ra_syntax" } |
18 | ra_arena = { path = "../ra_arena" } | 17 | ra_arena = { path = "../ra_arena" } |
diff --git a/crates/ra_hir/src/code_model.rs b/crates/ra_hir/src/code_model.rs index 52ee1834f..1bb2f9f28 100644 --- a/crates/ra_hir/src/code_model.rs +++ b/crates/ra_hir/src/code_model.rs | |||
@@ -1,11 +1,8 @@ | |||
1 | pub(crate) mod src; | 1 | pub(crate) mod src; |
2 | pub(crate) mod docs; | 2 | pub(crate) mod docs; |
3 | 3 | ||
4 | use std::iter; | ||
5 | use std::sync::Arc; | 4 | use std::sync::Arc; |
6 | 5 | ||
7 | use itertools::Itertools; | ||
8 | |||
9 | use ra_db::{CrateId, Edition, FileId, SourceRootId}; | 6 | use ra_db::{CrateId, Edition, FileId, SourceRootId}; |
10 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; | 7 | use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner}; |
11 | 8 | ||
@@ -849,20 +846,23 @@ impl Trait { | |||
849 | 846 | ||
850 | /// Returns an iterator over the whole super trait hierarchy (including the | 847 | /// Returns an iterator over the whole super trait hierarchy (including the |
851 | /// trait itself). | 848 | /// trait itself). |
852 | pub fn all_super_traits<'a>( | 849 | pub fn all_super_traits(self, db: &impl HirDatabase) -> Vec<Trait> { |
853 | self, | 850 | // we need to take care a bit here to avoid infinite loops in case of cycles |
854 | db: &'a impl HirDatabase, | 851 | // (i.e. if we have `trait A: B; trait B: A;`) |
855 | ) -> impl Iterator<Item = Trait> + 'a { | 852 | let mut result = vec![self]; |
856 | self.all_super_traits_inner(db).unique() | 853 | let mut i = 0; |
857 | } | 854 | while i < result.len() { |
858 | 855 | let t = result[i]; | |
859 | fn all_super_traits_inner<'a>( | 856 | // yeah this is quadratic, but trait hierarchies should be flat |
860 | self, | 857 | // enough that this doesn't matter |
861 | db: &'a impl HirDatabase, | 858 | for tt in t.direct_super_traits(db) { |
862 | ) -> impl Iterator<Item = Trait> + 'a { | 859 | if !result.contains(&tt) { |
863 | iter::once(self).chain(self.direct_super_traits(db).into_iter().flat_map(move |t| { | 860 | result.push(tt); |
864 | iter::once(t).chain(Box::new(t.all_super_traits(db)) as Box<dyn Iterator<Item = Trait>>) | 861 | } |
865 | })) | 862 | } |
863 | i += 1; | ||
864 | } | ||
865 | result | ||
866 | } | 866 | } |
867 | 867 | ||
868 | pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> { | 868 | pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> { |
@@ -882,7 +882,7 @@ impl Trait { | |||
882 | db: &impl HirDatabase, | 882 | db: &impl HirDatabase, |
883 | name: &Name, | 883 | name: &Name, |
884 | ) -> Option<TypeAlias> { | 884 | ) -> Option<TypeAlias> { |
885 | self.all_super_traits(db).find_map(|t| t.associated_type_by_name(db, name)) | 885 | self.all_super_traits(db).into_iter().find_map(|t| t.associated_type_by_name(db, name)) |
886 | } | 886 | } |
887 | 887 | ||
888 | pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> { | 888 | pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> { |
diff --git a/crates/ra_hir/src/ty/tests.rs b/crates/ra_hir/src/ty/tests.rs index 3f86a5c80..c4bddde85 100644 --- a/crates/ra_hir/src/ty/tests.rs +++ b/crates/ra_hir/src/ty/tests.rs | |||
@@ -3695,6 +3695,27 @@ fn test<T: Trait1, U: Trait2>(x: T, y: U) { | |||
3695 | } | 3695 | } |
3696 | 3696 | ||
3697 | #[test] | 3697 | #[test] |
3698 | fn super_trait_cycle() { | ||
3699 | // This just needs to not crash | ||
3700 | assert_snapshot!( | ||
3701 | infer(r#" | ||
3702 | trait A: B {} | ||
3703 | trait B: A {} | ||
3704 | |||
3705 | fn test<T: A>(x: T) { | ||
3706 | x.foo(); | ||
3707 | } | ||
3708 | "#), | ||
3709 | @r###" | ||
3710 | [44; 45) 'x': T | ||
3711 | [50; 66) '{ ...o(); }': () | ||
3712 | [56; 57) 'x': T | ||
3713 | [56; 63) 'x.foo()': {unknown} | ||
3714 | "### | ||
3715 | ); | ||
3716 | } | ||
3717 | |||
3718 | #[test] | ||
3698 | fn super_trait_assoc_type_bounds() { | 3719 | fn super_trait_assoc_type_bounds() { |
3699 | assert_snapshot!( | 3720 | assert_snapshot!( |
3700 | infer(r#" | 3721 | infer(r#" |