Skip to content

refactor: widen definitions that header declarations are matched to - #1937

Open
ahomescu wants to merge 2 commits into
ahomescu/fix_reorganize_definitions/deterministic_externsfrom
ahomescu/fix_reorganize_definitions/widen_matched_defs
Open

refactor: widen definitions that header declarations are matched to #1937
ahomescu wants to merge 2 commits into
ahomescu/fix_reorganize_definitions/deterministic_externsfrom
ahomescu/fix_reorganize_definitions/widen_matched_defs

Conversation

@ahomescu

Copy link
Copy Markdown
Contributor

Stack created with GitHub Stacks CLIGive Feedback 💬

@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 8cb0857 to 7a0502c Compare July 25, 2026 01:40
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 7a0502c to 5de2b14 Compare July 25, 2026 01:58
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 5de2b14 to 5918d0c Compare July 25, 2026 02:08
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 5918d0c to f603f8a Compare July 25, 2026 02:11
@ahomescu
ahomescu changed the base branch from ahomescu/fix_reorganize_definitions/compare_extern_fn_sigs to ahomescu/fix_reorganize_definitions/canonicalize_moved_paths July 25, 2026 02:16
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from f603f8a to 4363bbc Compare July 25, 2026 02:16
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 4363bbc to 2713866 Compare July 25, 2026 02:35
@ahomescu
ahomescu changed the base branch from ahomescu/fix_reorganize_definitions/canonicalize_moved_paths to ahomescu/fix_reorganize_definitions/compare_extern_fn_sigs July 25, 2026 03:17
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 2713866 to 158225c Compare July 25, 2026 03:17
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 158225c to 7ab6fcf Compare July 25, 2026 05:09
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 7ab6fcf to 94479f6 Compare July 25, 2026 05:33
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 94479f6 to b9966f7 Compare July 25, 2026 05:41
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from b9966f7 to 8d4820a Compare July 25, 2026 05:42
@ahomescu
ahomescu changed the base branch from ahomescu/fix_reorganize_definitions/compare_extern_fn_sigs to ahomescu/fix_reorganize_definitions/deterministic_externs July 25, 2026 05:42
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 8d4820a to d5e8b27 Compare July 25, 2026 05:48
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from d5e8b27 to 2e44580 Compare July 25, 2026 05:52
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 2e44580 to f942eb6 Compare July 25, 2026 05:59
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from f942eb6 to e527aa4 Compare July 25, 2026 06:05
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from e527aa4 to 74ced24 Compare July 25, 2026 06:16
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch 2 times, most recently from f243b12 to 713177e Compare July 25, 2026 06:43
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 713177e to 4ed9f3d Compare July 25, 2026 06:51
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch 2 times, most recently from 65b1320 to c721b16 Compare July 30, 2026 00:19
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch 2 times, most recently from 66c4b7f to f6d4751 Compare July 30, 2026 22:54
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from f6d4751 to 724b211 Compare July 30, 2026 22:56
ahomescu added 2 commits July 30, 2026 15:57
…ition

Regression test: `test_reorganize_private_no_mangle_def`
(`tests/snapshots/reorganize_private_no_mangle_def.rs`). `def::counter`
carries `#[no_mangle]` but no `pub`, and `user` includes a header declaring
it. `match_defs` only considers definitions that `is_exported` accepts,
which for a value means it carries `#[no_mangle]` or `#[export_name]` — a
statement about the C symbol being visible to another translation unit, not
about Rust visibility. So the declaration is matched to the definition and
`user::read` is rewritten to `crate::def::counter`, naming an item that is
private to `def`:

    error[E0603]: static `counter` is private

The recorded output shows the rewritten path next to the still-private
definition, with `new_expect_compile_error` marking the state as broken.
This is the second reason the output of `test_reorganize_definitions` does
not compile, reduced to one definition and one reference.
`match_defs` replaces a header declaration with a definition found elsewhere
in the crate and rewrites every path to the declaration to point at that
definition. It only accepts a definition `is_exported` agrees is exported,
but for a value that asks whether the item carries `#[no_mangle]` or
`#[export_name]` — whether the C symbol reaches another translation unit,
which says nothing about Rust visibility. A `#[no_mangle]` static private to
its module qualifies, and the paths rewritten to name it come from other
modules, which cannot:

    error[E0603]: static `counter` is private

`match_defs` now records each matched definition that is not `pub`, and
`widen_matched_defs` joins its visibility with `pub(crate)` — enough because
every path that now points at it is in this crate. This is the pass the
commented-out `imports` field on `HeaderDeclarations` anticipated ("must be
made pub(crate) at least").

With this and the previous commit, the output of `test_reorganize_definitions`
compiles for the first time, so its `new_expect_compile_error` is dropped
too; the only change to that snapshot here is `Bar` gaining `pub(crate)`.
@ahomescu
ahomescu force-pushed the ahomescu/fix_reorganize_definitions/widen_matched_defs branch from 724b211 to 0f84b7d Compare July 30, 2026 22:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant