Skip to content

Quoted code can only use the public API of pub types (private fields fail at splice time) #96

Description

@shadaj

When a type is pub (and reachable from the crate root), the staged copy of the crate re-exports it from the original crate instead of copying its definition. As a result, quoted code interacts with the type through its public API only — even though the q! block is written inside the defining crate where private fields are visible.

This means the following typechecks fine at the definition site, but fails when the spliced code is compiled (e.g. when building deployment binaries or running Hydro sim tests), with a confusing field ... is private error pointing at generated code:

pub struct Config {
    max_size: usize, // private field of a pub struct
}

// inside the same crate:
q!(|c: Config| c.max_size) // E0616 at staged-compile time

Workarounds: make the fields pub, add public accessors/constructors, or make the type fully private (private types are copied into the staged module with visibility lifted, so private field access works for them).

There are existing TODOs for this in the code:

  • stageleft_tool/src/lib.rs: // TODO(shadaj): warn if a pub struct or enum has private fields and is not marked for runtime
  • stageleft_test/src/submodule.rs: // TODO(shadaj): right now, public structs must have public fields because otherwise they may not be visible at splice time. This should be documented and ideally have some tooling support.

At minimum we should emit a warning during staging when quoted code touches private members of pub types; ideally this would be supported properly.

Related: #18, #43.

This is now documented as a known gotcha on the Hydro "Quoting Limitations" docs page; this issue tracks fixing it properly.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions