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: intra-doc link errors give incorrect spans on certain block doc comments #55964

Closed
QuietMisdreavus opened this issue Nov 15, 2018 · 2 comments · Fixed by #56010
Closed
Labels
A-intra-doc-links Area: Intra-doc links, the ability to link to items in docs by name T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@QuietMisdreavus
Copy link
Member

Given the following source:

/** # for example,
 *
 * time to introduce a link [error]*/
pub struct SomeStruct;

The span conversion calculation in the error reporting will give the wrong offset when reporting the resolution failure, leading to a confusing report:

$ cargo +nightly doc
 Documenting asdf v0.1.0 (/home/misdreavus/git/asdf)
warning: `[error]` cannot be resolved, ignoring it...
 --> src/lib.rs:3:33
  |
3 |   time to introduce a link [error]*/
  |  _________________________________^
4 | | pub struct SomeStruct;
  | |__^ cannot be resolved, ignoring
  |
  = note: #[warn(intra_doc_link_resolution_failure)] on by default
  = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`

    Finished dev [unoptimized + debuginfo] target(s) in 2.12s

Strangely enough, putting either the leading /** or the closing */ on its own line will cause the report to change to something that looks correct, if a little too broad:

/**
 * # for example,
 *
 * time to introduce a link [error]
 */
pub struct SomeStruct;
$ cargo +nightly doc
 Documenting asdf v0.1.0 (/home/misdreavus/git/asdf)
warning: `[error]` cannot be resolved, ignoring it...
 --> src/lib.rs:1:1
  |
1 | / /**
2 | |  * # for example,
3 | |  *
4 | |  * time to introduce a link [error]
5 | |  */
  | |___^
  |
  = note: #[warn(intra_doc_link_resolution_failure)] on by default
  = note: the link appears in this line:

           time to introduce a link [error]
                                     ^^^^^
  = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`

    Finished dev [unoptimized + debuginfo] target(s) in 2.12s

I believe the code at fault is this section here:

let line_offset = dox[..link_range.start].lines().count();
// The span starts in the `///`, so we don't have to account for the leading whitespace
let code_dox_len = if line_offset <= 1 {
doc_comment_padding
} else {
// The first `///`
doc_comment_padding +
// Each subsequent leading whitespace and `///`
code_dox.lines().skip(1).take(line_offset - 1).fold(0, |sum, line| {
sum + doc_comment_padding + line.len() - line.trim().len()
})
};
// Extract the specific span
let sp = sp.from_inner_byte_pos(
link_range.start + code_dox_len,
link_range.end + code_dox_len,
);

Because it assumes that every doc comment will have a padding of 3 characters (the /// or //!), it includes that in its calculation of each line's offset. However, for block-style comments, this assumption is false.

@QuietMisdreavus QuietMisdreavus added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-intra-doc-links Area: Intra-doc links, the ability to link to items in docs by name labels Nov 15, 2018
@euclio
Copy link
Contributor

euclio commented Nov 16, 2018

I'm working on this.

@euclio
Copy link
Contributor

euclio commented Nov 16, 2018

You can also reproduce this with unsugared docs:

warning: `[error]` cannot be resolved, ignoring it...
  --> src/test/rustdoc-ui/intra-links-warning.rs:71:17
   |
71 | #[doc = "single line [error]"]
   |                 ^^^^^ cannot be resolved, ignoring
   |
   = help: to escape `[` and `]` characters, just add '\' before them like `\[` or `\]`

euclio added a commit to euclio/rust that referenced this issue Nov 19, 2018
This commit improves the calculation of code spans for intra-doc
resolution failures. All sugared doc comments should now have the
correct spans, including those where the comment is longer than the
docs.

It also fixes an issue where the spans were calculated incorrectly for
certain unsugared doc comments. The diagnostic will now always use the
span of the attributes, as originally intended.

Fixes rust-lang#55964.
kennytm added a commit to GuillaumeGomez/rust that referenced this issue Dec 5, 2018
…reavus

fix intra-link resolution spans in block comments

This commit improves the calculation of code spans for intra-doc
resolution failures. All sugared doc comments should now have the
correct spans, including those where the comment is longer than the
docs.

It also fixes an issue where the spans were calculated incorrectly for
certain unsugared doc comments. The diagnostic will now always use the
span of the attributes, as originally intended.

Fixes rust-lang#55964.
euclio added a commit to euclio/rust that referenced this issue Dec 11, 2018
This commit improves the calculation of code spans for intra-doc
resolution failures. All sugared doc comments should now have the
correct spans, including those where the comment is longer than the
docs.

It also fixes an issue where the spans were calculated incorrectly for
certain unsugared doc comments. The diagnostic will now always use the
span of the attributes, as originally intended.

Fixes rust-lang#55964.
bors added a commit that referenced this issue Dec 12, 2018
fix intra-link resolution spans in block comments

This commit improves the calculation of code spans for intra-doc
resolution failures. All sugared doc comments should now have the
correct spans, including those where the comment is longer than the
docs.

It also fixes an issue where the spans were calculated incorrectly for
certain unsugared doc comments. The diagnostic will now always use the
span of the attributes, as originally intended.

Fixes #55964.

r? @QuietMisdreavus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-intra-doc-links Area: Intra-doc links, the ability to link to items in docs by name 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.

2 participants