fix: generate a Debug impl for NewType/NewTypeDeref wrappers when the inner type only manually implements it - #3411
Open
dhmztr wants to merge 1 commit into
Open
Conversation
…en the inner type only has a manual impl
`AliasVariation::NewType`/`NewTypeDeref` newtype wrapper structs only
ever got a `#[derive(...)]` attribute list built from
`derives_of_item`. If the wrapped type itself doesn't derive `Debug`
(e.g. a union, which always gets a hand-written `impl Debug` via
`--impl-debug` since unions can't derive it), the wrapper ended up
with no `Debug` impl at all - neither derived nor manual.
Any struct containing such a wrapper still generates a manual
`Debug` impl that formats the field with `{:?}`, which then fails to
compile with `` `Foo` doesn't implement `Debug` ``, since the
newtype's own `Debug` is missing entirely.
This mirrors the same "does this derive, and if not, do we need a
manual impl" logic that `CompInfo::codegen` already applies to
regular structs/unions, applied to the newtype wrapper case.
Fixes rust-lang#3268
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
Fixes #3268.
When using
--impl-debugtogether with--default-alias-style=new_typeornew_type_deref, a newtype wrapper around a type that only has a manualDebugimpl (e.g. a union, which can never#[derive(Debug)]and always gets a hand-written impl instead) ends up with noDebugimpl at all — neither derived nor manual. Any containing struct that references the wrapper still generates a manualDebugimpl formatting the field with{:?}, so the generated code fails to compile:This is the exact repro and error from the issue.
Root cause
Type::codegen'sAliasVariation::NewType | AliasVariation::NewTypeDerefbranch only ever builds a#[derive(...)]list viaderives_of_item. UnlikeCompInfo::codegen(the regular struct/union codegen path), it never falls back to generating a manualimpl DebugwhenDebugisn't in the derivable set.Fix
Mirror the same "not derivable → generate a manual impl instead, if
--impl-debugand friends allow it" logic thatCompInfo::codegenalready has (codegen/mod.rs:2588-2593/2931-2932), for the newtype wrapper case. The generated impl delegates viaf.debug_tuple(...).field(&self.0).finish(), matching what#[derive(Debug)]would produce for a tuple struct (including correct{:#?}pretty-printing).Test plan
main(rustcconfirmsE0277, same as the report)issue-3268-newtype-deref-union-debug.h/.rs, using the issue's exact reprorustc --crate-type libon the generated bindings)bindgen-testscorpus passes aside from 2 pre-existing, unrelated failures also present on unmodifiedmain(a toolchain-version-dependent const-generics codegen difference, and adump_preprocessed_inputtest needing theclangCLI binary rather than justlibclang)cargo clippy -- -D warningscleanScope note
The issue's second comment describes a related-looking but distinct bug (a struct field is silently omitted from the generated
Debugformat string when its type is astdint.htypedef likeuint32_t). I wasn't able to reproduce that one with several plausible flag/header combinations in my environment, so I'm leaving it out of this PR rather than guessing at a fix for behavior I can't actually observe. Happy to take a look if someone can share a minimal, reliably-reproducing case.