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
3 changes: 2 additions & 1 deletion ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ manifest, so exports that differ build apart in a target directory they share.

```toml
# ffi.toml, beside the package's Cargo.toml unless --manifest-path says otherwise.
# Paths from the facade's root left out: a module, an item, or one method or field. Each
# Paths from the facade's root left out: a module, an item, or one method or field. A type
# no facade module re-exports goes by its crate's name and its public path there. Each
# must name something exported, or what a `cfg` leaves out of this build, as moq-tokio's
# `unix` is on Windows. Nothing else leaves an item out. As in rustdoc, a path a
# method or constant shares with a field names the method, and `field@` the field.
Expand Down
19 changes: 14 additions & 5 deletions ffi/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,23 @@ fn parse_item(tcx: TyCtxt<'_>, source: &str) -> Option<Box<ast::Item>> {
}
}

/// Whether `path`, from the facade's root, names an item a `cfg` leaves out of this build,
/// or a field or variant of one: the config names it for the builds that have it. Its first
/// segment no module has is stripped from that module or from one the module
/// glob-re-exports.
/// Whether `path` names an item a `cfg` leaves out of this build, or a field or variant of
/// one: the config names it for the builds that have it. The path runs from the facade's
/// root, or from a crate's by its name, as the walk names a type no facade module
/// re-exports.
fn configured_out(tcx: TyCtxt<'_>, path: &str) -> bool {
let path = path.strip_prefix("field@").unwrap_or(path);
let segments: Vec<&str> = path.split("::").collect();
let mut module = LOCAL_CRATE.as_def_id();
// Any crate of the name, since a build may link several of its versions.
let mut krates = (tcx.crates(()).iter()).filter(|&&k| tcx.crate_name(k).as_str() == segments[0]);
stripped_under(tcx, LOCAL_CRATE.as_def_id(), &segments)
|| krates.any(|&k| stripped_under(tcx, k.as_def_id(), &segments[1..]))
}

/// Whether `segments`, from `module`, name an item a `cfg` strips, or a field or variant of
/// one. Its first segment no module has is stripped from that module or from one the module
/// glob-re-exports.
fn stripped_under(tcx: TyCtxt<'_>, mut module: DefId, segments: &[&str]) -> bool {
for (i, segment) in segments.iter().enumerate() {
let name = segment.split('<').next().unwrap_or(segment);
let children = match module.as_local() {
Expand Down
11 changes: 6 additions & 5 deletions ffi/tests/fixtures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3636,9 +3636,9 @@ fn foreign_role_names() {
/// An exclude may name what a `cfg` leaves out of this build, whether the facade's own
/// module or another crate's that the facade re-exports through a glob, or a chain of them,
/// or a field or variant of the facade's or another crate's type, under a `cfg` or one a
/// `cfg_attr` adds, an inline module's type and a const-generic one's included, but not
/// what no build has, nor what a module only some of whose items the facade re-exports
/// leaves out.
/// `cfg_attr` adds, an inline module's type and a const-generic one's included, and by the
/// defining crate's path as well as the facade's, but not what no build has, nor what a
/// module only some of whose items the facade re-exports leaves out.
#[test]
fn configured_out() {
let package = package("configured_out", &[]);
Expand All @@ -3654,7 +3654,7 @@ fn configured_out() {
("Cargo.toml", &manifest),
(
"src/lib.rs",
"pub use ::dep::Size;\n\npub mod dep {\n\tpub use ::dep::*;\n}\n\n#[cfg(any())]\npub mod local {}\n\n\
"pub use ::dep::Size;\n\npub mod glob {\n\tpub use ::dep::*;\n}\n\n#[cfg(any())]\npub mod local {}\n\n\
mod inner {\n\tpub fn kept() -> u32 {\n\t\t1\n\t}\n\n\t#[cfg(any())]\n\tpub mod gone {}\n}\n\n\
mod middle {\n\tpub use crate::inner::*;\n}\n\npub mod outer {\n\tpub use crate::middle::*;\n}\n\n\
pub struct Shape<const N: usize = { 1 }> {\n\tpub n: u32,\n\t#[cfg(any())]\n\tpub gone: u32,\n\t#[cfg_attr(all(), cfg_attr(all(), cfg(any())))]\n\tpub gated: u32,\n}\n\n\
Expand All @@ -3663,7 +3663,7 @@ fn configured_out() {
),
(
"ffi.toml",
"exclude = [\"Gone\", \"Kind::Gone\", \"dep::Area::scaled\", \"Shape::gated\", \"Shape::gone\", \"Shape::missing\", \"Size::depth\", \"nested::Inner::gone\", \"dep::gone::Gone\", \"gone::Gone\", \"local::Gone\", \"outer::gone::Gone\"]\n",
"exclude = [\"Gone\", \"Kind::Gone\", \"dep::Size::depth\", \"dep::Size::missing\", \"glob::Area::scaled\", \"Shape::gated\", \"Shape::gone\", \"Shape::missing\", \"Size::depth\", \"nested::Inner::gone\", \"glob::gone::Gone\", \"gone::Gone\", \"local::Gone\", \"outer::gone::Gone\"]\n",
),
],
);
Expand All @@ -3681,6 +3681,7 @@ fn configured_out() {
errors,
[
"error: rust2-ffi: exclude `Gone` in the config names nothing exported",
"error: rust2-ffi: exclude `dep::Size::missing` in the config names nothing exported",
"error: rust2-ffi: exclude `Shape::missing` in the config names nothing exported",
"error: rust2-ffi: exclude `gone::Gone` in the config names nothing exported",
],
Expand Down
Loading