Skip to content
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

rustdoc cuts off the last line in a #[doc] attribute if it only has a single character #90618

Closed
nicholasbishop opened this issue Nov 5, 2021 · 6 comments · Fixed by #90657
Closed
Labels
C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@nicholasbishop
Copy link
Contributor

When using #[doc = include_str!("...")] to insert another file as a docstring, it seems that the last line gets cut off if it contains only one character.

src/inc1:

inc1
ab

src/inc2:

inc2
x

(Both inc files have a trailing new line.)

src/lib.rs:

#[doc = include_str!("inc1")]  // OK: "inc1 ab"
pub const A: usize = 1;

#[doc = include_str!("inc2")]  // Bad: "inc2"
pub const B: usize = 2;

Running cargo doc --open on the above produces output like this:

A inc1 ab 
B inc2

That is, the inc1 file gets included correctly, but the inc2 file loses its second line. This bug only seems to happen if the last line is a single character.

Meta

Reproduced this bug on both stable and nightly.

rustc --version --verbose:

rustc 1.56.1 (59eed8a2a 2021-11-01)
binary: rustc
commit-hash: 59eed8a2aac0230a8b53e89d4e99d55912ba6b35
commit-date: 2021-11-01
host: x86_64-unknown-linux-gnu
release: 1.56.1
LLVM version: 13.0.0

rustc +nightly --version --verbose:

rustc 1.58.0-nightly (4961b107f 2021-11-04)
binary: rustc
commit-hash: 4961b107f204e15b26961eab0685df6be3ab03c6
commit-date: 2021-11-04
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0
@nicholasbishop nicholasbishop added the C-bug Category: This is a bug. label Nov 5, 2021
@jyn514
Copy link
Member

jyn514 commented Nov 5, 2021

@nicholasbishop does this also happen for string constants and other things using include_str!? Or just the doc attribute?

@nicholasbishop
Copy link
Contributor Author

Oh I didn't think to check that. Looks like the bug doesn't reproduce with a non-doc use of include_str.

This runs without error:

fn main() {
    let inc1 = include_str!("inc1");
    let inc2 = include_str!("inc2");

    assert_eq!(inc1, "inc1\nab\n");
    assert_eq!(inc2, "inc2\nx\n");
}

@jyn514 jyn514 added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) F-extended_key_value_attributes `#![feature(extended_key_value_attributes)] labels Nov 5, 2021
@jyn514
Copy link
Member

jyn514 commented Nov 5, 2021

Ok cool and follow-up question - does #[doc = "inc2\nx\n"] have the same issue? Or only with include_str?

@nicholasbishop
Copy link
Contributor Author

That does reproduce the issue.

#[doc = "inc2\nx\n"]
pub const C: usize = 2;

The rustdoc output looks like C inc2.

@jyn514 jyn514 added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. and removed A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) F-extended_key_value_attributes `#![feature(extended_key_value_attributes)] labels Nov 5, 2021
@jyn514
Copy link
Member

jyn514 commented Nov 5, 2021

cc @GuillaumeGomez, this is probably related to your changes to try and make the indentation smarter.

@jyn514 jyn514 changed the title Single-character last line cut off when using #[doc = include_str!("...")] rustdoc cuts off the last line in a #[doc] attribute if it only has a single character Nov 5, 2021
@GuillaumeGomez
Copy link
Member

You mean removing the smart indent? ;)

But yes, very likely. I'll take a look.

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 8, 2021
…emoved, r=jyn514

Fix bug with `#[doc]` string single-character last lines

Fixes rust-lang#90618.

This is because `.iter().all(|c| c == '*')` returns `true` if there is no character checked. And in case the last line has only one character, it simply returns `true`, making the last line behind removed.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 8, 2021
…emoved, r=jyn514

Fix bug with `#[doc]` string single-character last lines

Fixes rust-lang#90618.

This is because `.iter().all(|c| c == '*')` returns `true` if there is no character checked. And in case the last line has only one character, it simply returns `true`, making the last line behind removed.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Nov 8, 2021
…emoved, r=jyn514

Fix bug with `#[doc]` string single-character last lines

Fixes rust-lang#90618.

This is because `.iter().all(|c| c == '*')` returns `true` if there is no character checked. And in case the last line has only one character, it simply returns `true`, making the last line behind removed.
@bors bors closed this as completed in d9fc7d1 Nov 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants