Skip to content

Commit 74db87b

Browse files
committed
move feature_gate to libsyntax
1 parent 375c95b commit 74db87b

File tree

8 files changed

+69
-65
lines changed

8 files changed

+69
-65
lines changed

src/librustc/driver/driver.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use back::link;
1313
use back::write;
1414
use driver::session::Session;
1515
use driver::config;
16-
use front;
1716
use lint;
1817
use llvm::{ContextRef, ModuleRef};
1918
use metadata::common::LinkMeta;
@@ -194,8 +193,20 @@ pub fn phase_2_configure_and_expand(sess: &Session,
194193
*sess.crate_metadata.borrow_mut() =
195194
collect_crate_metadata(sess, krate.attrs.as_slice());
196195

197-
time(time_passes, "gated feature checking", (), |_|
198-
front::feature_gate::check_crate(sess, &krate));
196+
time(time_passes, "gated feature checking", (), |_| {
197+
let (features, unknown_features) =
198+
syntax::feature_gate::check_crate(&sess.parse_sess.span_diagnostic, &krate);
199+
200+
for uf in unknown_features.iter() {
201+
sess.add_lint(lint::builtin::UNKNOWN_FEATURES,
202+
ast::CRATE_NODE_ID,
203+
*uf,
204+
"unknown feature".to_string());
205+
}
206+
207+
sess.abort_if_errors();
208+
*sess.features.borrow_mut() = features;
209+
});
199210

200211
let any_exe = sess.crate_types.borrow().iter().any(|ty| {
201212
*ty == config::CrateTypeExecutable
@@ -225,7 +236,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
225236
let mut registry = Registry::new(&krate);
226237

227238
time(time_passes, "plugin registration", (), |_| {
228-
if sess.features.rustc_diagnostic_macros.get() {
239+
if sess.features.borrow().rustc_diagnostic_macros {
229240
registry.register_macro("__diagnostic_used",
230241
diagnostics::plugin::expand_diagnostic_used);
231242
registry.register_macro("__register_diagnostic",
@@ -277,7 +288,7 @@ pub fn phase_2_configure_and_expand(sess: &Session,
277288
os::setenv("PATH", os::join_paths(new_path.as_slice()).unwrap());
278289
}
279290
let cfg = syntax::ext::expand::ExpansionConfig {
280-
deriving_hash_type_parameter: sess.features.default_type_params.get(),
291+
deriving_hash_type_parameter: sess.features.borrow().default_type_params,
281292
crate_name: crate_name.to_string(),
282293
};
283294
let ret = syntax::ext::expand::expand_crate(&sess.parse_sess,

src/librustc/driver/session.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
use driver::config;
1313
use driver::driver;
14-
use front;
1514
use metadata::cstore::CStore;
1615
use metadata::filesearch;
1716
use lint;
@@ -21,6 +20,7 @@ use syntax::ast::NodeId;
2120
use syntax::codemap::Span;
2221
use syntax::diagnostic;
2322
use syntax::diagnostics;
23+
use syntax::feature_gate;
2424
use syntax::parse;
2525
use syntax::parse::token;
2626
use syntax::parse::ParseSess;
@@ -49,7 +49,7 @@ pub struct Session {
4949
pub lints: RefCell<NodeMap<Vec<(lint::LintId, codemap::Span, String)>>>,
5050
pub crate_types: RefCell<Vec<config::CrateType>>,
5151
pub crate_metadata: RefCell<Vec<String>>,
52-
pub features: front::feature_gate::Features,
52+
pub features: RefCell<feature_gate::Features>,
5353

5454
/// The maximum recursion limit for potentially infinitely recursive
5555
/// operations such as auto-dereference and monomorphization.
@@ -245,7 +245,7 @@ pub fn build_session_(sopts: config::Options,
245245
lints: RefCell::new(NodeMap::new()),
246246
crate_types: RefCell::new(Vec::new()),
247247
crate_metadata: RefCell::new(Vec::new()),
248-
features: front::feature_gate::Features::new(),
248+
features: RefCell::new(feature_gate::Features::new()),
249249
recursion_limit: Cell::new(64),
250250
};
251251

src/librustc/lib.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,6 @@ pub mod middle {
116116
pub mod weak_lang_items;
117117
}
118118

119-
pub mod front {
120-
pub mod feature_gate;
121-
}
122-
123119
pub mod metadata;
124120

125121
pub mod driver;

src/librustc/middle/resolve.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2811,7 +2811,7 @@ impl<'a> Resolver<'a> {
28112811
import_span: Span,
28122812
name: Name,
28132813
namespace: Namespace) {
2814-
if self.session.features.import_shadowing.get() {
2814+
if self.session.features.borrow().import_shadowing {
28152815
return
28162816
}
28172817

@@ -2837,7 +2837,7 @@ impl<'a> Resolver<'a> {
28372837
&mut ImportResolution,
28382838
import_span: Span,
28392839
name: Name) {
2840-
if self.session.features.import_shadowing.get() {
2840+
if self.session.features.borrow().import_shadowing {
28412841
return
28422842
}
28432843

@@ -2919,7 +2919,7 @@ impl<'a> Resolver<'a> {
29192919
module: &Module,
29202920
name: Name,
29212921
span: Span) {
2922-
if self.session.features.import_shadowing.get() {
2922+
if self.session.features.borrow().import_shadowing {
29232923
return
29242924
}
29252925

@@ -2937,7 +2937,7 @@ impl<'a> Resolver<'a> {
29372937
module: &Module,
29382938
name: Name,
29392939
span: Span) {
2940-
if self.session.features.import_shadowing.get() {
2940+
if self.session.features.borrow().import_shadowing {
29412941
return
29422942
}
29432943

src/librustc/middle/typeck/astconv.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ fn ast_path_substs<'tcx, AC: AstConv<'tcx>, RS: RegionScope>(
235235
}
236236

237237
if supplied_ty_param_count > required_ty_param_count
238-
&& !this.tcx().sess.features.default_type_params.get() {
238+
&& !this.tcx().sess.features.borrow().default_type_params {
239239
span_err!(this.tcx().sess, path.span, E0108,
240240
"default type parameters are experimental and possibly buggy");
241241
span_note!(this.tcx().sess, path.span,

src/librustc/middle/typeck/check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2131,7 +2131,7 @@ fn try_overloaded_call(fcx: &FnCtxt,
21312131
fcx.inh.method_map.borrow_mut().insert(method_call, method_callee);
21322132
write_call(fcx, call_expression, output_type);
21332133

2134-
if !fcx.tcx().sess.features.overloaded_calls.get() {
2134+
if !fcx.tcx().sess.features.borrow().overloaded_calls {
21352135
span_err!(fcx.tcx().sess, call_expression.span, E0056,
21362136
"overloaded calls are experimental");
21372137
span_note!(fcx.tcx().sess, call_expression.span,

src/librustc/front/feature_gate.rs renamed to src/libsyntax/feature_gate.rs

Lines changed: 43 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,17 @@
1818
//! Features are enabled in programs via the crate-level attributes of
1919
//! `#![feature(...)]` with a comma-separated list of features.
2020
21-
use lint;
21+
use abi::RustIntrinsic;
22+
use ast::NodeId;
23+
use ast;
24+
use attr;
25+
use attr::AttrMetaMethods;
26+
use codemap::Span;
27+
use diagnostic::SpanHandler;
28+
use visit;
29+
use visit::Visitor;
30+
use parse::token;
2231

23-
use syntax::abi::RustIntrinsic;
24-
use syntax::ast::NodeId;
25-
use syntax::ast;
26-
use syntax::attr;
27-
use syntax::attr::AttrMetaMethods;
28-
use syntax::codemap::Span;
29-
use syntax::visit;
30-
use syntax::visit::Visitor;
31-
use syntax::parse::token;
32-
33-
use driver::session::Session;
34-
35-
use std::cell::Cell;
3632
use std::slice;
3733

3834
/// This is a list of all known features since the beginning of time. This list
@@ -99,35 +95,35 @@ enum Status {
9995

10096
/// A set of features to be used by later passes.
10197
pub struct Features {
102-
pub default_type_params: Cell<bool>,
103-
pub overloaded_calls: Cell<bool>,
104-
pub rustc_diagnostic_macros: Cell<bool>,
105-
pub import_shadowing: Cell<bool>,
98+
pub default_type_params: bool,
99+
pub overloaded_calls: bool,
100+
pub rustc_diagnostic_macros: bool,
101+
pub import_shadowing: bool,
106102
}
107103

108104
impl Features {
109105
pub fn new() -> Features {
110106
Features {
111-
default_type_params: Cell::new(false),
112-
overloaded_calls: Cell::new(false),
113-
rustc_diagnostic_macros: Cell::new(false),
114-
import_shadowing: Cell::new(false),
107+
default_type_params: false,
108+
overloaded_calls: false,
109+
rustc_diagnostic_macros: false,
110+
import_shadowing: false,
115111
}
116112
}
117113
}
118114

119115
struct Context<'a> {
120116
features: Vec<&'static str>,
121-
sess: &'a Session,
117+
span_handler: &'a SpanHandler,
122118
}
123119

124120
impl<'a> Context<'a> {
125121
fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
126122
if !self.has_feature(feature) {
127-
self.sess.span_err(span, explain);
128-
self.sess.span_note(span, format!("add #![feature({})] to the \
129-
crate attributes to enable",
130-
feature).as_slice());
123+
self.span_handler.span_err(span, explain);
124+
self.span_handler.span_note(span, format!("add #![feature({})] to the \
125+
crate attributes to enable",
126+
feature).as_slice());
131127
}
132128
}
133129

@@ -404,48 +400,47 @@ impl<'a, 'v> Visitor<'v> for Context<'a> {
404400
}
405401
}
406402

407-
pub fn check_crate(sess: &Session, krate: &ast::Crate) {
403+
pub fn check_crate(span_handler: &SpanHandler, krate: &ast::Crate) -> (Features, Vec<Span>) {
408404
let mut cx = Context {
409405
features: Vec::new(),
410-
sess: sess,
406+
span_handler: span_handler,
411407
};
412408

409+
let mut unknown_features = Vec::new();
410+
413411
for attr in krate.attrs.iter() {
414412
if !attr.check_name("feature") {
415413
continue
416414
}
417415

418416
match attr.meta_item_list() {
419417
None => {
420-
sess.span_err(attr.span, "malformed feature attribute, \
421-
expected #![feature(...)]");
418+
span_handler.span_err(attr.span, "malformed feature attribute, \
419+
expected #![feature(...)]");
422420
}
423421
Some(list) => {
424422
for mi in list.iter() {
425423
let name = match mi.node {
426424
ast::MetaWord(ref word) => (*word).clone(),
427425
_ => {
428-
sess.span_err(mi.span,
429-
"malformed feature, expected just \
430-
one word");
426+
span_handler.span_err(mi.span,
427+
"malformed feature, expected just \
428+
one word");
431429
continue
432430
}
433431
};
434432
match KNOWN_FEATURES.iter()
435433
.find(|& &(n, _)| name.equiv(&n)) {
436434
Some(&(name, Active)) => { cx.features.push(name); }
437435
Some(&(_, Removed)) => {
438-
sess.span_err(mi.span, "feature has been removed");
436+
span_handler.span_err(mi.span, "feature has been removed");
439437
}
440438
Some(&(_, Accepted)) => {
441-
sess.span_warn(mi.span, "feature has been added to Rust, \
442-
directive not necessary");
439+
span_handler.span_warn(mi.span, "feature has been added to Rust, \
440+
directive not necessary");
443441
}
444442
None => {
445-
sess.add_lint(lint::builtin::UNKNOWN_FEATURES,
446-
ast::CRATE_NODE_ID,
447-
mi.span,
448-
"unknown feature".to_string());
443+
unknown_features.push(mi.span);
449444
}
450445
}
451446
}
@@ -455,11 +450,12 @@ pub fn check_crate(sess: &Session, krate: &ast::Crate) {
455450

456451
visit::walk_crate(&mut cx, krate);
457452

458-
sess.abort_if_errors();
459-
460-
sess.features.default_type_params.set(cx.has_feature("default_type_params"));
461-
sess.features.overloaded_calls.set(cx.has_feature("overloaded_calls"));
462-
sess.features.rustc_diagnostic_macros.set(cx.has_feature("rustc_diagnostic_macros"));
463-
sess.features.import_shadowing.set(cx.has_feature("import_shadowing"));
453+
(Features {
454+
default_type_params: cx.has_feature("default_type_params"),
455+
overloaded_calls: cx.has_feature("overloaded_calls"),
456+
rustc_diagnostic_macros: cx.has_feature("rustc_diagnostic_macros"),
457+
import_shadowing: cx.has_feature("import_shadowing"),
458+
},
459+
unknown_features)
464460
}
465461

src/libsyntax/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub mod codemap;
6262
pub mod config;
6363
pub mod crateid;
6464
pub mod diagnostic;
65+
pub mod feature_gate;
6566
pub mod fold;
6667
pub mod owned_slice;
6768
pub mod parse;

0 commit comments

Comments
 (0)