1 // Copyright 2013 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.
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.
13 //! This modules implements the gating necessary for preventing certain compiler
14 //! features from being used by default. This module will crawl a pre-expanded
15 //! AST to ensure that there are no features which are used that are not
18 //! Features are enabled in programs via the crate-level attributes of
19 //! #![feature(...)] with a comma-separated list of features.
25 use syntax::attr::AttrMetaMethods;
26 use syntax::codemap::Span;
28 use syntax::visit::Visitor;
29 use syntax::parse::token;
31 use driver::session::Session;
35 /// This is a list of all known features since the beginning of time. This list
36 /// can never shrink, it may only be expanded (in order to prevent old programs
37 /// from failing to compile). The status of each feature may change, however.
38 static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[
40 ("macro_rules", Active),
41 ("struct_variant", Active),
44 ("managed_boxes", Active),
45 ("non_ascii_idents", Active),
46 ("thread_local", Active),
47 ("link_args", Active),
49 ("plugin_registrar", Active),
50 ("log_syntax", Active),
51 ("trace_macros", Active),
52 ("concat_idents", Active),
55 ("default_type_params", Active),
58 ("struct_inherit", Active),
59 ("overloaded_calls", Active),
60 ("unboxed_closure_sugar", Active),
62 ("quad_precision_float", Active),
64 // A temporary feature gate used to enable parser extensions needed
65 // to bootstrap fix for #5723.
66 ("issue_5723_bootstrap", Active),
68 // These are used to test this portion of the compiler, they don't actually
70 ("test_accepted_feature", Accepted),
71 ("test_removed_feature", Removed),
75 /// Represents an active feature that is currently being implemented or
76 /// currently being considered for addition/removal.
79 /// Represents a feature which has since been removed (it was once Active)
82 /// This language feature has since been Accepted (it was once Active)
86 /// A set of features to be used by later passes.
88 pub default_type_params: Cell<bool>,
89 pub quad_precision_float: Cell<bool>,
90 pub issue_5723_bootstrap: Cell<bool>,
91 pub overloaded_calls: Cell<bool>,
95 pub fn new() -> Features {
97 default_type_params: Cell::new(false),
98 quad_precision_float: Cell::new(false),
99 issue_5723_bootstrap: Cell::new(false),
100 overloaded_calls: Cell::new(false),
106 features: Vec<&'static str>,
110 impl<'a> Context<'a> {
111 fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
112 if !self.has_feature(feature) {
113 self.sess.span_err(span, explain);
114 self.sess.span_note(span, format!("add #![feature({})] to the \
115 crate attributes to enable",
116 feature).as_slice());
120 fn gate_box(&self, span: Span) {
121 self.gate_feature("managed_boxes", span,
122 "The managed box syntax is being replaced by the \
123 `std::gc::Gc` and `std::rc::Rc` types. Equivalent \
124 functionality to managed trait objects will be \
125 implemented but is currently missing.");
128 fn has_feature(&self, feature: &str) -> bool {
129 self.features.iter().any(|n| n.as_slice() == feature)
133 impl<'a> Visitor<()> for Context<'a> {
134 fn visit_ident(&mut self, sp: Span, id: ast::Ident, _: ()) {
135 if !token::get_ident(id).get().is_ascii() {
136 self.gate_feature("non_ascii_idents", sp,
137 "non-ascii idents are not fully supported.");
141 fn visit_view_item(&mut self, i: &ast::ViewItem, _: ()) {
143 ast::ViewItemUse(ref path) => {
145 ast::ViewPathGlob(..) => {
146 self.gate_feature("globs", path.span,
147 "glob import statements are \
148 experimental and possibly buggy");
153 ast::ViewItemExternCrate(..) => {
154 for attr in i.attrs.iter() {
155 if attr.name().get() == "phase"{
156 self.gate_feature("phase", attr.span,
157 "compile time crate loading is \
158 experimental and possibly buggy");
163 visit::walk_view_item(self, i, ())
166 fn visit_item(&mut self, i: &ast::Item, _:()) {
167 for attr in i.attrs.iter() {
168 if attr.name().equiv(&("thread_local")) {
169 self.gate_feature("thread_local", i.span,
170 "`#[thread_local]` is an experimental feature, and does not \
171 currently handle destructors. There is no corresponding \
172 `#[task_local]` mapping to the task model");
176 ast::ItemEnum(ref def, _) => {
177 for variant in def.variants.iter() {
178 match variant.node.kind {
179 ast::StructVariantKind(..) => {
180 self.gate_feature("struct_variant", variant.span,
181 "enum struct variants are \
182 experimental and possibly buggy");
189 ast::ItemForeignMod(..) => {
190 if attr::contains_name(i.attrs.as_slice(), "link_args") {
191 self.gate_feature("link_args", i.span,
192 "the `link_args` attribute is not portable \
193 across platforms, it is recommended to \
194 use `#[link(name = \"foo\")]` instead")
199 if attr::contains_name(i.attrs.as_slice(), "plugin_registrar") {
200 self.gate_feature("plugin_registrar", i.span,
201 "compiler plugins are experimental and possibly buggy");
205 ast::ItemStruct(struct_definition, _) => {
206 if attr::contains_name(i.attrs.as_slice(), "simd") {
207 self.gate_feature("simd", i.span,
208 "SIMD types are experimental and possibly buggy");
210 match struct_definition.super_struct {
211 Some(ref path) => self.gate_feature("struct_inherit", path.span,
212 "struct inheritance is experimental \
213 and possibly buggy"),
216 if struct_definition.is_virtual {
217 self.gate_feature("struct_inherit", i.span,
218 "struct inheritance (`virtual` keyword) is \
219 experimental and possibly buggy");
226 visit::walk_item(self, i, ());
229 fn visit_mac(&mut self, macro: &ast::Mac, _: ()) {
230 let ast::MacInvocTT(ref path, _, _) = macro.node;
231 let id = path.segments.last().unwrap().identifier;
232 let quotes = ["quote_tokens", "quote_expr", "quote_ty",
233 "quote_item", "quote_pat", "quote_stmt"];
234 let msg = " is not stable enough for use and are subject to change";
237 if id == token::str_to_ident("macro_rules") {
238 self.gate_feature("macro_rules", path.span, "macro definitions are \
239 not stable enough for use and are subject to change");
242 else if id == token::str_to_ident("asm") {
243 self.gate_feature("asm", path.span, "inline assembly is not \
244 stable enough for use and is subject to change");
247 else if id == token::str_to_ident("log_syntax") {
248 self.gate_feature("log_syntax", path.span, "`log_syntax!` is not \
249 stable enough for use and is subject to change");
252 else if id == token::str_to_ident("trace_macros") {
253 self.gate_feature("trace_macros", path.span, "`trace_macros` is not \
254 stable enough for use and is subject to change");
257 else if id == token::str_to_ident("concat_idents") {
258 self.gate_feature("concat_idents", path.span, "`concat_idents` is not \
259 stable enough for use and is subject to change");
263 for "e in quotes.iter() {
264 if id == token::str_to_ident(quote) {
265 self.gate_feature("quote",
267 format!("{}{}", quote, msg).as_slice());
273 fn visit_foreign_item(&mut self, i: &ast::ForeignItem, _: ()) {
275 ast::ForeignItemFn(..) | ast::ForeignItemStatic(..) => {
276 if attr::contains_name(i.attrs.as_slice(), "linkage") {
277 self.gate_feature("linkage", i.span,
278 "the `linkage` attribute is experimental \
279 and not portable across platforms")
283 visit::walk_foreign_item(self, i, ())
286 fn visit_ty(&mut self, t: &ast::Ty, _: ()) {
288 ast::TyClosure(closure, _) if closure.onceness == ast::Once => {
289 self.gate_feature("once_fns", t.span,
290 "once functions are \
291 experimental and likely to be removed");
294 ast::TyBox(_) => { self.gate_box(t.span); }
295 ast::TyUnboxedFn(_) => {
296 self.gate_feature("unboxed_closure_sugar",
298 "unboxed closure trait sugar is experimental");
303 visit::walk_ty(self, t, ());
306 fn visit_expr(&mut self, e: &ast::Expr, _: ()) {
308 ast::ExprUnary(ast::UnBox, _) => {
309 self.gate_box(e.span);
313 visit::walk_expr(self, e, ());
316 fn visit_generics(&mut self, generics: &ast::Generics, _: ()) {
317 for type_parameter in generics.ty_params.iter() {
318 match type_parameter.default {
320 self.gate_feature("default_type_params", ty.span,
321 "default type parameters are \
322 experimental and possibly buggy");
327 visit::walk_generics(self, generics, ());
331 pub fn check_crate(sess: &Session, krate: &ast::Crate) {
332 let mut cx = Context {
333 features: Vec::new(),
337 for attr in krate.attrs.iter() {
338 if !attr.check_name("feature") {
342 match attr.meta_item_list() {
344 sess.span_err(attr.span, "malformed feature attribute, \
345 expected #![feature(...)]");
348 for &mi in list.iter() {
349 let name = match mi.node {
350 ast::MetaWord(ref word) => (*word).clone(),
352 sess.span_err(mi.span,
353 "malformed feature, expected just \
358 match KNOWN_FEATURES.iter()
359 .find(|& &(n, _)| name.equiv(&n)) {
360 Some(&(name, Active)) => { cx.features.push(name); }
361 Some(&(_, Removed)) => {
362 sess.span_err(mi.span, "feature has been removed");
364 Some(&(_, Accepted)) => {
365 sess.span_warn(mi.span, "feature has added to rust, \
366 directive not necessary");
369 sess.add_lint(lint::UnknownFeatures,
372 "unknown feature".to_string());
380 visit::walk_crate(&mut cx, krate, ());
382 sess.abort_if_errors();
384 sess.features.default_type_params.set(cx.has_feature("default_type_params"));
385 sess.features.quad_precision_float.set(cx.has_feature("quad_precision_float"));
386 sess.features.issue_5723_bootstrap.set(cx.has_feature("issue_5723_bootstrap"));
387 sess.features.overloaded_calls.set(cx.has_feature("overloaded_calls"));