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 @@ -245,7 +245,8 @@ glue includes is [`runtime/rt.rs`](runtime/rt.rs), the one the C++ header includ
`--census dir` exports nothing. It writes `<package>.md` for each exported package,
which counts what exports and ranks every refusal by construct and by the type it names,
lists each refused item at its span, and ends with the `exclude` list that exports the
rest. It ignores the config's `exclude`.
rest. It ignores the config's `exclude`. It still fails on an `instance not inferred`
refusal, which is a bug in the exporter's inference rather than in the crate.

## Tests

Expand Down
7 changes: 5 additions & 2 deletions ffi/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ pub struct Why {
pub note: Option<(Span, String)>,
}

/// The construct of an instance inference missed: a bug in the exporter, not the crate.
pub const NOT_INFERRED: &str = "instance not inferred";

fn why(construct: &'static str, detail: impl Into<String>) -> Why {
Why {
construct,
Expand Down Expand Up @@ -2476,9 +2479,9 @@ impl<'tcx> Collector<'tcx> {
return Err(why("type parameter", name));
}
// Inference admits or rejects each instance a signature names, so one
// it did not is a gap in inference, which the census counts.
// it did not is a gap in inference, which even a census fails on.
if key.args.is_some() && self.items.iter().any(|(item, _)| *item == did) {
let mut why = why("instance not inferred", name);
let mut why = why(NOT_INFERRED, name);
let note =
format!("`{}` exports only at the instances it infers", strip_args(tcx, did));
why.note = Some((tcx.def_span(did), note));
Expand Down
64 changes: 39 additions & 25 deletions ffi/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,35 +115,24 @@ impl Callbacks for Export {
));
}
let written = match &self.output {
Output::Census(dir) => collected.reports.iter().enumerate().try_for_each(|(i, report)| {
let text = census::render(&collected.api, report, i == 0, &abi.clashes, &|span| at(tcx, span));
write(&dir.join(format!("{}.md", report.package)), &text)
}),
Output::Census(dir) => {
let written = collected.reports.iter().enumerate().try_for_each(|(i, report)| {
let text = census::render(&collected.api, report, i == 0, &abi.clashes, &|span| at(tcx, span));
write(&dir.join(format!("{}.md", report.package)), &text)
});
// A census reports what a crate uses, but a missed instance is the exporter's bug.
for refusal in collected.reports.iter().flat_map(|report| &report.refusals) {
if refusal.why.iter().any(|why| why.construct == collect::NOT_INFERRED) {
refuse(tcx, refusal, "inference missed an instance, a bug in rust2-ffi");
}
}
written
}
Output::Dir(out) => {
for report in &collected.reports {
let table = self.table(&collected, &report.krate);
for refusal in &report.refusals {
let reasons: Vec<String> = refusal
.why
.iter()
.map(|why| match why.detail.is_empty() {
true => why.construct.to_string(),
false => format!("{} `{}`", why.construct, why.detail),
})
.collect();
let mut err = tcx.dcx().struct_span_err(
refusal.span,
format!(
"rust2-ffi: cannot export {} `{}`: {}; exclude it in {table} to leave it out",
refusal.kind,
refusal.path,
reasons.join("; ")
),
);
for (span, note) in refusal.why.iter().filter_map(|why| why.note.as_ref()) {
err = err.with_span_note(*span, note.clone());
}
err.emit();
refuse(tcx, refusal, &format!("exclude it in {table} to leave it out"));
}
for (path, count) in &report.excluded {
if *count == 0 {
Expand Down Expand Up @@ -271,6 +260,31 @@ fn canonical(path: &Path) -> Option<PathBuf> {
std::fs::canonicalize(path).ok()
}

/// Emits `refusal` as an error at its span, with every reason and its notes, then `advice`.
fn refuse(tcx: TyCtxt<'_>, refusal: &collect::Refusal, advice: &str) {
let reasons: Vec<String> = refusal
.why
.iter()
.map(|why| match why.detail.is_empty() {
true => why.construct.to_string(),
false => format!("{} `{}`", why.construct, why.detail),
})
.collect();
let mut err = tcx.dcx().struct_span_err(
refusal.span,
format!(
"rust2-ffi: cannot export {} `{}`: {}; {advice}",
refusal.kind,
refusal.path,
reasons.join("; ")
),
);
for (span, note) in refusal.why.iter().filter_map(|why| why.note.as_ref()) {
err = err.with_span_note(*span, note.clone());
}
err.emit();
}

/// Where `span` starts, relative to the crate's directory when inside it.
fn at(tcx: TyCtxt<'_>, span: Span) -> String {
let sm = tcx.sess.source_map();
Expand Down
1 change: 0 additions & 1 deletion quest/m1/ffi-export/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ Every generator on the C ABI:

- [Payloads cross as bytes](/quest/m1/ffi-export/payload-bytes.md) - a payload that is not UTF-8 crosses unchanged, where today it aborts from C, C++, and Kotlin
- [Iterator and JSON arguments the exporter can't map yet](/quest/m1/ffi-export/instance-keys.md) - `IntoIterator` as a `Vec`, and `serde_json::Value`
- [Fail CI on an instance not inferred](/quest/m1/ffi-export/inference-gate.md) - a gap in instance inference fails CI instead of sitting in the census
- [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`
- [Setters for an object's pub fields](/quest/m1/ffi-export/field-setters.md) - C fills in a session's config, as Rust assigns it
- [Session config's foreign types](/quest/m1/ffi-export/foreign-types.md) - `Url`, `SocketAddr`, `PathBuf`, and `Vec<String>` cross the C ABI
Expand Down
20 changes: 0 additions & 20 deletions quest/m1/ffi-export/inference-gate.md

This file was deleted.