Skip to content

Commit 8e90412

Browse files
committed
rustdoc: adding some common feature gates when testing a markdown file.
The manual, tutorial and guides need the feature gates quite often, unfortunately, so this is the low-cost path to migrating to use rustdoc. This is only activated for pure-Markdown files. Preferably this would be avoided: #12773
1 parent 7a70ec1 commit 8e90412

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

src/librustdoc/markdown.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ pub fn render(input: &str, mut output: Path, matches: &getopts::Matches) -> int
163163
pub fn test(input: &str, libs: @RefCell<HashSet<Path>>, mut test_args: ~[~str]) -> int {
164164
let input_str = load_or_return!(input, 1, 2);
165165

166-
let mut collector = Collector::new(input.to_owned(), libs, true);
166+
let mut collector = Collector::new(input.to_owned(), libs, true, true);
167167
find_testable_code(input_str, &mut collector);
168168
test_args.unshift(~"rustdoctest");
169169
testing::test_main(test_args, collector.tests);

src/librustdoc/test.rs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn run(input: &str, libs: @RefCell<HashSet<Path>>, mut test_args: ~[~str]) -
7777
let (krate, _) = passes::unindent_comments(krate);
7878
let (krate, _) = passes::collapse_docs(krate);
7979

80-
let mut collector = Collector::new(krate.name.to_owned(), libs, false);
80+
let mut collector = Collector::new(krate.name.to_owned(), libs, false, false);
8181
collector.fold_crate(krate);
8282

8383
test_args.unshift(~"rustdoctest");
@@ -88,8 +88,8 @@ pub fn run(input: &str, libs: @RefCell<HashSet<Path>>, mut test_args: ~[~str]) -
8888
}
8989

9090
fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
91-
no_run: bool) {
92-
let test = maketest(test, cratename);
91+
no_run: bool, loose_feature_gating: bool) {
92+
let test = maketest(test, cratename, loose_feature_gating);
9393
let parsesess = parse::new_parse_sess();
9494
let input = driver::StrInput(test);
9595

@@ -162,11 +162,18 @@ fn runtest(test: &str, cratename: &str, libs: HashSet<Path>, should_fail: bool,
162162
}
163163
}
164164

165-
fn maketest(s: &str, cratename: &str) -> ~str {
165+
fn maketest(s: &str, cratename: &str, loose_feature_gating: bool) -> ~str {
166166
let mut prog = ~r"
167167
#[deny(warnings)];
168168
#[allow(unused_variable, dead_assignment, unused_mut, attribute_usage, dead_code)];
169169
";
170+
171+
if loose_feature_gating {
172+
// FIXME #12773: avoid inserting these when the tutorial & manual
173+
// etc. have been updated to not use them so prolifically.
174+
prog.push_str("#[ feature(macro_rules, globs, struct_variant, managed_boxes) ];\n");
175+
}
176+
170177
if !s.contains("extern crate") {
171178
if s.contains("extra") {
172179
prog.push_str("extern crate extra;\n");
@@ -194,18 +201,23 @@ pub struct Collector {
194201
priv use_headers: bool,
195202
priv current_header: Option<~str>,
196203
priv cratename: ~str,
204+
205+
priv loose_feature_gating: bool
197206
}
198207

199208
impl Collector {
200-
pub fn new(cratename: ~str, libs: @RefCell<HashSet<Path>>, use_headers: bool) -> Collector {
209+
pub fn new(cratename: ~str, libs: @RefCell<HashSet<Path>>,
210+
use_headers: bool, loose_feature_gating: bool) -> Collector {
201211
Collector {
202212
tests: ~[],
203213
names: ~[],
204214
libs: libs,
205215
cnt: 0,
206216
use_headers: use_headers,
207217
current_header: None,
208-
cratename: cratename
218+
cratename: cratename,
219+
220+
loose_feature_gating: loose_feature_gating
209221
}
210222
}
211223

@@ -220,6 +232,7 @@ impl Collector {
220232
let libs = self.libs.borrow();
221233
let libs = (*libs.get()).clone();
222234
let cratename = self.cratename.to_owned();
235+
let loose_feature_gating = self.loose_feature_gating;
223236
debug!("Creating test {}: {}", name, test);
224237
self.tests.push(testing::TestDescAndFn {
225238
desc: testing::TestDesc {
@@ -228,7 +241,7 @@ impl Collector {
228241
should_fail: false, // compiler failures are test failures
229242
},
230243
testfn: testing::DynTestFn(proc() {
231-
runtest(test, cratename, libs, should_fail, no_run);
244+
runtest(test, cratename, libs, should_fail, no_run, loose_feature_gating);
232245
}),
233246
});
234247
}

0 commit comments

Comments
 (0)