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

Fix injected errors when running doctests on a crate named after a keyword #79775

Merged
merged 1 commit into from
Feb 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/librustdoc/doctest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/doctest/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}"
Expand Down
2 changes: 1 addition & 1 deletion src/test/rustdoc/playground-arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"