use std::fmt::Write as _;
use raf_source::{
Budget, LoadError, LoadErrorKind, LoadOptions, MemoryTree, PARSE_BYTES_PER_TOKEN, RelPath,
Usage, Workspace, parse_source,
};
const DEMO: &str = "[package]\nname = \"demo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n";
const TOKENS: usize = 100_000;
fn lowered() -> LoadOptions {
let mut options = LoadOptions::default();
options.max_file_tokens = TOKENS;
options
}
fn tokens_at_lowered(text: &str) -> usize {
load_with(&[("src/lib.rs", text)], &lowered())
.unwrap_or_else(|error| panic!("loads: {error}"))
.usage()
.max_file_tokens
}
fn tree(files: &[(&str, &str)]) -> MemoryTree {
let mut all = vec![("Cargo.toml", DEMO)];
all.extend_from_slice(files);
all.into_iter().collect()
}
fn load_with(files: &[(&str, &str)], options: &LoadOptions) -> Result<Workspace, LoadError> {
Workspace::load_with(&tree(files), &["."], options)
}
fn usage(files: &[(&str, &str)]) -> Usage {
load_with(files, &LoadOptions::default())
.unwrap_or_else(|error| panic!("loads: {error}"))
.usage()
}
fn refused(result: Result<Workspace, LoadError>, file: &str) -> (Budget, usize, Option<usize>) {
let error = result.expect_err("over the budget");
assert_eq!(error.file.as_str(), file, "{error}");
match error.kind {
LoadErrorKind::Budget { budget, limit } => {
let text = error.to_string();
assert!(
text.contains(file) && text.contains(budget.name()),
"{text}"
);
(budget, limit, error.line)
}
other => panic!("not a budget refusal: {other:?}"),
}
}
fn semis(count: usize) -> String {
"; ".repeat(count)
}
fn padded(head: &str, head_tokens: usize, tokens: usize) -> String {
let pad = tokens - head_tokens - 1;
format!("{head}{}}}\n", semis(pad))
}
fn comparisons(tokens: usize) -> String {
let head = format!(
"fn f() {{ let _ = {}",
"a<b; ".repeat((tokens - 13) / 4 + 1)
);
let head_tokens = 8 + 4 * ((tokens - 13) / 4 + 1);
padded(&head, head_tokens, tokens)
}
fn deep_generic_arguments(tokens: usize) -> String {
let one = format!("let _: {}u8{}; ", "A<".repeat(64), ">".repeat(64));
let per = 3 + 2 * 64 + 1 + 64 + 1;
let count = (tokens - 7) / per;
padded(
&format!("fn f() {{ {}", one.repeat(count)),
5 + per * count,
tokens,
)
}
fn attribute_run(tokens: usize) -> String {
let count = (tokens - 6) / 4;
padded(
&format!("{}fn f() {{ ", "#[a] ".repeat(count)),
4 * count + 5,
tokens,
)
}
fn statement_lists(tokens: usize) -> String {
let big = (1_usize << (tokens - 13).ilog2()) + 1;
let rest = tokens - big - 6;
format!(
"fn p() {{ {}}}\nfn a() {{ {}}}\n",
semis(rest - 6),
semis(big)
)
}
#[test]
fn a_file_at_the_token_limit_parses_and_one_more_token_is_refused_naming_its_line() {
let at = format!("m!{{ {} }}\n", "a ".repeat(TOKENS - 4));
assert_eq!(tokens_at_lowered(&at), TOKENS);
let over = format!("m!{{ {}\n}}\n", "a ".repeat(TOKENS - 3));
let error = load_with(&[("src/lib.rs", &over)], &lowered()).expect_err("one over");
assert!(
matches!(
error.kind,
LoadErrorKind::Budget {
budget: Budget::FileTokens,
limit: TOKENS
}
),
"{error}"
);
assert_eq!(error.file.as_str(), "src/lib.rs");
assert_eq!(error.line, Some(2));
let file = RelPath::new("src/lib.rs").expect("path");
let default = Budget::FileTokens.limit();
let past_default = format!("m!{{ {}\n}}\n", "a ".repeat(default - 3));
let error = parse_source(&file, &past_default, |_| ()).expect_err("over the default");
assert!(
matches!(
error.kind,
LoadErrorKind::Budget {
budget: Budget::FileTokens,
limit
} if limit == default
),
"{error}"
);
assert_eq!(error.line, Some(2));
}
type Shape = (&'static str, fn(usize) -> String);
#[test]
fn each_costly_shape_at_the_token_limit_loads_and_one_more_token_is_refused() {
let shapes: [Shape; 4] = [
("comparisons", comparisons),
("deep generic arguments", deep_generic_arguments),
("attribute run", attribute_run),
("statement lists", statement_lists),
];
for (name, shape) in shapes {
assert_eq!(tokens_at_lowered(&shape(TOKENS)), TOKENS, "{name}");
let over = shape(TOKENS + 1);
let (budget, limit, _) = refused(
load_with(&[("src/lib.rs", &over)], &lowered()),
"src/lib.rs",
);
assert_eq!((budget, limit), (Budget::FileTokens, TOKENS), "{name}");
}
}
#[test]
fn a_doc_comment_counts_as_the_tokens_of_its_attribute_and_other_comments_count_nothing() {
let text = "//! inner\n/*! inner block */\n/// outer\n/** outer block */\n\
//// plain\n/*** plain */\n/**/\n// plain\nfn f() {}\n";
assert_eq!(
usage(&[("src/lib.rs", text)]).max_file_tokens,
7 + 7 + 6 + 6 + 6
);
let docs = format!("{}fn f() {{}}\n", "/// a\n".repeat(TOKENS / 6));
let (budget, _, line) = refused(
load_with(&[("src/lib.rs", &docs)], &lowered()),
"src/lib.rs",
);
assert_eq!(budget, Budget::FileTokens);
assert_eq!(line, Some(TOKENS / 6 + 1));
}
fn large_payloads() -> [(&'static str, String, usize); 3] {
let long = |index: usize| format!("N{index}_{}", "x".repeat(64 * 1024));
let names: Vec<String> = (0..16).map(long).collect();
[
(
"doc comment",
format!("/// {}\npub fn f() {{}}\n", "d".repeat(1 << 20)),
1,
),
(
"written self type",
format!(
"pub trait Tr {{}}\nimpl Tr for ({}) {{}}\n",
names.join(", ")
),
2,
),
(
"use group",
format!("use std::{{{}}};\n", names.join(", ")),
1,
),
]
}
#[test]
fn each_large_fact_payload_is_charged_and_refused_at_its_line_once_over_the_fact_budget() {
for (kind, text, line) in large_payloads() {
let files = [("src/lib.rs", text.as_str())];
let spent = usage(&files).fact_bytes;
assert!(spent > 1 << 20, "{kind}: {spent}");
let mut options = LoadOptions::default();
options.max_fact_bytes = spent;
load_with(&files, &options).unwrap_or_else(|e| panic!("{kind} at the limit: {e}"));
options.max_fact_bytes = spent - 1;
let (budget, limit, _) = refused(load_with(&files, &options), "src/lib.rs");
assert_eq!((budget, limit), (Budget::FactBytes, spent - 1), "{kind}");
options.max_fact_bytes = 1 << 20;
let (budget, _, at) = refused(load_with(&files, &options), "src/lib.rs");
assert_eq!((budget, at), (Budget::FactBytes, Some(line)), "{kind}");
}
}
#[test]
fn a_verbatim_use_group_and_a_bodiless_function_are_charged_as_their_plain_forms() {
let long = "x".repeat(64 * 1024);
let pairs = [
(
format!("pub mod a {{ pub fn f() {{}} }}\npub use {{ ::std::{long}, crate::a::f }};\n"),
format!(
"pub mod a {{ pub fn f() {{}} }}\npub use ::std::{long};\npub use crate::a::f;\n"
),
2,
),
(
format!(
"pub struct S;\nimpl S {{\n #[doc = \"{long}\"] pub fn new() -> &'static S;\n}}\n"
),
format!(
"pub struct S;\nimpl S {{\n #[doc = \"{long}\"] pub fn new() -> &'static S {{}}\n}}\n"
),
3,
),
(
format!(
"pub struct S;\nimpl S {{\n #[doc = \"{long}\"] pub fn new<T>() -> &'static S;\n}}\n"
),
format!(
"pub struct S;\nimpl S {{\n #[doc = \"{long}\"] pub fn new<T>() -> &'static S {{}}\n}}\n"
),
3,
),
];
for (verbatim, plain, at) in &pairs {
let files = [("src/lib.rs", verbatim.as_str())];
let spent = usage(&files).fact_bytes;
assert_eq!(
spent,
usage(&[("src/lib.rs", plain.as_str())]).fact_bytes,
"{verbatim:.40}"
);
assert!(spent > 64 * 1024, "{spent}");
let mut options = LoadOptions::default();
options.max_fact_bytes = spent;
load_with(&files, &options).unwrap_or_else(|e| panic!("at the limit: {e}"));
options.max_fact_bytes = 64 * 1024;
let (budget, _, line) = refused(load_with(&files, &options), "src/lib.rs");
assert_eq!((budget, line), (Budget::FactBytes, Some(*at)));
}
}
#[test]
fn the_fact_budget_counts_over_every_file_of_the_load() {
let doc = format!("/// {}\npub fn f() {{}}\n", "d".repeat(4096));
let files = [
("src/lib.rs", "mod a;\nmod b;\n"),
("src/a.rs", doc.as_str()),
("src/b.rs", doc.as_str()),
];
let spent = usage(&files).fact_bytes;
let mut options = LoadOptions::default();
options.max_fact_bytes = spent;
load_with(&files, &options).expect("at the limit");
options.max_fact_bytes = spent - 1;
let (budget, _, _) = refused(load_with(&files, &options), "src/b.rs");
assert_eq!(budget, Budget::FactBytes);
}
#[test]
fn wide_discovery_and_member_frontiers_spill_under_the_item_budget() {
let mut files = vec![(
"Cargo.toml".to_owned(),
"[workspace]\nmembers = [\"p*\"]\nresolver = \"2\"\n".to_owned(),
)];
for n in 0..10 {
files.push((
format!("p{n}/Cargo.toml"),
format!("[package]\nname = \"p{n}\"\nversion = \"0.1.0\"\nedition = \"2021\"\n"),
));
files.push((format!("p{n}/src/lib.rs"), "pub fn member() {}".to_owned()));
}
let tree: MemoryTree = files
.iter()
.map(|(path, text)| (path.as_str(), text.as_str()))
.collect();
let mut options = LoadOptions::default();
options.max_glob_bytes = 0;
options.max_work = 0;
let ws = Workspace::load_with(&tree, &["."], &options).expect("wide no-import workspace");
let spent = ws.usage();
assert_eq!(spent.glob_bytes, 0);
assert_eq!(spent.work, 0);
assert_eq!(ws.target_ids().count(), 10);
options.max_item_bytes = spent.item_bytes;
Workspace::load_with(&tree, &["."], &options).expect("exact item grant");
options.max_item_bytes -= 1;
let error = Workspace::load_with(&tree, &["."], &options).expect_err("one item byte less");
assert!(
matches!(error.kind, LoadErrorKind::Budget { budget: Budget::ItemBytes, limit } if limit == spent.item_bytes - 1)
);
}
#[test]
fn nested_definition_emission_spills_with_only_the_item_budget() {
let source = format!(
"{}pub fn leaf() {{}}{}",
"pub mod a {".repeat(12),
"}".repeat(12)
);
let files = [("src/lib.rs", source.as_str())];
let mut options = LoadOptions::default();
options.max_glob_bytes = 0;
options.max_work = 0;
let spent = load_with(&files, &options)
.expect("definition-only stack spill")
.usage();
assert_eq!(spent.glob_bytes, 0);
assert_eq!(spent.work, 0);
assert!(spent.item_bytes > 0);
options.max_item_bytes = spent.item_bytes;
load_with(&files, &options).expect("exact item grant");
options.max_item_bytes -= 1;
let (budget, limit, _) = refused(load_with(&files, &options), "src/lib.rs");
assert_eq!(budget, Budget::ItemBytes);
assert_eq!(limit, spent.item_bytes - 1);
}
#[test]
fn index_records_at_the_item_budget_load_and_one_byte_less_is_refused() {
let files = [
(
"src/lib.rs",
"pub mod a;\npub use a::f as g;\npub fn top() {}\n",
),
(
"src/a.rs",
"pub fn f() {}\npub struct S;\nimpl S { pub fn m() {} }\n",
),
];
let spent = usage(&files).item_bytes;
let mut options = LoadOptions::default();
options.max_item_bytes = spent;
load_with(&files, &options).expect("at the limit");
options.max_item_bytes = spent - 1;
let error = load_with(&files, &options).expect_err("one byte over");
assert!(
matches!(
error.kind,
LoadErrorKind::Budget {
budget: Budget::ItemBytes,
limit
} if limit == spent - 1
),
"{error}"
);
assert!(
error.to_string().contains(Budget::ItemBytes.name()),
"{error}"
);
assert_eq!(error.file.as_str(), "src/a.rs", "{error}");
assert_eq!(error.line, Some(3), "{error}");
}
#[test]
fn a_long_module_name_is_charged_in_every_item_path_and_refused_over_the_item_budget() {
let name = format!("m{}", "x".repeat(64 * 1024));
let fns = (0..100).fold(String::new(), |mut text, i| {
writeln!(text, " pub fn f{i}() {{}}").expect("write to a string");
text
});
let text = format!("pub mod {name} {{\n{fns}}}\n");
let files = [("src/lib.rs", text.as_str())];
assert!(usage(&files).item_bytes > 100 * 64 * 1024);
let mut options = LoadOptions::default();
options.max_item_bytes = 1 << 20;
let (budget, limit, line) = refused(load_with(&files, &options), "src/lib.rs");
assert_eq!((budget, limit), (Budget::ItemBytes, 1 << 20));
assert!(line.is_some_and(|line| line > 1), "{line:?}");
}
#[test]
fn a_file_reached_by_many_module_paths_is_charged_once_per_path_and_refused_over_the_item_budget() {
let fns = (0..100).fold(String::new(), |mut text, i| {
writeln!(text, "pub fn f{i}() {{}}").expect("write to a string");
text
});
let decls = (0..100).fold(String::new(), |mut text, i| {
writeln!(text, "#[path = \"leaf.rs\"] pub mod p{i};").expect("write to a string");
text
});
let files = [
("src/lib.rs", decls.as_str()),
("src/leaf.rs", fns.as_str()),
];
let spent = usage(&files).item_bytes;
assert!(spent > 100 * 100 * 256, "{spent}");
let mut options = LoadOptions::default();
options.max_item_bytes = spent / 2;
let (budget, _, _) = refused(load_with(&files, &options), "src/leaf.rs");
assert_eq!(budget, Budget::ItemBytes);
}
#[test]
fn the_bytes_read_at_the_total_budget_load_and_one_byte_less_is_refused_naming_the_file() {
let padding = format!("// {}\npub fn f() {{}}\n", "p".repeat(1000));
let files = [
("src/lib.rs", "pub mod a;\n"),
("src/a.rs", padding.as_str()),
];
let spent = usage(&files).total_bytes;
assert_eq!(spent, DEMO.len() + "pub mod a;\n".len() + padding.len());
let mut options = LoadOptions::default();
options.max_total_bytes = spent;
load_with(&files, &options).expect("at the limit");
options.max_total_bytes = spent - 1;
let (budget, limit, _) = refused(load_with(&files, &options), "src/a.rs");
assert_eq!((budget, limit), (Budget::TotalBytes, spent - 1));
options.max_total_bytes = DEMO.len() - 1;
let (budget, _, _) = refused(load_with(&files, &options), "Cargo.toml");
assert_eq!(budget, Budget::TotalBytes);
}
#[test]
fn the_default_budgets_are_the_stated_limits() {
let options = LoadOptions::default();
assert_eq!(options.max_file_tokens, Budget::FileTokens.limit());
assert_eq!(Budget::FileTokens.limit(), 2_000_000);
assert_eq!(PARSE_BYTES_PER_TOKEN, 1664);
const { assert!(320_000 * PARSE_BYTES_PER_TOKEN <= 512 << 20) };
assert_eq!(options.max_total_bytes, Budget::TotalBytes.limit());
assert_eq!(options.max_item_bytes, Budget::ItemBytes.limit());
assert_eq!(options.max_fact_bytes, Budget::FactBytes.limit());
assert_eq!(options.max_glob_bytes, Budget::GlobBytes.limit());
assert_eq!(Budget::TotalBytes.limit(), 256 << 20);
assert_eq!(Budget::ItemBytes.limit(), 512 << 20);
assert_eq!(Budget::FactBytes.limit(), 256 << 20);
assert_eq!(Budget::GlobBytes.limit(), 512 << 20);
assert_eq!(options.max_work, Budget::Work.limit());
assert_eq!(Budget::Work.limit(), 1 << 31);
assert_eq!(Budget::DirectoryDepth.limit(), 128);
}
#[test]
fn usage_reports_what_a_load_spent() {
let manifest = format!("{DEMO}[workspace]\n");
let tree: MemoryTree = [
("Cargo.toml", manifest.as_str()),
("src/lib.rs", "pub mod a;\n"),
("src/a/mod.rs", "pub fn f() {}\n"),
]
.into_iter()
.collect();
let spent = Workspace::load(&tree, &["."]).expect("loads").usage();
assert_eq!(
spent.total_bytes,
manifest.len() + "pub mod a;\n".len() + "pub fn f() {}\n".len()
);
assert!(spent.item_bytes > 0 && spent.fact_bytes > 0);
assert_eq!(spent.max_file_tokens, 7);
assert_eq!(spent.max_recursion, 4);
assert_eq!(spent.max_directory_depth, 2);
assert_eq!(spent.work, 0);
}
fn reached_many_times(levels: usize, leaf: &str) -> Vec<(String, String)> {
let mut files = vec![(
"src/lib.rs".to_owned(),
"#[path = \"l0.rs\"]\npub mod a;\n".to_owned(),
)];
for level in 0..levels {
let next = if level + 1 == levels {
"leaf.rs".to_owned()
} else {
format!("l{}.rs", level + 1)
};
files.push((
format!("src/l{level}.rs"),
format!("#[path = \"{next}\"]\npub mod a;\n#[path = \"{next}\"]\npub mod b;\n"),
));
}
files.push(("src/leaf.rs".to_owned(), leaf.to_owned()));
files
}
fn item_bytes_of(files: &[(String, String)]) -> usize {
let borrowed: Vec<(&str, &str)> = files
.iter()
.map(|(path, text)| (path.as_str(), text.as_str()))
.collect();
usage(&borrowed).item_bytes
}
#[test]
fn a_long_alias_path_in_a_file_reached_many_times_is_held_and_charged_once() {
let long = format!("pub type T = {}a;\n", "a::".repeat(20_000));
let short = "pub type T = a;\n";
let arrivals = 8;
let charged = item_bytes_of(&reached_many_times(arrivals, &long))
- item_bytes_of(&reached_many_times(arrivals, short));
let owned = 20_000 * (size_of::<String>() + 1);
assert!(
charged >= owned && charged < 2 * owned,
"{charged} for {owned}"
);
}
#[test]
fn a_long_extern_crate_name_in_a_file_reached_many_times_is_held_and_charged_once() {
let name = format!("c{}", "x".repeat(64 * 1024));
let long = format!("extern crate {name} as x;\n");
let short = "extern crate c as x;\n";
let arrivals = 8;
let charged = item_bytes_of(&reached_many_times(arrivals, &long))
- item_bytes_of(&reached_many_times(arrivals, short));
let owned = name.len() - 1;
assert!(
charged >= owned && charged < 2 * owned,
"{charged} for {owned}"
);
}
#[test]
fn a_chain_of_files_that_each_declare_the_next_twice_is_charged_what_it_keeps() {
let files = reached_many_times(12, "pub type T = a;\n");
let arrivals = (1 << 13) - 1;
let kept = 1_511 * arrivals;
let charged = item_bytes_of(&files);
assert!(charged >= kept, "{charged} charged for {kept} kept");
}
#[test]
fn nested_inline_modules_whose_scopes_hold_one_name_are_charged_what_they_keep() {
let one = format!("{}{}", "mod a { ".repeat(100), " }".repeat(100));
let text: String = (0..20).fold(String::new(), |mut text, i| {
writeln!(text, "mod b{i} {{ {one} }}").expect("write to a string");
text
});
let modules = 20 * 101;
let kept = 1_443 * modules;
let charged = usage(&[("src/lib.rs", &text)]).item_bytes;
assert!(charged >= kept, "{charged} charged for {kept} kept");
}
fn glob_fan(names: usize, importers: usize) -> String {
let mut text = String::from("pub mod big {\n");
for i in 0..names {
writeln!(text, " pub fn f{i}() {{}}").expect("write to a string");
}
text.push_str("}\n");
for i in 0..importers {
writeln!(text, "pub mod g{i} {{ pub use super::big::*; }}").expect("write to a string");
}
text
}
fn glob_chain(length: usize) -> String {
let mut text = String::new();
for i in 0..length {
writeln!(text, "mod m{i} {{ pub use super::m{}::*; }}", i + 1).expect("write to a string");
}
writeln!(text, "pub mod m{length} {{ pub fn f() {{}} }}").expect("write to a string");
text
}
fn mutual_globs(count: usize) -> String {
let mut text = String::new();
for i in 0..count {
writeln!(
text,
"pub use m{i}::*;\npub mod m{i} {{ pub use crate::*; pub fn g{i}() {{}} }}"
)
.expect("write to a string");
}
text
}
fn generated_bindings(children: usize) -> Vec<(String, String)> {
let mut files = vec![(
"src/lib.rs".to_owned(),
"mod features;\npub use features::*;\npub fn top() {}\n".to_owned(),
)];
let mut parent = String::new();
for i in 0..children {
writeln!(parent, "mod gen_{i};\npub use gen_{i}::*;").expect("write to a string");
let mut child = format!("use super::*;\nextern \"C\" {{\n pub type T{i};\n");
for m in 0..12 {
writeln!(
child,
" pub fn m{m}(this: &T{i});\n pub fn own_{i}_{m}(this: &T{i});"
)
.expect("write to a string");
}
child.push_str("}\n");
files.push((format!("src/features/gen_{i}.rs"), child));
}
files.push(("src/features/mod.rs".to_owned(), parent));
files
}
fn glob_bytes_of(text: &str) -> usize {
usage(&[("src/lib.rs", text)]).glob_bytes
}
#[test]
fn retained_import_reads_and_blocker_cycles_obey_exact_storage_and_work_limits() {
for (text, path, target) in [
(
"pub mod a { pub struct T; }\npub use a::T as Alias;\n",
"demo::Alias",
Some("demo::a::T"),
),
(
"pub mod a { pub struct T; }\n\
pub mod b { pub(crate) use crate::a::*; }\n\
pub mod c { pub use crate::a::*; pub use crate::b::*; }\n\
pub use c::T as Alias;\n",
"demo::Alias",
Some("demo::a::T"),
),
(
"pub mod a { pub struct T; }\n\
pub mod b { pub use crate::a::*; pub use self::T; }\n",
"demo::b::T",
Some("demo::a::T"),
),
(
"pub mod gate {\n\
pub mod a { pub(in crate::gate) struct T; }\n\
pub mod b { pub(in crate::gate) use super::a::*; }\n\
pub(in crate::gate) use b::T as Alias;\n}\n",
"demo::gate::Alias",
Some("demo::gate::a::T"),
),
(
"pub mod a { pub use crate::b::T; }\n\
pub mod b { pub use crate::a::T; }\n",
"demo::a::T",
None,
),
] {
let files = [("src/lib.rs", text)];
let workspace =
load_with(&files, &LoadOptions::default()).expect("loads without an artificial limit");
if let Some(target) = target {
assert_eq!(
workspace
.resolve(path)
.expect("valid neighboring import")
.path,
target
);
} else {
assert!(matches!(
workspace.resolve(path),
Err(raf_source::ResolveError::Cyclic(_))
));
}
let spent = workspace.usage();
assert!(
spent.glob_bytes > 0 && spent.work > 0,
"retained import reads are charged even without a glob"
);
for (budget, exact) in [
(Budget::GlobBytes, spent.glob_bytes),
(Budget::Work, spent.work),
] {
let mut options = LoadOptions::default();
match budget {
Budget::GlobBytes => options.max_glob_bytes = exact,
Budget::Work => options.max_work = exact,
_ => unreachable!("the two resolver budgets"),
}
let at_limit = load_with(&files, &options)
.expect("the exact simultaneous reservation is admitted");
assert_eq!(at_limit.usage(), spent);
assert_eq!(at_limit.resolve(path), workspace.resolve(path));
match budget {
Budget::GlobBytes => options.max_glob_bytes -= 1,
Budget::Work => options.max_work -= 1,
_ => unreachable!("the two resolver budgets"),
}
let (refused_budget, limit, line) = refused(load_with(&files, &options), "src/lib.rs");
assert_eq!((refused_budget, limit), (budget, exact - 1));
let line = line.expect("the responsible import line");
assert!(
text.lines()
.nth(line - 1)
.is_some_and(|line| line.contains("use ")),
"{line}: {text}"
);
load_with(&files, &LoadOptions::default())
.expect("a refusal does not contaminate another load");
}
}
}
#[test]
fn glob_tables_at_the_glob_budget_load_and_one_byte_less_is_refused() {
let text = "pub mod a { pub fn f() {} pub struct S; }\n\
pub mod b { pub use crate::a::*; }\n\
pub mod c { pub use crate::b::*; }\n";
let files = [("src/lib.rs", text)];
let spent = usage(&files).glob_bytes;
assert!(spent > 0, "a load with globs charges its glob tables");
let mut options = LoadOptions::default();
options.max_glob_bytes = spent;
let ws = load_with(&files, &options).expect("at the limit");
assert_eq!(
ws.resolve("demo::c::f").expect("resolves").path,
"demo::a::f"
);
options.max_glob_bytes = spent - 1;
let (budget, limit, line) = refused(load_with(&files, &options), "src/lib.rs");
assert_eq!((budget, limit), (Budget::GlobBytes, spent - 1));
assert!(matches!(line, Some(2 | 3)), "{line:?}");
assert_eq!(glob_bytes_of("pub mod a { pub fn f() {} }\n"), 0);
}
#[test]
fn glob_table_work_at_the_work_budget_loads_and_one_step_less_is_refused() {
let text = "pub mod a { pub fn f() {} pub struct S; }\n\
pub mod b { pub use crate::a::*; }\n\
pub mod c { pub use crate::b::*; }\n";
let files = [("src/lib.rs", text)];
let spent = usage(&files).work;
assert!(spent > 0);
let mut options = LoadOptions::default();
options.max_work = spent;
let ws = load_with(&files, &options).expect("at the limit");
assert_eq!(
ws.resolve("demo::c::f").expect("resolves").path,
"demo::a::f"
);
assert_eq!(ws.usage().work, spent);
options.max_work = spent - 1;
let (budget, limit, line) = refused(load_with(&files, &options), "src/lib.rs");
assert_eq!((budget, limit), (Budget::Work, spent - 1));
assert_eq!(line, Some(2));
let error = load_with(&files, &options).expect_err("over the budget");
assert!(
error.to_string().contains(&format!(
"exceeds the budget for glob table work steps ({})",
spent - 1
)),
"{error}"
);
options.max_work = 0;
let ws = load_with(&[("src/lib.rs", "pub mod a { pub fn f() {} }\n")], &options)
.expect("no import-settling work");
assert_eq!(ws.usage().work, 0);
}
#[test]
fn a_module_that_many_modules_glob_is_kept_once_for_all_of_them() {
let one = glob_bytes_of(&glob_fan(1000, 1));
let many = glob_bytes_of(&glob_fan(1000, 400));
let per_importer = (many - one) / 399;
assert!(
per_importer < 1024,
"{per_importer} bytes for each importer of a thousand names"
);
assert!(one > 1000 * 64, "{one} bytes for a thousand names");
}
#[test]
fn a_chain_of_globs_is_charged_what_each_link_brings_in() {
let short = glob_bytes_of(&glob_chain(1000));
let long = glob_bytes_of(&glob_chain(2000));
assert!(
long < 2 * short + short / 10,
"{long} bytes for 2000 links, {short} for 1000"
);
let ws = load_with(
&[("src/lib.rs", &glob_chain(2000))],
&LoadOptions::default(),
)
.expect("loads");
assert_eq!(
ws.resolve("demo::m0::f").expect("resolves").path,
"demo::m2000::f"
);
}
#[test]
fn modules_that_glob_one_another_are_refused_over_the_glob_budget_naming_a_glob() {
let mut options = LoadOptions::default();
options.max_glob_bytes = 4 << 20;
let text = mutual_globs(200);
let (budget, limit, line) =
refused(load_with(&[("src/lib.rs", &text)], &options), "src/lib.rs");
assert_eq!((budget, limit), (Budget::GlobBytes, 4 << 20));
let line = line.expect("the line of a glob");
assert!(
text.lines()
.nth(line - 1)
.is_some_and(|glob| glob.contains("::*;")),
"{line}"
);
let ws = load_with(&[("src/lib.rs", &mutual_globs(20))], &options).expect("loads");
assert_eq!(
ws.resolve("demo::m0::g19").expect("resolves").path,
"demo::m19::g19"
);
}
#[test]
fn glob_tables_of_a_generated_binding_crate_are_charged_what_they_keep() {
let files = generated_bindings(200);
let borrowed: Vec<(&str, &str)> = files
.iter()
.map(|(path, text)| (path.as_str(), text.as_str()))
.collect();
let ws = load_with(&borrowed, &LoadOptions::default()).expect("loads");
let charged = ws.usage().glob_bytes;
assert!(charged >= 1_976_740, "{charged} charged");
assert_eq!(
ws.resolve("demo::T7").expect("resolves").path,
"demo::features::gen_7::T7"
);
}
fn repeated_globs(names: usize, globs: usize, u_first: bool) -> Vec<(String, String)> {
let mut a = String::new();
for i in 0..names {
writeln!(a, "pub struct S{i};").expect("write to a string");
}
let lib = if u_first {
"pub mod u;\npub mod a;\npub mod t;\n"
} else {
"pub mod a;\npub mod t;\npub mod u;\n"
};
vec![
("src/lib.rs".to_owned(), lib.to_owned()),
("src/a.rs".to_owned(), a),
(
"src/t.rs".to_owned(),
"pub use crate::a::*;\n".repeat(globs),
),
("src/u.rs".to_owned(), "pub use crate::t::*;\n".to_owned()),
]
}
fn borrowed(files: &[(String, String)]) -> Vec<(&str, &str)> {
files
.iter()
.map(|(path, text)| (path.as_str(), text.as_str()))
.collect()
}
#[test]
fn repeated_globs_of_one_module_are_charged_once_for_what_it_holds() {
for u_first in [false, true] {
let one = usage(&borrowed(&repeated_globs(2000, 1, u_first))).glob_bytes;
let files = repeated_globs(2000, 400, u_first);
let ws = load_with(&borrowed(&files), &LoadOptions::default()).expect("loads");
let many = ws.usage().glob_bytes;
let per_glob = (many - one) / 399;
assert!(
per_glob < 1024,
"{per_glob} bytes for each more glob of two thousand names ({u_first})"
);
assert_eq!(
ws.resolve("demo::u::S1999").expect("resolves").path,
"demo::a::S1999"
);
let mut options = LoadOptions::default();
options.max_glob_bytes = many - 1;
let error = load_with(&borrowed(&files), &options).expect_err("over the budget");
assert!(
matches!(
error.kind,
LoadErrorKind::Budget {
budget: Budget::GlobBytes,
..
}
),
"{error}"
);
}
}
#[test]
fn an_impl_block_is_charged_its_record_and_what_it_holds() {
let without = usage(&[("src/lib.rs", "pub struct S;\n")]).item_bytes;
let with = usage(&[("src/lib.rs", "pub struct S;\nimpl S {}\n")]).item_bytes;
assert!(
with - without >= 2 * size_of::<raf_source::ImplBlock>(),
"{} charged for a record of {}",
with - without,
size_of::<raf_source::ImplBlock>()
);
let source = "pub struct S;\n\
impl S { pub fn a() {} }\n\
impl S { pub fn b() {} }\n\
impl S { pub fn c() {} }\n\
impl S { pub fn d() {} }\n\
impl S { pub fn e() {} }\n\
impl S { pub fn f() {} }\n\
impl S { pub fn g() {} }\n\
impl S { pub fn h() {} }\n\
impl S { pub fn i() {} }\n";
let files = [("src/lib.rs", source)];
let exact = usage(&files).item_bytes;
let mut options = LoadOptions::default();
options.max_item_bytes = exact;
assert_eq!(
load_with(&files, &options)
.expect("all retained arrays admitted at the exact grant")
.impls()
.count(),
9
);
options.max_item_bytes = exact - 1;
assert_eq!(
refused(load_with(&files, &options), "src/lib.rs"),
(Budget::ItemBytes, exact - 1, Some(10))
);
}