rust-lang / rust-lang/rust

Trait implementation causes `Object File Too Large` error and compilation crash

Open
#135,849 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-trait-system C-bug I-compilemem I-compiletime I-crash T-compiler WG-trait-system-refactor
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running cargo +stable build --bin demo --features ssr in the linked thaw reproducer and compare it with the any-attr-disabled branch. Read tachys/src/view/any_view.rs around the AddAnyAttr implementation and review the related Rust compiler issue. Done means the reproducer no longer triggers the object-file-too-large error or compilation crash while preserving the intended attribute-spreading behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.