Skip to content

Commit 070d6b3

Browse files
committed
Move configuration 1 phase before crate metadata collection
Stripping unconfigured items prior to collecting crate metadata means we can say things like `#![cfg_attr(foo, crate_type="lib")]`. Fixes #25347.
1 parent 3ca008d commit 070d6b3

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

src/librustc_driver/driver.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -383,17 +383,8 @@ pub fn phase_2_configure_and_expand(sess: &Session,
383383
-> Option<ast::Crate> {
384384
let time_passes = sess.time_passes();
385385

386-
*sess.crate_types.borrow_mut() =
387-
collect_crate_types(sess, &krate.attrs);
388-
*sess.crate_metadata.borrow_mut() =
389-
collect_crate_metadata(sess, &krate.attrs);
390-
391-
time(time_passes, "recursion limit", (), |_| {
392-
middle::recursion_limit::update_recursion_limit(sess, &krate);
393-
});
394-
395-
// strip before expansion to allow macros to depend on
396-
// configuration variables e.g/ in
386+
// strip before anything else because crate metadata may use #[cfg_attr]
387+
// and so macros can depend on configuration variables, such as
397388
//
398389
// #[macro_use] #[cfg(foo)]
399390
// mod bar { macro_rules! baz!(() => {{}}) }
@@ -403,6 +394,15 @@ pub fn phase_2_configure_and_expand(sess: &Session,
403394
krate = time(time_passes, "configuration 1", krate, |krate|
404395
syntax::config::strip_unconfigured_items(sess.diagnostic(), krate));
405396

397+
*sess.crate_types.borrow_mut() =
398+
collect_crate_types(sess, &krate.attrs);
399+
*sess.crate_metadata.borrow_mut() =
400+
collect_crate_metadata(sess, &krate.attrs);
401+
402+
time(time_passes, "recursion limit", (), |_| {
403+
middle::recursion_limit::update_recursion_limit(sess, &krate);
404+
});
405+
406406
time(time_passes, "gated macro checking", (), |_| {
407407
let features =
408408
syntax::feature_gate::check_crate_macros(sess.codemap(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) --cfg foo lib.rs
5+
# if compilation worked, then it built as a library
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2015 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+
#![cfg_attr(foo, crate_type="lib")]
12+
13+
pub fn foo() {}

0 commit comments

Comments
 (0)