diff options
| author | Dylan DPC <dylan.dpc@gmail.com> | 2021-02-12 22:53:21 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-02-12 22:53:21 +0100 |
| commit | ab3f4f0bc0f953b84fa1e175b3f996209b22f9cf (patch) | |
| tree | 323a8d56b5a7d5438bd82ad4d83f48b95a075e96 | |
| parent | 3f5aee2d5241139d808f4fdece0026603489afd1 (diff) | |
| parent | 02ffe9ef0101cb7b0540ca6195d63442ad3df5ec (diff) | |
| download | rust-ab3f4f0bc0f953b84fa1e175b3f996209b22f9cf.tar.gz rust-ab3f4f0bc0f953b84fa1e175b3f996209b22f9cf.zip | |
Rollup merge of #79775 - jyn514:doctest, r=GuillaumeGomez
Fix injected errors when running doctests on a crate named after a keyword Closes https://github.com/rust-lang/rust/issues/79771
| -rw-r--r-- | src/librustdoc/doctest.rs | 7 | ||||
| -rw-r--r-- | src/librustdoc/doctest/tests.rs | 8 | ||||
| -rw-r--r-- | src/test/rustdoc/playground-arg.rs | 2 |
3 files changed, 10 insertions, 7 deletions
diff --git a/src/librustdoc/doctest.rs b/src/librustdoc/doctest.rs index eecfd337cdf..fb4774ae192 100644 --- a/src/librustdoc/doctest.rs +++ b/src/librustdoc/doctest.rs @@ -546,9 +546,12 @@ crate fn make_test( // compiler. if !already_has_extern_crate && !opts.no_crate_inject && cratename != Some("std") { if let Some(cratename) = cratename { - // Make sure its actually used if not included. + // Don't inject `extern crate` if the crate is never used. + // NOTE: this is terribly inaccurate because it doesn't actually + // parse the source, but only has false positives, not false + // negatives. if s.contains(cratename) { - prog.push_str(&format!("extern crate {};\n", cratename)); + prog.push_str(&format!("extern crate r#{};\n", cratename)); line_offset += 1; } } diff --git a/src/librustdoc/doctest/tests.rs b/src/librustdoc/doctest/tests.rs index 465b2b1d69b..c49e45c0e25 100644 --- a/src/librustdoc/doctest/tests.rs +++ b/src/librustdoc/doctest/tests.rs @@ -38,7 +38,7 @@ fn make_test_crate_name() { let input = "use asdf::qwop; assert_eq!(2+2, 4);"; let expected = "#![allow(unused)] -extern crate asdf; +extern crate r#asdf; fn main() { use asdf::qwop; assert_eq!(2+2, 4); @@ -128,7 +128,7 @@ fn make_test_opts_attrs() { let input = "use asdf::qwop; assert_eq!(2+2, 4);"; let expected = "#![feature(sick_rad)] -extern crate asdf; +extern crate r#asdf; fn main() { use asdf::qwop; assert_eq!(2+2, 4); @@ -141,7 +141,7 @@ assert_eq!(2+2, 4); opts.attrs.push("feature(hella_dope)".to_string()); let expected = "#![feature(sick_rad)] #![feature(hella_dope)] -extern crate asdf; +extern crate r#asdf; fn main() { use asdf::qwop; assert_eq!(2+2, 4); @@ -250,7 +250,7 @@ assert_eq!(asdf::foo, 4);"; let expected = "#![allow(unused)] extern crate hella_qwop; -extern crate asdf; +extern crate r#asdf; fn main() { assert_eq!(asdf::foo, 4); }" diff --git a/src/test/rustdoc/playground-arg.rs b/src/test/rustdoc/playground-arg.rs index 018716ad45a..dbe2297f818 100644 --- a/src/test/rustdoc/playground-arg.rs +++ b/src/test/rustdoc/playground-arg.rs @@ -11,4 +11,4 @@ pub fn dummy() {} // ensure that `extern crate foo;` was inserted into code snips automatically: -// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=%23!%5Ballow(unused)%5D%0Aextern%20crate%20foo%3B%0Afn%20main()%20%7B%0Ause%20foo%3A%3Adummy%3B%0Adummy()%3B%0A%7D&edition=2015"]' "Run" +// @matches foo/index.html '//a[@class="test-arrow"][@href="https://example.com/?code=%23!%5Ballow(unused)%5D%0Aextern%20crate%20r%23foo%3B%0Afn%20main()%20%7B%0Ause%20foo%3A%3Adummy%3B%0Adummy()%3B%0A%7D&edition=2015"]' "Run" |
