Skip to content

Improve include macro documentation #106453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jan 7, 2023
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 36 additions & 12 deletions library/core/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1315,22 +1315,46 @@ pub(crate) mod builtin {

/// Parses a file as an expression or an item according to the context.
///
/// The file is located relative to the current file (similarly to how
/// modules are found). The provided path is interpreted in a platform-specific
/// way at compile time. So, for instance, an invocation with a Windows path
/// containing backslashes `\` would not compile correctly on Unix.
/// <div class="example-wrap" style="display:inline-block">
/// <pre class="compile_fail" style="white-space:normal;font:inherit;">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the right way to do this because if someone enables the "see code blocks lines", it'll make a very weird output. We are talking about adding such a feature in #79710. I re-started the debate and hopefully, we might be able to have something not too far in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you be open to me removing the <div> so that this can be merged? This PR generally improves the documentation, so I see no reason in the html blocking the PR. I will follow the discussion on the better support for warning blocks and update it when the time comes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove the HTML tags and then it's good for me. We can come back to this at a later time.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done :)

///
/// **Warning**: For multi-file Rust projects, the `include!` macro is probably not what you
/// are looking for. Usually, multi-file Rust projects use
/// [modules](https://doc.rust-lang.org/reference/items/modules.html). Multi-file projects and
/// modules are explained in the Rust-by-Example book
/// [here](https://doc.rust-lang.org/rust-by-example/mod/split.html) and the module system is
/// explained in the Rust Book
/// [here](https://doc.rust-lang.org/book/ch07-02-defining-modules-to-control-scope-and-privacy.html).
///
/// </pre>
/// </div>
///
/// If the included file is parsed as an expression, it is placed in the surrounding code
/// [unhygienically](https://doc.rust-lang.org/reference/macros-by-example.html#hygiene). This
/// could result in variables or functions being different from what the file expected if there
/// are variables or functions that have the same name in the current file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This wording makes it sound like it's hygienic when in item context

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The latest commit alters the wording on the hygiene of the macro. This should make it clear that even for an item, it is unhygienic.

///
/// The included file is located relative to the current file (similarly to how modules are
/// found). The provided path is interpreted in a platform-specific way at compile time. So,
/// for instance, an invocation with a Windows path containing backslashes `\` would not
/// compile correctly on Unix.
///
/// Using this macro is often a bad idea, because if the file is
/// parsed as an expression, it is going to be placed in the
/// surrounding code unhygienically. This could result in variables
/// or functions being different from what the file expected if
/// there are variables or functions that have the same name in
/// the current file.
/// # Uses
///
/// The `include!` macro is primarily used for two purposes. It is used to include
/// documentation that is written in a separate file and it is used to include [build artifacts
/// usually as a result from the `build.rs`
/// script](https://doc.rust-lang.org/cargo/reference/build-scripts.html#outputs-of-the-build-script).
///
/// When using the `include` macro to include stretches of documentation, remember that the
/// included file still needs to be a valid rust syntax. It is also possible to
/// use the [`include_str`] macro as `#![doc = include_str!("...")]` (at the module level) or
/// `#[doc = include_str!("...")]` (at the item level) to include documentation from a plain
/// text or markdown file.
///
/// # Examples
///
/// Assume there are two files in the same directory with the following
/// contents:
/// Assume there are two files in the same directory with the following contents:
///
/// File 'monkeys.in':
///
Expand Down