Skip to content

Commit 283ec13

Browse files
committed
Fix type "items" order.
1 parent c50157f commit 283ec13

File tree

2 files changed

+36
-24
lines changed

2 files changed

+36
-24
lines changed

crates/ra_hir/src/code_model.rs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use hir_ty::{
2727
display::{HirDisplayError, HirFormatter},
2828
expr::ExprValidator,
2929
method_resolution, ApplicationTy, Canonical, GenericPredicate, InEnvironment, OpaqueTyId,
30-
Substs, TraitEnvironment, Ty, TyDefId, TypeCtor, TypeWalk,
30+
Substs, TraitEnvironment, Ty, TyDefId, TypeCtor,
3131
};
3232
use ra_db::{CrateId, CrateName, Edition, FileId};
3333
use ra_prof::profile;
@@ -1381,7 +1381,7 @@ impl Type {
13811381
}
13821382
}
13831383

1384-
/// Returns a flattened list of all the ADTs and Traits mentioned in the type
1384+
/// Returns a flattened list of all ADTs and Traits mentioned in the type
13851385
pub fn flattened_type_items(&self, db: &dyn HirDatabase) -> Vec<AdtOrTrait> {
13861386
fn push_new_item(item: AdtOrTrait, acc: &mut Vec<AdtOrTrait>) {
13871387
if !acc.contains(&item) {
@@ -1398,27 +1398,38 @@ impl Type {
13981398
match p {
13991399
GenericPredicate::Implemented(trait_ref) => {
14001400
push_new_item(Trait::from(trait_ref.trait_).into(), acc);
1401-
walk_types(db, &trait_ref.substs, acc);
1401+
walk_substs(db, &trait_ref.substs, acc);
14021402
}
14031403
GenericPredicate::Projection(_) => {}
14041404
GenericPredicate::Error => (),
14051405
}
14061406
}
14071407
}
14081408

1409-
fn walk_types<T: TypeWalk>(db: &dyn HirDatabase, tw: &T, acc: &mut Vec<AdtOrTrait>) {
1410-
tw.walk(&mut |ty| walk_type(db, ty, acc));
1409+
// TypeWalk::walk does not preserve items order!
1410+
fn walk_substs(db: &dyn HirDatabase, substs: &Substs, acc: &mut Vec<AdtOrTrait>) {
1411+
for ty in substs.iter() {
1412+
walk_type(db, ty, acc);
1413+
}
14111414
}
14121415

14131416
fn walk_type(db: &dyn HirDatabase, ty: &Ty, acc: &mut Vec<AdtOrTrait>) {
14141417
match ty.strip_references() {
14151418
Ty::Apply(ApplicationTy { ctor, parameters, .. }) => {
14161419
match ctor {
14171420
TypeCtor::Adt(adt_id) => push_new_item(Adt::from(*adt_id).into(), acc),
1421+
TypeCtor::AssociatedType(type_alias_id) => {
1422+
let trait_id = match type_alias_id.lookup(db.upcast()).container {
1423+
AssocContainerId::TraitId(it) => it,
1424+
_ => panic!("not an associated type"),
1425+
};
1426+
1427+
push_new_item(Trait::from(trait_id).into(), acc);
1428+
}
14181429
_ => (),
14191430
}
14201431
// adt params, tuples, etc...
1421-
walk_types(db, parameters, acc);
1432+
walk_substs(db, parameters, acc);
14221433
}
14231434
Ty::Dyn(predicates) => {
14241435
push_bounds(db, predicates, acc);
@@ -1451,7 +1462,7 @@ impl Type {
14511462
}
14521463
};
14531464
push_bounds(db, &bounds.value, acc);
1454-
walk_types(db, &opaque_ty.parameters, acc);
1465+
walk_substs(db, &opaque_ty.parameters, acc);
14551466
}
14561467
_ => (),
14571468
}

crates/ra_ide/src/hover.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,9 +234,10 @@ fn runnable_action(
234234
fn goto_type_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
235235
match def {
236236
Definition::Local(it) => {
237-
let ty = it.ty(db);
238-
let v = ty.flattened_type_items(db);
239-
let targets = v.into_iter()
237+
let targets = it
238+
.ty(db)
239+
.flattened_type_items(db)
240+
.into_iter()
240241
.map(|it| HoverGotoTypeData {
241242
mod_path: adt_or_trait_mod_path(db, &it),
242243
nav: it.to_nav(db),
@@ -1980,18 +1981,18 @@ fn func(foo: i32) { if true { <|>foo; }; }
19801981
}
19811982

19821983
#[test]
1983-
fn test_hover_arg_goto_type_action() {
1984+
fn test_hover_goto_type_action_links_order() {
19841985
let (_, actions) = check_hover_result(
19851986
"
19861987
//- /lib.rs
19871988
trait ImplTrait<T> {}
19881989
trait DynTrait<T> {}
19891990
struct B<T> {}
19901991
struct S {}
1991-
1992-
fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<S>>>) {}
1992+
1993+
fn foo(a<|>rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
19931994
",
1994-
&["&impl ImplTrait<B<dyn DynTrait<S>>>"],
1995+
&["&impl ImplTrait<B<dyn DynTrait<B<S>>>>"],
19951996
);
19961997
assert_debug_snapshot!(actions,
19971998
@r###"
@@ -2018,20 +2019,20 @@ fn func(foo: i32) { if true { <|>foo; }; }
20182019
},
20192020
},
20202021
HoverGotoTypeData {
2021-
mod_path: "S",
2022+
mod_path: "B",
20222023
nav: NavigationTarget {
20232024
file_id: FileId(
20242025
1,
20252026
),
2026-
full_range: 58..69,
2027-
name: "S",
2027+
full_range: 43..57,
2028+
name: "B",
20282029
kind: STRUCT_DEF,
20292030
focus_range: Some(
2030-
65..66,
2031+
50..51,
20312032
),
20322033
container_name: None,
20332034
description: Some(
2034-
"struct S",
2035+
"struct B",
20352036
),
20362037
docs: None,
20372038
},
@@ -2056,20 +2057,20 @@ fn func(foo: i32) { if true { <|>foo; }; }
20562057
},
20572058
},
20582059
HoverGotoTypeData {
2059-
mod_path: "B",
2060+
mod_path: "S",
20602061
nav: NavigationTarget {
20612062
file_id: FileId(
20622063
1,
20632064
),
2064-
full_range: 43..57,
2065-
name: "B",
2065+
full_range: 58..69,
2066+
name: "S",
20662067
kind: STRUCT_DEF,
20672068
focus_range: Some(
2068-
50..51,
2069+
65..66,
20692070
),
20702071
container_name: None,
20712072
description: Some(
2072-
"struct B",
2073+
"struct S",
20732074
),
20742075
docs: None,
20752076
},

0 commit comments

Comments
 (0)