Skip to content

Commit 2792855

Browse files
committed
Auto merge of #25399 - kballard:crate-attributes-cfg_attr, r=alexcrichton
Stripping unconfigured items prior to collecting crate metadata means we can say things like `#![cfg_attr(foo, crate_type="lib")]`. Fixes #25347.
2 parents 571f371 + 90b9529 commit 2792855

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-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,16 @@
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+
// no-prefer-dynamic
12+
// compile-flags: --cfg foo
13+
14+
#![cfg_attr(foo, crate_type="lib")]
15+
16+
pub fn foo() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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+
// aux-build:crate-attributes-using-cfg_attr.rs
12+
13+
extern crate crate_attributes_using_cfg_attr;
14+
15+
pub fn main() {}

0 commit comments

Comments
 (0)