rustcall_julia_core 0.2.0

Shared Rust core for RustCall.jl: FFI manifest model, syn-based extraction, wrapper codegen, generic specialization
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
//! Two gaps of the inline expander left by #471 (#477).
//!
//! 1. A method of a **generic** inline struct that is generic in its own right
//!    (`impl<T> W<T> { pub fn f<U>(..) }`) got a generic wrapper carrying both
//!    parameters, while instantiating the struct binds only `T`. It is refused
//!    at the method now, as #471 refuses a generic method of a concrete struct.
//! 2. A method taking a struct reference with a named lifetime
//!    (`fn f<'a>(&self, other: &'a Buf)`) got a wrapper spelling `other: &'a Buf`
//!    without declaring `'a`, so the expanded block did not compile. The
//!    wrapper declares the lifetimes its signature names now, with their
//!    bounds.
//!
//! Every block is compiled the way `rust"""` compiles it: `rustc` with no
//! `--edition`.

use std::{fs, process::Command};

use rustcall_julia_core::expand::expand;
use rustcall_julia_core::specialize::specialize;

fn rustc(label: &str, source: &str) -> std::process::Output {
    let dir = std::env::temp_dir().join(format!(
        "rustcall_inline_gaps_{}_{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();
    // No `--edition`, as `RustCall.compile_rust_to_shared_lib`.
    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 assert_compiles(label: &str, source: &str) {
    let out = rustc(label, source);
    assert!(
        out.status.success(),
        "{}\n{source}",
        String::from_utf8_lossy(&out.stderr)
    );
}

/// One line, without the line breaks `prettyplease` puts inside a long
/// parameter list (`f( a: A, b: B, )` reads `f(a: A, b: B)`).
fn flat(source: &str) -> String {
    source
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
        .replace("( ", "(")
        .replace(", )", ")")
}

const GENERIC_STRUCT_GENERIC_METHOD: &str = r#"
    #[julia]
    pub struct Wrap<T> { pub v: T }
    impl<T: Copy> Wrap<T> {
        pub fn new(v: T) -> Self { Wrap { v } }
        pub fn get(&self) -> T { self.v }
        pub fn pair<U: Copy>(&self, u: U) -> U { u }
    }
"#;

#[test]
fn a_generic_method_of_a_generic_struct_is_refused_at_the_method() {
    let expanded = expand(GENERIC_STRUCT_GENERIC_METHOD).unwrap();
    let source = flat(&expanded.source);
    assert!(
        source.contains("compile_error!"),
        "no refusal:\n{}",
        expanded.source
    );
    assert!(
        source.contains("`Wrap::pair` is generic over `U`"),
        "{source}"
    );
    assert!(source.contains("instantiated per struct type"), "{source}");

    // No generic wrapper carrying the unbound `U` ...
    let wrap = &expanded.manifest.structs[0];
    let wrappers: Vec<&str> = wrap
        .generic_wrappers
        .iter()
        .map(|w| w.name.as_str())
        .collect();
    assert!(!wrappers.contains(&"Wrap_pair"), "{wrappers:?}");
    assert!(!source.contains("fn Wrap_pair"), "{source}");
    // ... while the methods that use only the struct's parameter keep theirs.
    assert!(wrappers.contains(&"Wrap_get"), "{wrappers:?}");
    assert!(wrappers.contains(&"Wrap_new"), "{wrappers:?}");

    // The manifest describes what can be bound: no `pair`.
    let names: Vec<&str> = wrap.methods.iter().map(|m| m.name.as_str()).collect();
    assert_eq!(names, ["new", "get"]);

    // rustc stops at the refusal, not inside generated code.
    let out = rustc("refused", &expanded.source);
    assert!(!out.status.success());
    let stderr = String::from_utf8_lossy(&out.stderr);
    assert!(
        stderr.contains("`Wrap::pair` is generic over `U`"),
        "{stderr}"
    );
    assert!(!stderr.contains("cannot find type `U`"), "{stderr}");

    // What is left instantiates: `Wrap_get` at `T = i32` compiles.
    let get = wrap
        .generic_wrappers
        .iter()
        .find(|w| w.name == "Wrap_get")
        .unwrap();
    let out = specialize(
        &format!("{}\n{}", wrap.context_source, get.source),
        "Wrap_get",
        &[("T".into(), "i32".into())],
        "Wrap_get_i32",
    )
    .unwrap();
    assert!(
        out.source.contains("rustcall_Wrap_get_i32"),
        "{}",
        out.source
    );
}

/// `impl Trait` and a const parameter make a generic struct's method generic
/// in its own right as well; a lifetime does not.
#[test]
fn impl_trait_and_const_methods_of_a_generic_struct_are_refused() {
    let expanded = expand(
        r#"
        #[julia]
        pub struct Cell<T> { pub v: T }
        impl<T: Copy> Cell<T> {
            pub fn shown(&self, x: impl Copy) -> T { let _ = x; self.v }
            pub fn sized<const N: usize>(&self) -> usize { N }
            pub fn pick<'a>(&'a self, other: &'a Cell<T>) -> T { let _ = other; self.v }
        }
    "#,
    )
    .unwrap();
    let source = flat(&expanded.source);
    assert!(
        source.contains("`Cell::shown` uses `impl Trait`"),
        "{source}"
    );
    assert!(
        source.contains("`Cell::sized` is generic over `const N`"),
        "{source}"
    );
    let names: Vec<&str> = expanded.manifest.structs[0]
        .methods
        .iter()
        .map(|m| m.name.as_str())
        .collect();
    assert_eq!(names, ["pick"]);
}

/// The refusal exists only where the method does.
#[test]
fn the_generic_struct_refusal_carries_the_cfg() {
    let expanded = expand(
        r#"
        #[julia]
        pub struct Wrap<T> { pub v: T }
        impl<T: Copy> Wrap<T> {
            pub fn get(&self) -> T { self.v }
            #[cfg(rustcall_never)]
            pub fn pair<U: Copy>(&self, u: U) -> U { u }
        }
        #[cfg(rustcall_never)]
        impl<T: Copy> Wrap<T> {
            pub fn other<U: Copy>(&self, u: U) -> U { u }
        }

        #[cfg(rustcall_never)]
        #[julia]
        pub struct Gone<T> { pub v: T }
        #[cfg(rustcall_never)]
        impl<T: Copy> Gone<T> {
            pub fn pair<U: Copy>(&self, u: U) -> U { u }
        }
    "#,
    )
    .unwrap();
    assert!(
        expanded.source.contains("compile_error!"),
        "{}",
        expanded.source
    );
    assert_compiles("gated", &expanded.source);
}

const NAMED_LIFETIMES: &str = r#"
    #[julia]
    pub struct Buf { pub n: i32 }
    impl Buf {
        pub fn new(n: i32) -> Self { Buf { n } }
        pub fn sum<'a>(&self, other: &'a Buf) -> i32 { self.n + other.n }
        pub fn both<'a>(&'a self, other: &'a Buf) -> i32 { self.n * other.n }
        pub fn nested<'a, 'b: 'a>(&'a self, x: &'a Buf, y: &'b Buf) -> i32 { x.n - y.n + self.n }
        pub fn bounded<'a, 'b>(&self, x: &'a Buf, y: &'b Buf) -> i32 where 'b: 'a { x.n + y.n }
        pub fn absorb<'a>(&mut self, other: &'a mut Buf) { self.n += other.n; other.n = 0; }
        pub fn labelled<'a>(&self, other: &'a Buf, s: &'a str) -> usize { s.len() + other.n as usize }
    }

    #[julia]
    pub fn buf_total<'a>(a: &'a Buf, b: &'a Buf) -> i32 { a.n + b.n }

    pub mod ops {
        impl super::Buf {
            pub fn diff<'a>(&self, other: &'a super::Buf) -> i32 { self.n - other.n }
        }
    }
"#;

#[test]
fn a_named_lifetime_on_a_struct_reference_is_declared_on_the_wrapper() {
    let expanded = expand(NAMED_LIFETIMES).unwrap();
    let source = flat(&expanded.source);
    for sig in [
        "pub extern \"C\" fn rustcall_Buf_sum<'a>(ptr: *const Buf, other: &'a Buf)",
        "pub extern \"C\" fn rustcall_Buf_both<'a>(ptr: *const Buf, other: &'a Buf)",
        "pub extern \"C\" fn rustcall_Buf_nested<'a, 'b: 'a>(",
        "pub extern \"C\" fn rustcall_Buf_bounded<'a, 'b>(",
        "where 'b: 'a",
        "pub extern \"C\" fn rustcall_Buf_absorb<'a>(ptr: *mut Buf, other: &'a mut Buf)",
        "pub extern \"C\" fn rustcall_buf_total<'a>(a: &'a Buf, b: &'a Buf)",
        "pub extern \"C\" fn rustcall_Buf_diff<'a>(",
        // A lifetime only a string argument names is not declared: the
        // string becomes a pointer and a length.
        "pub extern \"C\" fn rustcall_Buf_labelled<'a>(ptr: *const Buf, other: &'a Buf, s_ptr: *const u8, s_len: usize)",
    ] {
        assert!(source.contains(sig), "missing `{sig}`:\n{source}");
    }
    assert_compiles("lifetimes", &expanded.source);

    // Every method is still reported for Julia to bind.
    let names: Vec<&str> = expanded.manifest.structs[0]
        .methods
        .iter()
        .map(|m| m.name.as_str())
        .collect();
    assert_eq!(
        names,
        ["new", "sum", "both", "nested", "bounded", "absorb", "labelled", "diff"]
    );
}

/// A lifetime only a lowered string names stays undeclared, so the wrappers of
/// `&'a str` functions are unchanged.
#[test]
fn a_lifetime_only_a_string_names_is_not_declared() {
    let expanded = expand(
        r#"
        #[julia]
        pub fn echo_len<'a>(s: &'a str) -> usize { s.len() }
    "#,
    )
    .unwrap();
    let source = flat(&expanded.source);
    assert!(
        source.contains("pub extern \"C\" fn rustcall_echo_len(s_ptr: *const u8, s_len: usize)"),
        "{source}"
    );
    assert_compiles("strings", &expanded.source);
}

/// A `for<'b>` binder inside a `where` predicate names a lifetime that is not
/// the item's: it must not drop the predicate from the wrapper, or the call
/// inside it loses the bound it needs (E0277, PR #480 review).
#[test]
fn a_higher_ranked_bound_is_kept_on_the_wrapper() {
    let expanded = expand(
        r#"
        pub trait Rel<T> { fn rel(&self, other: T) -> i32; }
        // Only for a `'static` argument, so the bound is not provable for an
        // arbitrary `'a`: a wrapper without it does not compile.
        impl<'x> Rel<&'static Buf> for &'x Buf {
            fn rel(&self, other: &'static Buf) -> i32 { self.n - other.n }
        }

        #[julia]
        pub struct Buf { pub n: i32 }
        impl Buf {
            pub fn new(n: i32) -> Self { Buf { n } }
            pub fn related<'a>(&self, other: &'a Buf) -> i32
            where
                for<'b> &'b Buf: Rel<&'a Buf>,
            {
                (&*self).rel(other)
            }
        }
    "#,
    )
    .unwrap();
    let source = flat(&expanded.source);
    assert!(
        source.contains(
            "pub extern \"C\" fn rustcall_Buf_related<'a>(ptr: *const Buf, other: &'a Buf)"
        ),
        "{source}"
    );
    assert!(
        source.contains(
            "other: &'a Buf) -> ::std::mem::MaybeUninit<i32> where for<'b> &'b Buf: Rel<&'a Buf>"
        ),
        "{source}"
    );
    assert_compiles("higher_ranked", &expanded.source);
}

/// A method `where` predicate naming `Self` is valid in the impl but not
/// verbatim on the wrapper, a free function where `Self` does not exist
/// (E0411). Every predicate is carried over, with `Self` spelled as the
/// impl's type `Buf`, qualified paths included (PR #480 review, #482). (A bare
/// `Self::Tag` is E0223 in an inherent impl of a concrete type, so it never
/// reaches the expander in a block that compiles.)
#[test]
fn a_self_predicate_is_carried_with_self_spelled_as_the_impl_type() {
    let expanded = expand(
        r#"
        pub trait Tagged { type Tag; fn tag() -> i32; }
        impl Tagged for Buf { type Tag = i32; fn tag() -> i32 { 3 } }
        pub trait Rel<T> { fn rel(&self, other: T) -> i32; }
        impl<'x, 'y> Rel<&'y Buf> for &'x Buf {
            fn rel(&self, other: &'y Buf) -> i32 { self.n - other.n }
        }

        #[julia]
        pub struct Buf { pub n: i32 }
        impl Buf {
            pub fn new(n: i32) -> Self { Buf { n } }
            pub fn run(&self) -> i32 where Self: Tagged { <Self as Tagged>::tag() + self.n }
            pub fn assoc(&self) -> i32 where Self: Tagged, <Self as Tagged>::Tag: Copy { self.n }
            pub fn plain(&self) -> i32 where Buf: Tagged { self.n }
            pub fn both<'a>(&self, other: &'a Buf) -> i32 where Self: Tagged, <Self as Tagged>::Tag: Copy + 'a { self.n + other.n }
        }
    "#,
    )
    .unwrap();
    let source = flat(&expanded.source);
    for sig in [
        "pub extern \"C\" fn rustcall_Buf_run(ptr: *const Buf) -> ::std::mem::MaybeUninit<i32> where Buf: Tagged, {",
        "pub extern \"C\" fn rustcall_Buf_assoc(ptr: *const Buf) -> ::std::mem::MaybeUninit<i32> where Buf: Tagged, <Buf as Tagged>::Tag: Copy, {",
        "pub extern \"C\" fn rustcall_Buf_plain(ptr: *const Buf) -> ::std::mem::MaybeUninit<i32> where Buf: Tagged, {",
        "pub extern \"C\" fn rustcall_Buf_both<'a>(ptr: *const Buf, other: &'a Buf) -> ::std::mem::MaybeUninit<i32> where Buf: Tagged, <Buf as Tagged>::Tag: Copy + 'a, {",
    ] {
        assert!(source.contains(sig), "missing `{sig}`:\n{source}");
    }
    assert_compiles("self_predicates", &expanded.source);
}

/// A `Self` predicate can be the only proof a call is valid: with
/// `Rel<&'static Buf>` as the only impl, `related<'a>` needs
/// `for<'b> &'b Self: Rel<&'a Buf>` on its wrapper, or `other` escapes as
/// `'static` (E0521). The wrapper carries it with `Self` spelled as the
/// impl's type, `<Self as Trait>::Assoc` included (PR #480 review, #482).
#[test]
fn a_self_predicate_is_carried_with_the_receiver_type() {
    let expanded = expand(
        r#"
        pub trait Rel<T> { type Out; fn rel(&self, other: T) -> i32; }
        impl<'x> Rel<&'static Buf> for &'x Buf {
            type Out = i32;
            fn rel(&self, other: &'static Buf) -> i32 { self.n - other.n }
        }

        #[julia]
        pub struct Buf { pub n: i32 }
        impl Buf {
            pub fn new(n: i32) -> Self { Buf { n } }
            pub fn related<'a>(&self, other: &'a Buf) -> i32
            where
                for<'b> &'b Self: Rel<&'a Buf>,
            {
                (&*self).rel(other)
            }
            pub fn qualified<'a>(&self, other: &'a Buf) -> i32
            where
                for<'b> &'b Self: Rel<&'a Buf>,
                for<'b> <&'b Self as Rel<&'a Buf>>::Out: Copy,
            {
                (&*self).rel(other)
            }
        }
    "#,
    )
    .unwrap();
    let source = flat(&expanded.source);
    assert!(
        source.contains(
            "fn rustcall_Buf_related<'a>(ptr: *const Buf, other: &'a Buf) -> ::std::mem::MaybeUninit<i32> where for<'b> &'b Buf: Rel<&'a Buf>"
        ),
        "{source}"
    );
    assert!(
        source.contains("for<'b> <&'b Buf as Rel<&'a Buf>>::Out: Copy"),
        "{source}"
    );
    assert_compiles("self_proof", &expanded.source);
}