Extend external_derive to cover proc-macro-generated impls - #2641
Open
hz2 wants to merge 2 commits into
Open
Conversation
hz2
force-pushed
the
external-derive-proc-macro
branch
from
July 9, 2026 23:25
fb795ec to
57b3c85
Compare
hz2
marked this pull request as ready for review
July 10, 2026 14:07
hz2
marked this pull request as draft
July 17, 2026 20:38
hz2
force-pushed
the
external-derive-proc-macro
branch
from
July 17, 2026 21:00
57b3c85 to
5712db8
Compare
hz2
marked this pull request as ready for review
July 17, 2026 21:03
hz2
force-pushed
the
external-derive-proc-macro
branch
2 times, most recently
from
July 19, 2026 14:52
c5ec020 to
3f83d25
Compare
tjhance
self-requested a review
August 3, 2026 16:06
hz2
force-pushed
the
external-derive-proc-macro
branch
from
August 8, 2026 16:41
3f83d25 to
ff4ed97
Compare
hz2
force-pushed
the
external-derive-proc-macro
branch
from
August 16, 2026 17:30
ff4ed97 to
52b1c4d
Compare
Proc-macro crates such as serde do not emit #[automatically_derived] on the impls they generate. As a result, #[verifier::external_derive] had no effect on serde-derived Serialize/Deserialize impls (issues verus-lang#1575, verus-lang#1577, verus-lang#1956): the gate in get_attributes_for_automatic_derive returned None immediately for any impl lacking that attribute. The fix checks span.from_expansion() as a secondary signal. When an impl is macro-expanded but not #[automatically_derived], and the self type has #[verifier::external_derive] (with AllExternal or a matching trait name), the impl is now marked external. If the type has no external_derive annotation at all, macro-expanded impls fall through to the normal classification path unchanged, preserving existing behavior. Tests added in rust_verify_test/tests/std.rs simulate proc-macro derives with macro_rules! (which produces macro-expanded impls without #[automatically_derived]) and verify that external_derive AllExternal and SomeExternal(TraitName) both suppress verification of the generated impl.
The new is_macro_expanded path in get_attributes_for_automatic_derive runs for any macro-expanded impl, not just #[automatically_derived] ones. Unlike derive impls, macro-expanded impls can target primitive types (e.g. vstd's std_specs/ops.rs generates impl ... for u8/usize/... via macro_rules!), whose Res is PrimTy and has no DefId. Calling path.res.def_id() on those panics; switch to opt_def_id() and fall through to normal classification when there's no def_id, matching the existing fallback arms.
hz2
force-pushed
the
external-derive-proc-macro
branch
from
August 23, 2026 00:46
52b1c4d to
4b2399a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
#[verifier::external_derive]had no effect on impls generated by proc-macro crates (e.g., serde'sSerialize/Deserialize). Those crates do not emit#[automatically_derived]on the impls they generate, soget_attributes_for_automatic_deriveinexternal.rsreturnedNoneimmediately, leaving the generated impl subject to normal (failing) classification.Fixes #1575, #1577, #1956.
Changes
source/rust_verify/src/external.rsThe
get_attributes_for_automatic_derivefunction previously returnedNoneat the top if!is_automatically_derived(attrs). This PR adds a secondary check: when an impl span is from a macro expansion (span.from_expansion()) and the self type carries#[verifier::external_derive](eitherAllExternalorSomeExternalwith a matching trait name), the impl is now marked external.If the type has no
external_deriveannotation at all, macro-expanded impls fall through toNoneunchanged, preserving all existing behavior.source/rust_verify_test/tests/std.rsTwo new tests under
--no-external-by-default:external_derive_proc_macro_all: uses amacro_rules!that produces an impl without#[automatically_derived](simulating a proc-macro derive), verifies that#[verifier::external_derive](AllExternal) suppresses verification of the generated impl.external_derive_proc_macro_named: same, but with#[verifier::external_derive(FakeSerialize)]to test the named-trait path.Testing
The new tests simulate proc-macro derives with
macro_rules!, which produces macro-expanded impls without#[automatically_derived]-- exactly the shape serde emits. The existingexternal_derive_attrandexternal_derive_attr_listtests are unchanged and continue to pass.