]> git.lizzy.rs Git - rust.git/blob - src/librustc/front/feature_gate.rs
auto merge of #12231 : wycats/rust/url_path_parse, r=alexcrichton
[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 /// This is a list of all known features since the beginning of time. This list
34 /// can never shrink, it may only be expanded (in order to prevent old programs
35 /// from failing to compile). The status of each feature may change, however.
36 static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[
37     ("globs", Active),
38     ("macro_rules", Active),
39     ("struct_variant", Active),
40     ("once_fns", Active),
41     ("asm", Active),
42     ("managed_boxes", Active),
43     ("non_ascii_idents", Active),
44     ("thread_local", Active),
45     ("link_args", Active),
46     ("phase", Active),
47     ("macro_registrar", Active),
48     ("log_syntax", Active),
49     ("trace_macros", Active),
50     ("simd", Active),
51     ("default_type_params", Active),
52     ("quote", Active),
53
54     // These are used to test this portion of the compiler, they don't actually
55     // mean anything
56     ("test_accepted_feature", Accepted),
57     ("test_removed_feature", Removed),
58 ];
59
60 enum Status {
61     /// Represents an active feature that is currently being implemented or
62     /// currently being considered for addition/removal.
63     Active,
64
65     /// Represents a feature which has since been removed (it was once Active)
66     Removed,
67
68     /// This language feature has since been Accepted (it was once Active)
69     Accepted,
70 }
71
72 struct Context {
73     features: ~[&'static str],
74     sess: Session,
75 }
76
77 impl Context {
78     fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
79         if !self.has_feature(feature) {
80             self.sess.span_err(span, explain);
81             self.sess.span_note(span, format!("add \\#[feature({})] to the \
82                                                   crate attributes to enable",
83                                                  feature));
84         }
85     }
86
87     fn gate_box(&self, span: Span) {
88         self.gate_feature("managed_boxes", span,
89                           "The managed box syntax is being replaced by the \
90                            `std::gc::Gc` and `std::rc::Rc` types. Equivalent \
91                            functionality to managed trait objects will be \
92                            implemented but is currently missing.");
93     }
94
95     fn has_feature(&self, feature: &str) -> bool {
96         self.features.iter().any(|n| n.as_slice() == feature)
97     }
98 }
99
100 impl Visitor<()> for Context {
101     fn visit_ident(&mut self, sp: Span, id: ast::Ident, _: ()) {
102         if !token::get_ident(id).get().is_ascii() {
103             self.gate_feature("non_ascii_idents", sp,
104                               "non-ascii idents are not fully supported.");
105         }
106     }
107
108     fn visit_view_item(&mut self, i: &ast::ViewItem, _: ()) {
109         match i.node {
110             ast::ViewItemUse(ref paths) => {
111                 for path in paths.iter() {
112                     match path.node {
113                         ast::ViewPathGlob(..) => {
114                             self.gate_feature("globs", path.span,
115                                               "glob import statements are \
116                                                experimental and possibly buggy");
117                         }
118                         _ => {}
119                     }
120                 }
121             }
122             ast::ViewItemExternMod(..) => {
123                 for attr in i.attrs.iter() {
124                     if attr.name().get() == "phase"{
125                         self.gate_feature("phase", attr.span,
126                                           "compile time crate loading is \
127                                            experimental and possibly buggy");
128                     }
129                 }
130             }
131         }
132         visit::walk_view_item(self, i, ())
133     }
134
135     fn visit_item(&mut self, i: &ast::Item, _:()) {
136         for attr in i.attrs.iter() {
137             if attr.name().equiv(&("thread_local")) {
138                 self.gate_feature("thread_local", i.span,
139                                   "`#[thread_local]` is an experimental feature, and does not \
140                                   currently handle destructors. There is no corresponding \
141                                   `#[task_local]` mapping to the task model");
142             }
143         }
144         match i.node {
145             ast::ItemEnum(ref def, _) => {
146                 for variant in def.variants.iter() {
147                     match variant.node.kind {
148                         ast::StructVariantKind(..) => {
149                             self.gate_feature("struct_variant", variant.span,
150                                               "enum struct variants are \
151                                                experimental and possibly buggy");
152                         }
153                         _ => {}
154                     }
155                 }
156             }
157
158             ast::ItemForeignMod(..) => {
159                 if attr::contains_name(i.attrs, "link_args") {
160                     self.gate_feature("link_args", i.span,
161                                       "the `link_args` attribute is not portable \
162                                        across platforms, it is recommended to \
163                                        use `#[link(name = \"foo\")]` instead")
164                 }
165             }
166
167             ast::ItemFn(..) => {
168                 if attr::contains_name(i.attrs, "macro_registrar") {
169                     self.gate_feature("macro_registrar", i.span,
170                                       "cross-crate macro exports are \
171                                        experimental and possibly buggy");
172                 }
173             }
174
175             ast::ItemStruct(..) => {
176                 if attr::contains_name(i.attrs, "simd") {
177                     self.gate_feature("simd", i.span,
178                                       "SIMD types are experimental and possibly buggy");
179                 }
180             }
181
182             _ => {}
183         }
184
185         visit::walk_item(self, i, ());
186     }
187
188     fn visit_mac(&mut self, macro: &ast::Mac, _: ()) {
189         let ast::MacInvocTT(ref path, _, _) = macro.node;
190         let id = path.segments.last().unwrap().identifier;
191         let quotes = ["quote_tokens", "quote_expr", "quote_ty",
192                       "quote_item", "quote_pat", "quote_stmt"];
193         let msg = " is not stable enough for use and are subject to change";
194
195
196         if id == token::str_to_ident("macro_rules") {
197             self.gate_feature("macro_rules", path.span, "macro definitions are \
198                 not stable enough for use and are subject to change");
199         }
200
201         else if id == token::str_to_ident("asm") {
202             self.gate_feature("asm", path.span, "inline assembly is not \
203                 stable enough for use and is subject to change");
204         }
205
206         else if id == token::str_to_ident("log_syntax") {
207             self.gate_feature("log_syntax", path.span, "`log_syntax!` is not \
208                 stable enough for use and is subject to change");
209         }
210
211         else if id == token::str_to_ident("trace_macros") {
212             self.gate_feature("trace_macros", path.span, "`trace_macros` is not \
213                 stable enough for use and is subject to change");
214         }
215
216         else {
217             for &quote in quotes.iter() {
218                 if id == token::str_to_ident(quote) {
219                   self.gate_feature("quote", path.span, quote + msg);
220                 }
221             }
222         }
223     }
224
225     fn visit_ty(&mut self, t: &ast::Ty, _: ()) {
226         match t.node {
227             ast::TyClosure(closure) if closure.onceness == ast::Once &&
228                     closure.sigil != ast::OwnedSigil => {
229                 self.gate_feature("once_fns", t.span,
230                                   "once functions are \
231                                    experimental and likely to be removed");
232
233             },
234             ast::TyBox(_) => { self.gate_box(t.span); }
235             _ => {}
236         }
237
238         visit::walk_ty(self, t, ());
239     }
240
241     fn visit_expr(&mut self, e: &ast::Expr, _: ()) {
242         match e.node {
243             ast::ExprUnary(_, ast::UnBox, _) => {
244                 self.gate_box(e.span);
245             }
246             _ => {}
247         }
248         visit::walk_expr(self, e, ());
249     }
250
251     fn visit_generics(&mut self, generics: &ast::Generics, _: ()) {
252         for type_parameter in generics.ty_params.iter() {
253             match type_parameter.default {
254                 Some(ty) => {
255                     self.gate_feature("default_type_params", ty.span,
256                                       "default type parameters are \
257                                        experimental and possibly buggy");
258                 }
259                 None => {}
260             }
261         }
262         visit::walk_generics(self, generics, ());
263     }
264 }
265
266 pub fn check_crate(sess: Session, krate: &ast::Crate) {
267     let mut cx = Context {
268         features: ~[],
269         sess: sess,
270     };
271
272     for attr in krate.attrs.iter() {
273         if !attr.name().equiv(&("feature")) {
274             continue
275         }
276
277         match attr.meta_item_list() {
278             None => {
279                 sess.span_err(attr.span, "malformed feature attribute, \
280                                           expected #[feature(...)]");
281             }
282             Some(list) => {
283                 for &mi in list.iter() {
284                     let name = match mi.node {
285                         ast::MetaWord(ref word) => (*word).clone(),
286                         _ => {
287                             sess.span_err(mi.span,
288                                           "malformed feature, expected just \
289                                            one word");
290                             continue
291                         }
292                     };
293                     match KNOWN_FEATURES.iter()
294                                         .find(|& &(n, _)| name.equiv(&n)) {
295                         Some(&(name, Active)) => { cx.features.push(name); }
296                         Some(&(_, Removed)) => {
297                             sess.span_err(mi.span, "feature has been removed");
298                         }
299                         Some(&(_, Accepted)) => {
300                             sess.span_warn(mi.span, "feature has added to rust, \
301                                                      directive not necessary");
302                         }
303                         None => {
304                             sess.add_lint(lint::UnknownFeatures,
305                                           ast::CRATE_NODE_ID,
306                                           mi.span,
307                                           ~"unknown feature");
308                         }
309                     }
310                 }
311             }
312         }
313     }
314
315     visit::walk_crate(&mut cx, krate, ());
316
317     sess.abort_if_errors();
318 }