Skip to content

multiline cfg_attr in a doctest outputs a corrupted snippet #55713

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

Closed
scampi opened this issue Nov 6, 2018 · 5 comments · Fixed by #95590
Closed

multiline cfg_attr in a doctest outputs a corrupted snippet #55713

scampi opened this issue Nov 6, 2018 · 5 comments · Fixed by #95590
Labels
A-doctests Area: Documentation tests, run by rustdoc T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@scampi
Copy link
Contributor

scampi commented Nov 6, 2018

With following doctest, where the cfg attribute spans multiple lines, the final program is invalid: the wrapping main method doesn't take into account the dangling stdsimd))] line that closes the attribute.

/// ```rust
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature,
/// # stdsimd))]
///
/// fn foo() {
///     let mut dst = [0];
///     add_quickly(&[1], &[2], &mut dst);
///     assert_eq!(dst[0], 3);
/// }
/// ```
 INFO 2018-11-06T10:25:21Z: rustdoc::test: final test program: #![allow(unused)]
#![cfg_attr(not(dox), feature(cfg_target_feature, target_feature,
extern crate stdsimd;
fn main() {
stdsimd))]

fn foo() {
    let mut dst = [0];
    add_quickly(&[1], &[2], &mut dst);
    assert_eq!(dst[0], 3);
}
}

Meta

$ rustc --version --verbose
rustc 1.32.0-nightly (13dab66a6 2018-11-05)
binary: rustc
commit-hash: 13dab66a6f6403f4eee092456f7f8f46199c5859
commit-date: 2018-11-05
host: x86_64-unknown-linux-gnu
release: 1.32.0-nightly
LLVM version: 8.0
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::_print
             at libstd/sys_common/backtrace.rs:71
   2: std::panicking::default_hook::{{closure}}
             at libstd/sys_common/backtrace.rs:59
             at libstd/panicking.rs:211
   3: std::panicking::default_hook
             at libstd/panicking.rs:221
   4: std::panicking::rust_panic_with_hook
             at libstd/panicking.rs:476
   5: std::panicking::begin_panic
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/libstd/panicking.rs:410
   6: rustdoc::test::run_test
             at librustdoc/test.rs:323
   7: <scoped_tls::ScopedKey<T>>::set
             at librustdoc/test.rs:654
             at /cargo/registry/src/github.1485827954.workers.dev-1ecc6299db9ec823/scoped-tls-0.1.2/src/lib.rs:155
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/libsyntax/lib.rs:123
             at /cargo/registry/src/github.1485827954.workers.dev-1ecc6299db9ec823/scoped-tls-0.1.2/src/lib.rs:155
   8: syntax::with_globals
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/libsyntax/lib.rs:122
   9: std::panicking::try::do_call
             at librustdoc/test.rs:651
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/libstd/panic.rs:319
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/libstd/panicking.rs:310
  10: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102
  11: rustc_driver::in_named_rustc_thread
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/libstd/panicking.rs:289
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/libstd/panic.rs:398
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/librustc_driver/lib.rs:1554
  12: <F as alloc::boxed::FnBox<A>>::call_box
             at librustdoc/test.rs:651
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/liballoc/boxed.rs:672
  13: <F as alloc::boxed::FnBox<A>>::call_box
             at libtest/lib.rs:1464
             at /rustc/13dab66a6f6403f4eee092456f7f8f46199c5859/src/liballoc/boxed.rs:672
  14: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:102
@nrc nrc added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. A-parser Area: The lexing & parsing of Rust source code to an AST T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 6, 2018
@nrc
Copy link
Member

nrc commented Nov 6, 2018

cc @QuietMisdreavus @eddyb

@eddyb
Copy link
Member

eddyb commented Nov 6, 2018

@eddyb
Copy link
Member

eddyb commented Nov 6, 2018

I don't see a message associated with the stack trace, is that missing?

@QuietMisdreavus QuietMisdreavus removed the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Nov 6, 2018
@QuietMisdreavus
Copy link
Member

The problem lies in how rustdoc parses doctests. It scans lines for crate-level attributes and extern crate statements, but it doesn't do anything more intelligent than checking the beginning of each line:

rust/src/librustdoc/test.rs

Lines 502 to 521 in bdfeace

for line in s.lines() {
let trimline = line.trim();
let header = trimline.chars().all(|c| c.is_whitespace()) ||
trimline.starts_with("#![") ||
trimline.starts_with("#[macro_use] extern crate") ||
trimline.starts_with("extern crate");
if !header || after_header {
after_header = true;
after.push_str(line);
after.push_str("\n");
} else {
if trimline.starts_with("#[macro_use] extern crate")
|| trimline.starts_with("extern crate") {
crates.push_str(line);
crates.push_str("\n");
}
before.push_str(line);
before.push_str("\n");
}
}

We recently changed the parsing for fn main and extern crate my_crate detection to use the libsyntax parser, but this initial scan is still a string-based search.


The way to fix the test in the OP is to actually remove the cfg_attr part of it - rustdoc doesn't pass on any --cfg information into doctest compilation, so those features are going to be enabled regardless.

@QuietMisdreavus QuietMisdreavus added A-doctests Area: Documentation tests, run by rustdoc and removed A-parser Area: The lexing & parsing of Rust source code to an AST labels Nov 6, 2018
@scampi
Copy link
Contributor Author

scampi commented Nov 9, 2018

@eddyb This is the associated message:

thread 'src/../../../stdsimd/mod.rs - stdsimd::arch (line 210)' panicked at 'couldn't compile the test', librustdoc/test.rs:323:13

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-doctests Area: Documentation tests, run by rustdoc 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.

4 participants