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 fails unpleasantly when it can't tokenise a code block #12834

Closed
chris-morgan opened this issue Mar 12, 2014 · 3 comments · Fixed by #33199
Closed

rustdoc fails unpleasantly when it can't tokenise a code block #12834

chris-morgan opened this issue Mar 12, 2014 · 3 comments · Fixed by #33199
Labels
T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@chris-morgan
Copy link
Member

Consider a file like this:

#[crate_id = "sad"];

/// ```
/// a + b ∈ Self ∀ a, b ∈ Self
/// ```
pub fn foo() {}

Here's rustdoc's opinion of this:

<stdin>:1:7: 1:8 error: unknown start of token: \u2208
<stdin>:1 a + b ∈ Self ∀ a, b ∈ Self
                ^
task '<unnamed>' failed at '~Any', /home/chris/vc/rust/src/libsyntax/diagnostic.rs:48

You see, it attempted to tokenise the contents of that code block.

The current solution for this is marking that code block as "notrust":

/// ```notrust
/// a + b ∈ Self ∀ a, b ∈ Self
/// ```
pub fn foo() {}

rustdoc should do one of two things:

  1. Permit tokenisation to fail silently, discarding the notion of syntax highlighting should it fail; or

  2. Retain the current notrust-must-be-explicit behaviour, but with two alterations:

    1. Give meaningful spans, if at all possible—sad.rs:4:11: 4:12 as the error span rather than <stdin>:1:7: 1:7.
    2. Give you a hint that that might be want you meant.

    In total, perhaps something like this:

    <stdin>:1:7: 1:8 error: unknown start of token: \u2208
    <stdin>:1 a + b ∈ Self ∀ a, b ∈ Self
                    ^
    sad.rs:3:5: 3:8 info: that code block does not tokenise as Rust code; perhaps you meant to mark it as notrust?
                          (e.g. use the opening fence "```notrust")
    sad.rs:3 /// ```
                 ^~~
    
@dennisoh
Copy link

I think that "A-rustdoc" label can be added.

@huonw huonw added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Jan 14, 2015
@steveklabnik
Copy link
Member

Triage: no changes.

@frewsxcv
Copy link
Member

Relevant issue: servo/servo#9704

bors added a commit that referenced this issue Apr 27, 2016
Make some fatal lexer errors recoverable

I've kept the changes to a minimum since I'm not really sure if this approach is a acceptable.

fixes #12834

cc @nrc
fasterthanlime pushed a commit to fasterthanlime/rust that referenced this issue Jul 22, 2022
…ain, r=Veykril

Add PROC_MACRO_TEST_TOOLCHAIN environment variable

This allows overriding the toolchain used to run `proc-macro-srv` tests.

---

Sample usage.

Testing the current ABI (variable unset/empty):

```shell
amos@tails ~/bearcove/rust-analyzer/crates/proc-macro-srv proc-macro-test-toolchain*
❯ PROC_MACRO_TEST_TOOLCHAIN="" cargo test --quiet

running 16 tests
................
test result: ok. 16 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s
```

Testing an older ABI:

```shell
amos@tails ~/bearcove/rust-analyzer/crates/proc-macro-srv proc-macro-test-toolchain*
❯ PROC_MACRO_TEST_TOOLCHAIN="1.58" cargo test --quiet

running 16 tests
................
test result: ok. 16 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s
```

Testing current nightly ABI:

```shell
❯ rustc +nightly --version
rustc 1.64.0-nightly (f858854 2022-07-18)

❯ PROC_MACRO_TEST_TOOLCHAIN="nightly" cargo test --quiet

running 16 tests
................
test result: ok. 16 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.01s
```

Testing future ABI (`rust-lang/rust` master):

```shell
amos@tails ~/bearcove/rust-analyzer/crates/proc-macro-srv proc-macro-test-toolchain
❯ PROC_MACRO_TEST_TOOLCHAIN="stage1" cargo test --quiet

running 16 tests
..........thread '<unnamed>' panicked at 'range end index 216221164920373249 out of range for slice of length 18', library/core/src/slice/index.rs:73:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
....F.
failures:

---- tests::test_fn_like_macro2 stdout ----
thread 'tests::test_fn_like_macro2' panicked at 'called `Result::unwrap()` on an `Err` value: "range end index 216221164920373249 out of range for slice of length 18"', crates/proc-macro-srv/src/tests/utils.rs:38:83
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

failures:
    tests::test_fn_like_macro2

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

error: test failed, to rerun pass '--lib
```

---

Tagging `@jonas-schievink:` this might be helpful when updating versioned ABIs later on.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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.

5 participants