use std::collections::{BTreeMap, BTreeSet};
use sealmap_extract::{aggregate_calls, ids, lower};
use sealmap_model::{
Call, Codebase, Confidence, Flow, Member, Package, Relation, RelationKind, SourceFile, Suffix, Symbol, SymbolId,
SymbolKind,
};
use crate::layout::Plan;
use crate::raw::*;
use crate::{Diagnostic, RustOptions};
const STD_ROOTS: &[&str] = &["std", "core", "alloc"];
const PRELUDE: &[&str] = &[
"Option",
"Some",
"None",
"Result",
"Ok",
"Err",
"Vec",
"String",
"Box",
"ToString",
"ToOwned",
"Into",
"From",
"TryFrom",
"TryInto",
"Iterator",
"IntoIterator",
"DoubleEndedIterator",
"ExactSizeIterator",
"Extend",
"Fn",
"FnMut",
"FnOnce",
"Send",
"Sync",
"Sized",
"Unpin",
"Copy",
"Clone",
"Default",
"Drop",
"PartialEq",
"Eq",
"PartialOrd",
"Ord",
"AsRef",
"AsMut",
"Debug",
"Display",
"Hash",
"Self",
"self",
"bool",
"char",
"str",
"u8",
"u16",
"u32",
"u64",
"u128",
"usize",
"i8",
"i16",
"i32",
"i64",
"i128",
"isize",
"f32",
"f64",
"drop",
"panic",
];
const COMMON_METHODS: &[&str] = &[
"new",
"default",
"clone",
"get",
"get_mut",
"insert",
"remove",
"push",
"pop",
"len",
"is_empty",
"iter",
"iter_mut",
"into_iter",
"map",
"and_then",
"unwrap",
"expect",
"ok",
"err",
"collect",
"to_string",
"to_owned",
"into",
"from",
"as_ref",
"as_str",
"contains",
"extend",
"join",
"split",
"parse",
"write",
"read",
"send",
"recv",
"lock",
"fmt",
"eq",
"cmp",
"hash",
"next",
"take",
"filter",
"find",
"first",
"last",
"clear",
"entry",
"keys",
"values",
"run",
"start",
"stop",
"close",
"open",
"flush",
"build",
"update",
"apply",
"file_name",
"extension",
"path",
"exists",
"display",
"to_str",
"as_bytes",
"trim",
"lines",
"chars",
"bytes",
"starts_with",
"ends_with",
"replace",
"find_map",
"position",
"max",
"min",
"sum",
"count",
"rev",
"zip",
"skip",
"chain",
"flatten",
"cloned",
"copied",
"sort",
"dedup",
"retain",
"drain",
"truncate",
"with_capacity",
"capacity",
"reserve",
"as_slice",
"to_vec",
"borrow",
"borrow_mut",
"set",
"reset",
"load",
"store",
"call",
"handle",
"process",
"execute",
"connect",
"kind",
"name",
"id",
"value",
"key",
"data",
"size",
"is_some",
"is_none",
"is_ok",
"is_err",
"map_err",
"or_else",
"unwrap_or",
"unwrap_or_default",
"unwrap_or_else",
"as_mut",
];
pub(crate) fn build(name: &str, files: Vec<RawFile>, plan: &Plan, opts: &RustOptions) -> (Codebase, Vec<Diagnostic>) {
let r = Resolver::new(&files, plan);
let mut cb = Codebase::new(name);
let mut diags = Vec::new();
for f in &files {
if let Some(e) = &f.error {
diags.push(Diagnostic { file: f.path.clone(), message: e.clone() });
}
}
for f in &files {
cb.add_file(SourceFile {
path: f.path.clone(),
language: "rust".into(),
module: module_sym(&f.role.module),
hash: f.hash.clone(),
lines: f.text_lines,
});
for m in &f.modules {
let id = module_sym(&m.path);
let mut s =
Symbol::new(id.clone(), m.path.last().cloned().unwrap_or_default(), SymbolKind::Module, f.path.clone());
s.span = m.span;
s.sig_hash = m.sig_hash;
s.body_hash = m.body_hash;
s.doc = m.doc.clone();
s.visibility = m.vis.clone();
s.tags = m.tags.clone();
cb.add_symbol(s);
for u in &m.uses {
if u.alias == "*" {
if let Some(t) = r.resolve_internal(&id, &u.target, None) {
cb.add_relation(Relation::new(id.clone(), t, RelationKind::Imports, Confidence::Exact));
}
continue;
}
let (t, c) = r.resolve(&id, &u.target, None, Ns::Type);
if keep_ref(&t) {
cb.add_relation(Relation::new(id.clone(), t, RelationKind::Imports, c));
}
}
}
for it in &f.items {
let module = module_sym(&it.module);
let id = ids::item_id(&module, it.kind, &it.name);
let mut s = Symbol::new(id.clone(), &it.name, it.kind, f.path.clone());
s.visibility = it.vis.clone();
s.span = it.span;
s.sig_hash = it.sig_hash;
s.body_hash = it.body_hash;
s.signature = it.signature.clone();
s.doc = it.doc.clone();
s.generics = it.generics.clone();
s.tags = it.tags.clone();
let scope = InScope::of(&it.type_params);
s.members =
it.members.iter().map(|m| r.member(&module, m, Some(&id), scope.with(&m.type_params))).collect();
for m in &s.members {
for t in &m.refs {
let c = if r.internal.contains(t) { Confidence::Exact } else { Confidence::External };
cb.add_relation(Relation::new(id.clone(), t.clone(), RelationKind::FieldType, c));
}
}
for st in &it.supertraits {
let (t, c) = r.resolve(&module, st, None, Ns::Type);
if keep_ref(&t) {
cb.add_relation(Relation::new(id.clone(), t, RelationKind::Extends, c));
}
}
r.add_uses(&mut cb, &id, &module, &it.sig_refs, Some(&id), scope);
let ctx =
FlowCtx { module: &module, self_ty: (it.kind == SymbolKind::Trait).then_some(&id), params: scope };
if !it.flow.is_empty() {
s.flow = r.flow(&ctx, &it.flow, opts);
}
for m in &it.methods {
let mid = ids::method_id(&id, None, &m.name);
let ms = r.method_symbol(&mid, &id, m, f, &ctx, opts, &mut cb);
cb.add_symbol(ms);
}
cb.add_symbol(s);
}
for imp in &f.impls {
let module = module_sym(&imp.module);
let ty = r.impl_self(&module, imp);
if let Some((segs, _)) = &imp.trait_ {
let (t, c) = r.resolve(&module, segs, Some(&ty), Ns::Type);
if keep_ref(&t) || r.internal.contains(&t) {
cb.add_relation(Relation::new(ty.clone(), t, RelationKind::Implements, c));
}
}
let ctx = FlowCtx { module: &module, self_ty: Some(&ty), params: InScope::of(&imp.type_params) };
for m in &imp.methods {
let mid = impl_method(&module, &ty, imp, &m.name);
let mut ms = r.method_symbol(&mid, &ty, m, f, &ctx, opts, &mut cb);
if let Some((_, disp)) = &imp.trait_ {
ms.tags.insert(0, format!("impl {disp}"));
}
cb.add_symbol(ms);
}
}
}
aggregate_calls(&mut cb);
(cb, diags)
}
fn module_sym(segments: &[String]) -> SymbolId {
let (krate, rest) = segments.split_first().map_or(("", &[][..]), |(k, r)| (k.as_str(), r));
ids::module_id(&crate_package(krate), rest)
}
fn crate_package(krate: &str) -> Package {
ids::package("cargo", krate)
}
fn impl_method(module: &SymbolId, ty: &SymbolId, imp: &RawImpl, name: &str) -> SymbolId {
let self_ty = imp.self_ty.as_ref().map_or_else(|| imp.self_text.clone(), |s| s.join("::"));
let trait_ = imp.trait_.as_ref().map(|(_, disp)| disp.as_str());
ids::impl_method_id(module, ty, &self_ty, trait_, name)
}
fn undefined_member(ty: &SymbolId, name: &str) -> SymbolId {
ids::method_id(ty, None, name)
}
fn keep_ref(id: &SymbolId) -> bool {
let root = id.root();
let first = root.as_deref().unwrap_or("?");
!STD_ROOTS.contains(&first) && !PRELUDE.contains(&first)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Ns {
Type,
Value,
Prefix,
}
#[derive(Debug, Clone, Default)]
struct Slots {
ty: Option<SymbolId>,
value: Option<SymbolId>,
}
impl Slots {
fn insert(&mut self, kind: SymbolKind, id: SymbolId) {
let slot = match kind {
SymbolKind::Function | SymbolKind::Method | SymbolKind::Const | SymbolKind::Static => &mut self.value,
SymbolKind::Macro => return,
_ => &mut self.ty,
};
if slot.is_none() {
*slot = Some(id);
}
}
fn get(&self, ns: Ns) -> Option<&SymbolId> {
match ns {
Ns::Type => self.ty.as_ref().or(self.value.as_ref()),
Ns::Value => self.value.as_ref().or(self.ty.as_ref()),
Ns::Prefix => self.ty.as_ref(),
}
}
}
struct FlowCtx<'a> {
module: &'a SymbolId,
self_ty: Option<&'a SymbolId>,
params: InScope<'a>,
}
#[derive(Debug, Clone, Copy, Default)]
struct InScope<'a> {
enclosing: &'a [String],
own: &'a [String],
}
impl<'a> InScope<'a> {
fn of(own: &'a [String]) -> Self {
InScope { enclosing: &[], own }
}
fn with(self, own: &'a [String]) -> Self {
debug_assert!(self.enclosing.is_empty(), "generic scopes nest one level");
InScope { enclosing: self.own, own }
}
fn contains(&self, name: &str) -> bool {
self.own.iter().chain(self.enclosing).any(|p| p == name)
}
fn heads(&self, segs: &[String]) -> bool {
segs.first().is_some_and(|s| self.contains(s))
}
}
struct Fields {
module: SymbolId,
params: Vec<String>,
refs: BTreeMap<String, Vec<Segs>>,
}
impl Fields {
fn field(&self, name: &str) -> Option<(&SymbolId, InScope<'_>, &[Segs])> {
Some((&self.module, InScope::of(&self.params), self.refs.get(name)?))
}
}
struct Resolver<'p> {
plan: &'p Plan,
crates: BTreeSet<String>,
items: BTreeMap<SymbolId, BTreeMap<String, Slots>>,
uses: BTreeMap<SymbolId, Vec<RawUse>>,
internal: BTreeSet<SymbolId>,
fields: BTreeMap<SymbolId, Fields>,
methods: BTreeMap<SymbolId, BTreeMap<String, SymbolId>>,
by_name: BTreeMap<String, BTreeSet<SymbolId>>,
impls: BTreeMap<SymbolId, BTreeSet<SymbolId>>,
trait_methods: BTreeMap<SymbolId, BTreeSet<String>>,
globs: BTreeMap<SymbolId, Vec<SymbolId>>,
}
impl<'p> Resolver<'p> {
fn new(files: &[RawFile], plan: &'p Plan) -> Self {
let mut r = Resolver {
plan,
crates: files.iter().map(|f| f.role.crate_name.clone()).collect(),
items: BTreeMap::new(),
uses: BTreeMap::new(),
internal: BTreeSet::new(),
fields: BTreeMap::new(),
methods: BTreeMap::new(),
by_name: BTreeMap::new(),
impls: BTreeMap::new(),
trait_methods: BTreeMap::new(),
globs: BTreeMap::new(),
};
for f in files {
for m in &f.modules {
let id = module_sym(&m.path);
r.internal.insert(id.clone());
r.uses.entry(id.clone()).or_default().extend(m.uses.iter().cloned());
if let Some(parent) = id.parent() {
let name = m.path.last().cloned().unwrap_or_default();
r.items.entry(parent).or_default().entry(name).or_default().insert(SymbolKind::Module, id);
}
}
for it in &f.items {
let module = module_sym(&it.module);
let id = ids::item_id(&module, it.kind, &it.name);
r.internal.insert(id.clone());
r.items
.entry(module.clone())
.or_default()
.entry(it.name.clone())
.or_default()
.insert(it.kind, id.clone());
if matches!(it.kind, SymbolKind::Struct | SymbolKind::Union | SymbolKind::Enum) {
let refs = it.members.iter().map(|m| (m.name.clone(), m.refs.clone())).collect();
r.fields
.insert(id.clone(), Fields { module: module.clone(), params: it.type_params.clone(), refs });
}
if it.kind == SymbolKind::Trait {
let names = r.trait_methods.entry(id.clone()).or_default();
for m in &it.members {
names.insert(m.name.clone());
}
for m in &it.methods {
names.insert(m.name.clone());
let mid = ids::method_id(&id, None, &m.name);
r.internal.insert(mid.clone());
r.methods.entry(id.clone()).or_default().insert(m.name.clone(), mid.clone());
r.by_name.entry(m.name.clone()).or_default().insert(mid);
}
}
}
}
for round in 0..2 {
let mut globs: BTreeMap<SymbolId, Vec<SymbolId>> = BTreeMap::new();
for (module, uses) in &r.uses {
for u in uses.iter().filter(|u| u.alias == "*") {
if let Some(t) = r.walk(module, &u.target, None, Ns::Type, 0, round > 0) {
if r.items.contains_key(&t) && !globs.get(module).is_some_and(|v| v.contains(&t)) {
globs.entry(module.clone()).or_default().push(t);
}
}
}
}
r.globs = globs;
}
for pass_trait in [false, true] {
for f in files {
for imp in &f.impls {
if imp.trait_.is_some() != pass_trait {
continue;
}
let module = module_sym(&imp.module);
let ty = r.impl_self(&module, imp);
if let Some((segs, _)) = &imp.trait_ {
let (t, _) = r.resolve(&module, segs, Some(&ty), Ns::Type);
r.impls.entry(ty.clone()).or_default().insert(t);
}
for m in &imp.methods {
let mid = impl_method(&module, &ty, imp, &m.name);
r.internal.insert(mid.clone());
r.methods.entry(ty.clone()).or_default().entry(m.name.clone()).or_insert(mid.clone());
r.by_name.entry(m.name.clone()).or_default().insert(mid);
}
}
}
}
r
}
fn is_crate(&self, root: &str) -> bool {
self.crates.contains(root)
|| self.plan.crates.get(root).is_some_and(|keys| keys.iter().any(|k| self.crates.contains(k)))
}
fn impl_self(&self, module: &SymbolId, imp: &RawImpl) -> SymbolId {
match &imp.self_ty {
Some(segs) => self.resolve(module, segs, None, Ns::Type).0,
None => SymbolId::path([imp.self_text.as_str()]).expect("one segment is a non-empty path"),
}
}
fn resolve(
&self,
module: &SymbolId,
segs: &[String],
self_ty: Option<&SymbolId>,
ns: Ns,
) -> (SymbolId, Confidence) {
match self.walk(module, segs, self_ty, ns, 0, true) {
Some(id) if self.internal.contains(&id) => (id, Confidence::Exact),
Some(id) => {
let in_workspace = id.root().is_some_and(|root| self.is_crate(&root));
(id, if in_workspace { Confidence::Inferred } else { Confidence::External })
}
None => (ids::path_id(segs).unwrap_or_else(|| ids::unresolved_method_id("")), Confidence::External),
}
}
fn resolve_in(
&self,
module: &SymbolId,
segs: &[String],
self_ty: Option<&SymbolId>,
ns: Ns,
params: InScope<'_>,
) -> (SymbolId, Confidence) {
if params.heads(segs) {
return (ids::path_id(segs).unwrap_or_else(|| ids::unresolved_method_id("")), Confidence::External);
}
self.resolve(module, segs, self_ty, ns)
}
fn resolve_internal(&self, module: &SymbolId, segs: &[String], self_ty: Option<&SymbolId>) -> Option<SymbolId> {
self.walk(module, segs, self_ty, Ns::Type, 0, true).filter(|id| self.internal.contains(id))
}
fn item(&self, module: &SymbolId, name: &str, ns: Ns) -> Option<&SymbolId> {
self.items.get(module).and_then(|m| m.get(name)).and_then(|s| s.get(ns))
}
fn walk(
&self,
module: &SymbolId,
segs: &[String],
self_ty: Option<&SymbolId>,
ns: Ns,
depth: u8,
use_globs: bool,
) -> Option<SymbolId> {
if depth > 8 || segs.is_empty() {
return None;
}
let first = segs[0].as_str();
let mut rest = &segs[1..];
let ns_at = |i: usize| if i + 1 == segs.len() { ns } else { Ns::Prefix };
let base: SymbolId = match first {
"crate" => module_sym(&[module.root().unwrap_or_default().into_owned()]),
"self" => module.clone(),
"super" => {
let mut m = module.parent().unwrap_or_else(|| module.clone());
while rest.first().map(String::as_str) == Some("super") {
m = m.parent().unwrap_or(m);
rest = &rest[1..];
}
m
}
"Self" => self_ty?.clone(),
name => {
if let Some(id) = self.item(module, name, ns_at(0)) {
id.clone()
} else if let Some(u) = self
.uses
.get(module)
.and_then(|us| us.iter().find(|u| u.alias == name && u.target.as_slice() != [name]))
{
let mut full = u.target.clone();
full.extend(rest.iter().cloned());
return self.walk(module, &full, self_ty, ns, depth + 1, use_globs).or_else(|| ids::path_id(&full));
} else if let Some(id) = use_globs.then(|| self.glob(module, name, ns_at(0))).flatten() {
id
} else if let Some(key) = module
.root()
.and_then(|from| self.plan.crate_named(&from, name))
.filter(|key| self.crates.contains(*key))
{
module_sym(&[key.to_owned()])
} else {
return ids::path_id(segs);
}
}
};
let offset = segs.len() - rest.len();
let mut cur = base;
for (i, seg) in rest.iter().enumerate() {
if let Some(id) = self.item(&cur, seg, ns_at(offset + i)) {
cur = id.clone();
} else if let Some(id) = self.methods.get(&cur).and_then(|m| m.get(seg)) {
cur = id.clone();
} else if self.uses.get(&cur).is_some_and(|us| us.iter().any(|u| &u.alias == seg)) {
return self.walk(&cur, &rest[i..], self_ty, ns, depth + 1, use_globs);
} else if i + 1 == rest.len()
&& self.internal.contains(&cur)
&& cur.last().is_some_and(|d| *d.suffix() == Suffix::Type)
{
cur = match ns {
Ns::Value => ids::method_id(&cur, None, seg),
Ns::Type | Ns::Prefix => ids::item_id(&cur, SymbolKind::TypeAlias, seg),
};
} else {
cur = cur.extend_path(seg);
}
}
Some(cur)
}
fn glob(&self, module: &SymbolId, name: &str, ns: Ns) -> Option<SymbolId> {
let mut queue: std::collections::VecDeque<&SymbolId> = self.globs.get(module)?.iter().collect();
let mut seen: BTreeSet<&SymbolId> = BTreeSet::from([module]);
while let Some(m) = queue.pop_front() {
if !seen.insert(m) {
continue;
}
if let Some(id) = self.item(m, name, ns) {
return Some(id.clone());
}
if let Some(next) = self.globs.get(m) {
queue.extend(next.iter());
}
}
None
}
fn refs(&self, module: &SymbolId, raw: &[Segs], self_ty: Option<&SymbolId>, params: InScope<'_>) -> Vec<SymbolId> {
let mut out = Vec::new();
for segs in raw {
if params.heads(segs) || (segs.len() > 1 && segs[0] == "Self") {
continue;
}
if segs.len() == 1 && PRELUDE.contains(&segs[0].as_str()) && !self.local(module, &segs[0]) {
continue;
}
let (id, _) = self.resolve(module, segs, self_ty, Ns::Type);
if (keep_ref(&id) || self.internal.contains(&id)) && Some(&id) != self_ty && !out.contains(&id) {
out.push(id);
}
}
out
}
fn local(&self, module: &SymbolId, name: &str) -> bool {
self.items.get(module).is_some_and(|m| m.contains_key(name))
|| self.uses.get(module).is_some_and(|us| us.iter().any(|u| u.alias == name))
}
fn member(&self, module: &SymbolId, m: &RawMember, owner: Option<&SymbolId>, params: InScope<'_>) -> Member {
Member {
name: m.name.clone(),
kind: m.kind,
ty: m.ty.clone(),
visibility: m.vis.clone(),
span: m.span,
refs: self.refs(module, &m.refs, owner, params),
}
}
fn add_uses(
&self,
cb: &mut Codebase,
from: &SymbolId,
module: &SymbolId,
raw: &[Segs],
self_ty: Option<&SymbolId>,
params: InScope<'_>,
) {
for t in self.refs(module, raw, self_ty, params) {
let c = if self.internal.contains(&t) { Confidence::Exact } else { Confidence::External };
cb.add_relation(Relation::new(from.clone(), t, RelationKind::Uses, c));
}
}
#[allow(clippy::too_many_arguments)]
fn method_symbol(
&self,
id: &SymbolId,
parent: &SymbolId,
m: &RawFn,
f: &RawFile,
ctx: &FlowCtx<'_>,
opts: &RustOptions,
cb: &mut Codebase,
) -> Symbol {
let mut s = Symbol::new(id.clone(), &m.name, SymbolKind::Method, f.path.clone());
s.parent = Some(parent.clone());
s.visibility = m.vis.clone();
s.span = m.span;
s.sig_hash = m.sig_hash;
s.body_hash = m.body_hash;
s.signature = Some(m.signature.clone());
s.doc = m.doc.clone();
s.generics = m.generics.clone();
s.tags = m.tags.clone();
let ctx = FlowCtx { params: ctx.params.with(&m.type_params), ..*ctx };
self.add_uses(cb, id, ctx.module, &m.sig_refs, ctx.self_ty, ctx.params);
if !m.flow.is_empty() {
s.flow = self.flow(&ctx, &m.flow, opts);
}
s
}
fn flow(&self, ctx: &FlowCtx<'_>, raw: &[RawStep], opts: &RustOptions) -> Option<Flow> {
lower::lower_flow(raw, &mut |c: &RawCall| self.call(ctx, c, opts))
}
fn call(&self, ctx: &FlowCtx<'_>, c: &RawCall, opts: &RustOptions) -> Option<Call> {
let (target, confidence) = match &c.callee {
Callee::Path(segs) => {
match segs[0].as_str() {
"Self" if ctx.self_ty.is_none() => return None,
"Self" | "self" => {}
first if PRELUDE.contains(&first) && !self.local(ctx.module, first) => return None,
_ => {}
}
let (id, c) = self.resolve_in(ctx.module, segs, ctx.self_ty, Ns::Value, ctx.params);
if segs.len() == 1 && c == Confidence::External && !self.local(ctx.module, &segs[0]) {
return None;
}
(id, c)
}
Callee::Method { recv, name } => self.method(ctx, recv, name)?,
};
let keep = opts.external_calls.keeps(confidence, || {
let root = target.root();
let root = root.as_deref().unwrap_or("");
keep_ref(&target) && root.starts_with(|c: char| c.is_ascii_lowercase()) && !self.is_crate(root)
});
keep.then(|| c.to_call(target, confidence))
}
fn method(&self, ctx: &FlowCtx<'_>, recv: &Recv, name: &str) -> Option<(SymbolId, Confidence)> {
let ty = match recv {
Recv::SelfValue => ctx.self_ty.cloned(),
Recv::SelfField(field) => match ctx.self_ty.and_then(|ty| self.fields.get(ty)?.field(field)) {
Some((module, params, refs)) => self.receiver_type(module, refs, None, params),
None => return Some(self.by_name_only(ctx, name)),
},
Recv::Typed(refs) => self.receiver_type(ctx.module, refs, ctx.self_ty, ctx.params),
Recv::Untyped => return Some(self.by_name_only(ctx, name)),
Recv::Derived(origin) => {
return Some(if self.internal_origin(ctx, origin) {
self.by_name_only(ctx, name)
} else {
(ids::unresolved_method_id(name), Confidence::External)
});
}
Recv::Returned(_) | Recv::Computed(_) | Recv::Unknown => None,
};
let Some(ty) = ty else { return Some((ids::unresolved_method_id(name), Confidence::External)) };
if let Some(id) = self.methods.get(&ty).and_then(|m| m.get(name)) {
return Some((id.clone(), Confidence::Exact));
}
if self.trait_methods.get(&ty).is_some_and(|n| n.contains(name)) {
return Some((ids::method_id(&ty, None, name), Confidence::Exact));
}
for tr in self.impls.get(&ty).into_iter().flatten() {
if self.trait_methods.get(tr).is_some_and(|n| n.contains(name)) {
return Some((ids::method_id(tr, None, name), Confidence::Exact));
}
}
Some((undefined_member(&ty, name), Confidence::External))
}
fn internal_origin(&self, ctx: &FlowCtx<'_>, origin: &Recv) -> bool {
let any_internal = |module: &SymbolId, refs: &[Segs], self_ty: Option<&SymbolId>, params: InScope<'_>| {
refs.iter().any(|segs| self.internal.contains(&self.resolve_in(module, segs, self_ty, Ns::Type, params).0))
};
match origin {
Recv::SelfValue | Recv::Untyped => true,
Recv::SelfField(field) => match ctx.self_ty.and_then(|ty| self.fields.get(ty)?.field(field)) {
Some((module, params, refs)) => any_internal(module, refs, None, params),
None => true,
},
Recv::Typed(refs) => any_internal(ctx.module, refs, ctx.self_ty, ctx.params),
Recv::Returned(segs) => {
self.internal.contains(&self.resolve_in(ctx.module, segs, ctx.self_ty, Ns::Value, ctx.params).0)
}
Recv::Derived(inner) | Recv::Computed(inner) => self.internal_origin(ctx, inner),
Recv::Unknown => false,
}
}
fn receiver_type(
&self,
module: &SymbolId,
refs: &[Segs],
self_ty: Option<&SymbolId>,
params: InScope<'_>,
) -> Option<SymbolId> {
const DEREF: &[&str] =
&["Box", "Arc", "Rc", "Cow", "Pin", "Ref", "RefMut", "MutexGuard", "RwLockReadGuard", "RwLockWriteGuard"];
for segs in refs {
let last = segs.last()?.as_str();
if segs.len() == 1 && params.contains(last) {
return None;
}
let (id, _) = self.resolve_in(module, segs, self_ty, Ns::Type, params);
if DEREF.contains(&last) && !self.internal.contains(&id) {
continue;
}
return Some(id);
}
None
}
fn by_name_only(&self, ctx: &FlowCtx<'_>, name: &str) -> (SymbolId, Confidence) {
if !COMMON_METHODS.contains(&name) {
let from = ctx.module.root();
let from = from.as_deref().unwrap_or("");
let mut reachable = self
.by_name
.get(name)
.into_iter()
.flatten()
.filter(|id| id.root().is_some_and(|to| self.plan.reaches(from, &to)));
if let (Some(id), None) = (reachable.next(), reachable.next()) {
return (id.clone(), Confidence::Inferred);
}
}
(ids::unresolved_method_id(name), Confidence::External)
}
}