Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,12 @@ A generic type infers the instances it exports at, with no config: the one at it
defaults, if each type parameter has one; the one each inherent impl is of, such as
`impl Pair<u32>`; each choice, for each parameter, among its default and the types the
exports implement a trait bounding it for, one of the exports', which are `()`, a
primitive, `String`, or an exported type; and each instance a signature or field names,
once that is all its export refuses, which may name more. An instance that holds an
instance of a type inside it, such as `Cell<Cell<u8>>`, is refused, as it could name
ever deeper ones.
primitive, `String`, or an exported type or an instance of one, such as
`impl Seed<u64> for Wrap<u32>`; and each instance a signature, `pub` field, or constant
names at any choice its bounds allow, whether or not the rest of it maps, which may name
more. Inference finishes before any type is mapped, so mapping never adds an instance.
An instance that holds an instance of a type inside it, such as `Cell<Cell<u8>>`, is
refused, as it could name ever deeper ones.

A record or enum holds only values and `Copy` handles: one with a field that does not map,
or that holds itself, is an object instead, and the census lists it. A borrowed return is
Expand Down
5 changes: 5 additions & 0 deletions ffi/fixtures/api/examples/transcript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ fn main() {
api_lib::kept(Point { x: 1, y: -2 }),
api_lib::BOXED.value
);
let sides = Pair { left: 2u32, right: 5 };
println!("apply {}", api_lib::apply(sides, |side| side * 3));
println!("report {}", api_lib::report(sides, Print));
println!("wrapped {}", api_lib::kept(api_lib::Wrap { value: 4u32 }));
println!("keeps {}", api_lib::keeps(Print).value);

let token = api_lib::Token::new(7);
println!("token {} dropped {}", token.id, api_lib::dropped());
Expand Down
36 changes: 36 additions & 0 deletions ffi/fixtures/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,14 @@ pub struct Boxed<T> {

pub const BOXED: Boxed<u16> = Boxed { value: 42 };

/// Whether `log` kept a line, boxed: an instance only a function taking a foreign
/// implementation names.
pub fn keeps(log: impl Log) -> Boxed<u8> {
Boxed {
value: u8::from(log.log(2, "keeps")),
}
}

/// `from`'s weight.
pub fn weigh(from: impl Seed<u32>) -> u32 {
from.seed()
Expand Down Expand Up @@ -769,6 +777,34 @@ impl Seed<u64> for Size {
}
}

/// Each side of `pair` through `f`, summed: a function inferred at each instance of
/// `Pair` whose sides are alike, taking a lent closure of the inferred type.
pub fn apply<T: Copy, F: Fn(T) -> u32>(pair: Pair<T>, f: F) -> u32 {
f(pair.left) + f(pair.right)
}

/// Each side of `pair` to `log`, returning how many it kept: inferred as `apply` is,
/// taking a foreign implementation.
pub fn report<T: Copy + fmt::Debug, L: Log>(pair: Pair<T>, log: L) -> u32 {
[pair.left, pair.right]
.iter()
.filter(|side| log.log(1, &format!("{side:?}")))
.count() as u32
}

/// A wrapped value, which only its impl of `Seed<u64>` at `Wrap<u32>` names: each
/// function taking a seed infers that instance.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Wrap<T> {
pub value: T,
}

impl Seed<u64> for Wrap<u32> {
fn seed(&self) -> u64 {
u64::from(self.value) * 10
}
}

/// A second `Point`, whose last segment clashes with the crate root's: an instance named
/// by it takes its module too, and one named by the other keeps the shorter name.
pub mod polar {
Expand Down
36 changes: 22 additions & 14 deletions ffi/fixtures/reject/expected.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,32 @@ fixtures/reject/src/lib.rs:113:2: rust2-ffi: cannot export method `Text::replace
fixtures/reject/src/lib.rs:118:2: rust2-ffi: cannot export method `Text::later`: returned object that borrows `impl std::future::Future<Output = Text<'_>>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:125:1: rust2-ffi: cannot export fn `either`: reference inside a parameter `std::option::Option<&str>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:130:1: rust2-ffi: cannot export struct `Lease`: object no export returns; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:149:1: rust2-ffi: cannot export instance `Cell<std::vec::Vec<String>>`: Vec `std::string::String`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:149:1: rust2-ffi: cannot export instance `Cell<std::vec::Vec<String>>`: type argument `std::vec::Vec<String>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:14:1: rust2-ffi: cannot export fn `map`: foreign type `std::collections::HashMap`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:159:2: rust2-ffi: cannot export method `Cell<u8>::nest`: instance holding an instance of a type inside it `reject_fixture::Cell<reject_fixture::Cell<u8>>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:165:1: rust2-ffi: cannot export fn `excluded_cell`: excluded type `reject_fixture::Cell<u16>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:170:1: rust2-ffi: cannot export fn `listed`: refused type `reject_fixture::Cell<std::vec::Vec<String>>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:175:1: rust2-ffi: cannot export struct `Fixed`: generic type; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:178:1: rust2-ffi: cannot export fn `fixed`: const generic `reject_fixture::Fixed`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:183:9: rust2-ffi: cannot export use `HashSet`: re-export of another crate's item `std`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:186:1: rust2-ffi: cannot export fn `lend`: Waker outside a parameter taken by value; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:178:2: rust2-ffi: cannot export method `Pong<reject_fixture::Ping<u8>>::ping`: instance holding an instance of a type inside it `reject_fixture::Ping<reject_fixture::Pong<reject_fixture::Ping<u8>>>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:189:1: rust2-ffi: cannot export instance `Grow<(u32,)>`: type argument `(u32,)`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:192:2: rust2-ffi: cannot export method `Grow<u32>::grow`: refused type `reject_fixture::Grow<(u32,)>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:19:1: rust2-ffi: cannot export fn `generic`: generic parameter `std::marker::Copy`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:201:2: rust2-ffi: fn `reject_fixture::Limit::max` and field `reject_fixture::Limit::max` are both `reject_limit_max` in C
fixtures/reject/src/lib.rs:193:2: note: field `reject_fixture::Limit::max` is here
fixtures/reject/src/lib.rs:207:1: rust2-ffi: cannot export fn `spawn`: closure kept past the call `impl FnOnce() + Send + 'static`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:212:1: rust2-ffi: cannot export fn `later`: closure kept past the call `impl FnOnce()`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:217:1: rust2-ffi: cannot export fn `twice`: closure bound `std::clone::Clone`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:223:1: rust2-ffi: cannot export fn `both`: closure type taken twice `F`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:229:1: rust2-ffi: cannot export fn `read`: lent object that borrows `&Text<'_>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:203:1: rust2-ffi: cannot export instance `Shell<(u32,)>`: type argument `(u32,)`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:208:1: rust2-ffi: cannot export struct `Core`: generic type; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:212:1: rust2-ffi: cannot export fn `shell`: refused type `reject_fixture::Shell<(u32,)>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:221:1: rust2-ffi: cannot export struct `Probe`: generic type; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:223:1: rust2-ffi: cannot export fn `probe`: const generic; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:228:1: rust2-ffi: cannot export fn `excluded_cell`: excluded type `reject_fixture::Cell<u16>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:233:1: rust2-ffi: cannot export fn `listed`: refused type `reject_fixture::Cell<std::vec::Vec<String>>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:238:1: rust2-ffi: cannot export struct `Fixed`: generic type; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:241:1: rust2-ffi: cannot export fn `fixed`: const generic `reject_fixture::Fixed`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:246:9: rust2-ffi: cannot export use `HashSet`: re-export of another crate's item `std`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:249:1: rust2-ffi: cannot export fn `lend`: Waker outside a parameter taken by value; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:264:2: rust2-ffi: fn `reject_fixture::Limit::max` and field `reject_fixture::Limit::max` are both `reject_limit_max` in C
fixtures/reject/src/lib.rs:256:2: note: field `reject_fixture::Limit::max` is here
fixtures/reject/src/lib.rs:26:1: rust2-ffi: cannot export fn `borrowing`: borrowing future from a function that is not async; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:270:1: rust2-ffi: cannot export fn `spawn`: closure kept past the call `impl FnOnce() + Send + 'static`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:275:1: rust2-ffi: cannot export fn `later`: closure kept past the call `impl FnOnce()`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:280:1: rust2-ffi: cannot export fn `twice`: closure bound `std::clone::Clone`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:286:1: rust2-ffi: cannot export fn `both`: closure type taken twice `F`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:292:1: rust2-ffi: cannot export fn `read`: lent object that borrows `&Text<'_>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:31:1: rust2-ffi: cannot export fn `nested`: nested Option `std::option::Option<std::boxed::Box<std::option::Option<u32>>>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:40:1: rust2-ffi: cannot export fn `peek`: reference to a handle inside a value `std::boxed::Box<Opaque>`; exclude it in the config to leave it out
fixtures/reject/src/lib.rs:45:1: rust2-ffi: cannot export static `COUNT`: static; exclude it in the config to leave it out
Expand Down
63 changes: 63 additions & 0 deletions ffi/fixtures/reject/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,69 @@ impl Cell<u8> {
}
}

/// Two generic types whose methods each wrap the other's instance in their own: inferring
/// from `Ping<u8>` names `Pong<Ping<u8>>`, whose `ping` names `Ping<Pong<Ping<u8>>>`, which
/// holds a `Ping` inside a `Ping` and ends inference there.
pub struct Ping<T>(T);

pub struct Pong<T>(T);

impl<T> Ping<T> {
pub fn pong(self) -> Pong<Ping<T>> {
Pong(self)
}
}

impl<T> Pong<T> {
pub fn ping(self) -> Ping<Pong<T>> {
Ping(self)
}
}

pub fn ping(value: u8) -> Ping<u8> {
Ping(value)
}

/// A generic type whose method wraps its argument in a tuple: `Grow<(u32,)>` is refused,
/// as a tuple is no type argument, so inference names nothing through its `grow`.
pub struct Grow<T>(T);

impl<T> Grow<T> {
pub fn grow(self) -> Grow<(T,)> {
Grow((self.0,))
}
}

pub fn grow(value: u32) -> Grow<u32> {
Grow(value)
}

/// A record whose instance at a tuple is refused before its fields map, so `Core<u8>`,
/// which only those fields name, is neither inferred nor needed.
pub struct Shell<T> {
pub core: Core<u8>,
pub tag: T,
}

pub struct Core<T> {
pub value: T,
}

pub fn shell() -> Shell<(u32,)> {
Shell {
core: Core { value: 1 },
tag: (2,),
}
}

/// A const generic function whose bound names an instance nothing else does: refused at
/// its const parameter, before any candidate for the other is mapped.
pub struct Probe<T>(T);

pub fn probe<const N: usize>(x: impl Into<Probe<u8>>) {
let _ = x;
}

/// An instance the config excludes.
pub fn excluded_cell(cell: Cell<u16>) -> u16 {
cell.0
Expand Down
Loading