]> git.lizzy.rs Git - rust.git/blob - src/librustc/front/feature_gate.rs
Merge pull request #12308 from kballard/vim-nested-comments
[rust.git] / src / librustc / front / feature_gate.rs
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.
4 //
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.
10
11 //! Feature gating
12 //!
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
16 //! enabled.
17 //!
18 //! Features are enabled in programs via the crate-level attributes of
19 //! #[feature(...)] with a comma-separated list of features.
20
21 use middle::lint;
22
23 use syntax::ast;
24 use syntax::attr;
25 use syntax::attr::AttrMetaMethods;
26 use syntax::codemap::Span;
27 use syntax::visit;
28 use syntax::visit::Visitor;
29 use syntax::parse::token;
30
31 use driver::session::Session;
32
33 use std::cell::Cell;
34
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)] = &[
39     ("globs", Active),
40     ("macro_rules", Active),
41     ("struct_variant", Active),
42     ("once_fns", Active),
43     ("asm", Active),
44     ("managed_boxes", Active),
45     ("non_ascii_idents", Active),
46     ("thread_local", Active),
47     ("link_args", Active),
48     ("phase", Active),
49     ("macro_registrar", Active),
50     ("log_syntax", Active),
51     ("trace_macros", Active),
52     ("simd", Active),
53     ("default_type_params", Active),
54     ("quote", Active),
55
56     // These are used to test this portion of the compiler, they don't actually
57     // mean anything
58     ("test_accepted_feature", Accepted),
59     ("test_removed_feature", Removed),
60 ];
61
62 enum Status {
63     /// Represents an active feature that is currently being implemented or
64     /// currently being considered for addition/removal.
65     Active,
66
67     /// Represents a feature which has since been removed (it was once Active)
68     Removed,
69
70     /// This language feature has since been Accepted (it was once Active)
71     Accepted,
72 }
73
74 /// A set of features to be used by later passes.
75 pub struct Features {
76     default_type_params: Cell<bool>
77 }
78
79 impl Features {
80     pub fn new() -> Features {
81         Features {
82             default_type_params: Cell::new(false)
83         }
84     }
85 }
86
87 struct Context {
88     features: ~[&'static str],
89     sess: Session,
90 }
91
92 impl Context {
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",
98                                                  feature));
99         }
100     }
101
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.");
108     }
109
110     fn has_feature(&self, feature: &str) -> bool {
111         self.features.iter().any(|n| n.as_slice() == feature)
112     }
113 }
114
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.");
120         }
121     }
122
123     fn visit_view_item(&mut self, i: &ast::ViewItem, _: ()) {
124         match i.node {
125             ast::ViewItemUse(ref paths) => {
126                 for path in paths.iter() {
127                     match path.node {
128                         ast::ViewPathGlob(..) => {
129                             self.gate_feature("globs", path.span,
130                                               "glob import statements are \
131                                                experimental and possibly buggy");
132                         }
133                         _ => {}
134                     }
135                 }
136             }
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");
143                     }
144                 }
145             }
146         }
147         visit::walk_view_item(self, i, ())
148     }
149
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");
157             }
158         }
159         match i.node {
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");
167                         }
168                         _ => {}
169                     }
170                 }
171             }
172
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")
179                 }
180             }
181
182             ast::ItemFn(..) => {
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");
187                 }
188             }
189
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");
194                 }
195             }
196
197             _ => {}
198         }
199
200         visit::walk_item(self, i, ());
201     }
202
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";
209
210
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");
214         }
215
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");
219         }
220
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");
224         }
225
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");
229         }
230
231         else {
232             for &quote in quotes.iter() {
233                 if id == token::str_to_ident(quote) {
234                   self.gate_feature("quote", path.span, quote + msg);
235                 }
236             }
237         }
238     }
239
240     fn visit_ty(&mut self, t: &ast::Ty, _: ()) {
241         match t.node {
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");
247
248             },
249             ast::TyBox(_) => { self.gate_box(t.span); }
250             _ => {}
251         }
252
253         visit::walk_ty(self, t, ());
254     }
255
256     fn visit_expr(&mut self, e: &ast::Expr, _: ()) {
257         match e.node {
258             ast::ExprUnary(ast::UnBox, _) => {
259                 self.gate_box(e.span);
260             }
261             _ => {}
262         }
263         visit::walk_expr(self, e, ());
264     }
265
266     fn visit_generics(&mut self, generics: &ast::Generics, _: ()) {
267         for type_parameter in generics.ty_params.iter() {
268             match type_parameter.default {
269                 Some(ty) => {
270                     self.gate_feature("default_type_params", ty.span,
271                                       "default type parameters are \
272                                        experimental and possibly buggy");
273                 }
274                 None => {}
275             }
276         }
277         visit::walk_generics(self, generics, ());
278     }
279 }
280
281 pub fn check_crate(sess: Session, krate: &ast::Crate) {
282     let mut cx = Context {
283         features: ~[],
284         sess: sess,
285     };
286
287     for attr in krate.attrs.iter() {
288         if !attr.name().equiv(&("feature")) {
289             continue
290         }
291
292         match attr.meta_item_list() {
293             None => {
294                 sess.span_err(attr.span, "malformed feature attribute, \
295                                           expected #[feature(...)]");
296             }
297             Some(list) => {
298                 for &mi in list.iter() {
299                     let name = match mi.node {
300                         ast::MetaWord(ref word) => (*word).clone(),
301                         _ => {
302                             sess.span_err(mi.span,
303                                           "malformed feature, expected just \
304                                            one word");
305                             continue
306                         }
307                     };
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");
313                         }
314                         Some(&(_, Accepted)) => {
315                             sess.span_warn(mi.span, "feature has added to rust, \
316                                                      directive not necessary");
317                         }
318                         None => {
319                             sess.add_lint(lint::UnknownFeatures,
320                                           ast::CRATE_NODE_ID,
321                                           mi.span,
322                                           ~"unknown feature");
323                         }
324                     }
325                 }
326             }
327         }
328     }
329
330     visit::walk_crate(&mut cx, krate, ());
331
332     sess.abort_if_errors();
333
334     sess.features.default_type_params.set(cx.has_feature("default_type_params"));
335 }