Skip to content

Commit a4b7ce9

Browse files
bors[bot]matklad
andauthored
Merge #5817
5817: Better API factoring around self access modes r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 1d19084 + 6175467 commit a4b7ce9

File tree

4 files changed

+30
-56
lines changed

4 files changed

+30
-56
lines changed

crates/hir/src/code_model.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use hir_def::{
1212
docs::Documentation,
1313
expr::{BindingAnnotation, Pat, PatId},
1414
import_map,
15+
lang_item::LangItemTarget,
1516
path::ModPath,
1617
per_ns::PerNs,
1718
resolver::{HasResolver, Resolver},
@@ -36,7 +37,7 @@ use rustc_hash::FxHashSet;
3637
use stdx::impl_from;
3738
use syntax::{
3839
ast::{self, AttrsOwner, NameOwner},
39-
AstNode,
40+
AstNode, SmolStr,
4041
};
4142

4243
use crate::{
@@ -1287,6 +1288,15 @@ impl Type {
12871288
db.trait_solve(self.krate, goal).is_some()
12881289
}
12891290

1291+
pub fn is_copy(&self, db: &dyn HirDatabase) -> bool {
1292+
let lang_item = db.lang_item(self.krate, SmolStr::new("copy"));
1293+
let copy_trait = match lang_item {
1294+
Some(LangItemTarget::TraitId(it)) => it,
1295+
_ => return false,
1296+
};
1297+
self.impls_trait(db, copy_trait.into(), &[])
1298+
}
1299+
12901300
pub fn as_callable(&self, db: &dyn HirDatabase) -> Option<Callable> {
12911301
let def = match self.ty.value {
12921302
Ty::Apply(ApplicationTy { ctor: TypeCtor::FnDef(def), parameters: _ }) => Some(def),

crates/hir/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub use crate::{
3838
ScopeDef, Static, Struct, Trait, Type, TypeAlias, TypeParam, Union, VariantDef, Visibility,
3939
},
4040
has_source::HasSource,
41-
semantics::{original_range, PathResolution, SelfKind, Semantics, SemanticsScope},
41+
semantics::{original_range, PathResolution, Semantics, SemanticsScope},
4242
};
4343

4444
pub use hir_def::{

crates/hir/src/semantics.rs

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,16 @@ use std::{cell::RefCell, fmt, iter::successors};
66

77
use base_db::{FileId, FileRange};
88
use hir_def::{
9-
lang_item::LangItemTarget,
109
resolver::{self, HasResolver, Resolver, TypeNs},
11-
src::HasSource,
12-
AsMacroCall, FunctionId, Lookup, TraitId, VariantId,
10+
AsMacroCall, FunctionId, TraitId, VariantId,
1311
};
1412
use hir_expand::{hygiene::Hygiene, name::AsName, ExpansionInfo};
1513
use hir_ty::associated_type_shorthand_candidates;
1614
use itertools::Itertools;
1715
use rustc_hash::{FxHashMap, FxHashSet};
1816
use syntax::{
1917
algo::{find_node_at_offset, skip_trivia_token},
20-
ast, AstNode, Direction, SmolStr, SyntaxNode, SyntaxToken, TextRange, TextSize,
18+
ast, AstNode, Direction, SyntaxNode, SyntaxToken, TextRange, TextSize,
2119
};
2220

2321
use crate::{
@@ -81,13 +79,6 @@ impl PathResolution {
8179
}
8280
}
8381

84-
pub enum SelfKind {
85-
Shared,
86-
Mutable,
87-
Consuming,
88-
Copied,
89-
}
90-
9182
/// Primary API to get semantic information, like types, from syntax trees.
9283
pub struct Semantics<'db, DB> {
9384
pub db: &'db DB,
@@ -197,10 +188,6 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
197188
self.imp.type_of_self(param)
198189
}
199190

200-
pub fn method_reciever_kind(&self, call: &ast::MethodCallExpr) -> Option<SelfKind> {
201-
self.imp.method_receiver_kind(call)
202-
}
203-
204191
pub fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<Function> {
205192
self.imp.resolve_method_call(call).map(Function::from)
206193
}
@@ -423,35 +410,6 @@ impl<'db> SemanticsImpl<'db> {
423410
self.analyze(param.syntax()).type_of_self(self.db, &param)
424411
}
425412

426-
fn method_receiver_kind(&self, call: &ast::MethodCallExpr) -> Option<SelfKind> {
427-
self.resolve_method_call(call).and_then(|func| {
428-
let lookup = func.lookup(self.db.upcast());
429-
let src = lookup.source(self.db.upcast());
430-
let param_list = src.value.param_list()?;
431-
let self_param = param_list.self_param()?;
432-
if self_param.amp_token().is_some() {
433-
return Some(if self_param.mut_token().is_some() {
434-
SelfKind::Mutable
435-
} else {
436-
SelfKind::Shared
437-
});
438-
}
439-
440-
let ty = self.type_of_expr(&call.expr()?)?;
441-
let krate = Function::from(func).krate(self.db)?;
442-
let lang_item = self.db.lang_item(krate.id, SmolStr::new("copy"));
443-
let copy_trait = match lang_item? {
444-
LangItemTarget::TraitId(copy_trait) => Trait::from(copy_trait),
445-
_ => return None,
446-
};
447-
Some(if ty.impls_trait(self.db, copy_trait, &[]) {
448-
SelfKind::Copied
449-
} else {
450-
SelfKind::Consuming
451-
})
452-
})
453-
}
454-
455413
fn resolve_method_call(&self, call: &ast::MethodCallExpr) -> Option<FunctionId> {
456414
self.analyze(call.syntax()).resolve_method_call(self.db, call)
457415
}

crates/ide/src/syntax_highlighting.rs

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod injection;
44
#[cfg(test)]
55
mod tests;
66

7-
use hir::{Name, SelfKind, Semantics, VariantDef};
7+
use hir::{Name, Semantics, VariantDef};
88
use ide_db::{
99
defs::{classify_name, classify_name_ref, Definition, NameClass, NameRefClass},
1010
RootDatabase,
@@ -720,15 +720,21 @@ fn highlight_method_call(
720720
if func.is_unsafe(sema.db) || sema.is_unsafe_method_call(&method_call) {
721721
h |= HighlightModifier::Unsafe;
722722
}
723-
724-
sema.method_reciever_kind(&method_call)
725-
.map(|self_kind| match self_kind {
726-
SelfKind::Shared => h,
727-
SelfKind::Mutable => h | HighlightModifier::Mutable,
728-
SelfKind::Consuming => h | HighlightModifier::Consuming,
729-
SelfKind::Copied => h,
730-
})
731-
.or_else(|| Some(h))
723+
if let Some(self_param) = func.self_param(sema.db) {
724+
match self_param.access(sema.db) {
725+
hir::Access::Shared => (),
726+
hir::Access::Exclusive => h |= HighlightModifier::Mutable,
727+
hir::Access::Owned => {
728+
if let Some(receiver_ty) = method_call.expr().and_then(|it| sema.type_of_expr(&it))
729+
{
730+
if !receiver_ty.is_copy(sema.db) {
731+
h |= HighlightModifier::Consuming
732+
}
733+
}
734+
}
735+
}
736+
}
737+
Some(h)
732738
}
733739

734740
fn highlight_name(

0 commit comments

Comments
 (0)