Skip to content

inner crates, aka multiple crates per file #139

@pnkfelix

Description

@pnkfelix

Proposal

Summary and problem statement

Add ability to express multiple crates (with corresponding multiple output object files) in a single source file.

Syntax might look something like:

crate first {  // generates `first.rlib`
  pub static DEF: i32 = 3;
}

crate second { // generates `second.rlib`
    extern crate first; pub use first::DEF as D;
}

// we will compile above to completion before
// we process body of (implicit) crate for source file.

extern crate second;

fn main() { println!("{}", second::D); } // prints "3"

Motivation, use-cases, and solution sketches

The problem: Today, any Rust example involving more than one crate or more than one code-gen unit requires multiple source files to demonstrate. This means you cannot use play.rust-lang.org for such cases. It means that people seeking to describe multi-crate scenarios in documentation or blog posts have to describe multiple files, or rely on the reader to infer such structure. It means that people seeking to describe multi-code-gen unit problems also end up having to either use multiple crates (and thus hit the problems above), or they rely on details of how a source file is mapped into separate code-gen units by the rust compiler.

One very important case that requires multiple crates today: procedural macros. I.e., you are forced to define a procedural macro in a separate crate from where it is used.

By adding the ability to define extra crates inline, we will side-step all of the above problems.

(Its entirely possible that this feature will be of most use to compiler developers, but I have tried to identify cases, like documentation and proc-macros, that have a more general audience. No matter how you slice it, its a niche group.)

Update: I am explicitly removing proc-macros from the scope of this proposal, due to it raising thorny issues (discussed on zulip) that I was not intending to try to address as part of this proposal.

Links and related work

Few languages support this (which to be honest I'm definitely thinking could be a sign that its a linguistic anti-pattern). The closest thread I can see is work on "multi-stage programming", but even that connection seems like a bit of a stretch.

Initial people involved

  • Owner, if known: pnkfelix wants to drive this
  • Liaison

What happens now?

This issue is part of the lang-team initiative process. Once this issue is filed, a Zulip topic will be opened for discussion, and the lang-team will review open proposals in its weekly triage meetings. You should receive feedback within a week or two.

This issue is not meant to be used for technical discussion. There is a Zulip stream for that. Use this issue to leave procedural comments, such as volunteering to review, indicating that you second the proposal (or third, etc), or raising a concern that you would like to be addressed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions