use std::cell::{Cell, RefCell};
use std::collections::HashMap;
use purrdf_hash::fixed::FixedState;
use std::mem::size_of;
use std::ops::Range;
use std::sync::Arc;
use quote::ToTokens as _;
use syn::parse::Parser as _;
use syn::spanned::Spanned as _;
use syn::visit::Visit;
use syn::{Attribute, Expr, ExprLit, Item, Lit, Meta, Visibility};
use crate::cfg::{CfgExpr, parse_list};
use crate::meter::FactAllowance;
use crate::verbatim::{self, Bodiless, Verbatim};
use crate::work::{self, Work};
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct SpanFact {
pub lines: Range<usize>,
pub start_byte: usize,
pub end_byte: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[non_exhaustive]
pub enum TestRole {
None,
CfgTest,
TestFn,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Vis {
Inherited,
Public,
Restricted(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ItemKind {
Fn,
Struct,
Enum,
Union,
Trait,
TypeAlias,
Const,
UnnamedConst,
Static,
MacroRules,
ExternCrate,
ForeignFn,
ForeignStatic,
ForeignType,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct PathFact {
pub(crate) segments: Vec<String>,
pub(crate) leading_colon: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum FieldShape {
Unit,
Tuple,
Named,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct AttrFact {
pub(crate) path: String,
pub(crate) args: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct ItemDetail {
pub(crate) bindings: Vec<String>,
pub(crate) initializer: Option<Range<usize>>,
pub(crate) nominal_const_arity: Option<usize>,
pub(crate) alias_const_arity: Option<usize>,
pub(crate) method_requires_inference: bool,
pub(crate) variants: Option<Box<[VariantFact]>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct VariantFact {
pub(crate) name: String,
pub(crate) shape: FieldShape,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ItemFact {
pub(crate) name: String,
pub(crate) renamed_from: Option<String>,
pub(crate) kind: ItemKind,
pub(crate) name_line: usize,
pub(crate) span: SpanFact,
pub(crate) body: Option<SpanFact>,
pub(crate) detail: Option<Arc<ItemDetail>>,
pub(crate) vis: Vis,
pub(crate) attrs: Vec<AttrFact>,
pub(crate) documented: bool,
pub(crate) doc_hidden: bool,
pub(crate) test: TestRole,
pub(crate) macro_export: bool,
pub(crate) members: Vec<ItemFact>,
pub(crate) shape: Option<FieldShape>,
pub(crate) alias: Option<PathFact>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct PathAttrs {
pub(crate) unconditional: Vec<String>,
pub(crate) conditional: Vec<ConditionalPath>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ConditionalPath {
pub(crate) predicate: String,
pub(crate) implies_test: bool,
pub(crate) path: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ModFact {
pub(crate) name: String,
pub(crate) name_line: usize,
pub(crate) name_start: usize,
pub(crate) span: SpanFact,
pub(crate) body_span: Option<SpanFact>,
pub(crate) vis: Vis,
pub(crate) attrs: Vec<AttrFact>,
pub(crate) documented: bool,
pub(crate) doc_hidden: bool,
pub(crate) test: TestRole,
pub(crate) paths: PathAttrs,
pub(crate) inline: Option<ModBody>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum UseTree {
Path {
name: String,
rest: Box<UseTree>,
},
Name(String),
Rename {
name: String,
alias: String,
},
Glob,
Group(Vec<UseTree>),
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct UseFact {
pub(crate) line: usize,
pub(crate) span: SpanFact,
pub(crate) vis: Vis,
pub(crate) leading_colon: bool,
pub(crate) tree: UseTree,
pub(crate) test: TestRole,
pub(crate) doc_hidden: bool,
pub(crate) configuration: ConfigurationDependence,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ConfigurationDependence {
Unconditional,
Conditional,
}
impl ConfigurationDependence {
pub(crate) fn of(attrs: &[AttrFact]) -> Self {
if attrs
.iter()
.any(|attr| matches!(attr.path.as_str(), "cfg" | "cfg_attr"))
{
Self::Conditional
} else {
Self::Unconditional
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ImplFact {
pub(crate) line: usize,
pub(crate) span: SpanFact,
pub(crate) body: SpanFact,
pub(crate) trait_path: Option<String>,
pub(crate) trait_segments: Option<PathFact>,
pub(crate) self_type: String,
pub(crate) self_path: Option<Vec<String>>,
pub(crate) self_leading_colon: bool,
pub(crate) generics: Vec<String>,
pub(crate) unconditional_self: Option<usize>,
pub(crate) items: Vec<ItemFact>,
pub(crate) test: TestRole,
}
fn nominal_const_arity(generics: &syn::Generics) -> Option<usize> {
(generics.where_clause.is_none()
&& generics.params.iter().all(|parameter| matches!(parameter, syn::GenericParam::Const(parameter) if parameter.attrs.is_empty())))
.then_some(generics.params.len())
}
fn has_written_variant_roster(attrs: &[AttrFact]) -> bool {
attrs.iter().all(|attribute| {
matches!(
attribute.path.as_str(),
"allow"
| "cfg"
| "deny"
| "deprecated"
| "derive"
| "doc"
| "expect"
| "forbid"
| "must_use"
| "non_exhaustive"
| "repr"
| "warn"
)
})
}
fn alias_const_arity(alias: &syn::ItemType) -> Option<usize> {
if !alias.generics.params.is_empty() || alias.generics.where_clause.is_some() {
return None;
}
let syn::Type::Path(path) = alias.ty.as_ref() else {
return None;
};
if path.qself.is_some()
|| path
.path
.segments
.iter()
.rev()
.skip(1)
.any(|segment| !matches!(segment.arguments, syn::PathArguments::None))
{
return None;
}
match &path.path.segments.last()?.arguments {
syn::PathArguments::None => Some(0),
syn::PathArguments::AngleBracketed(arguments) if arguments.args.iter().all(|argument| matches!(argument, syn::GenericArgument::Const(Expr::Lit(value)) if value.attrs.is_empty())) => Some(arguments.args.len()),
_ => None,
}
}
fn unconditional_self(block: &syn::ItemImpl) -> bool {
if block.trait_.is_some() || block.generics.where_clause.is_some() {
return false;
}
let syn::Type::Path(path) = block.self_ty.as_ref() else {
return false;
};
if path.qself.is_some()
|| path
.path
.segments
.iter()
.rev()
.skip(1)
.any(|segment| !matches!(segment.arguments, syn::PathArguments::None))
{
return false;
}
let Some(last) = path.path.segments.last() else {
return false;
};
let arguments = match &last.arguments {
syn::PathArguments::None => return block.generics.params.is_empty(),
syn::PathArguments::AngleBracketed(arguments) => &arguments.args,
syn::PathArguments::Parenthesized(_) => return false,
};
if arguments.len() != block.generics.params.len() {
return false;
}
arguments
.iter()
.zip(&block.generics.params)
.all(|(argument, parameter)| {
let syn::GenericParam::Const(parameter) = parameter else {
return false;
};
if !parameter.attrs.is_empty() || parameter.default.is_some() {
return false;
}
let path = match argument {
syn::GenericArgument::Type(syn::Type::Path(path)) if path.qself.is_none() => {
&path.path
}
syn::GenericArgument::Const(Expr::Path(path))
if path.qself.is_none() && path.attrs.is_empty() =>
{
&path.path
}
_ => return false,
};
path.is_ident(¶meter.ident)
})
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct UnexpandedFact {
pub(crate) invoked: String,
pub(crate) span: SpanFact,
pub(crate) test: TestRole,
pub(crate) identifiers: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct ModBody {
pub(crate) items: Vec<ItemFact>,
pub(crate) mods: Vec<ModFact>,
pub(crate) blocks: Vec<ModFact>,
pub(crate) uses: Vec<UseFact>,
pub(crate) impls: Vec<ImplFact>,
pub(crate) unexpanded: Vec<UnexpandedFact>,
pub(crate) disabled: Vec<SpanFact>,
}
impl ModFact {
pub(crate) fn block_segment(&self) -> String {
format!("{{{}@{}}}", self.name, self.name_start)
}
}
impl ModBody {
pub(crate) fn declarations(&self) -> usize {
self.mods.len() + self.blocks.len()
}
pub(crate) fn declaration(&self, position: usize) -> Option<&ModFact> {
self.mods.get(position).or_else(|| {
position
.checked_sub(self.mods.len())
.and_then(|local| self.blocks.get(local))
})
}
pub(crate) fn is_block_local(&self, position: usize) -> bool {
position >= self.mods.len()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub(crate) struct FileFacts {
pub(crate) body: ModBody,
pub(crate) documented: bool,
pub(crate) doc_hidden: bool,
pub(crate) attrs: Vec<AttrFact>,
}
#[derive(Debug)]
pub(crate) enum ExtractError {
Cfg { line: usize, message: String },
PathAttr {
line: usize,
module: Option<String>,
predicate: Option<String>,
message: String,
},
Unsupported { line: usize, what: String },
Position { line: usize },
Budget { line: usize, limit: usize },
}
type Found<T> = Result<T, ExtractError>;
const MARK_EVERY: usize = 64;
enum LineIndex {
Ascii,
Marks(Vec<usize>),
}
struct Positions<'t> {
text: &'t str,
starts: Vec<usize>,
long: RefCell<HashMap<usize, LineIndex, FixedState>>,
}
impl<'t> Positions<'t> {
fn new(text: &'t str) -> Self {
let mut starts = vec![0];
starts.extend(text.match_indices('\n').map(|(at, _)| at + 1));
Self {
text,
starts,
long: RefCell::new(HashMap::with_hasher(FixedState::new())),
}
}
fn byte(&self, line: usize, column: usize) -> Found<usize> {
work::add(Work::Positions, 1);
let outside = || ExtractError::Position { line };
let index = line.checked_sub(1).ok_or_else(outside)?;
let start = *self.starts.get(index).ok_or_else(outside)?;
let end = self
.starts
.get(index + 1)
.copied()
.unwrap_or(self.text.len());
let own = self.text.get(start..end).ok_or_else(outside)?;
let (base, rest, skip) = if own.len() <= MARK_EVERY {
(0, own, column)
} else {
let mut long = self.long.borrow_mut();
let entry = long.entry(index).or_insert_with(|| {
if own.is_ascii() {
work::add(Work::ColumnSteps, own.len());
LineIndex::Ascii
} else {
LineIndex::Marks(
own.char_indices()
.inspect(|_| work::add(Work::ColumnSteps, 1))
.step_by(MARK_EVERY)
.map(|(at, _)| at)
.collect(),
)
}
});
match entry {
LineIndex::Ascii => {
return if column <= own.len() {
Ok(start + column)
} else {
Err(outside())
};
}
LineIndex::Marks(marks) => {
let mark = (column / MARK_EVERY).min(marks.len().saturating_sub(1));
let base = marks.get(mark).copied().unwrap_or(0);
let rest = own.get(base..).ok_or_else(outside)?;
(base, rest, column - mark * MARK_EVERY)
}
}
};
let mut chars = rest
.char_indices()
.inspect(|_| work::add(Work::ColumnSteps, 1));
if let Some((offset, _)) = chars.nth(skip) {
return Ok(start + base + offset);
}
let all = rest.chars().count();
work::add(Work::ColumnSteps, all);
if all == skip {
Ok(start + base + rest.len())
} else {
Err(outside())
}
}
fn span(&self, span: proc_macro2::Span) -> Found<SpanFact> {
let (start, end) = (span.start(), span.end());
Ok(SpanFact {
lines: start.line..end.line + 1,
start_byte: self.byte(start.line, start.column)?,
end_byte: self.byte(end.line, end.column)?,
})
}
}
struct Bindings<F> {
visit: F,
expressions: bool,
}
impl<'ast, F: FnMut(&syn::Ident)> Visit<'ast> for Bindings<F> {
fn visit_pat(&mut self, pattern: &'ast syn::Pat) {
if !self.expressions {
match pattern {
syn::Pat::Const(_) => return,
syn::Pat::Guard(guard) => {
self.visit_pat(&guard.pat);
return;
}
_ => {}
}
}
syn::visit::visit_pat(self, pattern);
}
fn visit_pat_ident(&mut self, pat: &'ast syn::PatIdent) {
(self.visit)(&pat.ident);
syn::visit::visit_pat_ident(self, pat);
}
fn visit_type(&mut self, _: &'ast syn::Type) {}
fn visit_expr(&mut self, expression: &'ast Expr) {
if self.expressions {
syn::visit::visit_expr(self, expression);
}
}
}
pub fn visit_pattern_bindings(pattern: &syn::Pat, visit: impl FnMut(&syn::Ident)) {
Bindings {
visit,
expressions: false,
}
.visit_pat(pattern);
}
struct FactMeter {
allowance: FactAllowance,
spent: Cell<usize>,
}
impl FactMeter {
fn scratch() -> Self {
Self {
allowance: FactAllowance {
spent: 0,
limit: usize::MAX,
},
spent: Cell::new(0),
}
}
fn charge(&self, bytes: usize, line: usize) -> Found<()> {
let spent = self.spent.get().saturating_add(bytes);
self.spent.set(spent);
if self.allowance.spent.saturating_add(spent) > self.allowance.limit {
Err(ExtractError::Budget {
line,
limit: self.allowance.limit,
})
} else {
Ok(())
}
}
}
fn strings_bytes(strings: &[String]) -> usize {
strings
.iter()
.map(|text| size_of::<String>() + text.capacity())
.sum()
}
fn vis_bytes(vis: &Vis) -> usize {
match vis {
Vis::Restricted(text) => text.capacity(),
Vis::Inherited | Vis::Public => 0,
}
}
fn fit(mut text: String) -> String {
text.shrink_to_fit();
text
}
fn slack<T>(list: &mut Vec<T>) -> usize {
list.shrink_to_fit();
(list.capacity() - list.len()) * size_of::<T>()
}
fn unraw(ident: &syn::Ident) -> String {
let text = ident.to_string();
fit(match text.strip_prefix("r#") {
Some(plain) => plain.to_owned(),
None => text,
})
}
fn vis(visibility: &Visibility) -> Vis {
match visibility {
Visibility::Public(_) => Vis::Public,
Visibility::Restricted(restricted) => {
let path = restricted.path.to_token_stream().to_string();
Vis::Restricted(fit(if restricted.in_token.is_some() {
format!("in {path}")
} else {
path
}))
}
Visibility::Inherited => Vis::Inherited,
}
}
struct AttrSummary {
facts: Vec<AttrFact>,
documented: bool,
doc_hidden: bool,
own_test: TestRole,
macro_export: bool,
paths: PathAttrs,
}
fn string_value(expr: &Expr) -> Option<String> {
match expr {
Expr::Lit(ExprLit {
lit: Lit::Str(value),
..
}) => Some(value.value()),
_ => None,
}
}
fn path_text(path: &syn::Path) -> String {
path.segments
.iter()
.map(|segment| segment.ident.to_string())
.collect::<Vec<_>>()
.join("::")
}
fn cfg_error(line: usize, message: String) -> ExtractError {
ExtractError::Cfg { line, message }
}
fn path_error(line: usize, predicate: Option<&CfgExpr>, message: String) -> ExtractError {
ExtractError::PathAttr {
line,
module: None,
predicate: predicate.map(ToString::to_string),
message,
}
}
fn path_value(expr: &Expr, line: usize, predicate: Option<&CfgExpr>) -> Found<String> {
string_value(expr).ok_or_else(|| {
let written: String = expr
.to_token_stream()
.to_string()
.chars()
.take(60)
.collect();
path_error(
line,
predicate,
format!("the value of `path` is not a string literal: `{written}`"),
)
})
}
fn cfg_predicate(tokens: &proc_macro2::TokenStream, line: usize) -> Found<CfgExpr> {
let meta = crate::cfg::parse_meta
.parse2(tokens.clone())
.map_err(|error| cfg_error(line, error.to_string()))?;
CfgExpr::from_meta(&meta).map_err(|message| cfg_error(line, message))
}
fn apply_cfg_attr(
tokens: &proc_macro2::TokenStream,
outer: Option<&CfgExpr>,
summary: &mut AttrSummary,
line: usize,
meter: &FactMeter,
) -> Found<()> {
let metas = parse_list(tokens).map_err(|message| cfg_error(line, message))?;
let Some((first, applied)) = metas.split_first() else {
return Err(cfg_error(line, "`cfg_attr` has no predicate".to_owned()));
};
let own = CfgExpr::from_meta(first).map_err(|message| cfg_error(line, message))?;
let combined = match outer {
Some(outer) => CfgExpr::All(vec![outer.clone(), own]),
None => own,
};
for meta in applied {
match meta {
Meta::NameValue(pair) if pair.path.is_ident("path") => {
let conditional = ConditionalPath {
predicate: fit(combined.to_string()),
implies_test: combined.implies_test(),
path: fit(path_value(&pair.value, line, Some(&combined))?),
};
meter.charge(
size_of::<ConditionalPath>()
+ conditional.predicate.capacity()
+ conditional.path.capacity(),
line,
)?;
summary.paths.conditional.push(conditional);
}
Meta::List(list) if list.path.is_ident("cfg_attr") => {
apply_cfg_attr(&list.tokens, Some(&combined), summary, line, meter)?;
}
Meta::List(list) if list.path.is_ident("cfg") => {
let inner = cfg_predicate(&list.tokens, line)?;
let both = CfgExpr::All(vec![combined.clone(), inner]);
if both.implies_test() && summary.own_test != TestRole::TestFn {
summary.own_test = TestRole::CfgTest;
}
}
Meta::Path(path) if path.is_ident("test") => summary.own_test = TestRole::TestFn,
Meta::Path(_) | Meta::List(_) | Meta::NameValue(_) => {}
}
}
Ok(())
}
fn is_include_str(expr: &Expr) -> bool {
matches!(expr, Expr::Macro(call) if call.mac.path.segments.last().is_some_and(|s| s.ident == "include_str"))
}
fn summarize(attrs: &[Attribute], meter: &FactMeter) -> Found<AttrSummary> {
let mut summary = AttrSummary {
facts: Vec::new(),
documented: false,
doc_hidden: false,
own_test: TestRole::None,
macro_export: false,
paths: PathAttrs::default(),
};
for attr in attrs {
work::add(Work::Attributes, 1);
let line = attr.span().start().line;
let path = fit(path_text(attr.path()));
let args = fit(match &attr.meta {
Meta::Path(_) => String::new(),
Meta::List(list) => format!("({})", list.tokens),
Meta::NameValue(pair) => format!("= {}", pair.value.to_token_stream()),
});
meter.charge(
size_of::<AttrFact>() + path.capacity() + args.capacity(),
line,
)?;
summary.facts.push(AttrFact {
path: path.clone(),
args,
});
match (path.as_str(), &attr.meta) {
("doc", Meta::NameValue(pair)) => {
if is_include_str(&pair.value)
|| string_value(&pair.value).is_some_and(|text| !text.trim().is_empty())
{
summary.documented = true;
}
}
("doc", Meta::List(list)) => {
if list.tokens.to_string().trim() == "hidden" {
summary.doc_hidden = true;
}
}
("macro_export", _) => summary.macro_export = true,
("test", Meta::Path(_)) => summary.own_test = TestRole::TestFn,
("test", _) => {
return Err(cfg_error(line, "`test` takes no arguments".to_owned()));
}
("path", Meta::NameValue(pair)) => {
let value = fit(path_value(&pair.value, line, None)?);
meter.charge(size_of::<String>() + value.capacity(), line)?;
summary.paths.unconditional.push(value);
}
("path", _) => {
return Err(path_error(
line,
None,
"`path` must be `path = \"..\"`".to_owned(),
));
}
("cfg", Meta::List(list)) => {
let predicate = cfg_predicate(&list.tokens, line)?;
if predicate.implies_test() && summary.own_test != TestRole::TestFn {
summary.own_test = TestRole::CfgTest;
}
}
("cfg", _) => {
return Err(cfg_error(line, "`cfg` must be `cfg(predicate)`".to_owned()));
}
("cfg_attr", Meta::List(list)) => {
apply_cfg_attr(&list.tokens, None, &mut summary, line, meter)?;
}
("cfg_attr", _) => {
return Err(cfg_error(
line,
"`cfg_attr` must be `cfg_attr(predicate, attributes)`".to_owned(),
));
}
_ => {}
}
}
let line = attrs.last().map_or(1, |attr| attr.span().start().line);
let room = slack(&mut summary.facts)
+ slack(&mut summary.paths.unconditional)
+ slack(&mut summary.paths.conditional);
meter.charge(room, line)?;
Ok(summary)
}
fn role(own: TestRole, inherited: TestRole) -> TestRole {
if own == TestRole::TestFn {
TestRole::TestFn
} else if own == TestRole::CfgTest || inherited != TestRole::None {
TestRole::CfgTest
} else {
TestRole::None
}
}
fn self_path_of(ty: &syn::Type) -> Option<&syn::Path> {
let mut ty = ty;
loop {
match ty {
syn::Type::Paren(inner) => ty = &inner.elem,
syn::Type::Group(inner) => ty = &inner.elem,
syn::Type::Path(path) if path.qself.is_none() => return Some(&path.path),
syn::Type::TraitObject(object) => {
return object.bounds.iter().find_map(|bound| match bound {
syn::TypeParamBound::Trait(found) => Some(&found.path),
_ => None,
});
}
_ => return None,
}
}
}
fn unsupported(item: &impl quote::ToTokens) -> ExtractError {
ExtractError::Unsupported {
line: item.span().start().line,
what: item
.to_token_stream()
.to_string()
.chars()
.take(60)
.collect(),
}
}
fn use_role(attrs: &[Attribute], inherited: TestRole) -> Found<UseRole> {
let summary = summarize(attrs, &FactMeter::scratch())?;
Ok(UseRole {
test: role(summary.own_test, inherited),
doc_hidden: summary.doc_hidden,
configuration: ConfigurationDependence::of(&summary.facts),
})
}
#[derive(Debug, Clone, Copy)]
struct UseRole {
test: TestRole,
doc_hidden: bool,
configuration: ConfigurationDependence,
}
struct Extractor<'t> {
positions: Positions<'t>,
meter: FactMeter,
}
impl Extractor<'_> {
#[expect(
clippy::too_many_arguments,
reason = "every field of an item fact comes from the syntax node separately"
)]
fn item(
&self,
name: &syn::Ident,
kind: ItemKind,
vis_of: &Visibility,
attrs: &[Attribute],
whole: proc_macro2::Span,
body: Option<proc_macro2::Span>,
inherited: TestRole,
members: Vec<ItemFact>,
) -> Found<ItemFact> {
let name_line = name.span().start().line;
let summary = summarize(attrs, &self.meter)?;
let name = unraw(name);
let vis = vis(vis_of);
self.meter.charge(
size_of::<ItemFact>() + name.capacity() + vis_bytes(&vis),
name_line,
)?;
Ok(ItemFact {
name,
renamed_from: None,
kind,
name_line,
span: self.positions.span(whole)?,
body: body.map(|span| self.positions.span(span)).transpose()?,
detail: None,
vis,
attrs: summary.facts,
documented: summary.documented,
doc_hidden: summary.doc_hidden,
test: role(summary.own_test, inherited),
macro_export: summary.macro_export,
members,
shape: None,
alias: None,
})
}
fn with_bindings(
&self,
mut fact: ItemFact,
sig: &syn::Signature,
method: bool,
) -> Found<ItemFact> {
let mut names = Vec::new();
for input in &sig.inputs {
match input {
syn::FnArg::Receiver(_) => names.push("self".to_owned()),
syn::FnArg::Typed(typed) => {
Bindings {
visit: |ident: &syn::Ident| names.push(ident.to_string()),
expressions: true,
}
.visit_pat(&typed.pat);
}
}
}
let method_requires_inference =
method && (!sig.generics.params.is_empty() || sig.generics.where_clause.is_some());
if !names.is_empty() || method_requires_inference {
self.meter.charge(
size_of::<ItemDetail>() + strings_bytes(&names),
fact.name_line,
)?;
fact.detail = Some(Arc::new(ItemDetail {
bindings: names,
method_requires_inference,
..ItemDetail::default()
}));
}
Ok(fact)
}
fn with_initializer(
&self,
mut fact: ItemFact,
eq: proc_macro2::Span,
semi: proc_macro2::Span,
) -> Found<ItemFact> {
let start = self.positions.span(eq)?.end_byte;
let end = self.positions.span(semi)?.start_byte;
self.meter.charge(size_of::<ItemDetail>(), fact.name_line)?;
fact.detail = Some(Arc::new(ItemDetail {
initializer: Some(start..end),
..ItemDetail::default()
}));
Ok(fact)
}
fn module(&self, items: &[Item], inherited: TestRole, line: usize) -> Found<ModBody> {
let mut body = ModBody::default();
for item in items {
self.one(item, inherited, &mut body)?;
}
let room = slack(&mut body.items)
+ slack(&mut body.mods)
+ slack(&mut body.blocks)
+ slack(&mut body.uses)
+ slack(&mut body.impls)
+ slack(&mut body.unexpanded)
+ slack(&mut body.disabled);
self.meter.charge(room, line)?;
Ok(body)
}
fn with_nominal(&self, mut fact: ItemFact, generics: &syn::Generics) -> Found<ItemFact> {
let Some(arity) = nominal_const_arity(generics) else {
return Ok(fact);
};
self.meter.charge(size_of::<ItemDetail>(), fact.name_line)?;
fact.detail = Some(Arc::new(ItemDetail {
nominal_const_arity: Some(arity),
..ItemDetail::default()
}));
Ok(fact)
}
fn enum_fact(
&self,
declaration: &syn::ItemEnum,
whole: proc_macro2::Span,
inherited: TestRole,
) -> Found<ItemFact> {
let mut fact = self.item(
&declaration.ident,
ItemKind::Enum,
&declaration.vis,
&declaration.attrs,
whole,
Some(declaration.brace_token.span.join()),
inherited,
Vec::new(),
)?;
if !has_written_variant_roster(&fact.attrs) {
return self.with_nominal(fact, &declaration.generics);
}
self.meter.charge(
size_of::<ItemDetail>() + declaration.variants.len() * size_of::<VariantFact>(),
fact.name_line,
)?;
let mut variants = Vec::with_capacity(declaration.variants.len());
for variant in &declaration.variants {
let name = unraw(&variant.ident);
self.meter.charge(name.capacity(), fact.name_line)?;
let shape = match variant.fields {
syn::Fields::Named(_) => FieldShape::Named,
syn::Fields::Unnamed(_) => FieldShape::Tuple,
syn::Fields::Unit => FieldShape::Unit,
};
variants.push(VariantFact { name, shape });
}
fact.detail = Some(Arc::new(ItemDetail {
nominal_const_arity: nominal_const_arity(&declaration.generics),
variants: Some(variants.into_boxed_slice()),
..ItemDetail::default()
}));
Ok(fact)
}
fn struct_fact(
&self,
s: &syn::ItemStruct,
whole: proc_macro2::Span,
inherited: TestRole,
) -> Found<ItemFact> {
let (braces, shape) = match &s.fields {
syn::Fields::Named(named) => (Some(named.brace_token.span.join()), FieldShape::Named),
syn::Fields::Unnamed(_) => (None, FieldShape::Tuple),
syn::Fields::Unit => (None, FieldShape::Unit),
};
let mut fact = self.item(
&s.ident,
ItemKind::Struct,
&s.vis,
&s.attrs,
whole,
braces,
inherited,
Vec::new(),
)?;
fact.shape = Some(shape);
self.with_nominal(fact, &s.generics)
}
fn union_fact(
&self,
u: &syn::ItemUnion,
whole: proc_macro2::Span,
inherited: TestRole,
) -> Found<ItemFact> {
let mut fact = self.item(
&u.ident,
ItemKind::Union,
&u.vis,
&u.attrs,
whole,
Some(u.fields.brace_token.span.join()),
inherited,
Vec::new(),
)?;
fact.shape = Some(FieldShape::Named);
self.with_nominal(fact, &u.generics)
}
fn trait_fact(
&self,
t: &syn::ItemTrait,
whole: proc_macro2::Span,
inherited: TestRole,
body: &mut ModBody,
) -> Found<ItemFact> {
let own = summarize(&t.attrs, &FactMeter::scratch())?;
let inside = role(own.own_test, inherited);
let mut members = Vec::new();
for member in &t.items {
match member {
syn::TraitItem::Macro(invocation) => body.unexpanded.push(self.unexpanded(
&invocation.attrs,
&invocation.mac,
member.span(),
inside,
)?),
syn::TraitItem::Verbatim(tokens) if verbatim::definitely_disabled(tokens) => {
self.record_disabled(member.span(), body)?;
}
_ => members.push(self.trait_member(member, inside)?),
}
}
self.meter
.charge(slack(&mut members), t.ident.span().start().line)?;
self.item(
&t.ident,
ItemKind::Trait,
&t.vis,
&t.attrs,
whole,
Some(t.brace_token.span.join()),
inherited,
members,
)
}
fn named(
&self,
item: &Item,
inherited: TestRole,
body: &mut ModBody,
) -> Found<Option<ItemFact>> {
let whole = item.span();
let fact = match item {
Item::Fn(f) => {
let fact = self.item(
&f.sig.ident,
ItemKind::Fn,
&f.vis,
&f.attrs,
whole,
Some(f.block.brace_token.span.join()),
inherited,
Vec::new(),
)?;
self.with_bindings(fact, &f.sig, false)?
}
Item::Struct(s) => self.struct_fact(s, whole, inherited)?,
Item::Enum(e) => self.enum_fact(e, whole, inherited)?,
Item::Union(u) => self.union_fact(u, whole, inherited)?,
Item::Trait(t) => self.trait_fact(t, whole, inherited, body)?,
Item::Type(t) => {
let mut fact = self.item(
&t.ident,
ItemKind::TypeAlias,
&t.vis,
&t.attrs,
whole,
None,
inherited,
Vec::new(),
)?;
fact.alias = match &*t.ty {
syn::Type::Path(path) if path.qself.is_none() => Some(PathFact {
segments: path.path.segments.iter().map(|s| unraw(&s.ident)).collect(),
leading_colon: path.path.leading_colon.is_some(),
}),
_ => None,
};
if let Some(alias) = &fact.alias {
self.meter
.charge(strings_bytes(&alias.segments), fact.name_line)?;
}
if let Some(arity) = alias_const_arity(t) {
self.meter.charge(size_of::<ItemDetail>(), fact.name_line)?;
fact.detail = Some(Arc::new(ItemDetail {
alias_const_arity: Some(arity),
..ItemDetail::default()
}));
}
fact
}
Item::Const(c) => {
let fact = self.item(
&c.ident,
if c.ident == "_" {
ItemKind::UnnamedConst
} else {
ItemKind::Const
},
&c.vis,
&c.attrs,
whole,
None,
inherited,
Vec::new(),
)?;
self.with_initializer(fact, c.eq_token.span(), c.semi_token.span())?
}
Item::Static(s) => {
let fact = self.item(
&s.ident,
ItemKind::Static,
&s.vis,
&s.attrs,
whole,
None,
inherited,
Vec::new(),
)?;
self.with_initializer(fact, s.eq_token.span(), s.semi_token.span())?
}
_ => return Ok(None),
};
Ok(Some(fact))
}
fn one(&self, item: &Item, inherited: TestRole, body: &mut ModBody) -> Found<()> {
self.blocks(item, inherited, &mut body.blocks)?;
if let Some(fact) = self.named(item, inherited, body)? {
body.items.push(fact);
return Ok(());
}
let whole = item.span();
match item {
Item::Macro(m) => {
if let Some(ident) = &m.ident {
body.items.push(self.item(
ident,
ItemKind::MacroRules,
&Visibility::Inherited,
&m.attrs,
whole,
Some(m.mac.delimiter.span().join()),
inherited,
Vec::new(),
)?);
} else {
body.unexpanded
.push(self.unexpanded(&m.attrs, &m.mac, whole, inherited)?);
}
}
Item::ExternCrate(e) => {
let mut fact = self.item(
e.rename.as_ref().map_or(&e.ident, |(_, alias)| alias),
ItemKind::ExternCrate,
&e.vis,
&e.attrs,
whole,
None,
inherited,
Vec::new(),
)?;
if e.rename.is_some() {
let original = unraw(&e.ident);
self.meter.charge(original.capacity(), fact.name_line)?;
fact.renamed_from = Some(original);
}
body.items.push(fact);
}
Item::ForeignMod(block) => {
let own = summarize(&block.attrs, &FactMeter::scratch())?;
let inside = role(own.own_test, inherited);
for foreign in &block.items {
self.foreign(foreign, inside, body)?;
}
}
Item::Mod(m) => body.mods.push(self.module_fact(m, inherited)?),
Item::Impl(i) => {
let implementation = self.impl_fact(i, inherited, body)?;
body.impls.push(implementation);
}
Item::Use(u) => {
let role = use_role(&u.attrs, inherited)?;
body.uses.push(self.use_fact(
role,
&u.vis,
u.leading_colon.is_some(),
&u.tree,
whole,
)?);
}
Item::Verbatim(tokens) => match verbatim::item(tokens) {
Some(Verbatim::UseGroup(group)) => {
let role = use_role(&group.attrs, inherited)?;
for member in &group.members {
body.uses.push(self.use_fact(
role,
&group.vis,
member.leading_colon,
&member.tree,
whole,
)?);
}
}
Some(Verbatim::Bodiless(read)) => {
body.items
.push(self.bodiless(&read, whole, inherited, false)?);
}
None if verbatim::definitely_disabled(tokens) => {
self.record_disabled(whole, body)?;
}
None => return Err(unsupported(item)),
},
other => return Err(unsupported(other)),
}
Ok(())
}
fn record_disabled(&self, whole: proc_macro2::Span, body: &mut ModBody) -> Found<()> {
let span = self.positions.span(whole)?;
self.meter.charge(size_of::<SpanFact>(), span.lines.start)?;
body.disabled.push(span);
Ok(())
}
fn unexpanded(
&self,
attrs: &[Attribute],
invocation: &syn::Macro,
whole: proc_macro2::Span,
inherited: TestRole,
) -> Found<UnexpandedFact> {
let line = whole.start().line;
let summary = summarize(attrs, &FactMeter::scratch())?;
let mut invoked = if invocation.path.leading_colon.is_some() {
"::".to_owned()
} else {
String::new()
};
invoked.push_str(&path_text(&invocation.path));
let invoked = fit(invoked);
self.meter
.charge(size_of::<UnexpandedFact>() + invoked.capacity(), line)?;
let mut identifiers = Vec::new();
let mut stack =
purrdf_lex::walk::WorkList::<_, 8>::with(invocation.tokens.clone().into_iter());
while let Some(mut tokens) = stack.pop() {
while let Some(token) = tokens.next() {
match token {
proc_macro2::TokenTree::Ident(ident) => {
let name = unraw(&ident);
self.meter
.charge(size_of::<String>() + name.capacity(), line)?;
identifiers.push(name);
}
proc_macro2::TokenTree::Group(group) => {
self.meter
.charge(4 * size_of::<proc_macro2::token_stream::IntoIter>(), line)?;
stack.push(tokens);
stack.push(group.stream().into_iter());
break;
}
_ => {}
}
}
}
self.meter.charge(slack(&mut identifiers), line)?;
Ok(UnexpandedFact {
invoked,
span: self.positions.span(whole)?,
test: role(summary.own_test, inherited),
identifiers,
})
}
fn use_fact(
&self,
role: UseRole,
vis_of: &Visibility,
leading_colon: bool,
tree: &syn::UseTree,
whole: proc_macro2::Span,
) -> Found<UseFact> {
let line = whole.start().line;
let vis = vis(vis_of);
self.meter
.charge(size_of::<UseFact>() + vis_bytes(&vis), line)?;
Ok(UseFact {
line,
span: self.positions.span(whole)?,
vis,
leading_colon,
tree: self.use_tree(tree, line)?,
test: role.test,
doc_hidden: role.doc_hidden,
configuration: role.configuration,
})
}
fn bodiless(
&self,
read: &Bodiless,
whole: proc_macro2::Span,
inherited: TestRole,
method: bool,
) -> Found<ItemFact> {
match read {
Bodiless::Fn(f) => {
let fact = self.item(
&f.sig.ident,
ItemKind::Fn,
&f.vis,
&f.attrs,
whole,
None,
inherited,
Vec::new(),
)?;
self.with_bindings(fact, &f.sig, method)
}
Bodiless::Type(t) => self.item(
&t.ident,
ItemKind::TypeAlias,
&t.vis,
&t.attrs,
whole,
None,
inherited,
Vec::new(),
),
}
}
fn foreign(
&self,
item: &syn::ForeignItem,
inherited: TestRole,
body: &mut ModBody,
) -> Found<()> {
let whole = item.span();
match item {
syn::ForeignItem::Fn(f) => body.items.push(self.item(
&f.sig.ident,
ItemKind::ForeignFn,
&f.vis,
&f.attrs,
whole,
None,
inherited,
Vec::new(),
)?),
syn::ForeignItem::Static(s) => body.items.push(self.item(
&s.ident,
ItemKind::ForeignStatic,
&s.vis,
&s.attrs,
whole,
None,
inherited,
Vec::new(),
)?),
syn::ForeignItem::Type(t) => body.items.push(self.item(
&t.ident,
ItemKind::ForeignType,
&t.vis,
&t.attrs,
whole,
None,
inherited,
Vec::new(),
)?),
syn::ForeignItem::Macro(invocation) => body.unexpanded.push(self.unexpanded(
&invocation.attrs,
&invocation.mac,
whole,
inherited,
)?),
other => return Err(unsupported(other)),
}
Ok(())
}
fn trait_member(&self, member: &syn::TraitItem, inherited: TestRole) -> Found<ItemFact> {
let whole = member.span();
match member {
syn::TraitItem::Fn(f) => self
.item(
&f.sig.ident,
ItemKind::Fn,
&Visibility::Inherited,
&f.attrs,
whole,
f.default
.as_ref()
.map(|block| block.brace_token.span.join()),
inherited,
Vec::new(),
)
.and_then(|fact| self.with_bindings(fact, &f.sig, false)),
syn::TraitItem::Const(c) => self
.item(
&c.ident,
ItemKind::Const,
&Visibility::Inherited,
&c.attrs,
whole,
None,
inherited,
Vec::new(),
)
.and_then(|fact| match &c.default {
Some((eq, _)) => self.with_initializer(fact, eq.span(), c.semi_token.span()),
None => Ok(fact),
}),
syn::TraitItem::Type(t) => self.item(
&t.ident,
ItemKind::TypeAlias,
&Visibility::Inherited,
&t.attrs,
whole,
None,
inherited,
Vec::new(),
),
other => Err(unsupported(other)),
}
}
fn impl_member(
&self,
member: &syn::ImplItem,
inherited: TestRole,
body: &mut ModBody,
) -> Found<Option<ItemFact>> {
let whole = member.span();
let fact = match member {
syn::ImplItem::Fn(f) => {
let fact = self.item(
&f.sig.ident,
ItemKind::Fn,
&f.vis,
&f.attrs,
whole,
Some(f.block.brace_token.span.join()),
inherited,
Vec::new(),
)?;
self.with_bindings(fact, &f.sig, true)?
}
syn::ImplItem::Const(c) => {
let fact = self.item(
&c.ident,
ItemKind::Const,
&c.vis,
&c.attrs,
whole,
None,
inherited,
Vec::new(),
)?;
self.with_initializer(fact, c.eq_token.span(), c.semi_token.span())?
}
syn::ImplItem::Type(t) => self.item(
&t.ident,
ItemKind::TypeAlias,
&t.vis,
&t.attrs,
whole,
None,
inherited,
Vec::new(),
)?,
syn::ImplItem::Verbatim(tokens) => match verbatim::member(tokens) {
Some(read) => self.bodiless(&read, whole, inherited, true)?,
None if verbatim::definitely_disabled(tokens) => {
self.record_disabled(whole, body)?;
return Ok(None);
}
None => return Err(unsupported(member)),
},
other => return Err(unsupported(other)),
};
Ok(Some(fact))
}
fn impl_fact(
&self,
block: &syn::ItemImpl,
inherited: TestRole,
body: &mut ModBody,
) -> Found<ImplFact> {
let whole = block.span();
let line = block.impl_token.span.start().line;
let summary = summarize(&block.attrs, &FactMeter::scratch())?;
let test = role(summary.own_test, inherited);
let mut items = Vec::new();
for member in &block.items {
if let syn::ImplItem::Macro(invocation) = member {
body.unexpanded.push(self.unexpanded(
&invocation.attrs,
&invocation.mac,
member.span(),
test,
)?);
} else if let Some(fact) = self.impl_member(member, test, body)? {
items.push(fact);
}
}
let written_path = self_path_of(&block.self_ty);
let self_path: Option<Vec<String>> = written_path.map(|path| {
path.segments
.iter()
.map(|segment| unraw(&segment.ident))
.collect()
});
let self_leading_colon = written_path.is_some_and(|path| path.leading_colon.is_some());
let mut generics = block
.generics
.params
.iter()
.filter_map(|param| match param {
syn::GenericParam::Type(param) => Some(unraw(¶m.ident)),
syn::GenericParam::Const(param) => Some(unraw(¶m.ident)),
syn::GenericParam::Lifetime(_) => None,
})
.collect::<Vec<_>>();
let trait_path = block.trait_.as_ref().map(|(path, _)| {
let written = path.to_token_stream().to_string();
fit(match block.modifiers.polarity {
Some(_) => format!("!{written}"),
None => written,
})
});
let trait_segments = block.trait_.as_ref().map(|(path, _)| PathFact {
segments: path.segments.iter().map(|s| unraw(&s.ident)).collect(),
leading_colon: path.leading_colon.is_some(),
});
let self_type = fit(block.self_ty.to_token_stream().to_string());
self.meter.charge(
size_of::<ImplFact>()
+ trait_path.as_ref().map_or(0, String::capacity)
+ trait_segments
.as_ref()
.map_or(0, |path| strings_bytes(&path.segments))
+ self_type.capacity()
+ self_path.as_deref().map_or(0, strings_bytes)
+ strings_bytes(&generics)
+ slack(&mut generics)
+ slack(&mut items),
line,
)?;
Ok(ImplFact {
line,
span: self.positions.span(whole)?,
body: self.positions.span(block.brace_token.span.join())?,
trait_path,
trait_segments,
self_type,
self_path,
self_leading_colon,
generics,
unconditional_self: unconditional_self(block).then_some(block.generics.params.len()),
items,
test,
})
}
fn module_fact(&self, module: &syn::ItemMod, inherited: TestRole) -> Found<ModFact> {
let name_line = module.ident.span().start().line;
let summary = summarize(&module.attrs, &self.meter).map_err(|error| match error {
ExtractError::PathAttr {
line,
module: None,
predicate,
message,
} => ExtractError::PathAttr {
line,
module: Some(unraw(&module.ident)),
predicate,
message,
},
other => other,
})?;
let test = role(summary.own_test, inherited);
let (body_span, inline) = match &module.content {
Some((brace, items)) => (
Some(self.positions.span(brace.span.join())?),
Some(self.module(items, test, name_line)?),
),
None => (None, None),
};
let name = unraw(&module.ident);
let vis = vis(&module.vis);
self.meter.charge(
size_of::<ModFact>() + name.capacity() + vis_bytes(&vis),
name_line,
)?;
let name_start = self.positions.span(module.ident.span())?.start_byte;
Ok(ModFact {
name,
name_line,
name_start,
span: self.positions.span(module.span())?,
body_span,
vis,
attrs: summary.facts,
documented: summary.documented,
doc_hidden: summary.doc_hidden,
test,
paths: summary.paths,
inline,
})
}
}
impl Extractor<'_> {
fn blocks(&self, item: &Item, inherited: TestRole, found: &mut Vec<ModFact>) -> Found<()> {
let attrs = match item {
Item::Fn(f) => &f.attrs,
Item::Const(c) => &c.attrs,
Item::Static(s) => &s.attrs,
Item::Impl(i) => &i.attrs,
Item::Trait(t) => &t.attrs,
_ => return Ok(()),
};
let own = summarize(attrs, &FactMeter::scratch())?;
let mut finder = BlockMods {
extractor: self,
test: role(own.own_test, inherited),
found,
failed: None,
};
finder.visit_item(item);
finder.failed.map_or(Ok(()), Err)
}
}
struct BlockMods<'e, 'f, 'x> {
extractor: &'e Extractor<'x>,
test: TestRole,
found: &'f mut Vec<ModFact>,
failed: Option<ExtractError>,
}
impl BlockMods<'_, '_, '_> {
fn within(&mut self, attrs: &[Attribute], visit: impl FnOnce(&mut Self)) {
if self.failed.is_some() {
return;
}
match summarize(attrs, &FactMeter::scratch()) {
Ok(own) => {
let outer = self.test;
self.test = role(own.own_test, outer);
visit(self);
self.test = outer;
}
Err(error) => self.failed = Some(error),
}
}
}
impl<'ast> Visit<'ast> for BlockMods<'_, '_, '_> {
fn visit_type(&mut self, _: &'ast syn::Type) {}
fn visit_pat(&mut self, _: &'ast syn::Pat) {}
fn visit_generics(&mut self, _: &'ast syn::Generics) {}
fn visit_item(&mut self, item: &'ast Item) {
if matches!(
item,
Item::Fn(_)
| Item::Const(_)
| Item::Static(_)
| Item::Impl(_)
| Item::Trait(_)
| Item::Mod(_)
) {
syn::visit::visit_item(self, item);
}
}
fn visit_item_mod(&mut self, module: &'ast syn::ItemMod) {
if self.failed.is_some() {
return;
}
match self.extractor.module_fact(module, self.test) {
Ok(fact) => self.found.push(fact),
Err(error) => self.failed = Some(error),
}
}
fn visit_item_fn(&mut self, function: &'ast syn::ItemFn) {
self.within(&function.attrs, |this| {
syn::visit::visit_item_fn(this, function);
});
}
fn visit_item_const(&mut self, item: &'ast syn::ItemConst) {
self.within(&item.attrs, |this| {
syn::visit::visit_item_const(this, item);
});
}
fn visit_item_static(&mut self, item: &'ast syn::ItemStatic) {
self.within(&item.attrs, |this| {
syn::visit::visit_item_static(this, item);
});
}
fn visit_item_impl(&mut self, item: &'ast syn::ItemImpl) {
self.within(&item.attrs, |this| {
syn::visit::visit_item_impl(this, item);
});
}
fn visit_item_trait(&mut self, item: &'ast syn::ItemTrait) {
self.within(&item.attrs, |this| {
syn::visit::visit_item_trait(this, item);
});
}
fn visit_impl_item_const(&mut self, item: &'ast syn::ImplItemConst) {
self.within(&item.attrs, |this| {
syn::visit::visit_impl_item_const(this, item);
});
}
fn visit_trait_item_const(&mut self, item: &'ast syn::TraitItemConst) {
self.within(&item.attrs, |this| {
syn::visit::visit_trait_item_const(this, item);
});
}
fn visit_impl_item_fn(&mut self, function: &'ast syn::ImplItemFn) {
self.within(&function.attrs, |this| {
syn::visit::visit_impl_item_fn(this, function);
});
}
fn visit_trait_item_fn(&mut self, function: &'ast syn::TraitItemFn) {
self.within(&function.attrs, |this| {
syn::visit::visit_trait_item_fn(this, function);
});
}
}
impl Extractor<'_> {
fn use_tree(&self, tree: &syn::UseTree, line: usize) -> Found<UseTree> {
let node = match tree {
syn::UseTree::Path(path) => {
let name = unraw(&path.ident);
self.meter.charge(name.capacity(), line)?;
UseTree::Path {
name,
rest: Box::new(self.use_tree(&path.tree, line)?),
}
}
syn::UseTree::Name(name) => {
let name = unraw(&name.ident);
self.meter.charge(name.capacity(), line)?;
UseTree::Name(name)
}
syn::UseTree::Rename(rename) => {
let (name, alias) = (unraw(&rename.ident), unraw(&rename.rename));
self.meter
.charge(name.capacity() + alias.capacity(), line)?;
UseTree::Rename { name, alias }
}
syn::UseTree::Glob(_) => UseTree::Glob,
syn::UseTree::Group(group) => {
let mut items = group
.items
.iter()
.map(|item| self.use_tree(item, line))
.collect::<Found<Vec<_>>>()?;
self.meter.charge(slack(&mut items), line)?;
UseTree::Group(items)
}
};
self.meter.charge(size_of::<UseTree>(), line)?;
Ok(node)
}
}
pub(crate) fn extract(
tree: &syn::File,
text: &str,
allowance: FactAllowance,
) -> Result<(FileFacts, usize), ExtractError> {
let extractor = Extractor {
positions: Positions::new(text),
meter: FactMeter {
allowance,
spent: Cell::new(0),
},
};
extractor.meter.charge(size_of::<FileFacts>(), 1)?;
let own = summarize(&tree.attrs, &extractor.meter)?;
let inherited = if own.own_test == TestRole::CfgTest {
TestRole::CfgTest
} else {
TestRole::None
};
let facts = FileFacts {
body: extractor.module(&tree.items, inherited, 1)?,
documented: own.documented,
doc_hidden: own.doc_hidden,
attrs: own.facts,
};
Ok((facts, extractor.meter.spent.get()))
}
#[cfg(test)]
mod tests {
use super::*;
use crate::RelPath;
#[test]
fn a_column_is_turned_into_its_byte_offset_on_short_long_and_wide_lines() {
let wide: String = "é".repeat(200);
let long = "x".repeat(300);
let text = format!("ab\n{long}\n{wide}\ntail");
let positions = Positions::new(&text);
assert_eq!(positions.byte(1, 1).ok(), Some(1));
assert_eq!(positions.byte(1, 2).ok(), Some(2));
assert_eq!(positions.byte(2, 0).ok(), Some(3));
assert_eq!(positions.byte(2, 299).ok(), Some(3 + 299));
let start = 3 + 301;
for column in [0, 1, 63, 64, 65, 130, 199, 200] {
assert_eq!(
positions.byte(3, column).ok(),
Some(start + 2 * column),
"column {column}"
);
}
assert_eq!(positions.byte(4, 4).ok(), Some(text.len()));
assert!(matches!(
positions.byte(3, 202),
Err(ExtractError::Position { line: 3 })
));
assert!(matches!(
positions.byte(2, 302),
Err(ExtractError::Position { line: 2 })
));
assert!(matches!(
positions.byte(9, 0),
Err(ExtractError::Position { line: 9 })
));
}
const DEMO: &str = "[package]\nname = \"demo\"\nversion = \"0.1.0\"\n";
fn loaded(files: &[(&str, &str)]) -> crate::Workspace {
let mut all = vec![("Cargo.toml", DEMO)];
all.extend_from_slice(files);
let tree: crate::MemoryTree = all.into_iter().collect();
crate::Workspace::load(&tree, &["."]).unwrap_or_else(|e| panic!("{e}"))
}
#[test]
fn facts_carry_names_lines_documentation_visibility_and_macro_export() {
let ws = loaded(&[(
"src/lib.rs",
"/// Documented.\npub fn documented() {}\n\n#[doc(hidden)]\npub fn hidden() {}\n\n///\nfn blank() {}\n\n#[macro_export]\nmacro_rules! shout { () => {} }\n\npub(crate) struct S { x: u8 }\nextern crate alloc as heap;\n",
)]);
let file = ws
.file(&RelPath::new("src/lib.rs").expect("path"))
.expect("loaded");
let items = &file.facts.body.items;
assert_eq!(items[0].name, "documented");
assert!(items[0].documented && !items[0].doc_hidden);
assert_eq!(items[0].name_line, 2);
assert!(!items[1].documented && items[1].doc_hidden);
assert!(!items[2].documented);
assert!(items[3].macro_export);
assert_eq!(items[4].vis, Vis::Restricted("crate".to_owned()));
assert!(items[4].body.is_some());
assert_eq!(items[5].name, "heap");
assert_eq!(items[5].renamed_from.as_deref(), Some("alloc"));
let span = &items[0].span;
assert_eq!(
&file.text[span.start_byte..span.end_byte],
"/// Documented.\npub fn documented() {}"
);
}
#[test]
fn use_trees_and_impl_headers_are_recorded_as_written() {
let ws = loaded(&[(
"src/lib.rs",
"use ::std::{fmt, io::{self, Read as R}};\nuse crate::a::*;\nstruct Holder<T>(T);\nimpl<T> Holder<T> { fn get(&self) {} }\nimpl Trait for a::b::Thing { fn run(&self) {} }\ntrait Trait {}\n",
)]);
let file = ws
.file(&RelPath::new("src/lib.rs").expect("path"))
.expect("loaded");
assert_eq!(file.facts.body.uses.len(), 2);
assert!(file.facts.body.uses[0].leading_colon);
assert_eq!(file.facts.body.impls.len(), 2);
let inherent = &file.facts.body.impls[0];
assert_eq!(
inherent.self_path.as_deref(),
Some(&["Holder".to_owned()][..])
);
assert!(inherent.trait_path.is_none());
assert_eq!(inherent.items[0].name, "get");
let by_trait = &file.facts.body.impls[1];
assert_eq!(by_trait.trait_path.as_deref(), Some("Trait"));
assert_eq!(
by_trait.self_path.as_deref(),
Some(&["a".to_owned(), "b".to_owned(), "Thing".to_owned()][..])
);
}
#[test]
fn facts_record_struct_shapes_unnamed_consts_leading_colons_and_documentation_forms() {
let ws = loaded(&[
(
"src/lib.rs",
"//! The crate.\n\
pub struct Unit;\n\
pub struct Tuple(u8);\n\
pub struct Named { x: u8 }\n\
const _: () = ();\n\
#[doc = include_str!(\"lib.rs\")]\n\
pub fn guide() {}\n\
impl Foo for ::other::Thing {}\n\
impl Foo for local::Thing {}\n\
mod documented;\n\
trait Foo {}\n",
),
("src/documented.rs", "//! Inner docs.\n"),
]);
let lib = ws
.file(&RelPath::new("src/lib.rs").expect("path"))
.expect("loaded");
assert!(lib.facts.documented);
let shapes: Vec<_> = lib
.facts
.body
.items
.iter()
.map(|i| (i.name.as_str(), i.shape))
.collect();
assert_eq!(
shapes[..3],
[
("Unit", Some(FieldShape::Unit)),
("Tuple", Some(FieldShape::Tuple)),
("Named", Some(FieldShape::Named)),
]
);
let unnamed = &lib.facts.body.items[3];
assert_eq!(
(unnamed.name.as_str(), unnamed.kind),
("_", ItemKind::UnnamedConst)
);
assert!(lib.facts.body.items[4].documented);
assert!(lib.facts.body.impls[0].self_leading_colon);
assert!(!lib.facts.body.impls[1].self_leading_colon);
let inner = ws
.file(&RelPath::new("src/documented.rs").expect("path"))
.expect("loaded");
assert!(inner.facts.documented);
let bare = loaded(&[("src/lib.rs", "pub fn plain() {}")]);
assert!(
!bare
.file(&RelPath::new("src/lib.rs").expect("path"))
.expect("loaded")
.facts
.documented
);
}
#[test]
fn a_verbatim_use_group_is_one_use_fact_per_member_in_its_module_with_the_whole_span() {
let text = "pub mod a { pub fn f() {} }\n\
pub mod m {\n #[cfg(test)]\n pub(crate) use {\n ::std::fmt::Debug as D,\n crate::a::f,\n };\n}\n";
let ws = loaded(&[("src/lib.rs", text)]);
let lib = ws
.file(&RelPath::new("src/lib.rs").expect("path"))
.expect("loaded");
assert_eq!(lib.facts.body.uses, []);
let m = lib.facts.body.mods[1].inline.as_ref().expect("inline");
let start = text.find("#[cfg(test)]").expect("attr");
let end = text.find("};").expect("semi") + 2;
let read: Vec<_> = m
.uses
.iter()
.map(|u| {
(
u.line,
u.span.lines.clone(),
u.span.start_byte,
u.span.end_byte,
u.leading_colon,
u.test,
)
})
.collect();
assert_eq!(
read,
[
(3, 3..8, start, end, true, TestRole::CfgTest),
(3, 3..8, start, end, false, TestRole::CfgTest),
]
);
assert!(
m.uses
.iter()
.all(|u| u.vis == Vis::Restricted("crate".to_owned()))
);
assert_eq!(
m.uses[0].tree,
UseTree::Path {
name: "std".to_owned(),
rest: Box::new(UseTree::Path {
name: "fmt".to_owned(),
rest: Box::new(UseTree::Rename {
name: "Debug".to_owned(),
alias: "D".to_owned(),
}),
}),
}
);
}
#[test]
fn a_large_attribute_on_a_verbatim_use_group_is_read_once_for_all_its_members() {
use std::fmt::Write as _;
const MEMBERS: usize = 6000;
let names: Vec<String> = (0..MEMBERS).map(|i| format!("a{i}")).collect();
let mut members = String::new();
for i in 0..MEMBERS {
writeln!(members, " ::std::m{i},").expect("a string takes the text");
}
let text = format!(
"#[cfg(all(test, any({})))]\npub use {{\n{members}}};\n",
names.join(", ")
);
let tree = syn::parse_file(&text).expect("the text parses");
let allowance = FactAllowance {
spent: 0,
limit: usize::MAX,
};
let (extracted, tally) =
work::measure_kind(Work::Attributes, 2, || extract(&tree, &text, allowance));
let (facts, _) = extracted.expect("the facts are read");
assert_eq!(tally.get(Work::Attributes), 1);
let uses = &facts.body.uses;
assert_eq!(uses.len(), MEMBERS);
for (i, u) in uses.iter().enumerate() {
assert_eq!(
(
u.line,
u.span.lines.clone(),
u.span.start_byte,
u.span.end_byte
),
(1, 1..MEMBERS + 4, 0, text.len() - 1),
"{i}"
);
assert!(u.leading_colon, "{i}");
assert_eq!((u.test, &u.vis), (TestRole::CfgTest, &Vis::Public), "{i}");
assert_eq!(
u.tree,
UseTree::Path {
name: "std".to_owned(),
rest: Box::new(UseTree::Name(format!("m{i}"))),
},
"{i}"
);
}
}
#[test]
fn a_long_line_of_wide_characters_is_indexed_once_for_all_its_positions() {
use std::fmt::Write as _;
const ITEMS: usize = 20_000;
let mut text = String::new();
let mut starts = Vec::new();
for index in 0..ITEMS {
starts.push(text.len());
write!(text, "pub fn é{index}() {{}} ").expect("a string takes the text");
}
text.push('\n');
let line_chars = text.chars().count();
let tree = syn::parse_file(&text).expect("the text parses");
let allowance = FactAllowance {
spent: 0,
limit: usize::MAX,
};
let ceiling = line_chars + 8 * ITEMS * (MARK_EVERY + 1);
let (extracted, tally) = work::measure(ceiling, || extract(&tree, &text, allowance));
let (facts, _) = extracted.expect("the facts are read");
let positions = tally.get(Work::Positions);
assert!(positions >= 2 * ITEMS, "{positions}");
assert!(tally.get(Work::ColumnSteps) > 0);
assert!(
tally.get(Work::ColumnSteps) <= line_chars + positions * (MARK_EVERY + 1),
"{} column steps for {positions} positions on a line of {line_chars}",
tally.get(Work::ColumnSteps)
);
assert_eq!(facts.body.items.len(), ITEMS);
for (index, start) in starts.iter().enumerate().step_by(997) {
let item = &facts.body.items[index];
assert_eq!(item.span.start_byte, *start, "é{index}");
}
}
}