Skip to content

Commit c2ac7cf

Browse files
authored
Rollup merge of rust-lang#79775 - jyn514:doctest, r=GuillaumeGomez
Fix injected errors when running doctests on a crate named after a keyword Closes rust-lang#79771
2 parents d416093 + 02ffe9e commit c2ac7cf

File tree

3 files changed

+10
-7
lines changed

3 files changed

+10
-7
lines changed

src/librustdoc/doctest.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -546,9 +546,12 @@ crate fn make_test(
546546
// compiler.
547547
if !already_has_extern_crate && !opts.no_crate_inject && cratename != Some("std") {
548548
if let Some(cratename) = cratename {
549-
// Make sure its actually used if not included.
549+
// Don't inject `extern crate` if the crate is never used.
550+
// NOTE: this is terribly inaccurate because it doesn't actually
551+
// parse the source, but only has false positives, not false
552+
// negatives.
550553
if s.contains(cratename) {
551-
prog.push_str(&format!("extern crate {};\n", cratename));
554+
prog.push_str(&format!("extern crate r#{};\n", cratename));
552555
line_offset += 1;
553556
}
554557
}

src/librustdoc/doctest/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn make_test_crate_name() {
3838
let input = "use asdf::qwop;
3939
assert_eq!(2+2, 4);";
4040
let expected = "#![allow(unused)]
41-
extern crate asdf;
41+
extern crate r#asdf;
4242
fn main() {
4343
use asdf::qwop;
4444
assert_eq!(2+2, 4);
@@ -128,7 +128,7 @@ fn make_test_opts_attrs() {
128128
let input = "use asdf::qwop;
129129
assert_eq!(2+2, 4);";
130130
let expected = "#![feature(sick_rad)]
131-
extern crate asdf;
131+
extern crate r#asdf;
132132
fn main() {
133133
use asdf::qwop;
134134
assert_eq!(2+2, 4);
@@ -141,7 +141,7 @@ assert_eq!(2+2, 4);
141141
opts.attrs.push("feature(hella_dope)".to_string());
142142
let expected = "#![feature(sick_rad)]
143143
#![feature(hella_dope)]
144-
extern crate asdf;
144+
extern crate r#asdf;
145145
fn main() {
146146
use asdf::qwop;
147147
assert_eq!(2+2, 4);
@@ -250,7 +250,7 @@ assert_eq!(asdf::foo, 4);";
250250

251251
let expected = "#![allow(unused)]
252252
extern crate hella_qwop;
253-
extern crate asdf;
253+
extern crate r#asdf;
254254
fn main() {
255255
assert_eq!(asdf::foo, 4);
256256
}"

src/test/rustdoc/playground-arg.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
pub fn dummy() {}
1212

1313
// ensure that `extern crate foo;` was inserted into code snips automatically:
14-
// @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"
14+
// @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"

0 commit comments

Comments
 (0)