Skip to content

include! docs don't explain how mod is resolved #149810

@StanleyRoberts

Description

@StanleyRoberts

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-docsArea: Documentation for any part of the project, including the compiler, standard library, and toolsneeds-triageThis issue may need triage. Remove it if it has been sufficiently triaged.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions