use std::collections::BTreeSet;
use super::item_head::{
angle_group, angle_group_end, attribute_path, cfg_attr_metas, heads_a_path,
opens_a_qualified_path, opens_an_angle_group, variant_positions,
};
use super::token_tree::{Delimiter, Kind, Node, TokenTree};
pub(super) struct Occurrence {
pub at: usize,
pub segments: String,
pub is_call: bool,
}
pub(super) struct PathRun {
pub segments: Vec<String>,
pub end: usize,
pub turbofishes: Vec<usize>,
}
pub(super) fn path_run(tree: &TokenTree, head: usize) -> PathRun {
let mut segments = vec![tree.text(head).to_string()];
let mut turbofishes = Vec::new();
let mut j = head + 1;
while tree.is(j, "::") {
if heads_a_path(tree, j + 1) {
segments.push(tree.text(j + 1).to_string());
j += 2;
} else if opens_an_angle_group(tree, j + 1) {
match angle_group_end(tree, j + 1) {
Some(close) => {
turbofishes.push(j + 1);
j = close + 1;
}
None => break,
}
} else {
break;
}
}
PathRun {
segments,
end: j,
turbofishes,
}
}
const ITEM_KEYWORDS: [&str; 9] = [
"fn", "struct", "enum", "union", "trait", "type", "mod", "const", "static",
];
fn names_a_single_segment_path(tree: &TokenTree, head: usize, end: usize) -> bool {
matches!(tree.kind(head), Kind::Ident | Kind::RawIdent)
&& !head
.checked_sub(1)
.is_some_and(|before| ITEM_KEYWORDS.contains(&tree.text(before)))
&& !(tree.is(end, ":") || tree.is(end, "!") || tree.is(end, "@"))
}
pub(super) fn occurrences(tree: &TokenTree, statements: &[(usize, usize)]) -> Vec<Occurrence> {
let mut scan = Scan {
tree,
variants: variant_positions(tree),
qualified_tails: BTreeSet::new(),
work: vec![(0, tree.len())],
out: Vec::new(),
};
while let Some((from, to)) = scan.work.pop() {
scan.read(from, to);
}
scan.out.retain(|occurrence| {
let before = statements.partition_point(|&(at, _)| at <= occurrence.at);
before == 0 || statements[before - 1].1 < occurrence.at
});
scan.out
}
struct Scan<'t, 's> {
tree: &'t TokenTree<'s>,
variants: BTreeSet<usize>,
qualified_tails: BTreeSet<usize>,
work: Vec<(usize, usize)>,
out: Vec<Occurrence>,
}
fn inner_qualified_tail(tree: &TokenTree, shift: usize) -> Option<usize> {
let group = angle_group(tree, shift)?;
let inner = group.inner_close?;
(inner != group.close && tree.is(inner + 1, "::")).then_some(inner + 1)
}
fn attribute_arguments(tree: &TokenTree, open: usize, close: usize) -> Vec<(usize, usize)> {
let mut ranges = Vec::new();
let mut metas = vec![(open + 1, close)];
while let Some((start, end)) = metas.pop() {
match attribute_path(tree, start, end) {
(Some("cfg"), _) => {}
(Some("cfg_attr"), after) if tree.kind(after) == Kind::Open(Delimiter::Parenthesis) => {
metas.extend(cfg_attr_metas(tree, after));
}
(_, after) => ranges.push((after, end)),
}
}
ranges
}
impl Scan<'_, '_> {
fn turbofish(&mut self, open: usize) -> Option<usize> {
let close = angle_group_end(self.tree, open)?;
if self.tree.is(open, "<<") {
self.qualified_tails
.extend(inner_qualified_tail(self.tree, open));
}
self.work.push((open + 1, close));
Some(close)
}
fn read(&mut self, from: usize, to: usize) {
let tree = self.tree;
let mut i = from;
while i < to {
if let Node::Attribute { open, close, .. } = tree.node_at(i) {
self.work.extend(attribute_arguments(tree, open, close));
i = close + 1;
continue;
}
if tree.is(i, ".") {
i += 1;
if i < to && tree.kind(i) == Kind::Literal && tree.text(i).ends_with('.') {
i += 1;
}
if i < to && matches!(tree.kind(i), Kind::Ident | Kind::RawIdent | Kind::Keyword) {
i += 1;
if tree.is(i, "::") && opens_an_angle_group(tree, i + 1) {
if let Some(close) = self.turbofish(i + 1) {
i = close + 1;
}
}
}
continue;
}
if tree.is(i, "<") || tree.is(i, "<<") {
if let Some(tail) = opens_a_qualified_path(tree, i) {
self.qualified_tails.insert(tail);
if tree.is(i, "<<") {
self.qualified_tails.extend(inner_qualified_tail(tree, i));
}
}
i += 1;
continue;
}
let (start, head, rooted) = if tree.is(i, "::") && heads_a_path(tree, i + 1) {
if self.qualified_tails.contains(&i) {
let tail = path_run(tree, i + 1);
for open in tail.turbofishes {
self.turbofish(open);
}
i = tail.end;
continue;
}
(i, i + 1, true)
} else if heads_a_path(tree, i) {
(i, i, false)
} else {
i += 1;
continue;
};
let PathRun {
segments,
end,
turbofishes,
} = path_run(tree, head);
for open in turbofishes {
self.turbofish(open);
}
let defines = !rooted
&& (self.variants.contains(&head)
|| head
.checked_sub(1)
.is_some_and(|p| tree.is(p, "fn") || tree.is(p, "struct")));
let applied = end < to && tree.kind(end) == Kind::Open(Delimiter::Parenthesis);
let is_call = applied && !defines;
if is_call
|| (!defines
&& (rooted
|| segments.len() > 1
|| names_a_single_segment_path(tree, head, end)))
{
let joined = segments.join("::");
self.out.push(Occurrence {
at: start,
segments: if rooted {
format!("::{joined}")
} else {
joined
},
is_call,
});
}
i = end;
}
}
}
#[cfg(test)]
mod tests {
use super::super::token_tree::Edition;
use super::*;
fn read(source: &str) -> Vec<(String, bool)> {
let tree = TokenTree::lex(source, Edition::Rust2018);
let spans: Vec<(usize, usize)> = super::super::use_tree::use_statements(&tree)
.iter()
.map(|statement| (statement.at, statement.end))
.collect();
occurrences(&tree, &spans)
.into_iter()
.map(|o| (o.segments, o.is_call))
.collect()
}
fn calls(source: &str) -> Vec<String> {
read(source)
.into_iter()
.filter(|(_, is_call)| *is_call)
.map(|(path, _)| path)
.collect()
}
#[test]
fn a_role_is_read_from_the_tokens_beside_a_run() {
assert_eq!(
read("fn f() { a::b(); Vec::<u8>::new(); ::std::x::y; x.m(); P(1); r#match(x) }"),
[
("a::b".to_string(), true),
("Vec::new".to_string(), true),
("::std::x::y".to_string(), false),
("x".to_string(), false),
("P".to_string(), true),
("match".to_string(), true),
("x".to_string(), false),
("u8".to_string(), false),
]
);
}
#[test]
fn a_single_identifier_is_a_mention_only_where_it_names_something() {
assert_eq!(
read(
"fn f(p: Instant) { let g: fn() = now; let mut m = 1; let _ = &mut Clock; for k in v {} S { field: 1 }; m!(); self.x; }\n\
struct S { field: u8 } const C: u8 = 1; static D: u8 = 1; type T = U; mod n {} trait R {}"
),
[
("Instant".to_string(), false),
("now".to_string(), false),
("m".to_string(), false),
("Clock".to_string(), false),
("k".to_string(), false),
("v".to_string(), false),
("S".to_string(), false),
("u8".to_string(), false),
("u8".to_string(), false),
("u8".to_string(), false),
("U".to_string(), false),
]
);
}
#[test]
fn a_definition_is_not_a_call() {
let read = read("pub fn g() {} pub struct P(pub u8); enum E { A(u8), #[x] B(u8) }");
assert!(
read.iter().all(|(path, is_call)| path == "u8" && !is_call),
"{read:?}"
);
}
#[test]
fn a_qualified_paths_tail_is_not_read_and_a_comparison_opens_no_group() {
assert!(calls("fn g() { <W>::md5x(); return <W>::f(); }").is_empty());
assert_eq!(
calls(
"fn g() { a < b && c > ::std::process::id(); x.await < b && c > ::std::process::id() }"
),
["::std::process::id", "::std::process::id"]
);
assert_eq!(
calls("fn g() { let b = { 1 } < n && k > ::std::process::id(); }"),
["::std::process::id"]
);
}
#[test]
fn an_attributes_name_and_predicate_are_passed_over() {
assert!(
calls("#[cfg(any(unix, not(x)))] #![allow(y)] #[cfg_attr(a::b(), c::d(e))] fn g() {}")
.is_empty()
);
assert_eq!(calls("#[m::attr(x = a::b())] fn g() {}"), ["a::b"]);
}
}