Skip to content

Create type-stable @generated version of _show_default for --trim #59608

@topolarity

Description

@topolarity

I was kind of hoping we'd define a decent --trim fallback for show_default, e.g. w/ @generated to iterate struct fields type-stably.

Originally posted by @topolarity in #59598 (review)

show_default is a "default implementation" in Base - it's an automatic implementation of printing based purely on structural / type information.

The current version in Base._show_default iterates the fields dynamically (simplified):

for i in 1:nf
    show(recur_io, getfield(x, i))
    # ...
end

which is inherently type-unstable (and is additionally marked @nospecialize, so getfield(x, i) is inferred ::Any for any argument anywhere). This is fine for typical Julia execution, where printing is not especially performance-sensitive, and instead it's beneficial to avoid generating specialized code for all structs (hence the @nospecialize)

For --trim compatibility, we need an @generated equivalent that behaves similarly to #[derive(Display)] in Rust and inspects the type structurally and generates a type-stable implementation of show_fallback for the type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    display and printingAesthetics and correctness of printed representations of objects.trimmingIssues with trimming functionality or PR's relevant to its performance/functionality

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions