use std::{fs, process::Command};
use rustcall_julia_core::expand::expand;
fn rustc(label: &str, source: &str) -> std::process::Output {
let dir = std::env::temp_dir().join(format!(
"rustcall_generic_method_{}_{label}",
std::process::id()
));
fs::create_dir_all(&dir).unwrap();
let input = dir.join("lib.rs");
fs::write(
&input,
format!("#![allow(non_snake_case, unexpected_cfgs, dead_code)]\n{source}"),
)
.unwrap();
let out = Command::new("rustc")
.args(["--crate-type=cdylib", "-C", "panic=unwind"])
.arg(&input)
.arg("--out-dir")
.arg(&dir)
.output()
.unwrap();
fs::remove_dir_all(dir).unwrap();
out
}
fn flat(source: &str) -> String {
source.split_whitespace().collect::<Vec<_>>().join(" ")
}
const GENERIC_METHOD: &str = r#"
#[julia]
pub struct Acc { pub total: i64 }
#[julia]
impl Acc {
pub fn new() -> Self { Acc { total: 0 } }
pub fn add(&mut self, x: i64) { self.total += x; }
pub fn echo<T: Copy>(&self, x: T) -> T { x }
}
"#;
#[test]
fn a_generic_method_is_refused_at_the_method() {
let expanded = expand(GENERIC_METHOD).unwrap();
let source = flat(&expanded.source);
assert!(
source.contains("compile_error!"),
"no refusal:\n{}",
expanded.source
);
assert!(
source.contains("`Acc::echo` is generic over `T`"),
"{source}"
);
assert!(source.contains("generic free function"), "{source}");
assert!(!source.contains("fn rustcall_Acc_echo"), "{source}");
assert!(source.contains("fn rustcall_Acc_add("), "{source}");
assert!(source.contains("fn rustcall_Acc_new("), "{source}");
let acc = &expanded.manifest.structs[0];
let methods: Vec<(&str, &str)> = acc
.methods
.iter()
.map(|m| (m.name.as_str(), m.skip_reason.as_str()))
.collect();
assert_eq!(
methods,
[("new", ""), ("add", ""), ("echo", "generic_signature:T")]
);
let out = rustc("refused", &expanded.source);
assert!(!out.status.success());
let stderr = String::from_utf8_lossy(&out.stderr);
assert!(
stderr.contains("`Acc::echo` is generic over `T`"),
"{stderr}"
);
assert!(!stderr.contains("cannot find type `T`"), "{stderr}");
}
#[test]
fn const_and_impl_trait_are_refused_lifetimes_are_not() {
let src = r#"
#[julia]
pub struct Buf { pub n: i32 }
impl Buf {
pub fn sized<const N: usize>(&self) -> usize { N }
pub fn shown(&self, x: impl Copy) -> i32 { let _ = x; self.n }
pub fn pick<'a>(&'a self, s: &'a str) -> usize { s.len() + self.n as usize }
}
"#;
let expanded = expand(src).unwrap();
let source = flat(&expanded.source);
assert!(
source.contains("`Buf::sized` is generic over `const N`"),
"{source}"
);
assert!(
source.contains("`Buf::shown` uses `impl Trait`"),
"{source}"
);
assert!(!source.contains("fn rustcall_Buf_sized"), "{source}");
assert!(!source.contains("fn rustcall_Buf_shown"), "{source}");
assert!(source.contains("fn rustcall_Buf_pick("), "{source}");
let methods: Vec<(&str, &str)> = expanded.manifest.structs[0]
.methods
.iter()
.map(|m| (m.name.as_str(), m.skip_reason.as_str()))
.collect();
assert_eq!(
methods,
[
("sized", "generic_signature:const N"),
("shown", "impl_trait"),
("pick", "")
]
);
let lifetimes_only = expand(
r#"
#[julia]
pub struct Buf { pub n: i32 }
impl Buf {
pub fn pick<'a>(&'a self, s: &'a str) -> usize { s.len() + self.n as usize }
}
"#,
)
.unwrap();
let out = rustc("lifetimes", &lifetimes_only.source);
assert!(
out.status.success(),
"{}",
String::from_utf8_lossy(&out.stderr)
);
}
#[test]
fn a_generic_method_in_a_foreign_block_is_refused() {
let src = r#"
#[julia]
pub struct Gauge { pub v: f64 }
pub mod ops {
impl super::Gauge {
pub fn scaled<T: Into<f64>>(&self, k: T) -> f64 { self.v * k.into() }
pub fn read(&self) -> f64 { self.v }
}
}
"#;
let expanded = expand(src).unwrap();
let source = flat(&expanded.source);
assert!(
source.contains("`Gauge::scaled` is generic over `T`"),
"{source}"
);
assert!(!source.contains("fn rustcall_Gauge_scaled"), "{source}");
assert!(source.contains("fn rustcall_Gauge_read("), "{source}");
let methods: Vec<(&str, &str)> = expanded.manifest.structs[0]
.methods
.iter()
.map(|m| (m.name.as_str(), m.skip_reason.as_str()))
.collect();
assert_eq!(methods, [("scaled", "generic_signature:T"), ("read", "")]);
}
#[test]
fn the_refusal_carries_the_cfg() {
let src = r#"
#[julia]
pub struct Acc { pub total: i64 }
impl Acc {
pub fn new() -> Self { Acc { total: 0 } }
#[cfg(rustcall_never)]
pub fn echo<T: Copy>(&self, x: T) -> T { x }
}
#[cfg(rustcall_never)]
impl Acc {
pub fn other<T: Copy>(&self, x: T) -> T { x }
}
#[cfg(rustcall_never)]
#[julia]
pub struct Gone { pub x: i32 }
#[cfg(rustcall_never)]
impl Gone {
pub fn echo<T: Copy>(&self, x: T) -> T { x }
}
"#;
let expanded = expand(src).unwrap();
assert!(
expanded.source.contains("compile_error!"),
"{}",
expanded.source
);
let out = rustc("gated", &expanded.source);
assert!(
out.status.success(),
"{}\n{}",
String::from_utf8_lossy(&out.stderr),
expanded.source
);
}