Skip to content

Commit cf4d4f6

Browse files
bors[bot]Veykril
andauthored
Merge #8773
8773: fix: Correctly support SelfType when searching for usages r=Veykril a=Veykril Fixes #7443 Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
2 parents 6cd11bb + 3a34641 commit cf4d4f6

File tree

6 files changed

+222
-78
lines changed

6 files changed

+222
-78
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: 59 additions & 5 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();
@@ -1163,21 +1163,75 @@ fn foo<const FOO$0: usize>() -> usize {
11631163
}
11641164

11651165
#[test]
1166-
fn test_find_self_ty_in_trait_def() {
1166+
fn test_trait() {
11671167
check(
11681168
r#"
1169-
trait Foo {
1170-
fn f() -> Self$0;
1169+
trait Foo$0 where Self: {}
1170+
1171+
impl Foo for () {}
1172+
"#,
1173+
expect![[r#"
1174+
Foo Trait FileId(0) 0..24 6..9
1175+
1176+
FileId(0) 31..34
1177+
"#]],
1178+
);
1179+
}
1180+
1181+
#[test]
1182+
fn test_trait_self() {
1183+
check(
1184+
r#"
1185+
trait Foo where Self$0 {
1186+
fn f() -> Self;
11711187
}
1188+
1189+
impl Foo for () {}
11721190
"#,
11731191
expect![[r#"
11741192
Self TypeParam FileId(0) 6..9 6..9
11751193
1176-
FileId(0) 26..30
1194+
FileId(0) 16..20
1195+
FileId(0) 37..41
11771196
"#]],
11781197
);
11791198
}
11801199

1200+
#[test]
1201+
fn test_self_ty() {
1202+
check(
1203+
r#"
1204+
struct $0Foo;
1205+
1206+
impl Foo where Self: {
1207+
fn f() -> Self;
1208+
}
1209+
"#,
1210+
expect![[r#"
1211+
Foo Struct FileId(0) 0..11 7..10
1212+
1213+
FileId(0) 18..21
1214+
FileId(0) 28..32
1215+
FileId(0) 50..54
1216+
"#]],
1217+
);
1218+
check(
1219+
r#"
1220+
struct Foo;
1221+
1222+
impl Foo where Self: {
1223+
fn f() -> Self$0;
1224+
}
1225+
"#,
1226+
expect![[r#"
1227+
impl Impl FileId(0) 13..57 18..21
1228+
1229+
FileId(0) 18..21
1230+
FileId(0) 28..32
1231+
FileId(0) 50..54
1232+
"#]],
1233+
);
1234+
}
11811235
#[test]
11821236
fn test_self_variant_with_payload() {
11831237
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)