Skip to content

Commit 41f470f

Browse files
committed
Correctly support SelfType when searching for usages
1 parent 96c5df9 commit 41f470f

File tree

6 files changed

+210
-76
lines changed

6 files changed

+210
-76
lines changed

crates/hir/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2071,6 +2071,10 @@ impl Type {
20712071
Some(adt.into())
20722072
}
20732073

2074+
pub fn as_builtin(&self) -> Option<BuiltinType> {
2075+
self.ty.as_builtin().map(|inner| BuiltinType { inner })
2076+
}
2077+
20742078
pub fn as_dyn_trait(&self) -> Option<Trait> {
20752079
self.ty.dyn_trait().map(Into::into)
20762080
}

crates/hir_ty/src/chalk_ext.rs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
//! Various extensions traits for Chalk types.
22
3-
use chalk_ir::Mutability;
3+
use chalk_ir::{FloatTy, IntTy, Mutability, Scalar, UintTy};
44
use hir_def::{
5-
type_ref::Rawness, AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId,
5+
builtin_type::{BuiltinFloat, BuiltinInt, BuiltinType, BuiltinUint},
6+
type_ref::Rawness,
7+
AssocContainerId, FunctionId, GenericDefId, HasModule, Lookup, TraitId,
68
};
79

810
use crate::{
@@ -18,6 +20,7 @@ pub trait TyExt {
1820
fn is_unknown(&self) -> bool;
1921

2022
fn as_adt(&self) -> Option<(hir_def::AdtId, &Substitution)>;
23+
fn as_builtin(&self) -> Option<BuiltinType>;
2124
fn as_tuple(&self) -> Option<&Substitution>;
2225
fn as_fn_def(&self, db: &dyn HirDatabase) -> Option<FunctionId>;
2326
fn as_reference(&self) -> Option<(&Ty, Lifetime, Mutability)>;
@@ -59,6 +62,35 @@ impl TyExt for Ty {
5962
}
6063
}
6164

65+
fn as_builtin(&self) -> Option<BuiltinType> {
66+
match self.kind(&Interner) {
67+
TyKind::Str => Some(BuiltinType::Str),
68+
TyKind::Scalar(Scalar::Bool) => Some(BuiltinType::Bool),
69+
TyKind::Scalar(Scalar::Char) => Some(BuiltinType::Char),
70+
TyKind::Scalar(Scalar::Float(fty)) => Some(BuiltinType::Float(match fty {
71+
FloatTy::F64 => BuiltinFloat::F64,
72+
FloatTy::F32 => BuiltinFloat::F32,
73+
})),
74+
TyKind::Scalar(Scalar::Int(ity)) => Some(BuiltinType::Int(match ity {
75+
IntTy::Isize => BuiltinInt::Isize,
76+
IntTy::I8 => BuiltinInt::I8,
77+
IntTy::I16 => BuiltinInt::I16,
78+
IntTy::I32 => BuiltinInt::I32,
79+
IntTy::I64 => BuiltinInt::I64,
80+
IntTy::I128 => BuiltinInt::I128,
81+
})),
82+
TyKind::Scalar(Scalar::Uint(ity)) => Some(BuiltinType::Uint(match ity {
83+
UintTy::Usize => BuiltinUint::Usize,
84+
UintTy::U8 => BuiltinUint::U8,
85+
UintTy::U16 => BuiltinUint::U16,
86+
UintTy::U32 => BuiltinUint::U32,
87+
UintTy::U64 => BuiltinUint::U64,
88+
UintTy::U128 => BuiltinUint::U128,
89+
})),
90+
_ => None,
91+
}
92+
}
93+
6294
fn as_tuple(&self) -> Option<&Substitution> {
6395
match self.kind(&Interner) {
6496
TyKind::Tuple(_, substs) => Some(substs),

crates/ide/src/references.rs

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub(crate) fn find_all_refs(
6565
(find_def(&sema, &syntax, position)?, false)
6666
};
6767

68-
let mut usages = def.usages(sema).set_scope(search_scope).all();
68+
let mut usages = def.usages(sema).set_scope(search_scope).include_self_refs().all();
6969
if is_literal_search {
7070
// filter for constructor-literals
7171
let refs = usages.references.values_mut();
@@ -1166,18 +1166,67 @@ fn foo<const FOO$0: usize>() -> usize {
11661166
fn test_find_self_ty_in_trait_def() {
11671167
check(
11681168
r#"
1169-
trait Foo {
1169+
trait Foo where Self: {
11701170
fn f() -> Self$0;
11711171
}
11721172
"#,
11731173
expect![[r#"
11741174
Self TypeParam FileId(0) 6..9 6..9
11751175
1176-
FileId(0) 26..30
1176+
FileId(0) 16..20
1177+
FileId(0) 38..42
11771178
"#]],
11781179
);
1180+
// check(
1181+
// r#"
1182+
// trait Foo$0 where Self: {
1183+
// fn f() -> Self;
1184+
// }
1185+
// "#,
1186+
// expect![[r#"
1187+
// Foo Trait FileId(0) 0..45 6..9
1188+
1189+
// FileId(0) 16..20
1190+
// FileId(0) 38..42
1191+
// "#]],
1192+
// );
11791193
}
11801194

1195+
#[test]
1196+
fn test_self_ty() {
1197+
check(
1198+
r#"
1199+
struct $0Foo;
1200+
1201+
impl Foo where Self: {
1202+
fn f() -> Self;
1203+
}
1204+
"#,
1205+
expect![[r#"
1206+
Foo Struct FileId(0) 0..11 7..10
1207+
1208+
FileId(0) 18..21
1209+
FileId(0) 28..32
1210+
FileId(0) 50..54
1211+
"#]],
1212+
);
1213+
check(
1214+
r#"
1215+
struct Foo;
1216+
1217+
impl Foo where Self: {
1218+
fn f() -> Self$0;
1219+
}
1220+
"#,
1221+
expect![[r#"
1222+
impl Impl FileId(0) 13..57 18..21
1223+
1224+
FileId(0) 18..21
1225+
FileId(0) 28..32
1226+
FileId(0) 50..54
1227+
"#]],
1228+
);
1229+
}
11811230
#[test]
11821231
fn test_self_variant_with_payload() {
11831232
check(

crates/ide/src/references/rename.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1888,4 +1888,21 @@ impl Foo {
18881888
"error: Cannot rename `Self`",
18891889
);
18901890
}
1891+
1892+
#[test]
1893+
fn test_rename_ignores_self_ty() {
1894+
check(
1895+
"Fo0",
1896+
r#"
1897+
struct $0Foo;
1898+
1899+
impl Foo where Self: {}
1900+
"#,
1901+
r#"
1902+
struct Fo0;
1903+
1904+
impl Fo0 where Self: {}
1905+
"#,
1906+
);
1907+
}
18911908
}

crates/ide_assists/src/handlers/convert_tuple_struct_to_named_struct.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ fn edit_struct_references(
107107
names: &[ast::Name],
108108
) {
109109
let strukt_def = Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(strukt)));
110-
let usages = strukt_def.usages(&ctx.sema).include_self_kw_refs(true).all();
110+
let usages = strukt_def.usages(&ctx.sema).include_self_refs().all();
111111

112112
let edit_node = |edit: &mut AssistBuilder, node: SyntaxNode| -> Option<()> {
113113
match_ast! {

0 commit comments

Comments
 (0)