Skip to content

Trait implementation causes Object File Too Large error and compilation crash #135849

Open
@benwis

Description

@benwis

For Leptos 0.7, we've switched our HTML rendering engine to use large recursive static types to represent the HTML tree, giving us significant benefits to final binary size and runtime speed, but unfortunately not to compile time.

To fix that, we've brought back the old behavior where we type erase the HTML components in the tree behind a feature flag for users to develop faster. However, we also added generic attribute spreading in this release to make our maintenance and user's code nicer and easier to write.

The crux of the issue here is implementing the trait that does that causes an error: cannot encode offset of relocations; object file too large on Mac after compiling for 10ish minutes and using 50GB of ram, with other issues on other platforms, but only with the AddAnyAttr trait. Not having attribute spreading in the type erased version severely limits its usefulness.

The trait can be found here but is basically this:

// Pre-erases output to reduce compile-time explosions
impl AddAnyAttr for AnyView {
    type Output<SomeNewAttr: Attribute> = AnyView;

    #[inline(never)]
    fn add_any_attr<NewAttr: Attribute>(
        self,
        attr: NewAttr,
    ) -> Self::Output<NewAttr>
    where
        Self::Output<NewAttr>: RenderHtml,
    {
        let attr = attr.into_cloneable_owned();
        let any_attr = attr.into_any_attr();
        self.value.dyn_add_any_attr(any_attr)
    }
}

A similar issue was discussed broadly here, but I wanted to provide a specific reproducer for a more targeted issue in the hopes of a solution or a workaround. Please let me know if this is out of line.

This can be reproduced by running cargo +stable build --bin demo --features ssr in the main branch of this repo.

To fix(and disable attribute spreading), in the workspace Cargo.toml change the leptos branch to "any-attr-disabled", see the extra commit on that branch for the minimal change that allows compilation to succeed in about 1m30s. This leads us to the idea that this specific trait implementation brakes everything, even though it seems to be perfectly valid Rust.

Thanks to @zakstucke and all the other Leptos users who have worked on this and tested various workarounds

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-trait-systemArea: Trait systemC-bugCategory: This is a bug.I-compilememIssue: Problems and improvements with respect to memory usage during compilation.I-compiletimeIssue: Problems and improvements with respect to compile times.I-crashIssue: The compiler crashes (SIGSEGV, SIGABRT, etc). Use I-ICE instead when the compiler panics.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-trait-system-refactorThe Rustc Trait System Refactor Initiative (-Znext-solver)

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions