Skip to content

Commit 0e9d6f9

Browse files
committed
Rollup merge of #49864 - QuietMisdreavus:doctest-target-features, r=GuillaumeGomez
add target features when extracting and running doctests When rendering documentation, rustdoc will happily load target features into the cfg environment from the current target, but fails to do this when doing anything with doctests. This would lead to situations where, thanks to #48759, functions tagged with `#[target_feature]` couldn't run doctests, thanks to the automatic `#[doc(cfg(target_feature = "..."))]`. Currently, there's no way to pass codegen options to rustdoc that will affect its rustc sessions, but for now this will let you use target features that come default on the platform you're targeting. Fixes #49723
2 parents 709ec40 + 3366032 commit 0e9d6f9

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

src/librustdoc/test.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use rustc::session::config::{OutputType, OutputTypes, Externs};
2828
use rustc::session::search_paths::{SearchPaths, PathKind};
2929
use rustc_metadata::dynamic_lib::DynamicLibrary;
3030
use tempdir::TempDir;
31-
use rustc_driver::{self, driver, Compilation};
31+
use rustc_driver::{self, driver, target_features, Compilation};
3232
use rustc_driver::driver::phase_2_configure_and_expand;
3333
use rustc_metadata::cstore::CStore;
3434
use rustc_resolve::MakeGlobMap;
@@ -96,8 +96,10 @@ pub fn run(input_path: &Path,
9696
let trans = rustc_driver::get_trans(&sess);
9797
let cstore = CStore::new(trans.metadata_loader());
9898
rustc_lint::register_builtins(&mut sess.lint_store.borrow_mut(), Some(&sess));
99-
sess.parse_sess.config =
100-
config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
99+
100+
let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
101+
target_features::add_configuration(&mut cfg, &sess, &*trans);
102+
sess.parse_sess.config = cfg;
101103

102104
let krate = panictry!(driver::phase_1_parse_input(&driver::CompileController::basic(),
103105
&sess,
@@ -271,8 +273,11 @@ fn run_test(test: &str, cratename: &str, filename: &FileName, line: usize,
271273
let outdir = Mutex::new(TempDir::new("rustdoctest").ok().expect("rustdoc needs a tempdir"));
272274
let libdir = sess.target_filesearch(PathKind::All).get_lib_path();
273275
let mut control = driver::CompileController::basic();
274-
sess.parse_sess.config =
275-
config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
276+
277+
let mut cfg = config::build_configuration(&sess, config::parse_cfgspecs(cfgs.clone()));
278+
target_features::add_configuration(&mut cfg, &sess, &*trans);
279+
sess.parse_sess.config = cfg;
280+
276281
let out = Some(outdir.lock().unwrap().path().to_path_buf());
277282

278283
if no_run {
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// only-x86_64
12+
// compile-flags:--test
13+
// should-fail
14+
// no-system-llvm
15+
16+
// #49723: rustdoc didn't add target features when extracting or running doctests
17+
18+
#![feature(doc_cfg)]
19+
20+
/// Foo
21+
///
22+
/// # Examples
23+
///
24+
/// ```
25+
/// #![feature(cfg_target_feature)]
26+
///
27+
/// #[cfg(target_feature = "sse")]
28+
/// assert!(false);
29+
/// ```
30+
#[doc(cfg(target_feature = "sse"))]
31+
pub unsafe fn foo() {}

0 commit comments

Comments
 (0)