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 ("macro_registrar", Active),
50 ("log_syntax", Active),
51 ("trace_macros", Active),
53 ("default_type_params", Active),
56 // These are used to test this portion of the compiler, they don't actually
58 ("test_accepted_feature", Accepted),
59 ("test_removed_feature", Removed),
63 /// Represents an active feature that is currently being implemented or
64 /// currently being considered for addition/removal.
67 /// Represents a feature which has since been removed (it was once Active)
70 /// This language feature has since been Accepted (it was once Active)
74 /// A set of features to be used by later passes.
76 default_type_params: Cell<bool>
80 pub fn new() -> Features {
82 default_type_params: Cell::new(false)
88 features: ~[&'static str],
93 fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
94 if !self.has_feature(feature) {
95 self.sess.span_err(span, explain);
96 self.sess.span_note(span, format!("add \\#[feature({})] to the \
97 crate attributes to enable",
102 fn gate_box(&self, span: Span) {
103 self.gate_feature("managed_boxes", span,
104 "The managed box syntax is being replaced by the \
105 `std::gc::Gc` and `std::rc::Rc` types. Equivalent \
106 functionality to managed trait objects will be \
107 implemented but is currently missing.");
110 fn has_feature(&self, feature: &str) -> bool {
111 self.features.iter().any(|n| n.as_slice() == feature)
115 impl Visitor<()> for Context {
116 fn visit_ident(&mut self, sp: Span, id: ast::Ident, _: ()) {
117 if !token::get_ident(id).get().is_ascii() {
118 self.gate_feature("non_ascii_idents", sp,
119 "non-ascii idents are not fully supported.");
123 fn visit_view_item(&mut self, i: &ast::ViewItem, _: ()) {
125 ast::ViewItemUse(ref paths) => {
126 for path in paths.iter() {
128 ast::ViewPathGlob(..) => {
129 self.gate_feature("globs", path.span,
130 "glob import statements are \
131 experimental and possibly buggy");
137 ast::ViewItemExternMod(..) => {
138 for attr in i.attrs.iter() {
139 if attr.name().get() == "phase"{
140 self.gate_feature("phase", attr.span,
141 "compile time crate loading is \
142 experimental and possibly buggy");
147 visit::walk_view_item(self, i, ())
150 fn visit_item(&mut self, i: &ast::Item, _:()) {
151 for attr in i.attrs.iter() {
152 if attr.name().equiv(&("thread_local")) {
153 self.gate_feature("thread_local", i.span,
154 "`#[thread_local]` is an experimental feature, and does not \
155 currently handle destructors. There is no corresponding \
156 `#[task_local]` mapping to the task model");
160 ast::ItemEnum(ref def, _) => {
161 for variant in def.variants.iter() {
162 match variant.node.kind {
163 ast::StructVariantKind(..) => {
164 self.gate_feature("struct_variant", variant.span,
165 "enum struct variants are \
166 experimental and possibly buggy");
173 ast::ItemForeignMod(..) => {
174 if attr::contains_name(i.attrs, "link_args") {
175 self.gate_feature("link_args", i.span,
176 "the `link_args` attribute is not portable \
177 across platforms, it is recommended to \
178 use `#[link(name = \"foo\")]` instead")
183 if attr::contains_name(i.attrs, "macro_registrar") {
184 self.gate_feature("macro_registrar", i.span,
185 "cross-crate macro exports are \
186 experimental and possibly buggy");
190 ast::ItemStruct(..) => {
191 if attr::contains_name(i.attrs, "simd") {
192 self.gate_feature("simd", i.span,
193 "SIMD types are experimental and possibly buggy");
200 visit::walk_item(self, i, ());
203 fn visit_mac(&mut self, macro: &ast::Mac, _: ()) {
204 let ast::MacInvocTT(ref path, _, _) = macro.node;
205 let id = path.segments.last().unwrap().identifier;
206 let quotes = ["quote_tokens", "quote_expr", "quote_ty",
207 "quote_item", "quote_pat", "quote_stmt"];
208 let msg = " is not stable enough for use and are subject to change";
211 if id == token::str_to_ident("macro_rules") {
212 self.gate_feature("macro_rules", path.span, "macro definitions are \
213 not stable enough for use and are subject to change");
216 else if id == token::str_to_ident("asm") {
217 self.gate_feature("asm", path.span, "inline assembly is not \
218 stable enough for use and is subject to change");
221 else if id == token::str_to_ident("log_syntax") {
222 self.gate_feature("log_syntax", path.span, "`log_syntax!` is not \
223 stable enough for use and is subject to change");
226 else if id == token::str_to_ident("trace_macros") {
227 self.gate_feature("trace_macros", path.span, "`trace_macros` is not \
228 stable enough for use and is subject to change");
232 for "e in quotes.iter() {
233 if id == token::str_to_ident(quote) {
234 self.gate_feature("quote", path.span, quote + msg);
240 fn visit_ty(&mut self, t: &ast::Ty, _: ()) {
242 ast::TyClosure(closure) if closure.onceness == ast::Once &&
243 closure.sigil != ast::OwnedSigil => {
244 self.gate_feature("once_fns", t.span,
245 "once functions are \
246 experimental and likely to be removed");
249 ast::TyBox(_) => { self.gate_box(t.span); }
253 visit::walk_ty(self, t, ());
256 fn visit_expr(&mut self, e: &ast::Expr, _: ()) {
258 ast::ExprUnary(ast::UnBox, _) => {
259 self.gate_box(e.span);
263 visit::walk_expr(self, e, ());
266 fn visit_generics(&mut self, generics: &ast::Generics, _: ()) {
267 for type_parameter in generics.ty_params.iter() {
268 match type_parameter.default {
270 self.gate_feature("default_type_params", ty.span,
271 "default type parameters are \
272 experimental and possibly buggy");
277 visit::walk_generics(self, generics, ());
281 pub fn check_crate(sess: Session, krate: &ast::Crate) {
282 let mut cx = Context {
287 for attr in krate.attrs.iter() {
288 if !attr.name().equiv(&("feature")) {
292 match attr.meta_item_list() {
294 sess.span_err(attr.span, "malformed feature attribute, \
295 expected #[feature(...)]");
298 for &mi in list.iter() {
299 let name = match mi.node {
300 ast::MetaWord(ref word) => (*word).clone(),
302 sess.span_err(mi.span,
303 "malformed feature, expected just \
308 match KNOWN_FEATURES.iter()
309 .find(|& &(n, _)| name.equiv(&n)) {
310 Some(&(name, Active)) => { cx.features.push(name); }
311 Some(&(_, Removed)) => {
312 sess.span_err(mi.span, "feature has been removed");
314 Some(&(_, Accepted)) => {
315 sess.span_warn(mi.span, "feature has added to rust, \
316 directive not necessary");
319 sess.add_lint(lint::UnknownFeatures,
330 visit::walk_crate(&mut cx, krate, ());
332 sess.abort_if_errors();
334 sess.features.default_type_params.set(cx.has_feature("default_type_params"));