rust-lang / rust-lang/rust

`include!` docs don't explain how `mod` is resolved

Open
#149,810 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

A-docs needs-triage
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Location (URL)

https://doc.rust-lang.org/std/macro.include.html

Summary

The include! macro appears to resolve modules before being 'pasted' into the current file. E.g. The following crate fails to compile

/
├── module/
│   └── file.rs
├── module.rs
├── include/
│   └── include_src.rs
└── lib.rs

with file.rs:

pub fn foo() { todo!() }

include_src.rs:

mod file;

fn bar() { file::foo() }

and module.rs:

include!("include/include_src.rs");

The include! macro documentation does not mention this behaviour.

As an aside:
This behaviour is very unintuitive to me, I expected include! to behave exactly like pasting the included code into the current file.
This also makes writing build scripts whose outputs contain mod problematic. In my project, include_src.rs is an output of a build script; I've had to replace the mods with mod file { include!(concat!(env!("CARGO_MANIFEST_DIR"), "/src/module/file.rs")); } which isn't ideal and also seems to disable r-a's Intellisense for file.rs.
It would be useful to have a macro that does not do any sort of parsing/resolution to allow this pattern.

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

Open the std::include! documentation at the linked URL and compare it with the module/file.rs example in the issue. Verify the demonstrated module-resolution behavior, then document how mod is resolved relative to included code and clarify how this differs from literal pasting; the documentation should explain the failing example without requiring the proposed macro design.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
50/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.