Fix crash for super trait cycles
This commit is contained in:
parent
9db34eec20
commit
8fb3cab76c
4 changed files with 39 additions and 20 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1007,7 +1007,6 @@ dependencies = [
|
|||
"chalk-solve 0.1.0 (git+https://github.com/rust-lang/chalk.git)",
|
||||
"ena 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"insta 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"lalrpop-intern 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"once_cell 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
|
|
|
@ -12,7 +12,6 @@ rustc-hash = "1.0"
|
|||
parking_lot = "0.9.0"
|
||||
ena = "0.13"
|
||||
once_cell = "1.0.1"
|
||||
itertools = "0.8.0"
|
||||
|
||||
ra_syntax = { path = "../ra_syntax" }
|
||||
ra_arena = { path = "../ra_arena" }
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
pub(crate) mod src;
|
||||
pub(crate) mod docs;
|
||||
|
||||
use std::iter;
|
||||
use std::sync::Arc;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
use ra_db::{CrateId, Edition, FileId, SourceRootId};
|
||||
use ra_syntax::ast::{self, NameOwner, TypeAscriptionOwner};
|
||||
|
||||
|
@ -849,20 +846,23 @@ impl Trait {
|
|||
|
||||
/// Returns an iterator over the whole super trait hierarchy (including the
|
||||
/// trait itself).
|
||||
pub fn all_super_traits<'a>(
|
||||
self,
|
||||
db: &'a impl HirDatabase,
|
||||
) -> impl Iterator<Item = Trait> + 'a {
|
||||
self.all_super_traits_inner(db).unique()
|
||||
}
|
||||
|
||||
fn all_super_traits_inner<'a>(
|
||||
self,
|
||||
db: &'a impl HirDatabase,
|
||||
) -> impl Iterator<Item = Trait> + 'a {
|
||||
iter::once(self).chain(self.direct_super_traits(db).into_iter().flat_map(move |t| {
|
||||
iter::once(t).chain(Box::new(t.all_super_traits(db)) as Box<dyn Iterator<Item = Trait>>)
|
||||
}))
|
||||
pub fn all_super_traits(self, db: &impl HirDatabase) -> Vec<Trait> {
|
||||
// we need to take care a bit here to avoid infinite loops in case of cycles
|
||||
// (i.e. if we have `trait A: B; trait B: A;`)
|
||||
let mut result = vec![self];
|
||||
let mut i = 0;
|
||||
while i < result.len() {
|
||||
let t = result[i];
|
||||
// yeah this is quadratic, but trait hierarchies should be flat
|
||||
// enough that this doesn't matter
|
||||
for tt in t.direct_super_traits(db) {
|
||||
if !result.contains(&tt) {
|
||||
result.push(tt);
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
pub fn associated_type_by_name(self, db: &impl DefDatabase, name: &Name) -> Option<TypeAlias> {
|
||||
|
@ -882,7 +882,7 @@ impl Trait {
|
|||
db: &impl HirDatabase,
|
||||
name: &Name,
|
||||
) -> Option<TypeAlias> {
|
||||
self.all_super_traits(db).find_map(|t| t.associated_type_by_name(db, name))
|
||||
self.all_super_traits(db).into_iter().find_map(|t| t.associated_type_by_name(db, name))
|
||||
}
|
||||
|
||||
pub(crate) fn trait_data(self, db: &impl DefDatabase) -> Arc<TraitData> {
|
||||
|
|
|
@ -3694,6 +3694,27 @@ fn test<T: Trait1, U: Trait2>(x: T, y: U) {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn super_trait_cycle() {
|
||||
// This just needs to not crash
|
||||
assert_snapshot!(
|
||||
infer(r#"
|
||||
trait A: B {}
|
||||
trait B: A {}
|
||||
|
||||
fn test<T: A>(x: T) {
|
||||
x.foo();
|
||||
}
|
||||
"#),
|
||||
@r###"
|
||||
[44; 45) 'x': T
|
||||
[50; 66) '{ ...o(); }': ()
|
||||
[56; 57) 'x': T
|
||||
[56; 63) 'x.foo()': {unknown}
|
||||
"###
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn super_trait_assoc_type_bounds() {
|
||||
assert_snapshot!(
|
||||
|
|
Loading…
Add table
Reference in a new issue