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.
22 use abi::RustIntrinsic;
26 use attr::AttrMetaMethods;
27 use codemap::{CodeMap, Span};
28 use diagnostic::SpanHandler;
34 use std::ascii::AsciiExt;
37 // if you change this list without updating src/doc/reference.md, @cmr will be sad
38 static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[
40 ("macro_rules", Accepted),
41 ("struct_variant", Accepted),
43 ("managed_boxes", Removed),
44 ("non_ascii_idents", Active),
45 ("thread_local", Active),
46 ("link_args", Active),
48 ("plugin_registrar", Active),
49 ("log_syntax", Active),
50 ("trace_macros", Active),
51 ("concat_idents", Active),
52 ("unsafe_destructor", Active),
53 ("intrinsics", Active),
54 ("lang_items", Active),
57 ("default_type_params", Accepted),
59 ("link_llvm_intrinsics", Active),
61 ("struct_inherit", Removed),
63 ("quad_precision_float", Removed),
65 ("rustc_diagnostic_macros", Active),
66 ("unboxed_closures", Active),
67 ("import_shadowing", Active),
68 ("advanced_slice_patterns", Active),
69 ("tuple_indexing", Accepted),
70 ("associated_types", Accepted),
71 ("visible_private_types", Active),
72 ("slicing_syntax", Active),
75 ("while_let", Accepted),
79 // A temporary feature gate used to enable parser extensions needed
80 // to bootstrap fix for #5723.
81 ("issue_5723_bootstrap", Accepted),
83 // A way to temporarily opt out of opt in copy. This will *never* be accepted.
84 ("opt_out_copy", Deprecated),
86 // A way to temporarily opt out of the new orphan rules. This will *never* be accepted.
87 ("old_orphan_check", Deprecated),
89 // A way to temporarily opt out of the new impl rules. This will *never* be accepted.
90 ("old_impl_check", Deprecated),
92 // OIBIT specific features
93 ("optin_builtin_traits", Active),
95 // These are used to test this portion of the compiler, they don't actually
97 ("test_accepted_feature", Accepted),
98 ("test_removed_feature", Removed),
102 /// Represents an active feature that is currently being implemented or
103 /// currently being considered for addition/removal.
106 /// Represents a feature gate that is temporarily enabling deprecated behavior.
107 /// This gate will never be accepted.
110 /// Represents a feature which has since been removed (it was once Active)
113 /// This language feature has since been Accepted (it was once Active)
117 /// A set of features to be used by later passes.
119 pub struct Features {
120 pub unboxed_closures: bool,
121 pub rustc_diagnostic_macros: bool,
122 pub import_shadowing: bool,
123 pub visible_private_types: bool,
125 pub opt_out_copy: bool,
126 pub old_orphan_check: bool,
130 pub fn new() -> Features {
132 unboxed_closures: false,
133 rustc_diagnostic_macros: false,
134 import_shadowing: false,
135 visible_private_types: false,
138 old_orphan_check: false,
144 features: Vec<&'static str>,
145 span_handler: &'a SpanHandler,
149 impl<'a> Context<'a> {
150 fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
151 if !self.has_feature(feature) {
152 self.span_handler.span_err(span, explain);
153 self.span_handler.span_help(span, format!("add #![feature({})] to the \
154 crate attributes to enable",
155 feature).index(&FullRange));
159 fn has_feature(&self, feature: &str) -> bool {
160 self.features.iter().any(|&n| n == feature)
164 struct MacroVisitor<'a> {
165 context: &'a Context<'a>
168 impl<'a, 'v> Visitor<'v> for MacroVisitor<'a> {
169 fn visit_mac(&mut self, mac: &ast::Mac) {
170 let ast::MacInvocTT(ref path, _, _) = mac.node;
171 let id = path.segments.last().unwrap().identifier;
173 if id == token::str_to_ident("asm") {
174 self.context.gate_feature("asm", path.span, "inline assembly is not \
175 stable enough for use and is subject to change");
178 else if id == token::str_to_ident("log_syntax") {
179 self.context.gate_feature("log_syntax", path.span, "`log_syntax!` is not \
180 stable enough for use and is subject to change");
183 else if id == token::str_to_ident("trace_macros") {
184 self.context.gate_feature("trace_macros", path.span, "`trace_macros` is not \
185 stable enough for use and is subject to change");
188 else if id == token::str_to_ident("concat_idents") {
189 self.context.gate_feature("concat_idents", path.span, "`concat_idents` is not \
190 stable enough for use and is subject to change");
195 struct PostExpansionVisitor<'a> {
196 context: &'a Context<'a>
199 impl<'a> PostExpansionVisitor<'a> {
200 fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
201 if !self.context.cm.span_is_internal(span) {
202 self.context.gate_feature(feature, span, explain)
207 impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
208 fn visit_name(&mut self, sp: Span, name: ast::Name) {
209 if !token::get_name(name).get().is_ascii() {
210 self.gate_feature("non_ascii_idents", sp,
211 "non-ascii idents are not fully supported.");
215 fn visit_view_item(&mut self, i: &ast::ViewItem) {
217 ast::ViewItemUse(..) => {}
218 ast::ViewItemExternCrate(..) => {
219 for attr in i.attrs.iter() {
220 if attr.check_name("plugin") {
221 self.gate_feature("plugin", attr.span,
222 "compiler plugins are experimental \
223 and possibly buggy");
228 visit::walk_view_item(self, i)
231 fn visit_item(&mut self, i: &ast::Item) {
232 for attr in i.attrs.iter() {
233 if attr.name() == "thread_local" {
234 self.gate_feature("thread_local", i.span,
235 "`#[thread_local]` is an experimental feature, and does not \
236 currently handle destructors. There is no corresponding \
237 `#[task_local]` mapping to the task model");
238 } else if attr.name() == "linkage" {
239 self.gate_feature("linkage", i.span,
240 "the `linkage` attribute is experimental \
241 and not portable across platforms")
245 ast::ItemForeignMod(ref foreign_module) => {
246 if attr::contains_name(i.attrs.index(&FullRange), "link_args") {
247 self.gate_feature("link_args", i.span,
248 "the `link_args` attribute is not portable \
249 across platforms, it is recommended to \
250 use `#[link(name = \"foo\")]` instead")
252 if foreign_module.abi == RustIntrinsic {
253 self.gate_feature("intrinsics",
255 "intrinsics are subject to change")
260 if attr::contains_name(i.attrs.index(&FullRange), "plugin_registrar") {
261 self.gate_feature("plugin_registrar", i.span,
262 "compiler plugins are experimental and possibly buggy");
266 ast::ItemStruct(..) => {
267 if attr::contains_name(i.attrs.index(&FullRange), "simd") {
268 self.gate_feature("simd", i.span,
269 "SIMD types are experimental and possibly buggy");
273 ast::ItemImpl(_, polarity, _, _, _, _) => {
275 ast::ImplPolarity::Negative => {
276 self.gate_feature("optin_builtin_traits",
278 "negative trait bounds are not yet fully implemented; \
279 use marker types for now");
284 if attr::contains_name(i.attrs.as_slice(),
285 "unsafe_destructor") {
286 self.gate_feature("unsafe_destructor",
288 "`#[unsafe_destructor]` allows too \
289 many unsafe patterns and may be \
290 removed in the future");
293 if attr::contains_name(i.attrs.index(&FullRange),
294 "old_orphan_check") {
298 "the new orphan check rules will eventually be strictly enforced");
301 if attr::contains_name(i.attrs.index(&FullRange),
303 self.gate_feature("old_impl_check",
305 "`#[old_impl_check]` will be removed in the future");
312 visit::walk_item(self, i);
315 fn visit_foreign_item(&mut self, i: &ast::ForeignItem) {
316 if attr::contains_name(i.attrs.index(&FullRange), "linkage") {
317 self.gate_feature("linkage", i.span,
318 "the `linkage` attribute is experimental \
319 and not portable across platforms")
322 let links_to_llvm = match attr::first_attr_value_str_by_name(i.attrs.as_slice(),
324 Some(val) => val.get().starts_with("llvm."),
328 self.gate_feature("link_llvm_intrinsics", i.span,
329 "linking to LLVM intrinsics is experimental");
332 visit::walk_foreign_item(self, i)
335 fn visit_ty(&mut self, t: &ast::Ty) {
336 visit::walk_ty(self, t);
339 fn visit_expr(&mut self, e: &ast::Expr) {
341 ast::ExprRange(..) => {
342 self.gate_feature("slicing_syntax",
344 "range syntax is experimental");
348 visit::walk_expr(self, e);
351 fn visit_attribute(&mut self, attr: &ast::Attribute) {
352 if attr::contains_name(slice::ref_slice(attr), "lang") {
353 self.gate_feature("lang_items",
355 "language items are subject to change");
359 fn visit_pat(&mut self, pattern: &ast::Pat) {
361 ast::PatVec(_, Some(_), ref last) if !last.is_empty() => {
362 self.gate_feature("advanced_slice_patterns",
364 "multiple-element slice matches anywhere \
365 but at the end of a slice (e.g. \
366 `[0, ..xs, 0]` are experimental")
370 visit::walk_pat(self, pattern)
373 fn visit_fn(&mut self,
374 fn_kind: visit::FnKind<'v>,
375 fn_decl: &'v ast::FnDecl,
376 block: &'v ast::Block,
380 visit::FkItemFn(_, _, _, abi) if abi == RustIntrinsic => {
381 self.gate_feature("intrinsics",
383 "intrinsics are subject to change")
387 visit::walk_fn(self, fn_kind, fn_decl, block, span);
391 fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::Crate,
393 -> (Features, Vec<Span>)
394 where F: FnOnce(&mut Context, &ast::Crate)
396 let mut cx = Context {
397 features: Vec::new(),
398 span_handler: span_handler,
402 let mut unknown_features = Vec::new();
404 for attr in krate.attrs.iter() {
405 if !attr.check_name("feature") {
409 match attr.meta_item_list() {
411 span_handler.span_err(attr.span, "malformed feature attribute, \
412 expected #![feature(...)]");
415 for mi in list.iter() {
416 let name = match mi.node {
417 ast::MetaWord(ref word) => (*word).clone(),
419 span_handler.span_err(mi.span,
420 "malformed feature, expected just \
425 match KNOWN_FEATURES.iter()
426 .find(|& &(n, _)| name == n) {
427 Some(&(name, Active)) => {
428 cx.features.push(name);
430 Some(&(name, Deprecated)) => {
431 cx.features.push(name);
432 span_handler.span_warn(
434 "feature is deprecated and will only be available \
435 for a limited time, please rewrite code that relies on it");
437 Some(&(_, Removed)) => {
438 span_handler.span_err(mi.span, "feature has been removed");
440 Some(&(_, Accepted)) => {
441 span_handler.span_warn(mi.span, "feature has been added to Rust, \
442 directive not necessary");
445 unknown_features.push(mi.span);
453 check(&mut cx, krate);
456 unboxed_closures: cx.has_feature("unboxed_closures"),
457 rustc_diagnostic_macros: cx.has_feature("rustc_diagnostic_macros"),
458 import_shadowing: cx.has_feature("import_shadowing"),
459 visible_private_types: cx.has_feature("visible_private_types"),
460 quote: cx.has_feature("quote"),
461 opt_out_copy: cx.has_feature("opt_out_copy"),
462 old_orphan_check: cx.has_feature("old_orphan_check"),
467 pub fn check_crate_macros(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::Crate)
468 -> (Features, Vec<Span>) {
469 check_crate_inner(cm, span_handler, krate,
470 |ctx, krate| visit::walk_crate(&mut MacroVisitor { context: ctx }, krate))
473 pub fn check_crate(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::Crate)
474 -> (Features, Vec<Span>) {
475 check_crate_inner(cm, span_handler, krate,
476 |ctx, krate| visit::walk_crate(&mut PostExpansionVisitor { context: ctx },