Skip to content

Commit

Permalink
rustdoc: Fix documenting rustc-macro crates
Browse files Browse the repository at this point in the history
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 #36820
  • Loading branch information
alexcrichton committed Sep 30, 2016
1 parent 86affcd commit 7724a04
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,6 @@ impl<'a> Step<'a> {
Source::CheckCodegenUnits { compiler } |
Source::CheckIncremental { compiler } |
Source::CheckUi { compiler } |
Source::CheckRustdoc { compiler } |
Source::CheckPretty { compiler } |
Source::CheckCFail { compiler } |
Source::CheckRPassValgrind { compiler } |
Expand All @@ -509,6 +508,7 @@ impl<'a> Step<'a> {
self.debugger_scripts(compiler.stage),
]
}
Source::CheckRustdoc { compiler } |
Source::CheckRPassFull { compiler } |
Source::CheckRFailFull { compiler } |
Source::CheckCFailFull { compiler } |
Expand Down
7 changes: 7 additions & 0 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,11 @@ top_level_options!(
alt_std_name: Option<String> [TRACKED],
// Indicates how the compiler should treat unstable features
unstable_features: UnstableFeatures [TRACKED],

// Indicates whether this run of the compiler is actually rustdoc. This
// is currently just a hack and will be removed eventually, so please
// try to not rely on this too much.
actually_rustdoc: bool [TRACKED],
}
);

Expand Down Expand Up @@ -439,6 +444,7 @@ pub fn basic_options() -> Options {
libs: Vec::new(),
unstable_features: UnstableFeatures::Disallow,
debug_assertions: true,
actually_rustdoc: false,
}
}

Expand Down Expand Up @@ -1536,6 +1542,7 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
libs: libs,
unstable_features: UnstableFeatures::from_environment(),
debug_assertions: debug_assertions,
actually_rustdoc: false,
},
cfg)
}
Expand Down
29 changes: 17 additions & 12 deletions src/librustc_driver/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,18 +703,23 @@ pub fn phase_2_configure_and_expand<'a, F>(sess: &Session,
sess.diagnostic())
});

krate = time(time_passes, "maybe creating a macro crate", || {
let crate_types = sess.crate_types.borrow();
let is_rustc_macro_crate = crate_types.contains(&config::CrateTypeRustcMacro);
let num_crate_types = crate_types.len();
syntax_ext::rustc_macro_registrar::modify(&sess.parse_sess,
&mut resolver,
krate,
is_rustc_macro_crate,
num_crate_types,
sess.diagnostic(),
&sess.features.borrow())
});
// If we're in rustdoc we're always compiling as an rlib, but that'll trip a
// bunch of checks in the `modify` function below. For now just skip this
// step entirely if we're rustdoc as it's not too useful anyway.
if !sess.opts.actually_rustdoc {
krate = time(time_passes, "maybe creating a macro crate", || {
let crate_types = sess.crate_types.borrow();
let num_crate_types = crate_types.len();
let is_rustc_macro_crate = crate_types.contains(&config::CrateTypeRustcMacro);
syntax_ext::rustc_macro_registrar::modify(&sess.parse_sess,
&mut resolver,
krate,
is_rustc_macro_crate,
num_crate_types,
sess.diagnostic(),
&sess.features.borrow())
});
}

if sess.opts.debugging_opts.input_stats {
println!("Post-expansion node count: {}", count_nodes(&krate));
Expand Down
1 change: 1 addition & 0 deletions src/librustdoc/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ pub fn run_core(search_paths: SearchPaths,
target_triple: triple.unwrap_or(config::host_triple().to_string()),
// Ensure that rustdoc works even if rustc is feature-staged
unstable_features: UnstableFeatures::Allow,
actually_rustdoc: true,
..config::basic_options().clone()
};

Expand Down
24 changes: 24 additions & 0 deletions src/test/rustdoc/rustc-macro-crate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// no-prefer-dynamic

#![feature(rustc_macro)]
#![feature(rustc_macro_lib)]
#![crate_type = "rustc-macro"]

extern crate rustc_macro;

use rustc_macro::TokenStream;

#[rustc_macro_derive(Foo)]
pub fn foo(input: TokenStream) -> TokenStream {
input
}

0 comments on commit 7724a04

Please sign in to comment.