use rustcall_julia_core::cfg::CfgSet;
use rustcall_julia_core::expand::{expand, expand_with_cfg};
use rustcall_julia_core::extract::{extract_with_cfg, FilePosition};
use rustcall_julia_core::manifest::Mode;
use rustcall_julia_core::specialize::specialize;
const SRC: &str = r#"
#[cfg(unix)]
#[julia]
pub fn only_unix() -> i32 { 1 }
#[cfg(windows)]
#[julia]
pub fn only_windows() -> i32 { 2 }
#[cfg(all(unix, feature = "extra"))]
#[julia]
pub fn needs_feature() -> Result<i32, i32> { Ok(3) }
#[julia]
pub fn always() -> Option<i32> { Some(4) }
#[cfg(not(windows))]
#[julia]
pub struct Point { pub x: f64, #[cfg(windows)] pub win_only: f64 }
#[cfg(windows)]
#[julia]
pub struct WinPoint { pub x: f64 }
impl Point {
pub fn x(&self) -> f64 { self.x }
#[cfg(windows)]
pub fn win(&self) -> f64 { 0.0 }
}
#[cfg(unix)]
mod nested {
#[julia]
pub fn in_unix_mod() -> i32 { 5 }
#[cfg(windows)]
#[julia]
pub fn in_unix_mod_win() -> i32 { 6 }
}
#[cfg(windows)]
#[julia]
mod winmod {
#[julia]
pub fn gone() -> i32 { 7 }
}
"#;
fn names(m: &rustcall_julia_core::Manifest) -> Vec<String> {
m.functions.iter().map(|f| f.name.clone()).collect()
}
#[test]
fn without_cfg_everything_is_reported_with_predicates() {
let e = expand(SRC).unwrap();
let f = |n: &str| e.manifest.functions.iter().find(|f| f.name == n).unwrap();
assert_eq!(f("only_unix").cfg, "unix");
assert_eq!(f("only_windows").cfg, "windows");
assert_eq!(f("needs_feature").cfg, "all(unix, feature = \"extra\")");
assert_eq!(f("always").cfg, "");
assert!(names(&e.manifest).contains(&"gone".to_string()));
let s = |n: &str| e.manifest.structs.iter().find(|s| s.name == n).unwrap();
assert_eq!(s("Point").cfg, "not(windows)");
assert_eq!(s("WinPoint").cfg, "windows");
}
#[test]
fn inline_mode_drops_disabled_items() {
let set = CfgSet::default().with_name("unix");
let e = expand_with_cfg(SRC, Some(&set)).unwrap();
let n = names(&e.manifest);
assert!(n.contains(&"only_unix".to_string()));
assert!(n.contains(&"always".to_string()));
assert!(n.contains(&"in_unix_mod".to_string()));
assert!(!n.contains(&"only_windows".to_string()));
assert!(
!n.contains(&"needs_feature".to_string()),
"feature not enabled"
);
assert!(!n.contains(&"in_unix_mod_win".to_string()));
assert!(!n.contains(&"gone".to_string()));
let structs: Vec<_> = e.manifest.structs.iter().map(|s| s.name.as_str()).collect();
assert_eq!(structs, vec!["Point"]);
let point = &e.manifest.structs[0];
assert!(point.fields.iter().all(|f| f.name != "win_only"));
assert!(point.methods.iter().any(|m| m.name == "x"));
assert!(point.methods.iter().all(|m| m.name != "win"));
assert!(!e.source.contains("only_windows"));
assert!(!e.source.contains("WinPoint"));
assert!(!e.source.contains("winmod"));
assert!(!e.source.contains("win_only"));
assert!(!e.source.contains("#[cfg(unix)]"), "{}", e.source);
assert!(!e.source.contains("#[cfg(not(windows))]"));
let lenient = CfgSet::default().with_name("unix").lenient();
let el = expand_with_cfg(SRC, Some(&lenient)).unwrap();
assert!(el.source.contains("feature = \"extra\""), "{}", el.source);
assert!(!el.source.contains("#[cfg(unix)]"));
let with_feature = set.clone().with_pair("feature", "extra");
let e2 = expand_with_cfg(SRC, Some(&with_feature)).unwrap();
assert!(names(&e2.manifest).contains(&"needs_feature".to_string()));
}
#[test]
fn crate_mode_drops_disabled_items() {
let set = CfgSet::default().with_name("windows");
let m = extract_with_cfg(SRC, Mode::Crate, Some(&set)).unwrap();
let n = names(&m);
assert_eq!(n, vec!["only_windows", "always", "gone"]);
let structs: Vec<_> = m.structs.iter().map(|s| s.name.as_str()).collect();
assert_eq!(structs, vec!["WinPoint"]);
}
#[test]
fn result_wrappers_keep_cfg_attributes() {
let src = "#[julia]\n#[cfg(windows)]\npub fn r() -> Result<i32, i32> { Ok(1) }\n#[julia]\n#[cfg(windows)]\npub fn o() -> Option<i32> { None }";
let e = expand(src).unwrap();
let gated = e.source.matches("#[cfg(windows)]").count();
assert_eq!(gated, 12, "{}", e.source);
}
#[test]
fn unevaluable_predicate_fails_closed() {
let set = CfgSet::default();
let err = expand_with_cfg(
"#[cfg(version(\"1.80\"))] #[julia] pub fn f() -> i32 { 0 }",
Some(&set),
)
.err()
.expect("unevaluable predicate must be an error");
assert!(err.to_string().contains("cannot evaluate #[cfg]"), "{err}");
}
#[test]
fn specialize_keeps_predicate() {
let src = "#[cfg(unix)] pub fn id<T: Copy>(x: T) -> T { x }";
let sp = specialize(src, "id", &[("T".to_string(), "i32".to_string())], "id_i32").unwrap();
assert_eq!(sp.manifest.functions[0].cfg, "unix");
assert!(sp.source.contains("#[cfg(unix)]"));
}
const PARAM_SRC: &str = r#"
#[julia]
pub fn takes(a: i32, #[cfg(any())] b: i32, #[cfg(all())] c: i32) -> i32 { a + c }
#[julia]
pub fn maybe(a: i32, #[cfg(feature = "extra")] b: i32) -> Result<i32, i32> { Ok(a) }
#[julia]
pub struct Holder { pub v: i32 }
impl Holder {
pub fn sum(&self, a: i32, #[cfg(windows)] b: i32) -> i32 { self.v + a }
}
"#;
#[test]
fn prunes_disabled_function_parameters() {
let set = CfgSet::default().with_name("unix");
let e = expand_with_cfg(PARAM_SRC, Some(&set)).unwrap();
let takes = e
.manifest
.functions
.iter()
.find(|f| f.name == "takes")
.unwrap();
let arg_names: Vec<&str> = takes.args.iter().map(|a| a.name.as_str()).collect();
assert_eq!(arg_names, vec!["a", "c"], "{:?}", takes.args);
let maybe = e
.manifest
.functions
.iter()
.find(|f| f.name == "maybe")
.unwrap();
let arg_names: Vec<&str> = maybe.args.iter().map(|a| a.name.as_str()).collect();
assert_eq!(arg_names, vec!["a"]);
let holder = e
.manifest
.structs
.iter()
.find(|s| s.name == "Holder")
.unwrap();
let sum = holder.methods.iter().find(|m| m.name == "sum").unwrap();
let arg_names: Vec<&str> = sum.args.iter().map(|a| a.name.as_str()).collect();
assert_eq!(arg_names, vec!["a"], "{:?}", sum.args);
assert!(!e.source.contains("#[cfg(any())]"), "{}", e.source);
assert!(!e.source.contains("#[cfg(all())]"), "{}", e.source);
assert!(!e.source.contains("#[cfg(windows)]"), "{}", e.source);
}
#[test]
fn keeps_undecided_function_parameters_in_lenient_mode() {
let set = CfgSet::default().with_name("unix").lenient();
let e = expand_with_cfg(PARAM_SRC, Some(&set)).unwrap();
let maybe = e
.manifest
.functions
.iter()
.find(|f| f.name == "maybe")
.unwrap();
let arg_names: Vec<&str> = maybe.args.iter().map(|a| a.name.as_str()).collect();
assert_eq!(arg_names, vec!["a", "b"], "{:?}", maybe.args);
assert!(e.source.contains("feature = \"extra\""), "{}", e.source);
let holder = e
.manifest
.structs
.iter()
.find(|s| s.name == "Holder")
.unwrap();
let sum = holder.methods.iter().find(|m| m.name == "sum").unwrap();
assert_eq!(
sum.args.iter().map(|a| a.name.as_str()).collect::<Vec<_>>(),
vec!["a"]
);
}
#[test]
fn unevaluable_parameter_predicate_fails_closed() {
let set = CfgSet::default();
let err = expand_with_cfg(
"#[julia] pub fn f(a: i32, #[cfg(version(\"1.80\"))] b: i32) -> i32 { a }",
Some(&set),
)
.err()
.expect("unevaluable parameter predicate must be an error");
assert!(err.to_string().contains("cannot evaluate #[cfg]"), "{err}");
}
#[test]
fn crate_level_cfg_disables_every_item() {
let src = r#"
#![cfg(any())]
#[julia]
pub fn f() -> i32 { 1 }
#[julia]
pub struct S { pub x: i32 }
"#;
let set = CfgSet::default().with_name("unix");
let e = expand_with_cfg(src, Some(&set)).unwrap();
assert!(
e.manifest.functions.is_empty(),
"{:?}",
e.manifest.functions
);
assert!(e.manifest.structs.is_empty(), "{:?}", e.manifest.structs);
assert!(!e.source.contains("fn f"), "{}", e.source);
assert!(!e.source.contains("struct S"), "{}", e.source);
let m = extract_with_cfg(src, Mode::Crate, Some(&set)).unwrap();
assert!(m.functions.is_empty() && m.structs.is_empty());
let indirect = "#![cfg_attr(unix, cfg(any()))]\n#[julia]\npub fn f() -> i32 { 1 }\n";
assert!(expand_with_cfg(indirect, Some(&set))
.unwrap()
.manifest
.functions
.is_empty());
}
#[test]
fn crate_level_cfg_that_holds_keeps_the_items() {
let set = CfgSet::default().with_name("unix");
let e = expand_with_cfg(
"#![cfg(unix)]\n#[julia]\npub fn f() -> i32 { 1 }\n",
Some(&set),
)
.unwrap();
assert_eq!(names(&e.manifest), vec!["f"]);
assert!(!e.source.contains("cfg(unix)"), "{}", e.source);
let lenient = CfgSet::default().with_name("unix").lenient();
let e = expand_with_cfg(
"#![cfg(feature = \"x\")]\n#[julia]\npub fn f() -> i32 { 1 }\n",
Some(&lenient),
)
.unwrap();
assert_eq!(names(&e.manifest), vec!["f"]);
assert!(e.source.contains("feature = \"x\""), "{}", e.source);
}
#[test]
fn unevaluable_crate_level_predicate_fails_closed() {
let err = expand_with_cfg(
"#![cfg(version(\"1.80\"))]\n#[julia]\npub fn f() -> i32 { 1 }\n",
Some(&CfgSet::default()),
)
.err()
.expect("unevaluable crate predicate must be an error");
assert!(err.to_string().contains("cannot evaluate #[cfg]"), "{err}");
}
#[test]
fn prunes_disabled_generic_parameters() {
let set = CfgSet::default().with_name("unix");
let src = r#"
#[julia]
pub fn f<#[cfg(any())] T, U: Copy>(x: U) -> U { x }
#[julia]
pub fn g<#[cfg(any())] 'a, #[cfg(all())] T: Copy>(x: T) -> T { x }
#[julia]
pub fn h<#[cfg(windows)] const N: usize, T: Copy>(x: T) -> T { x }
"#;
let e = expand_with_cfg(src, Some(&set)).unwrap();
let params = |n: &str| -> Vec<String> {
e.manifest
.functions
.iter()
.find(|f| f.name == n)
.unwrap()
.type_params
.iter()
.map(|p| p.name.clone())
.collect()
};
assert_eq!(params("f"), vec!["U"], "{}", e.source);
assert_eq!(params("g"), vec!["T"], "{}", e.source);
assert!(!e.source.contains("'a"), "{}", e.source);
assert!(!e.source.contains("#[cfg(all())]"), "{}", e.source);
assert_eq!(params("h"), vec!["T"], "{}", e.source);
assert!(!e.source.contains("const N"), "{}", e.source);
assert!(!e.source.contains("compile_error"), "{}", e.source);
let src = r#"
#[julia]
pub struct W<#[cfg(any())] T, U: Copy> { pub v: U }
impl<#[cfg(any())] T, U: Copy> W<U> {
pub fn v(&self) -> U { self.v }
}
"#;
let e = expand_with_cfg(src, Some(&set)).unwrap();
assert!(!e.source.contains("T,"), "{}", e.source);
assert!(e.source.contains("W<U"), "{}", e.source);
let lenient = CfgSet::default().with_name("unix").lenient();
let e = expand_with_cfg(
"#[julia]\npub fn f<#[cfg(feature = \"x\")] T, U: Copy>(x: U) -> U { x }\n",
Some(&lenient),
)
.unwrap();
assert!(e.source.contains("feature = \"x\""), "{}", e.source);
}
#[test]
fn unevaluable_generic_parameter_predicate_fails_closed() {
let err = expand_with_cfg(
"#[julia]\npub fn f<#[cfg(version(\"1.80\"))] T: Copy>(x: T) -> T { x }\n",
Some(&CfgSet::default()),
)
.err()
.expect("unevaluable generic parameter predicate must be an error");
assert!(err.to_string().contains("cannot evaluate #[cfg]"), "{err}");
}
#[test]
fn reports_cfg_inside_function_bodies() {
let src = r#"
#[julia]
pub fn plain<T: Copy>(x: T) -> T { x }
#[julia]
pub fn uses_macro<T: Copy>(x: T) -> T { if cfg!(panic = "unwind") { x } else { x } }
#[julia]
pub fn uses_attr<T: Copy>(x: T) -> T {
#[cfg(debug_assertions)]
let _y = 1;
x
}
#[julia]
pub fn uses_cfg_attr<T: Copy>(x: T) -> T {
#[cfg_attr(unix, allow(unused))]
let _y = 1;
x
}
#[julia]
pub fn nested_item<T: Copy>(x: T) -> T {
fn helper() -> i32 { std::cfg!(unix) as i32 }
let _ = helper();
x
}
#[julia]
pub fn not_generic() -> i32 { cfg!(unix) as i32 }
"#;
let e = expand(src).unwrap();
let has = |n: &str| {
e.manifest
.functions
.iter()
.find(|f| f.name == n)
.unwrap()
.body_has_cfg
};
assert!(!has("plain"));
assert!(has("uses_macro"));
assert!(has("uses_attr"));
assert!(has("uses_cfg_attr"));
assert!(has("nested_item"));
assert!(has("not_generic"));
}
fn nested_cfg_attr(depth: usize) -> String {
let mut s = "cfg(any())".to_string();
for _ in 0..depth {
s = format!("cfg_attr(unix, {s})");
}
format!("#[{s}]\n#[julia]\npub fn deep() -> i32 {{ 1 }}\n")
}
#[test]
fn deeply_nested_cfg_attr_is_fully_expanded() {
let set = CfgSet::default().with_name("unix");
for depth in [1usize, 8, 10, 32] {
let e = expand_with_cfg(&nested_cfg_attr(depth), Some(&set)).unwrap();
assert!(
e.manifest.functions.is_empty(),
"depth {depth}: {:?}",
e.manifest.functions
);
assert!(!e.source.contains("deep"), "depth {depth}: {}", e.source);
}
let src = "#[cfg_attr(unix, cfg_attr(unix, cfg_attr(unix, cfg(all()))))]\n#[julia]\npub fn deep() -> i32 { 1 }\n";
let e = expand_with_cfg(src, Some(&set)).unwrap();
assert_eq!(names(&e.manifest), vec!["deep"]);
assert!(!e.source.contains("cfg"), "{}", e.source);
}
#[test]
fn absurdly_nested_cfg_attr_fails_closed() {
let set = CfgSet::default().with_name("unix");
let err = expand_with_cfg(&nested_cfg_attr(100), Some(&set))
.err()
.expect("nesting past the limit must be an error");
let msg = err.to_string();
assert!(
msg.contains("cannot evaluate #[cfg]") && msg.contains("nested deeper"),
"{msg}"
);
}
#[test]
fn generated_items_propagate_cfg_attr() {
let src = r#"
#[julia]
#[cfg_attr(not(feature = "ffi"), cfg(any()))]
pub fn r() -> Result<i32, i32> { Ok(1) }
#[julia]
#[cfg_attr(not(feature = "ffi"), cfg(any()))]
pub fn o() -> Option<i32> { None }
"#;
let e = expand(src).unwrap();
let gated = e.source.matches("cfg_attr(not(feature = \"ffi\")").count();
assert_eq!(gated, 12, "{}", e.source);
let mixed = r#"
#[julia]
#[cfg_attr(not(feature = "ffi"), cfg(any()), allow(dead_code))]
pub fn r() -> Result<i32, i32> { Ok(1) }
"#;
let e = expand(mixed).unwrap();
assert_eq!(
e.source
.matches("cfg_attr(not(feature = \"ffi\"), cfg(any()))")
.count(),
5,
"{}",
e.source
);
assert_eq!(
e.source.matches("allow(dead_code)").count(),
1,
"{}",
e.source
);
}
#[test]
fn enclosing_module_cfg_is_inherited() {
let src = r#"
#[cfg(feature = "x")]
#[julia]
pub mod a {
#[julia]
pub fn run() -> i32 { 1 }
#[cfg(unix)]
#[julia]
pub fn nix() -> i32 { 2 }
#[julia]
pub struct C { pub v: i32 }
#[cfg(feature = "y")]
#[julia]
pub mod deep {
#[julia]
pub fn d() -> i32 { 3 }
}
}
#[julia]
pub fn root() -> i32 { 0 }
"#;
let check = |m: &rustcall_julia_core::Manifest| {
let f = |name: &str| m.functions.iter().find(|f| f.name == name).unwrap();
assert_eq!(f("root").cfg, "");
assert_eq!(f("run").cfg, "feature = \"x\"");
assert_eq!(f("run").cfg_features, vec!["x"]);
assert_eq!(f("nix").cfg, "all(feature = \"x\", unix)");
assert_eq!(f("nix").cfg_features, vec!["x"]);
assert_eq!(f("d").cfg, "all(feature = \"x\", feature = \"y\")");
assert_eq!(f("d").cfg_features, vec!["x", "y"]);
let c = m.structs.iter().find(|s| s.name == "C").unwrap();
assert_eq!(c.cfg, "feature = \"x\"");
assert_eq!(c.cfg_features, vec!["x"]);
};
check(&extract_with_cfg(src, Mode::Crate, None).unwrap());
check(&expand_with_cfg(src, None).unwrap().manifest);
let py = r#"
#[cfg(feature = "x")]
pub mod a {
#[pyfunction] pub fn run() -> i32 { 1 }
#[pyclass] pub struct C { #[pyo3(get)] pub v: i32 }
}
"#;
let m = extract_with_cfg(py, Mode::Crate, None).unwrap();
assert_eq!(m.functions[0].cfg, "feature = \"x\"");
assert_eq!(m.functions[0].cfg_features, vec!["x"]);
assert_eq!(m.structs[0].cfg, "feature = \"x\"");
let mut scan = rustcall_julia_core::extract::TreeScan::new();
let mut manifest = rustcall_julia_core::Manifest::new(Mode::Crate);
let pending = scan
.file(
"#[cfg(feature = \"x\")] pub mod a;",
None,
&FilePosition::module(&[], true, &[]),
&mut manifest,
"src/lib.rs",
)
.unwrap()
.modules;
assert_eq!(pending.len(), 1);
assert_eq!(pending[0].cfg.len(), 1);
let more = scan
.file(
"#[pyfunction] pub fn run() -> i32 { 1 }",
None,
&FilePosition::module(
&pending[0].module_path,
pending[0].reachable,
&pending[0].cfg,
),
&mut manifest,
"src/a.rs",
)
.unwrap();
assert!(more.modules.is_empty() && more.includes.is_empty());
scan.finish(&mut manifest).unwrap();
assert_eq!(manifest.functions[0].module_path, vec!["a"]);
assert_eq!(manifest.functions[0].cfg, "feature = \"x\"");
assert_eq!(manifest.functions[0].cfg_features, vec!["x"]);
}
#[test]
fn pymethods_blocks_inherit_their_module_cfg() {
let src = r#"
pub mod shapes { #[pyclass] pub struct Circle { pub r: f64 } }
#[cfg(feature = "x")]
pub mod extra {
#[pymethods]
impl super::shapes::Circle {
pub fn area(&self) -> f64 { 0.0 }
#[cfg(unix)]
pub fn nix(&self) -> f64 { 0.0 }
}
}
#[cfg(feature = "y")]
#[pymethods]
impl shapes::Circle {
pub fn perimeter(&self) -> f64 { 0.0 }
}
"#;
let m = extract_with_cfg(src, Mode::Crate, None).unwrap();
let circle = m.structs.iter().find(|s| s.name == "Circle").unwrap();
let method = |name: &str| circle.methods.iter().find(|mm| mm.name == name).unwrap();
assert_eq!(method("area").cfg, "feature = \"x\"");
assert_eq!(method("nix").cfg, "all(feature = \"x\", unix)");
assert_eq!(method("perimeter").cfg, "feature = \"y\"");
assert_eq!(circle.cfg, "");
}
#[test]
fn generated_struct_helpers_carry_the_struct_cfg() {
let item: syn::ItemStruct =
syn::parse_str("#[cfg(feature = \"x\")] pub struct C { pub v: i32, pub label: String }")
.unwrap();
let file: syn::File = syn::parse2(rustcall_julia_core::codegen::transform_struct_crate(
item,
&["a".to_string()],
))
.unwrap();
let has_cfg = |attrs: &[syn::Attribute]| {
attrs
.iter()
.any(|a| a.path().is_ident("cfg") && quote::quote!(#a).to_string().contains("feature"))
};
let mut fns = 0;
for it in &file.items {
match it {
syn::Item::Fn(f) => {
fns += 1;
assert!(
has_cfg(&f.attrs),
"helper `{}` lost the struct's cfg",
f.sig.ident
);
}
syn::Item::Struct(s) => assert!(has_cfg(&s.attrs), "`{}` lost its cfg", s.ident),
syn::Item::Macro(m) if m.mac.path.is_ident("thread_local") => {
assert!(has_cfg(&m.attrs), "a helper panic slot lost its cfg");
}
_ => {}
}
}
assert_eq!(fns, 11);
let imp: syn::ItemImpl = syn::parse_str(
"#[cfg(feature = \"x\")] impl C { #[julia] pub fn get(&self) -> i32 { self.v } }",
)
.unwrap();
let file: syn::File = syn::parse2(rustcall_julia_core::codegen::transform_impl_crate(
imp,
&["a".to_string()],
))
.unwrap();
let wrappers: Vec<&syn::ItemFn> = file
.items
.iter()
.filter_map(|it| match it {
syn::Item::Fn(f) => Some(f),
_ => None,
})
.collect();
assert!(!wrappers.is_empty());
for f in wrappers {
assert!(
has_cfg(&f.attrs),
"wrapper `{}` lost the block's cfg",
f.sig.ident
);
}
let imp_out = file
.items
.iter()
.find_map(|it| match it {
syn::Item::Impl(i) => Some(i),
_ => None,
})
.unwrap();
let syn::ImplItem::Fn(method) = &imp_out.items[0] else {
panic!()
};
assert!(method.attrs.is_empty());
}