From 054b9f48b7a9e25dbddc12025a92d6c90e7b6181 Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 19:35:52 -0700 Subject: [PATCH 1/3] chore(quest): claim quest/m1/ffi-export/crates.md Quest-Claim: d493ca2cdba1f552 From 07906cecca18053e0394fb40ef8d372fdc21d72e Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 20:02:26 -0700 Subject: [PATCH 2/3] feat(ffi)!: name a field apart from the method of its name with field@ A field and a method of one name claimed one C name that `rename` could not split, so moq's config excluded moq-tokio's `Backoff::{initial,max,timeout}` both ways. As in rustdoc, a path a method or constant shares with a field now names the method, and `field@` before it names the field, in `exclude` and `rename`. moq exports both: `moq_backoff_initial` resolves the default and `moq_backoff_initial_setting` reads the field. The quest keeps what remains: moq-clock's C names, which wait on moq moving it into kio::clock. Co-Authored-By: Claude Opus 5.5 --- ffi/README.md | 6 ++++-- ffi/fixtures/api/examples/transcript.rs | 10 ++++++++++ ffi/fixtures/api/ffi.toml | 9 +++++++-- ffi/fixtures/api/src/lib.rs | 22 ++++++++++++++++++++++ ffi/fixtures/reject/expected.txt | 3 +++ ffi/fixtures/reject/ffi.toml | 4 ++++ ffi/fixtures/reject/src/lib.rs | 16 ++++++++++++++++ ffi/moq/ffi.toml | 11 ++++++----- ffi/moq/moq.c | 7 +++++++ ffi/src/abi.rs | 24 ++++++++++++++++++++---- ffi/src/collect.rs | 16 +++++++++++++--- ffi/src/config.rs | 6 ++++-- ffi/src/driver.rs | 22 ++++++++++++++++++---- ffi/src/glue.rs | 2 +- ffi/src/ir.rs | 6 ++++-- ffi/tests/api.c | 6 ++++++ quest/m1/ffi-export/README.md | 2 +- quest/m1/ffi-export/crates.md | 21 +++++++++++---------- 18 files changed, 157 insertions(+), 36 deletions(-) diff --git a/ffi/README.md b/ffi/README.md index 7c5dc1a35..eada90b67 100644 --- a/ffi/README.md +++ b/ffi/README.md @@ -31,7 +31,8 @@ package = "moq-net" # The prefix of every C name, the package's name in snake case by default. prefix = "moq" # Paths from the crate root left out: a module, an item, or one method or field. Each -# must name something exported. Nothing else leaves an item out. +# must name something exported. 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. exclude = ["track::Producer::poll_closed"] # C names, without the prefix, for paths whose names the naming rule makes clash. @@ -121,7 +122,8 @@ refused, and the census counts why. Joining loses where segments split, and every export shares the prefix, so every C name is registered, and two items claiming one is a compile error at both spans, fixed by a `rename`: moq-net's `session::Stats` and `Session::stats` are both - `moq_session_stats`. An instance is named by `instances`, and a type instance's + `moq_session_stats`. So are a field and the method of its name, as moq-tokio's + `Backoff::initial`; `field@Backoff::initial` names the field. An instance is named by `instances`, and a type instance's methods through that name. An `exclude` of a method by the generic type's path leaves it out of every instance, and by the instance's path (`"Producer::stats"`) out of that one; a `rename` of a method by the generic type's path names it at every diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index fa31d5309..ef91596fa 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -241,4 +241,14 @@ fn main() { println!("token {} dropped {}", token.id, api_lib::dropped()); drop(token); println!("dropped {}", api_lib::dropped()); + + let retry = api_lib::Retry::new(Some(5)); + let limit = retry.limit; + println!( + "retry {} {} {} {}", + u8::from(limit.is_some()), + limit.unwrap_or(0), + retry.limit(), + retry.delay() + ); } diff --git a/ffi/fixtures/api/ffi.toml b/ffi/fixtures/api/ffi.toml index 250528b14..65cd8da62 100644 --- a/ffi/fixtures/api/ffi.toml +++ b/ffi/fixtures/api/ffi.toml @@ -1,8 +1,13 @@ package = "api-fixture" prefix = "api" # The list's tail is not `Clone`, so its getter has no copy to return, one instance leaves -# out a method the others keep, and `Seed` is generic, so no callback implements it. -exclude = ["Node::next", "Pair::swap", "Seed"] +# out a method the others keep, `Seed` is generic, so no callback implements it, and +# `field@` leaves out the field `Retry::delay` alone, which the method takes the name of. +exclude = ["Node::next", "Pair::swap", "Seed", "field@Retry::delay"] + +# The field `Retry::limit`, whose name its method takes. +[rename] +"field@Retry::limit" = "retry_limit_setting" # Generic types at the instances C uses, each with its C name: `Pair` is # `Pair`, and `swap` returns each `Pair` of a `Size` as the other. diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index f714a5758..d068a8448 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -461,6 +461,28 @@ impl Drop for Token { } } +/// Retry settings: each field is unset until C sets it, and the method of its name +/// resolves it, as Rust's accessors do. Not exhaustive, so an object with getters. +#[non_exhaustive] +pub struct Retry { + pub limit: Option, + pub delay: Option, +} + +impl Retry { + pub fn new(limit: Option) -> Self { + Self { limit, delay: None } + } + + pub fn limit(&self) -> u32 { + self.limit.unwrap_or(3) + } + + pub fn delay(&self) -> u32 { + self.delay.unwrap_or(100) + } +} + /// How many tokens have dropped. pub fn dropped() -> u32 { DROPPED.load(std::sync::atomic::Ordering::Relaxed) diff --git a/ffi/fixtures/reject/expected.txt b/ffi/fixtures/reject/expected.txt index 66b78c3a0..1ef24e68f 100644 --- a/ffi/fixtures/reject/expected.txt +++ b/ffi/fixtures/reject/expected.txt @@ -21,6 +21,8 @@ fixtures/reject/src/lib.rs:180:1: rust2-ffi: instance `copied()` in the config e fixtures/reject/src/lib.rs:180:1: rust2-ffi: instance `copied(U: u8)` in the config exports nothing: `U` is none of its type parameters `T` fixtures/reject/src/lib.rs:186:2: rust2-ffi: instance `Bounded::get(U: u64)` in the config exports nothing: nothing exported reaches it 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:202: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:194: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:31:1: rust2-ffi: cannot export fn `nested`: nested Option `std::option::Option>>`; exclude it in the config to leave it out fixtures/reject/src/lib.rs:36:1: rust2-ffi: instance `Opaque` in the config exports nothing: `reject_fixture::Opaque` takes no type arguments @@ -42,3 +44,4 @@ fixtures/reject/src/lib.rs:9:1: rust2-ffi: cannot export fn `pair`: tuple; exclu fixtures/reject/src/lib.rs:9:1: rust2-ffi: instance `pair(T: u8)` in the config exports nothing: it takes no type parameters no span: rust2-ffi: instance `Missing` in the config exports nothing: `reject_fixture::Missing` names no exported struct or enum no span: rust2-ffi: instance `missing(T: u8)` in the config exports nothing: `missing` names no exported function or method +no span: rust2-ffi: rename `field@Limit::min` in the config names nothing exported diff --git a/ffi/fixtures/reject/ffi.toml b/ffi/fixtures/reject/ffi.toml index 3e61a3615..08c941106 100644 --- a/ffi/fixtures/reject/ffi.toml +++ b/ffi/fixtures/reject/ffi.toml @@ -3,6 +3,10 @@ prefix = "reject" # An exclusion leaves an item out, so nothing refuses it. exclude = ["Excluded"] +# A field that is none: `min` names nothing of `Limit`. +[rename] +"field@Limit::min" = "limit_min" + # Instances that cannot export: an unmet bound, a default argument no key can spell, a # type that is not generic, and a path that names nothing. `Cell` and `Cell` export, and nothing else of `Cell` does. [instances] diff --git a/ffi/fixtures/reject/src/lib.rs b/ffi/fixtures/reject/src/lib.rs index 3b5eb25d1..fceaf0080 100644 --- a/ffi/fixtures/reject/src/lib.rs +++ b/ffi/fixtures/reject/src/lib.rs @@ -187,3 +187,19 @@ impl Bounded { U::from(self.0) } } + +/// A field and a method of one name, which claim one C name until `field@` renames one. +#[non_exhaustive] +pub struct Limit { + pub max: u32, +} + +impl Limit { + pub fn new(max: u32) -> Self { + Self { max } + } + + pub fn max(&self) -> u32 { + self.max.max(1) + } +} diff --git a/ffi/moq/ffi.toml b/ffi/moq/ffi.toml index 2d762effd..e6435ad15 100644 --- a/ffi/moq/ffi.toml +++ b/ffi/moq/ffi.toml @@ -139,11 +139,6 @@ exclude = [ [crates.moq-tokio] exclude = [ "Addrs", - # Not refused: each names both a field and a method, whose getters `rename` cannot - # tell apart, so both are left out. - "Backoff::initial", - "Backoff::max", - "Backoff::timeout", "Client::connect", "Listener::accept_health", "Listener::local_addr", @@ -183,6 +178,7 @@ exclude = [ "crypto::install_default", "crypto::provider", "failover::Attempt", + "field@Log::level", "listen::Config::preferred_v4", "listen::Config::preferred_v6", "listen::Config::version", @@ -329,6 +325,11 @@ exclude = [ "connect::Error" = "connect_target_error" # The rule makes both this type and `Connection::monitor` `moq_connection_monitor`. "connection::Monitor" = "monitor" +# The rule makes each of these fields and the method of its name, which resolves its +# default, `moq_backoff_`. +"field@Backoff::initial" = "backoff_initial_setting" +"field@Backoff::max" = "backoff_max_setting" +"field@Backoff::timeout" = "backoff_timeout_setting" [crates.hang] exclude = [ diff --git a/ffi/moq/moq.c b/ffi/moq/moq.c index 3d3da4b5a..c8a7a86fd 100644 --- a/ffi/moq/moq.c +++ b/ffi/moq/moq.c @@ -248,6 +248,13 @@ int main(void) { CHECK(moq_connect_error_is_auth(&unauthorized)); CHECK(is(moq_connect_error_to_string(&unauthorized), "unauthorized")); + /* A backoff's fields are settings left unset, and the methods of their names resolve them. */ + moq_backoff *backoff = moq_backoff_default(); + CHECK(moq_backoff_initial_setting(backoff) == NULL); + moq_duration initial = moq_backoff_initial(backoff); + CHECK(initial.secs == 1 && initial.nanos == 0); + moq_backoff_free(backoff); + if (failures == 0) { printf("ok\n"); } diff --git a/ffi/src/abi.rs b/ffi/src/abi.rs index efae91827..38b8f9342 100644 --- a/ffi/src/abi.rs +++ b/ffi/src/abi.rs @@ -12,7 +12,7 @@ use std::collections::{BTreeMap, HashMap}; use rustc_span::Span; -use crate::ir::{Api, Dyn, Function, Int, Receiver, Type}; +use crate::ir::{Access, Api, Dyn, Function, Int, Receiver, Type}; /// A C name claimed twice: the name, then what claimed it first and second. pub type Clash = (String, (String, Span), (String, Span)); @@ -161,16 +161,29 @@ impl<'a> Abi<'a> { } /// A function's C name: an instance's as the config names it, else a method's through - /// its type's, unless renamed itself. + /// its type's, unless renamed itself. A field is renamed by `field@` and its path, or + /// by its path unless a method or constant takes it. fn function_name(&self, function: &Function, owner: Option<&Ty>) -> String { if let Some(instance) = &function.instance { return format!("{}_{}", self.prefix, instance.name); } + let path = function.path.join("::"); + let shared = match function.access { + Access::Field { shared } => { + let key = format!("field@{path}"); + if let Some(renamed) = self.rename.get(&key) { + self.renamed.borrow_mut().insert(key); + return format!("{}_{renamed}", self.prefix); + } + shared + } + _ => false, + }; let owner = match owner { Some(Ty::Struct(name) | Ty::Enum(name) | Ty::Handle { name, .. }) => name, _ => return self.name(&function.path), }; - match self.rename.contains_key(&function.path.join("::")) { + match !shared && self.rename.contains_key(&path) { true => self.name(&function.path), false => format!("{owner}_{}", snake(function.name())), } @@ -454,7 +467,10 @@ impl<'a> Abi<'a> { functions.extend(api.functions.iter().map(|f| (f, None))); for (function, owner) in functions { let export = self.export(function, owner.as_ref()); - let what = format!("fn `{}`", function.path.join("::")); + let what = match function.access { + Access::Field { .. } => format!("field `{}`", function.path.join("::")), + _ => format!("fn `{}`", function.path.join("::")), + }; self.claim(export.call.name.clone(), what.clone(), function.span); if let Some((handle, poll, free)) = &export.future { self.claim(handle.clone(), what.clone(), function.span); diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index 76a543e4b..fe559a9f4 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1179,7 +1179,17 @@ impl<'tcx> Collector<'tcx> { } let name = field.name.to_string(); let shown = [&site.shown[..], std::slice::from_ref(&name)].concat(); - if self.exclude(&shown) { + // As in rustdoc, `field@` and the path name the field, and the path alone does + // unless a method or constant takes the name, which it then names. + let shared = (tcx.inherent_impls(site.key.def_id).iter()) + .flat_map(|&imp| tcx.associated_items(imp).filter_by_name_unhygienic(field.name)) + .any(|item| { + matches!(item.kind, ty::AssocKind::Fn { .. } | ty::AssocKind::Const { .. }) + && tcx.visibility(item.def_id).is_public() + }); + let mut key = shown.clone(); + key[1].insert_str(0, "field@"); + if self.exclude(&key) || (!shared && self.exclude(&shown)) { continue; } let span = tcx.def_span(field.did); @@ -1196,10 +1206,10 @@ impl<'tcx> Collector<'tcx> { err: None, future: false, eager: false, - access: Access::Field, + access: Access::Field { shared }, instance: None, }), - Err(why) => self.refuse(&shown, "field", span, vec![why], false), + Err(why) => self.refuse(if shared { &key } else { &shown }, "field", span, vec![why], false), } } getters diff --git a/ffi/src/config.rs b/ffi/src/config.rs index 6c4a9b486..e6be19d57 100644 --- a/ffi/src/config.rs +++ b/ffi/src/config.rs @@ -15,12 +15,14 @@ pub struct Config { /// The prefix of every C name, the package's name in snake case by default. pub prefix: Option, /// Paths from the crate root left out of the export: a module, an item, or one - /// method (`track::Producer::poll_closed`). Each must name something exported. + /// method or field (`track::Producer::poll_closed`). Each must name something + /// exported. As in rustdoc, a path a method or constant shares with a field names the + /// method, and `field@` before it names the field. #[serde(default)] pub exclude: Vec, /// C names, without the prefix, for paths from the crate root whose names the rule /// makes clash: `"session::Stats" = "session_stats_snapshot"`. A type's methods are - /// named through its new name. + /// named through its new name. A field is named by its path as `exclude` names it. #[serde(default)] pub rename: BTreeMap, /// Generic types exported at instances, each with its C name, without the prefix: diff --git a/ffi/src/driver.rs b/ffi/src/driver.rs index 72d0f9476..9a010bd0d 100644 --- a/ffi/src/driver.rs +++ b/ffi/src/driver.rs @@ -96,18 +96,21 @@ impl Callbacks for Export { return Compilation::Stop; }; let collected = collect::collect(tcx, &crates); - // Each rename, by the Rust path it names, which starts with its crate's name. + // Each rename, by the Rust path it names, which starts with its crate's name, after + // a field's `field@`. let mut renames = BTreeMap::new(); for (report, package) in collected.reports.iter().zip(&self.crates) { for (path, name) in self.items(&package.name).1 { - renames.insert(format!("{}::{path}", report.krate), name.clone()); + let (field, path) = field(path); + renames.insert(format!("{field}{}::{path}", report.krate), name.clone()); } } let abi = Abi::new(&collected.api, &self.config.prefix(), &renames, &collected.instances); for key in abi.unused_renames() { + let (field, key) = field(key); let (krate, path) = key.split_once("::").expect("a rename starts with its crate"); tcx.dcx().err(format!( - "rust2-ffi: rename `{path}` in {} names nothing exported", + "rust2-ffi: rename `{field}{path}` in {} names nothing exported", self.table(&collected, krate) )); } @@ -177,7 +180,10 @@ impl Callbacks for Export { format!("rust2-ffi: {second} and {first} are both `{name}` in C"), ) .with_span_note(*first_span, format!("{first} is here")) - .with_help("rename or exclude one in the config") + .with_help(match first.starts_with("field ") || second.starts_with("field ") { + true => "rename or exclude one in the config, a field by `field@` and its path", + false => "rename or exclude one in the config", + }) .emit(); } if tcx.dcx().has_errors().is_some() { @@ -312,3 +318,11 @@ pub fn write(path: &Path, text: &str) -> Result<(), String> { } std::fs::write(path, text).map_err(|err| format!("writing {}: {err}", path.display())) } + +/// A config path's `field@`, if it names a field by it, and the path after it. +fn field(path: &str) -> (&str, &str) { + match path.strip_prefix("field@") { + Some(path) => ("field@", path), + None => ("", path), + } +} diff --git a/ffi/src/glue.rs b/ffi/src/glue.rs index 84e1f1c0f..9d342f700 100644 --- a/ffi/src/glue.rs +++ b/ffi/src/glue.rs @@ -895,7 +895,7 @@ impl Gen<'_> { } let call = match function.access { Access::Call => format!("{callee}({})", args.join(", ")), - Access::Field => format!("&({}).{}", args[0], function.name()), + Access::Field { .. } => format!("&({}).{}", args[0], function.name()), Access::Const => callee, }; let body = stmts.join("\n\t"); diff --git a/ffi/src/ir.rs b/ffi/src/ir.rs index a3f7763f6..54334c8eb 100644 --- a/ffi/src/ir.rs +++ b/ffi/src/ir.rs @@ -177,8 +177,10 @@ pub struct Instance { pub enum Access { /// It calls the function. Call, - /// It reads a `pub` field of its receiver. - Field, + /// It reads a `pub` field of its receiver. `shared` when a method or constant of its + /// type takes its name, which its path then names, so the config names the field + /// `field@`, as rustdoc does. + Field { shared: bool }, /// It reads a constant that is not a `bool` or integer. Const, } diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 49d636c54..84e2cf54c 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -347,6 +347,12 @@ static int script(void) { printf("token %u dropped %u\n", api_token_id(token), api_dropped()); api_token_free(token); printf("dropped %u\n", api_dropped()); + + /* A field and the method of its name, each its own function. */ + api_retry *retry = api_retry_new((api_option_u32){.some = true, .value = 5}); + api_option_u32 limit = api_retry_limit_setting(retry); + printf("retry %d %u %u %u\n", limit.some, limit.value, api_retry_limit(retry), api_retry_delay(retry)); + api_retry_free(retry); /* Each future dropped its waker once it finished. */ return drops == 4 ? 0 : 1; } diff --git a/quest/m1/ffi-export/README.md b/quest/m1/ffi-export/README.md index d728049b2..aee626bde 100644 --- a/quest/m1/ffi-export/README.md +++ b/quest/m1/ffi-export/README.md @@ -38,7 +38,7 @@ Every generator on the C ABI: - [Paths without a lifetime](/quest/m1/ffi-export/paths.md) - moq's owned `Path` exports, and its excludes go - [Instances the config can't name yet](/quest/m1/ffi-export/instance-keys.md) - `&T` keys, `IntoIterator` as a `Vec`, `serde_json::Value`, and `Catalog<()>` - [Closures lent for the call](/quest/m1/ffi-export/lent-closures.md) - a callback borrowed for one call, as `modify`'s `Guard` needs -- [One library from several crates](/quest/m1/ffi-export/crates.md) - the C names the several-crate export still can't give +- [moq-clock's C names once it moves into kio](/quest/m1/ffi-export/crates.md) - `moq_now` and `moq_origin` become the clock's, after moq moves it into `kio::clock` - [Driving sessions and origins](/quest/m1/ffi-export/driving.md) - how a binding polls moq's `Driver` - [A contract symbol](/quest/m1/ffi-export/contract.md) - a header and a library from different builds refuse to mix - [C++ bindings](/quest/m1/ffi-export/cpp.md) - C++17, 20, and 23 over the C ABI, with native futures diff --git a/quest/m1/ffi-export/crates.md b/quest/m1/ffi-export/crates.md index 14cb63ba4..461763eb4 100644 --- a/quest/m1/ffi-export/crates.md +++ b/quest/m1/ffi-export/crates.md @@ -1,17 +1,18 @@ -# [M] One library from several crates +# [S] moq-clock's C names once it moves into kio ## Goal -moq-net, moq-pattern, moq-clock, moq-tokio, hang, and moq-mux export into one library -with every C name the bindings need, so nothing is excluded for a name clash. +moq-clock's items export with C names that say they are the clock's: today `now()` and +`origin()` are `moq_now` and `moq_origin`, which reads like moq-net's origin types. -## Plan +This waits on moq's [kio::clock quest](https://github.com/moq-dev/moq/blob/main/quest/m1/kio-clock.md), +which moves moq-clock into `kio::clock`. It has landed once `rs/moq-clock` is gone from +moq's `main`. -They export through an empty package standing in for moq-ffi's remainder, which -`just ffi standin` writes. Left: +## Plan -- moq-clock's C names (`moq_now`, `moq_origin`) wait for its merge into kio. -- A field and a method of one name, such as moq-tokio's `Backoff::initial`, claim one C - name that `rename` cannot split, so [the config](/ffi/moq/ffi.toml) excludes both. +Then bump the moq pin and export `kio::clock` in moq-clock's place, in +[the config](/ffi/moq/ffi.toml) and `just ffi standin`, so the rule names its items +`moq_clock_*` and moq-clock's renames go. -Public API: the exporter's config and the C names it emits. +Public API: the C names moq's export emits. From 844b72eedb5cd6ee4d74f22dd4f7c6ec6227e87b Mon Sep 17 00:00:00 2001 From: Luke Curley <432854+kixelated@users.noreply.github.com> Date: Tue, 27 Oct 2026 23:55:09 -0700 Subject: [PATCH 3/3] fix(ffi): a field shares its path only with a method of its own instance A method of another instance's impl, or whose bounds the instance fails, no longer takes a field's bare path. Add ffi/MIGRATE.md for the breaking config change. Co-Authored-By: Claude Opus 5.5 --- ffi/MIGRATE.md | 7 +++++++ ffi/fixtures/api/examples/transcript.rs | 1 + ffi/fixtures/api/ffi.toml | 7 +++++-- ffi/fixtures/api/src/lib.rs | 20 ++++++++++++++++++++ ffi/src/collect.rs | 11 ++++++++--- ffi/tests/api.c | 3 +++ 6 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 ffi/MIGRATE.md diff --git a/ffi/MIGRATE.md b/ffi/MIGRATE.md new file mode 100644 index 000000000..6bb4c3ef5 --- /dev/null +++ b/ffi/MIGRATE.md @@ -0,0 +1,7 @@ +# Migrating + +## Unreleased + +- In `exclude` and `rename`, a path that a field shares with a method or constant of its + type, such as `Backoff::initial`, names only the method, as in rustdoc. Name the field + `field@Backoff::initial`: an `exclude` that left out both now needs both entries. diff --git a/ffi/fixtures/api/examples/transcript.rs b/ffi/fixtures/api/examples/transcript.rs index ef91596fa..283aacf88 100644 --- a/ffi/fixtures/api/examples/transcript.rs +++ b/ffi/fixtures/api/examples/transcript.rs @@ -251,4 +251,5 @@ fn main() { retry.limit(), retry.delay() ); + println!("slot {}", api_lib::Slot::::new(200).value()); } diff --git a/ffi/fixtures/api/ffi.toml b/ffi/fixtures/api/ffi.toml index 65cd8da62..80c123bf7 100644 --- a/ffi/fixtures/api/ffi.toml +++ b/ffi/fixtures/api/ffi.toml @@ -2,8 +2,9 @@ package = "api-fixture" prefix = "api" # The list's tail is not `Clone`, so its getter has no copy to return, one instance leaves # out a method the others keep, `Seed` is generic, so no callback implements it, and -# `field@` leaves out the field `Retry::delay` alone, which the method takes the name of. -exclude = ["Node::next", "Pair::swap", "Seed", "field@Retry::delay"] +# `field@` leaves out a field alone where a method takes its path, as `Retry`'s and +# `Slot`'s do. `Slot` has no such method, so its path alone names its field. +exclude = ["Node::next", "Pair::swap", "Seed", "Slot::value", "field@Retry::delay", "field@Slot::value"] # The field `Retry::limit`, whose name its method takes. [rename] @@ -16,6 +17,8 @@ exclude = ["Node::next", "Pair::swap", "Seed", "field@Ret "Pair" = "size_pair" "Pair" = "pair_size" "Stack" = "counters" +"Slot" = "byte_slot" +"Slot" = "slot" # Generic functions and methods likewise, each type parameter by name, an `impl Trait` one # by its argument's, in any order. "Counter::seeded(from: api_fixture::Point)" = "counter_seeded_point" diff --git a/ffi/fixtures/api/src/lib.rs b/ffi/fixtures/api/src/lib.rs index d068a8448..95ef55e78 100644 --- a/ffi/fixtures/api/src/lib.rs +++ b/ffi/fixtures/api/src/lib.rs @@ -616,3 +616,23 @@ pub fn grow(area: &dep::Area, by: u32) -> dep::Area { height: size.height + by, }) } + +/// A slot whose method `value` only `Slot` has, so its path alone names `Slot`'s +/// field and `Slot`'s method. Not exhaustive, so an object with getters. +#[non_exhaustive] +pub struct Slot { + pub value: T, +} + +impl Slot { + pub fn new(value: T) -> Self { + Self { value } + } +} + +impl Slot { + /// The value widened and doubled, apart from the field. + pub fn value(&self) -> u16 { + u16::from(self.value) * 2 + } +} diff --git a/ffi/src/collect.rs b/ffi/src/collect.rs index fe559a9f4..84ad921ae 100644 --- a/ffi/src/collect.rs +++ b/ffi/src/collect.rs @@ -1173,6 +1173,7 @@ impl<'tcx> Collector<'tcx> { fn getters(&mut self, site: &Site<'tcx>) -> Vec { let tcx = self.tcx; let mut getters = Vec::new(); + let self_ty = self.site_ty(site); for field in &tcx.adt_def(site.key.def_id).non_enum_variant().fields { if !field.vis.is_public() { continue; @@ -1180,12 +1181,16 @@ impl<'tcx> Collector<'tcx> { let name = field.name.to_string(); let shown = [&site.shown[..], std::slice::from_ref(&name)].concat(); // As in rustdoc, `field@` and the path name the field, and the path alone does - // unless a method or constant takes the name, which it then names. + // unless a method or constant of this instance takes the name, which it then names. let shared = (tcx.inherent_impls(site.key.def_id).iter()) - .flat_map(|&imp| tcx.associated_items(imp).filter_by_name_unhygienic(field.name)) - .any(|item| { + .filter_map(|&imp| Some((imp, self.impl_args(imp, self_ty)?))) + .flat_map(|(imp, parent)| { + (tcx.associated_items(imp).filter_by_name_unhygienic(field.name)).map(move |item| (item, parent)) + }) + .any(|(item, parent)| { matches!(item.kind, ty::AssocKind::Fn { .. } | ty::AssocKind::Const { .. }) && tcx.visibility(item.def_id).is_public() + && self.applies(item.def_id, parent) }); let mut key = shown.clone(); key[1].insert_str(0, "field@"); diff --git a/ffi/tests/api.c b/ffi/tests/api.c index 84e2cf54c..2c23298f8 100644 --- a/ffi/tests/api.c +++ b/ffi/tests/api.c @@ -353,6 +353,9 @@ static int script(void) { api_option_u32 limit = api_retry_limit_setting(retry); printf("retry %d %u %u %u\n", limit.some, limit.value, api_retry_limit(retry), api_retry_delay(retry)); api_retry_free(retry); + api_byte_slot *slot = api_byte_slot_new(200); + printf("slot %u\n", api_byte_slot_value(slot)); + api_byte_slot_free(slot); /* Each future dropped its waker once it finished. */ return drops == 4 ? 0 : 1; }