Skip to content

Commit b772b5b

Browse files
authored
Rollup merge of rust-lang#66895 - Centril:rustc_feature, r=oli-obk
Feature gating *declarations* => new crate `rustc_feature` This PR moves the data-oriented parts of feature gating into its own crate, `rustc_feature`. The parts consist of some data types as well as `accepted`, `active`, `removed`, and `builtin_attrs`. Feature gate checking itself remains in `syntax::feature_gate::check`. The parts which define how to emit feature gate errors could probably be moved to `rustc_errors` or to the new `rustc_session` crate introduced in rust-lang#66878. The visitor itself could probably be moved as a pass in `rustc_passes` depending on how the dependency edges work out. The PR also contains some drive-by cleanup of feature gate checking. As such, the PR probably best read commit-by-commit. r? @oli-obk cc @petrochenkov cc @Mark-Simulacrum
2 parents b4bffce + ded177a commit b772b5b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+444
-460
lines changed

Cargo.lock

+18
Original file line numberDiff line numberDiff line change
@@ -3199,6 +3199,7 @@ dependencies = [
31993199
"rustc_data_structures",
32003200
"rustc_error_codes",
32013201
"rustc_errors",
3202+
"rustc_feature",
32023203
"rustc_fs_util",
32033204
"rustc_index",
32043205
"rustc_macros",
@@ -3574,6 +3575,7 @@ dependencies = [
35743575
"rustc_data_structures",
35753576
"rustc_error_codes",
35763577
"rustc_errors",
3578+
"rustc_feature",
35773579
"rustc_interface",
35783580
"rustc_lint",
35793581
"rustc_metadata",
@@ -3607,6 +3609,15 @@ dependencies = [
36073609
"unicode-width",
36083610
]
36093611

3612+
[[package]]
3613+
name = "rustc_feature"
3614+
version = "0.0.0"
3615+
dependencies = [
3616+
"lazy_static 1.3.0",
3617+
"rustc_data_structures",
3618+
"syntax_pos",
3619+
]
3620+
36103621
[[package]]
36113622
name = "rustc_fs_util"
36123623
version = "0.0.0"
@@ -3682,6 +3693,7 @@ dependencies = [
36823693
"rustc",
36833694
"rustc_data_structures",
36843695
"rustc_error_codes",
3696+
"rustc_feature",
36853697
"rustc_index",
36863698
"rustc_target",
36873699
"syntax",
@@ -3786,6 +3798,7 @@ dependencies = [
37863798
"rustc_data_structures",
37873799
"rustc_error_codes",
37883800
"rustc_errors",
3801+
"rustc_feature",
37893802
"rustc_lexer",
37903803
"rustc_target",
37913804
"smallvec 1.0.0",
@@ -3802,6 +3815,7 @@ dependencies = [
38023815
"rustc_data_structures",
38033816
"rustc_error_codes",
38043817
"rustc_errors",
3818+
"rustc_feature",
38053819
"rustc_index",
38063820
"rustc_parse",
38073821
"rustc_target",
@@ -3844,6 +3858,7 @@ dependencies = [
38443858
"rustc_data_structures",
38453859
"rustc_error_codes",
38463860
"rustc_errors",
3861+
"rustc_feature",
38473862
"rustc_metadata",
38483863
"smallvec 1.0.0",
38493864
"syntax",
@@ -4442,6 +4457,7 @@ dependencies = [
44424457
"rustc_data_structures",
44434458
"rustc_error_codes",
44444459
"rustc_errors",
4460+
"rustc_feature",
44454461
"rustc_index",
44464462
"rustc_lexer",
44474463
"rustc_macros",
@@ -4458,6 +4474,7 @@ dependencies = [
44584474
"log",
44594475
"rustc_data_structures",
44604476
"rustc_errors",
4477+
"rustc_feature",
44614478
"rustc_lexer",
44624479
"rustc_parse",
44634480
"serialize",
@@ -4475,6 +4492,7 @@ dependencies = [
44754492
"rustc_data_structures",
44764493
"rustc_error_codes",
44774494
"rustc_errors",
4495+
"rustc_feature",
44784496
"rustc_parse",
44794497
"rustc_target",
44804498
"smallvec 1.0.0",

src/librustc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ rustc-rayon = "0.3.0"
2222
rustc-rayon-core = "0.3.0"
2323
polonius-engine = "0.10.0"
2424
rustc_apfloat = { path = "../librustc_apfloat" }
25+
rustc_feature = { path = "../librustc_feature" }
2526
rustc_target = { path = "../librustc_target" }
2627
rustc_macros = { path = "../librustc_macros" }
2728
rustc_data_structures = { path = "../librustc_data_structures" }

src/librustc/arena.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ macro_rules! arena_types {
101101
[few] resolve_lifetimes: rustc::middle::resolve_lifetime::ResolveLifetimes,
102102
[few] lint_levels: rustc::lint::LintLevelMap,
103103
[few] stability_index: rustc::middle::stability::Index<'tcx>,
104-
[few] features: syntax::feature_gate::Features,
104+
[few] features: rustc_feature::Features,
105105
[few] all_traits: Vec<rustc::hir::def_id::DefId>,
106106
[few] privacy_access_levels: rustc::middle::privacy::AccessLevels,
107107
[few] target_features_whitelist: rustc_data_structures::fx::FxHashMap<

src/librustc/ich/impls_syntax.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use crate::ich::StableHashingContext;
55

66
use syntax::ast;
7-
use syntax::feature_gate;
87
use syntax_pos::SourceFile;
98

109
use crate::hir::def_id::{DefId, CrateNum, CRATE_DEF_INDEX};
@@ -156,7 +155,7 @@ fn stable_normalized_pos(np: ::syntax_pos::NormalizedPos,
156155
}
157156

158157

159-
impl<'tcx> HashStable<StableHashingContext<'tcx>> for feature_gate::Features {
158+
impl<'tcx> HashStable<StableHashingContext<'tcx>> for rustc_feature::Features {
160159
fn hash_stable(&self, hcx: &mut StableHashingContext<'tcx>, hasher: &mut StableHasher) {
161160
// Unfortunately we cannot exhaustively list fields here, since the
162161
// struct is macro generated.

src/librustc/lint/levels.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ impl<'a> LintLevelsBuilder<'a> {
232232
// don't have any lint names (`#[level(reason = "foo")]`)
233233
if let ast::LitKind::Str(rationale, _) = name_value.kind {
234234
if !self.sess.features_untracked().lint_reasons {
235-
feature_gate::emit_feature_err(
235+
feature_gate::feature_err(
236236
&self.sess.parse_sess,
237237
sym::lint_reasons,
238238
item.span,
239-
feature_gate::GateIssue::Language,
240239
"lint reasons are experimental"
241-
);
240+
)
241+
.emit();
242242
}
243243
reason = Some(rationale);
244244
} else {

src/librustc/middle/stability.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@ use crate::ty::query::Providers;
1313
use crate::middle::privacy::AccessLevels;
1414
use crate::session::{DiagnosticMessageId, Session};
1515
use errors::DiagnosticBuilder;
16+
use rustc_feature::GateIssue;
1617
use syntax::symbol::{Symbol, sym};
1718
use syntax_pos::{Span, MultiSpan};
1819
use syntax::ast::{Attribute, CRATE_NODE_ID};
1920
use syntax::errors::Applicability;
20-
use syntax::feature_gate::{GateIssue, emit_feature_err};
21+
use syntax::feature_gate::{feature_err, feature_err_issue};
2122
use syntax::attr::{self, Stability, Deprecation, RustcDeprecation};
2223
use crate::ty::{self, TyCtxt};
2324
use crate::util::nodemap::{FxHashSet, FxHashMap};
@@ -512,9 +513,8 @@ pub fn report_unstable(
512513
if is_soft {
513514
soft_handler(lint::builtin::SOFT_UNSTABLE, span, &msg)
514515
} else {
515-
emit_feature_err(
516-
&sess.parse_sess, feature, span, GateIssue::Library(issue), &msg
517-
);
516+
feature_err_issue(&sess.parse_sess, feature, span, GateIssue::Library(issue), &msg)
517+
.emit();
518518
}
519519
}
520520
}
@@ -842,15 +842,19 @@ impl Visitor<'tcx> for Checker<'tcx> {
842842
let ty = self.tcx.type_of(def_id);
843843

844844
if adt_def.has_dtor(self.tcx) {
845-
emit_feature_err(&self.tcx.sess.parse_sess,
846-
sym::untagged_unions, item.span, GateIssue::Language,
847-
"unions with `Drop` implementations are unstable");
845+
feature_err(
846+
&self.tcx.sess.parse_sess, sym::untagged_unions, item.span,
847+
"unions with `Drop` implementations are unstable"
848+
)
849+
.emit();
848850
} else {
849851
let param_env = self.tcx.param_env(def_id);
850852
if !param_env.can_type_implement_copy(self.tcx, ty).is_ok() {
851-
emit_feature_err(&self.tcx.sess.parse_sess,
852-
sym::untagged_unions, item.span, GateIssue::Language,
853-
"unions with non-`Copy` fields are unstable");
853+
feature_err(
854+
&self.tcx.sess.parse_sess, sym::untagged_unions, item.span,
855+
"unions with non-`Copy` fields are unstable"
856+
)
857+
.emit();
854858
}
855859
}
856860
}

src/librustc/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1130,7 +1130,7 @@ rustc_queries! {
11301130
desc { |tcx| "estimating size for `{}`", tcx.def_path_str(def.def_id()) }
11311131
}
11321132

1133-
query features_query(_: CrateNum) -> &'tcx feature_gate::Features {
1133+
query features_query(_: CrateNum) -> &'tcx rustc_feature::Features {
11341134
eval_always
11351135
desc { "looking up enabled feature gates" }
11361136
}

src/librustc/session/config.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::session::{early_error, early_warn, Session};
77
use crate::session::search_paths::SearchPath;
88

99
use rustc_data_structures::fx::FxHashSet;
10+
use rustc_feature::UnstableFeatures;
1011

1112
use rustc_target::spec::{LinkerFlavor, MergeFunctions, PanicStrategy, RelroLevel};
1213
use rustc_target::spec::{Target, TargetTriple};
@@ -16,7 +17,6 @@ use syntax::ast;
1617
use syntax::source_map::{FileName, FilePathMapping};
1718
use syntax::edition::{Edition, EDITION_NAME_LIST, DEFAULT_EDITION};
1819
use syntax::symbol::{sym, Symbol};
19-
use syntax::feature_gate::UnstableFeatures;
2020

2121
use errors::emitter::HumanReadableErrorType;
2222
use errors::{ColorConfig, FatalError, Handler};
@@ -2701,7 +2701,7 @@ pub fn parse_crate_types_from_list(list_list: Vec<String>) -> Result<Vec<CrateTy
27012701

27022702
pub mod nightly_options {
27032703
use getopts;
2704-
use syntax::feature_gate::UnstableFeatures;
2704+
use rustc_feature::UnstableFeatures;
27052705
use super::{ErrorOutputType, OptionStability, RustcOptGroup};
27062706
use crate::session::early_error;
27072707

@@ -2850,9 +2850,9 @@ mod dep_tracking {
28502850
use super::{CrateType, DebugInfo, ErrorOutputType, OptLevel, OutputTypes,
28512851
Passes, Sanitizer, LtoCli, LinkerPluginLto, SwitchWithOptPath,
28522852
SymbolManglingVersion};
2853+
use rustc_feature::UnstableFeatures;
28532854
use rustc_target::spec::{MergeFunctions, PanicStrategy, RelroLevel, TargetTriple};
28542855
use syntax::edition::Edition;
2855-
use syntax::feature_gate::UnstableFeatures;
28562856

28572857
pub trait DepTrackingHash {
28582858
fn hash(&self, hasher: &mut DefaultHasher, error_format: ErrorOutputType);

src/librustc/session/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use errors::emitter::{Emitter, EmitterWriter};
2121
use errors::emitter::HumanReadableErrorType;
2222
use errors::annotate_snippet_emitter_writer::{AnnotateSnippetEmitterWriter};
2323
use syntax::edition::Edition;
24-
use syntax::feature_gate;
2524
use errors::json::JsonEmitter;
2625
use syntax::source_map;
2726
use syntax::sess::ParseSess;
@@ -86,7 +85,7 @@ pub struct Session {
8685
/// `rustc_codegen_llvm::back::symbol_names` module for more information.
8786
pub crate_disambiguator: Once<CrateDisambiguator>,
8887

89-
features: Once<feature_gate::Features>,
88+
features: Once<rustc_feature::Features>,
9089

9190
/// The maximum recursion limit for potentially infinitely recursive
9291
/// operations such as auto-dereference and monomorphization.
@@ -470,11 +469,11 @@ impl Session {
470469
/// DO NOT USE THIS METHOD if there is a TyCtxt available, as it circumvents
471470
/// dependency tracking. Use tcx.features() instead.
472471
#[inline]
473-
pub fn features_untracked(&self) -> &feature_gate::Features {
472+
pub fn features_untracked(&self) -> &rustc_feature::Features {
474473
self.features.get()
475474
}
476475

477-
pub fn init_features(&self, features: feature_gate::Features) {
476+
pub fn init_features(&self, features: rustc_feature::Features) {
478477
self.features.set(features);
479478
}
480479

src/librustc/ty/context.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ use rustc_macros::HashStable;
7272
use syntax::ast;
7373
use syntax::attr;
7474
use syntax::source_map::MultiSpan;
75-
use syntax::feature_gate;
7675
use syntax::symbol::{Symbol, kw, sym};
7776
use syntax_pos::Span;
7877
use syntax::expand::allocator::AllocatorKind;
@@ -1312,7 +1311,7 @@ impl<'tcx> TyCtxt<'tcx> {
13121311
self.cstore.allocator_kind()
13131312
}
13141313

1315-
pub fn features(self) -> &'tcx feature_gate::Features {
1314+
pub fn features(self) -> &'tcx rustc_feature::Features {
13161315
self.features_query(LOCAL_CRATE)
13171316
}
13181317

src/librustc/ty/query/mod.rs

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ use std::any::type_name;
5656
use syntax_pos::{Span, DUMMY_SP};
5757
use syntax::attr;
5858
use syntax::ast;
59-
use syntax::feature_gate;
6059
use syntax::symbol::Symbol;
6160

6261
#[macro_use]

src/librustc_codegen_llvm/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ extern crate libc;
3030
#[macro_use] extern crate rustc;
3131
extern crate rustc_target;
3232
#[macro_use] extern crate rustc_data_structures;
33+
extern crate rustc_feature;
3334
extern crate rustc_index;
3435
extern crate rustc_incremental;
3536
extern crate rustc_codegen_utils;

src/librustc_codegen_llvm/llvm_util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc::session::config::PrintRequest;
66
use rustc_target::spec::{MergeFunctions, PanicStrategy};
77
use libc::c_int;
88
use std::ffi::CString;
9-
use syntax::feature_gate::UnstableFeatures;
9+
use rustc_feature::UnstableFeatures;
1010
use syntax::symbol::sym;
1111

1212
use std::str;

src/librustc_driver/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ rustc_target = { path = "../librustc_target" }
1919
rustc_lint = { path = "../librustc_lint" }
2020
rustc_data_structures = { path = "../librustc_data_structures" }
2121
errors = { path = "../librustc_errors", package = "rustc_errors" }
22+
rustc_feature = { path = "../librustc_feature" }
2223
rustc_metadata = { path = "../librustc_metadata" }
2324
rustc_mir = { path = "../librustc_mir" }
2425
rustc_parse = { path = "../librustc_parse" }

src/librustc_driver/lib.rs

+9-15
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ use errors::{PResult, registry::Registry};
4444
use rustc_interface::interface;
4545
use rustc_interface::util::get_codegen_sysroot;
4646
use rustc_data_structures::sync::SeqCst;
47-
47+
use rustc_feature::{find_gated_cfg, UnstableFeatures};
4848
use rustc_serialize::json::ToJson;
4949

5050
use std::borrow::Cow;
@@ -61,10 +61,9 @@ use std::str;
6161
use std::time::Instant;
6262

6363
use syntax::ast;
64-
use syntax::source_map::FileLoader;
65-
use syntax::feature_gate::{GatedCfg, UnstableFeatures};
66-
use syntax::symbol::sym;
67-
use syntax_pos::{DUMMY_SP, FileName};
64+
use syntax_pos::source_map::FileLoader;
65+
use syntax_pos::symbol::sym;
66+
use syntax_pos::FileName;
6867

6968
pub mod pretty;
7069
mod args;
@@ -684,12 +683,6 @@ impl RustcDefaultCalls {
684683
.is_nightly_build();
685684

686685
let mut cfgs = sess.parse_sess.config.iter().filter_map(|&(name, ref value)| {
687-
let gated_cfg = GatedCfg::gate(&ast::MetaItem {
688-
path: ast::Path::from_ident(ast::Ident::with_dummy_span(name)),
689-
kind: ast::MetaItemKind::Word,
690-
span: DUMMY_SP,
691-
});
692-
693686
// Note that crt-static is a specially recognized cfg
694687
// directive that's printed out here as part of
695688
// rust-lang/rust#37406, but in general the
@@ -700,10 +693,11 @@ impl RustcDefaultCalls {
700693
// through to build scripts.
701694
let value = value.as_ref().map(|s| s.as_str());
702695
let value = value.as_ref().map(|s| s.as_ref());
703-
if name != sym::target_feature || value != Some("crt-static") {
704-
if !allow_unstable_cfg && gated_cfg.is_some() {
705-
return None
706-
}
696+
if (name != sym::target_feature || value != Some("crt-static"))
697+
&& !allow_unstable_cfg
698+
&& find_gated_cfg(|cfg_sym| cfg_sym == name).is_some()
699+
{
700+
return None;
707701
}
708702

709703
if let Some(value) = value {

src/librustc_feature/Cargo.toml

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
authors = ["The Rust Project Developers"]
3+
name = "rustc_feature"
4+
version = "0.0.0"
5+
edition = "2018"
6+
7+
[lib]
8+
name = "rustc_feature"
9+
path = "lib.rs"
10+
doctest = false
11+
12+
[dependencies]
13+
rustc_data_structures = { path = "../librustc_data_structures" }
14+
lazy_static = "1.0.0"
15+
syntax_pos = { path = "../libsyntax_pos" }

src/libsyntax/feature_gate/accepted.rs src/librustc_feature/accepted.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! List of the accepted feature gates.
22
3-
use crate::symbol::sym;
43
use super::{State, Feature};
4+
use syntax_pos::symbol::sym;
55

66
macro_rules! declare_features {
77
($(

0 commit comments

Comments
 (0)