Skip to content

Incorrect span used for doctest when both an outer doc comment (///) and inner doc comment (//!) are used on a module #117753

Open
@IceTDrinker

Description

@IceTDrinker

Code

my_module.rs

//! Malformed example
//!
//! ```
//! not valid rust code
//! ```

lib.rs

/// Some outer doc, remove/comment it to get the output I would expect
pub mod my_module;

cargo +nightly test --doc

Current output

cargo +nightly test --doc
   Compiling doctest_wrong_line v0.1.0 (/doctest_wrong_line)
    Finished test [unoptimized + debuginfo] target(s) in 0.02s
   Doc-tests doctest_wrong_line

running 1 test
test src/lib.rs - my_module (line 4) ... FAILED

failures:

---- src/lib.rs - my_module (line 4) stdout ----
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `valid`
 --> src/lib.rs:5:5
  |
3 | not valid rust code
  |     ^^^^^ expected one of 8 possible tokens

error: aborting due to previous error

Couldn't compile the test.

failures:
    src/lib.rs - my_module (line 4)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

error: doctest failed, to rerun pass `--doc`

Desired output

cargo +nightly test --doc
   Compiling doctest_wrong_line v0.1.0 (/doctest_wrong_line)
    Finished test [unoptimized + debuginfo] target(s) in 0.02s
   Doc-tests doctest_wrong_line

running 1 test
test src/my_module.rs - my_module (line 3) ... FAILED

failures:

---- src/my_module.rs - my_module (line 3) stdout ----
error: expected one of `!`, `.`, `::`, `;`, `?`, `{`, `}`, or an operator, found `valid`
 --> src/my_module.rs:4:5
  |
3 | not valid rust code
  |     ^^^^^ expected one of 8 possible tokens

error: aborting due to previous error

Couldn't compile the test.

failures:
    src/my_module.rs - my_module (line 3)

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s

error: doctest failed, to rerun pass `--doc`

Rationale and extra context

the lines indicate lib.rs as the location where the failure happens which is not correct though I understand it may appear so to the compiler given it might be fusing the doc content at some point and not keep sub blocks original span (though it's confusing as I ended up in my lib.rs at a location that did not exist)

Basically adding a /// comment on top of the module on the lib.rs should not be modifying the span of the docstring coming from the module

In my case it indicated a non existent line in a rather small lib.rs

Other cases

No response

Anything else?

I tried various combinations of keywords to find related issues in the repo but could not find any (though some look similar they use an include macro which is not the case here #81070)

Thanks for the high quality of the tooling, those are edge cases which happen because well, if it can happen it will 😄

Cheers

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsA-doctestsArea: Documentation tests, run by rustdocT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions