Skip to content

Commit 7724a04

Browse files
committed
rustdoc: Fix documenting rustc-macro crates
This commit adds a "hack" to the session to track whether we're a rustdoc session or not. If we're rustdoc then we skip the expansion to add the rustc-macro infrastructure. Closes rust-lang#36820
1 parent 86affcd commit 7724a04

File tree

5 files changed

+50
-13
lines changed

5 files changed

+50
-13
lines changed

src/bootstrap/step.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -486,7 +486,6 @@ impl<'a> Step<'a> {
486486
Source::CheckCodegenUnits { compiler } |
487487
Source::CheckIncremental { compiler } |
488488
Source::CheckUi { compiler } |
489-
Source::CheckRustdoc { compiler } |
490489
Source::CheckPretty { compiler } |
491490
Source::CheckCFail { compiler } |
492491
Source::CheckRPassValgrind { compiler } |
@@ -509,6 +508,7 @@ impl<'a> Step<'a> {
509508
self.debugger_scripts(compiler.stage),
510509
]
511510
}
511+
Source::CheckRustdoc { compiler } |
512512
Source::CheckRPassFull { compiler } |
513513
Source::CheckRFailFull { compiler } |
514514
Source::CheckCFailFull { compiler } |

src/librustc/session/config.rs

+7
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,11 @@ top_level_options!(
287287
alt_std_name: Option<String> [TRACKED],
288288
// Indicates how the compiler should treat unstable features
289289
unstable_features: UnstableFeatures [TRACKED],
290+
291+
// Indicates whether this run of the compiler is actually rustdoc. This
292+
// is currently just a hack and will be removed eventually, so please
293+
// try to not rely on this too much.
294+
actually_rustdoc: bool [TRACKED],
290295
}
291296
);
292297

@@ -439,6 +444,7 @@ pub fn basic_options() -> Options {
439444
libs: Vec::new(),
440445
unstable_features: UnstableFeatures::Disallow,
441446
debug_assertions: true,
447+
actually_rustdoc: false,
442448
}
443449
}
444450

@@ -1536,6 +1542,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
15361542
libs: libs,
15371543
unstable_features: UnstableFeatures::from_environment(),
15381544
debug_assertions: debug_assertions,
1545+
actually_rustdoc: false,
15391546
},
15401547
cfg)
15411548
}

src/librustc_driver/driver.rs

+17-12
Original file line numberDiff line numberDiff line change
@@ -703,18 +703,23 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
703703
sess.diagnostic())
704704
});
705705

706-
krate = time(time_passes, "maybe creating a macro crate", || {
707-
let crate_types = sess.crate_types.borrow();
708-
let is_rustc_macro_crate = crate_types.contains(&config::CrateTypeRustcMacro);
709-
let num_crate_types = crate_types.len();
710-
syntax_ext::rustc_macro_registrar::modify(&sess.parse_sess,
711-
&mut resolver,
712-
krate,
713-
is_rustc_macro_crate,
714-
num_crate_types,
715-
sess.diagnostic(),
716-
&sess.features.borrow())
717-
});
706+
// If we're in rustdoc we're always compiling as an rlib, but that'll trip a
707+
// bunch of checks in the `modify` function below. For now just skip this
708+
// step entirely if we're rustdoc as it's not too useful anyway.
709+
if !sess.opts.actually_rustdoc {
710+
krate = time(time_passes, "maybe creating a macro crate", || {
711+
let crate_types = sess.crate_types.borrow();
712+
let num_crate_types = crate_types.len();
713+
let is_rustc_macro_crate = crate_types.contains(&config::CrateTypeRustcMacro);
714+
syntax_ext::rustc_macro_registrar::modify(&sess.parse_sess,
715+
&mut resolver,
716+
krate,
717+
is_rustc_macro_crate,
718+
num_crate_types,
719+
sess.diagnostic(),
720+
&sess.features.borrow())
721+
});
722+
}
718723

719724
if sess.opts.debugging_opts.input_stats {
720725
println!("Post-expansion node count: {}", count_nodes(&krate));

src/librustdoc/core.rs

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ pub fn run_core(search_paths: SearchPaths,
150150
target_triple: triple.unwrap_or(config::host_triple().to_string()),
151151
// Ensure that rustdoc works even if rustc is feature-staged
152152
unstable_features: UnstableFeatures::Allow,
153+
actually_rustdoc: true,
153154
..config::basic_options().clone()
154155
};
155156

src/test/rustdoc/rustc-macro-crate.rs

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2016 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+
// no-prefer-dynamic
12+
13+
#![feature(rustc_macro)]
14+
#![feature(rustc_macro_lib)]
15+
#![crate_type = "rustc-macro"]
16+
17+
extern crate rustc_macro;
18+
19+
use rustc_macro::TokenStream;
20+
21+
#[rustc_macro_derive(Foo)]
22+
pub fn foo(input: TokenStream) -> TokenStream {
23+
input
24+
}

0 commit comments

Comments
 (0)