]> git.lizzy.rs Git - rust.git/blob - src/libsyntax/feature_gate.rs
core: Fix size_hint for signed integer Range<T> iterators
[rust.git] / src / libsyntax / 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 module 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 //! For the purpose of future feature-tracking, once code for detection of feature
22 //! gate usage is added, *do not remove it again* even once the feature
23 //! becomes stable.
24
25 use self::Status::*;
26 use self::AttributeType::*;
27
28 use abi::Abi;
29 use ast::NodeId;
30 use ast;
31 use attr;
32 use attr::AttrMetaMethods;
33 use codemap::{CodeMap, Span};
34 use diagnostic::SpanHandler;
35 use visit;
36 use visit::Visitor;
37 use parse::token::{self, InternedString};
38
39 use std::ascii::AsciiExt;
40
41 // If you change this list without updating src/doc/reference.md, @cmr will be sad
42 // Don't ever remove anything from this list; set them to 'Removed'.
43 // The version numbers here correspond to the version in which the current status
44 // was set. This is most important for knowing when a particular feature became
45 // stable (active).
46 // NB: The featureck.py script parses this information directly out of the source
47 // so take care when modifying it.
48 const KNOWN_FEATURES: &'static [(&'static str, &'static str, Status)] = &[
49     ("globs", "1.0.0", Accepted),
50     ("macro_rules", "1.0.0", Accepted),
51     ("struct_variant", "1.0.0", Accepted),
52     ("asm", "1.0.0", Active),
53     ("managed_boxes", "1.0.0", Removed),
54     ("non_ascii_idents", "1.0.0", Active),
55     ("thread_local", "1.0.0", Active),
56     ("link_args", "1.0.0", Active),
57     ("plugin_registrar", "1.0.0", Active),
58     ("log_syntax", "1.0.0", Active),
59     ("trace_macros", "1.0.0", Active),
60     ("concat_idents", "1.0.0", Active),
61     ("intrinsics", "1.0.0", Active),
62     ("lang_items", "1.0.0", Active),
63
64     ("simd", "1.0.0", Active),
65     ("default_type_params", "1.0.0", Accepted),
66     ("quote", "1.0.0", Active),
67     ("link_llvm_intrinsics", "1.0.0", Active),
68     ("linkage", "1.0.0", Active),
69     ("struct_inherit", "1.0.0", Removed),
70
71     ("quad_precision_float", "1.0.0", Removed),
72
73     ("rustc_diagnostic_macros", "1.0.0", Active),
74     ("unboxed_closures", "1.0.0", Active),
75     ("reflect", "1.0.0", Active),
76     ("import_shadowing", "1.0.0", Removed),
77     ("advanced_slice_patterns", "1.0.0", Active),
78     ("tuple_indexing", "1.0.0", Accepted),
79     ("associated_types", "1.0.0", Accepted),
80     ("visible_private_types", "1.0.0", Active),
81     ("slicing_syntax", "1.0.0", Accepted),
82     ("box_syntax", "1.0.0", Active),
83     ("on_unimplemented", "1.0.0", Active),
84     ("simd_ffi", "1.0.0", Active),
85     ("allocator", "1.0.0", Active),
86
87     ("if_let", "1.0.0", Accepted),
88     ("while_let", "1.0.0", Accepted),
89
90     ("plugin", "1.0.0", Active),
91     ("start", "1.0.0", Active),
92     ("main", "1.0.0", Active),
93
94     ("fundamental", "1.0.0", Active),
95
96     // Deprecate after snapshot
97     // SNAP 5520801
98     ("unsafe_destructor", "1.0.0", Active),
99
100     // A temporary feature gate used to enable parser extensions needed
101     // to bootstrap fix for #5723.
102     ("issue_5723_bootstrap", "1.0.0", Accepted),
103
104     // A way to temporarily opt out of opt in copy. This will *never* be accepted.
105     ("opt_out_copy", "1.0.0", Removed),
106
107     // OIBIT specific features
108     ("optin_builtin_traits", "1.0.0", Active),
109
110     // macro reexport needs more discussion and stabilization
111     ("macro_reexport", "1.0.0", Active),
112
113     // These are used to test this portion of the compiler, they don't actually
114     // mean anything
115     ("test_accepted_feature", "1.0.0", Accepted),
116     ("test_removed_feature", "1.0.0", Removed),
117
118     // Allows use of #[staged_api]
119     ("staged_api", "1.0.0", Active),
120
121     // Allows using items which are missing stability attributes
122     ("unmarked_api", "1.0.0", Active),
123
124     // Allows using #![no_std]
125     ("no_std", "1.0.0", Active),
126
127     // Allows using `box` in patterns; RFC 469
128     ("box_patterns", "1.0.0", Active),
129
130     // Allows using the unsafe_no_drop_flag attribute (unlikely to
131     // switch to Accepted; see RFC 320)
132     ("unsafe_no_drop_flag", "1.0.0", Active),
133
134     // Allows the use of custom attributes; RFC 572
135     ("custom_attribute", "1.0.0", Active),
136
137     // Allows the use of #[derive(Anything)] as sugar for
138     // #[derive_Anything].
139     ("custom_derive", "1.0.0", Active),
140
141     // Allows the use of rustc_* attributes; RFC 572
142     ("rustc_attrs", "1.0.0", Active),
143
144     // Allows the use of `static_assert`
145     ("static_assert", "1.0.0", Active),
146
147     // Allows the use of #[allow_internal_unstable]. This is an
148     // attribute on macro_rules! and can't use the attribute handling
149     // below (it has to be checked before expansion possibly makes
150     // macros disappear).
151     ("allow_internal_unstable", "1.0.0", Active),
152
153     // #23121. Array patterns have some hazards yet.
154     ("slice_patterns", "1.0.0", Active),
155
156     // Allows use of unary negate on unsigned integers, e.g. -e for e: u8
157     ("negate_unsigned", "1.0.0", Active),
158 ];
159 // (changing above list without updating src/doc/reference.md makes @cmr sad)
160
161 enum Status {
162     /// Represents an active feature that is currently being implemented or
163     /// currently being considered for addition/removal.
164     Active,
165
166     /// Represents a feature which has since been removed (it was once Active)
167     Removed,
168
169     /// This language feature has since been Accepted (it was once Active)
170     Accepted,
171 }
172
173 // Attributes that have a special meaning to rustc or rustdoc
174 pub const KNOWN_ATTRIBUTES: &'static [(&'static str, AttributeType)] = &[
175     // Normal attributes
176
177     ("warn", Normal),
178     ("allow", Normal),
179     ("forbid", Normal),
180     ("deny", Normal),
181
182     ("macro_reexport", Normal),
183     ("macro_use", Normal),
184     ("macro_export", Normal),
185     ("plugin_registrar", Normal),
186
187     ("cfg", Normal),
188     ("cfg_attr", Normal),
189     ("main", Normal),
190     ("start", Normal),
191     ("test", Normal),
192     ("bench", Normal),
193     ("simd", Normal),
194     ("repr", Normal),
195     ("path", Normal),
196     ("abi", Normal),
197     ("automatically_derived", Normal),
198     ("no_mangle", Normal),
199     ("no_link", Normal),
200     ("derive", Normal),
201     ("should_panic", Normal),
202     ("ignore", Normal),
203     ("no_implicit_prelude", Normal),
204     ("reexport_test_harness_main", Normal),
205     ("link_args", Normal),
206     ("macro_escape", Normal),
207
208     ("unsafe_destructor", Gated("unsafe_destructor",
209                                 "`#[unsafe_destructor]` does nothing anymore")),
210     ("staged_api", Gated("staged_api",
211                          "staged_api is for use by rustc only")),
212     ("plugin", Gated("plugin",
213                      "compiler plugins are experimental \
214                       and possibly buggy")),
215     ("no_std", Gated("no_std",
216                      "no_std is experimental")),
217     ("lang", Gated("lang_items",
218                      "language items are subject to change")),
219     ("linkage", Gated("linkage",
220                       "the `linkage` attribute is experimental \
221                        and not portable across platforms")),
222     ("thread_local", Gated("thread_local",
223                             "`#[thread_local]` is an experimental feature, and does not \
224                              currently handle destructors. There is no corresponding \
225                              `#[task_local]` mapping to the task model")),
226
227     ("rustc_on_unimplemented", Gated("on_unimplemented",
228                                      "the `#[rustc_on_unimplemented]` attribute \
229                                       is an experimental feature")),
230     ("allocator", Gated("allocator",
231                         "the `#[allocator]` attribute is an experimental feature")),
232     ("rustc_variance", Gated("rustc_attrs",
233                              "the `#[rustc_variance]` attribute \
234                               is an experimental feature")),
235     ("rustc_error", Gated("rustc_attrs",
236                           "the `#[rustc_error]` attribute \
237                            is an experimental feature")),
238     ("rustc_move_fragments", Gated("rustc_attrs",
239                                    "the `#[rustc_move_fragments]` attribute \
240                                     is an experimental feature")),
241
242     ("allow_internal_unstable", Gated("allow_internal_unstable",
243                                       EXPLAIN_ALLOW_INTERNAL_UNSTABLE)),
244
245     ("fundamental", Gated("fundamental",
246                           "the `#[fundamental]` attribute \
247                            is an experimental feature")),
248
249     // FIXME: #14408 whitelist docs since rustdoc looks at them
250     ("doc", Whitelisted),
251
252     // FIXME: #14406 these are processed in trans, which happens after the
253     // lint pass
254     ("cold", Whitelisted),
255     ("export_name", Whitelisted),
256     ("inline", Whitelisted),
257     ("link", Whitelisted),
258     ("link_name", Whitelisted),
259     ("link_section", Whitelisted),
260     ("no_builtins", Whitelisted),
261     ("no_mangle", Whitelisted),
262     ("no_stack_check", Whitelisted),
263     ("packed", Whitelisted),
264     ("static_assert", Gated("static_assert",
265                             "`#[static_assert]` is an experimental feature, and has a poor API")),
266     ("no_debug", Whitelisted),
267     ("omit_gdb_pretty_printer_section", Whitelisted),
268     ("unsafe_no_drop_flag", Gated("unsafe_no_drop_flag",
269                                   "unsafe_no_drop_flag has unstable semantics \
270                                    and may be removed in the future")),
271
272     // used in resolve
273     ("prelude_import", Whitelisted),
274
275     // FIXME: #14407 these are only looked at on-demand so we can't
276     // guarantee they'll have already been checked
277     ("deprecated", Whitelisted),
278     ("must_use", Whitelisted),
279     ("stable", Whitelisted),
280     ("unstable", Whitelisted),
281
282     ("rustc_paren_sugar", Gated("unboxed_closures",
283                                 "unboxed_closures are still evolving")),
284     ("rustc_reflect_like", Gated("reflect",
285                                  "defining reflective traits is still evolving")),
286
287     // Crate level attributes
288     ("crate_name", CrateLevel),
289     ("crate_type", CrateLevel),
290     ("crate_id", CrateLevel),
291     ("feature", CrateLevel),
292     ("no_start", CrateLevel),
293     ("no_main", CrateLevel),
294     ("no_builtins", CrateLevel),
295     ("recursion_limit", CrateLevel),
296 ];
297
298 #[derive(PartialEq, Copy, Clone, Debug)]
299 pub enum AttributeType {
300     /// Normal, builtin attribute that is consumed
301     /// by the compiler before the unused_attribute check
302     Normal,
303
304     /// Builtin attribute that may not be consumed by the compiler
305     /// before the unused_attribute check. These attributes
306     /// will be ignored by the unused_attribute lint
307     Whitelisted,
308
309     /// Is gated by a given feature gate and reason
310     /// These get whitelisted too
311     Gated(&'static str, &'static str),
312
313     /// Builtin attribute that is only allowed at the crate level
314     CrateLevel,
315 }
316
317 /// A set of features to be used by later passes.
318 pub struct Features {
319     pub unboxed_closures: bool,
320     pub rustc_diagnostic_macros: bool,
321     pub visible_private_types: bool,
322     pub allow_quote: bool,
323     pub allow_asm: bool,
324     pub allow_log_syntax: bool,
325     pub allow_concat_idents: bool,
326     pub allow_trace_macros: bool,
327     pub allow_internal_unstable: bool,
328     pub allow_custom_derive: bool,
329     pub simd_ffi: bool,
330     pub unmarked_api: bool,
331     pub negate_unsigned: bool,
332     /// spans of #![feature] attrs for stable language features. for error reporting
333     pub declared_stable_lang_features: Vec<Span>,
334     /// #![feature] attrs for non-language (library) features
335     pub declared_lib_features: Vec<(InternedString, Span)>
336 }
337
338 impl Features {
339     pub fn new() -> Features {
340         Features {
341             unboxed_closures: false,
342             rustc_diagnostic_macros: false,
343             visible_private_types: false,
344             allow_quote: false,
345             allow_asm: false,
346             allow_log_syntax: false,
347             allow_concat_idents: false,
348             allow_trace_macros: false,
349             allow_internal_unstable: false,
350             allow_custom_derive: false,
351             simd_ffi: false,
352             unmarked_api: false,
353             negate_unsigned: false,
354             declared_stable_lang_features: Vec::new(),
355             declared_lib_features: Vec::new()
356         }
357     }
358 }
359
360 struct Context<'a> {
361     features: Vec<&'static str>,
362     span_handler: &'a SpanHandler,
363     cm: &'a CodeMap,
364 }
365
366 impl<'a> Context<'a> {
367     fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
368         let has_feature = self.has_feature(feature);
369         debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", feature, span, has_feature);
370         if !has_feature {
371             emit_feature_err(self.span_handler, feature, span, explain);
372         }
373     }
374     fn has_feature(&self, feature: &str) -> bool {
375         self.features.iter().any(|&n| n == feature)
376     }
377
378     fn check_attribute(&self, attr: &ast::Attribute) {
379         debug!("check_attribute(attr = {:?})", attr);
380         let name = &*attr.name();
381         for &(n, ty) in KNOWN_ATTRIBUTES {
382             if n == name {
383                 if let Gated(gate, desc) = ty {
384                     self.gate_feature(gate, attr.span, desc);
385                 }
386                 debug!("check_attribute: {:?} is known, {:?}", name, ty);
387                 return;
388             }
389         }
390         if name.starts_with("rustc_") {
391             self.gate_feature("rustc_attrs", attr.span,
392                               "unless otherwise specified, attributes \
393                                with the prefix `rustc_` \
394                                are reserved for internal compiler diagnostics");
395         } else if name.starts_with("derive_") {
396             self.gate_feature("custom_derive", attr.span,
397                               "attributes of the form `#[derive_*]` are reserved
398                                for the compiler");
399         } else {
400             self.gate_feature("custom_attribute", attr.span,
401                        &format!("The attribute `{}` is currently \
402                                 unknown to the compiler and \
403                                 may have meaning \
404                                 added to it in the future",
405                                 name));
406         }
407     }
408 }
409
410 pub fn emit_feature_err(diag: &SpanHandler, feature: &str, span: Span, explain: &str) {
411     diag.span_err(span, explain);
412
413     // #23973: do not suggest `#![feature(...)]` if we are in beta/stable
414     if option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some() { return; }
415     diag.fileline_help(span, &format!("add #![feature({})] to the \
416                                    crate attributes to enable",
417                                   feature));
418 }
419
420 pub fn emit_feature_warn(diag: &SpanHandler, feature: &str, span: Span, explain: &str) {
421     diag.span_warn(span, explain);
422
423     // #23973: do not suggest `#![feature(...)]` if we are in beta/stable
424     if option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some() { return; }
425     if diag.handler.can_emit_warnings {
426         diag.fileline_help(span, &format!("add #![feature({})] to the \
427                                        crate attributes to silence this warning",
428                                       feature));
429     }
430 }
431
432 pub const EXPLAIN_ASM: &'static str =
433     "inline assembly is not stable enough for use and is subject to change";
434
435 pub const EXPLAIN_LOG_SYNTAX: &'static str =
436     "`log_syntax!` is not stable enough for use and is subject to change";
437
438 pub const EXPLAIN_CONCAT_IDENTS: &'static str =
439     "`concat_idents` is not stable enough for use and is subject to change";
440
441 pub const EXPLAIN_TRACE_MACROS: &'static str =
442     "`trace_macros` is not stable enough for use and is subject to change";
443 pub const EXPLAIN_ALLOW_INTERNAL_UNSTABLE: &'static str =
444     "allow_internal_unstable side-steps feature gating and stability checks";
445
446 pub const EXPLAIN_CUSTOM_DERIVE: &'static str =
447     "`#[derive]` for custom traits is not stable enough for use and is subject to change";
448
449 struct MacroVisitor<'a> {
450     context: &'a Context<'a>
451 }
452
453 impl<'a, 'v> Visitor<'v> for MacroVisitor<'a> {
454     fn visit_mac(&mut self, mac: &ast::Mac) {
455         let ast::MacInvocTT(ref path, _, _) = mac.node;
456         let id = path.segments.last().unwrap().identifier;
457
458         // Issue 22234: If you add a new case here, make sure to also
459         // add code to catch the macro during or after expansion.
460         //
461         // We still keep this MacroVisitor (rather than *solely*
462         // relying on catching cases during or after expansion) to
463         // catch uses of these macros within conditionally-compiled
464         // code, e.g. `#[cfg]`-guarded functions.
465
466         if id == token::str_to_ident("asm") {
467             self.context.gate_feature("asm", path.span, EXPLAIN_ASM);
468         }
469
470         else if id == token::str_to_ident("log_syntax") {
471             self.context.gate_feature("log_syntax", path.span, EXPLAIN_LOG_SYNTAX);
472         }
473
474         else if id == token::str_to_ident("trace_macros") {
475             self.context.gate_feature("trace_macros", path.span, EXPLAIN_TRACE_MACROS);
476         }
477
478         else if id == token::str_to_ident("concat_idents") {
479             self.context.gate_feature("concat_idents", path.span, EXPLAIN_CONCAT_IDENTS);
480         }
481     }
482
483     fn visit_attribute(&mut self, attr: &'v ast::Attribute) {
484         self.context.check_attribute(attr);
485     }
486 }
487
488 struct PostExpansionVisitor<'a> {
489     context: &'a Context<'a>
490 }
491
492 impl<'a> PostExpansionVisitor<'a> {
493     fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
494         if !self.context.cm.span_allows_unstable(span) {
495             self.context.gate_feature(feature, span, explain)
496         }
497     }
498 }
499
500 impl<'a, 'v> Visitor<'v> for PostExpansionVisitor<'a> {
501     fn visit_attribute(&mut self, attr: &ast::Attribute) {
502         if !self.context.cm.span_allows_unstable(attr.span) {
503             self.context.check_attribute(attr);
504         }
505     }
506
507     fn visit_name(&mut self, sp: Span, name: ast::Name) {
508         if !token::get_name(name).is_ascii() {
509             self.gate_feature("non_ascii_idents", sp,
510                               "non-ascii idents are not fully supported.");
511         }
512     }
513
514     fn visit_item(&mut self, i: &ast::Item) {
515         match i.node {
516             ast::ItemExternCrate(_) => {
517                 if attr::contains_name(&i.attrs[..], "macro_reexport") {
518                     self.gate_feature("macro_reexport", i.span,
519                                       "macros reexports are experimental \
520                                        and possibly buggy");
521                 }
522             }
523
524             ast::ItemForeignMod(ref foreign_module) => {
525                 if attr::contains_name(&i.attrs[..], "link_args") {
526                     self.gate_feature("link_args", i.span,
527                                       "the `link_args` attribute is not portable \
528                                        across platforms, it is recommended to \
529                                        use `#[link(name = \"foo\")]` instead")
530                 }
531                 if foreign_module.abi == Abi::RustIntrinsic {
532                     self.gate_feature("intrinsics",
533                                       i.span,
534                                       "intrinsics are subject to change")
535                 }
536             }
537
538             ast::ItemFn(..) => {
539                 if attr::contains_name(&i.attrs[..], "plugin_registrar") {
540                     self.gate_feature("plugin_registrar", i.span,
541                                       "compiler plugins are experimental and possibly buggy");
542                 }
543                 if attr::contains_name(&i.attrs[..], "start") {
544                     self.gate_feature("start", i.span,
545                                       "a #[start] function is an experimental \
546                                        feature whose signature may change \
547                                        over time");
548                 }
549                 if attr::contains_name(&i.attrs[..], "main") {
550                     self.gate_feature("main", i.span,
551                                       "declaration of a nonstandard #[main] \
552                                        function may change over time, for now \
553                                        a top-level `fn main()` is required");
554                 }
555             }
556
557             ast::ItemStruct(..) => {
558                 if attr::contains_name(&i.attrs[..], "simd") {
559                     self.gate_feature("simd", i.span,
560                                       "SIMD types are experimental and possibly buggy");
561                 }
562             }
563
564             ast::ItemDefaultImpl(..) => {
565                 self.gate_feature("optin_builtin_traits",
566                                   i.span,
567                                   "default trait implementations are experimental \
568                                    and possibly buggy");
569             }
570
571             ast::ItemImpl(_, polarity, _, _, _, _) => {
572                 match polarity {
573                     ast::ImplPolarity::Negative => {
574                         self.gate_feature("optin_builtin_traits",
575                                           i.span,
576                                           "negative trait bounds are not yet fully implemented; \
577                                           use marker types for now");
578                     },
579                     _ => {}
580                 }
581             }
582
583             _ => {}
584         }
585
586         visit::walk_item(self, i);
587     }
588
589     fn visit_foreign_item(&mut self, i: &ast::ForeignItem) {
590         let links_to_llvm = match attr::first_attr_value_str_by_name(&i.attrs,
591                                                                      "link_name") {
592             Some(val) => val.starts_with("llvm."),
593             _ => false
594         };
595         if links_to_llvm {
596             self.gate_feature("link_llvm_intrinsics", i.span,
597                               "linking to LLVM intrinsics is experimental");
598         }
599
600         visit::walk_foreign_item(self, i)
601     }
602
603     fn visit_expr(&mut self, e: &ast::Expr) {
604         match e.node {
605             ast::ExprBox(..) | ast::ExprUnary(ast::UnOp::UnUniq, _) => {
606                 self.gate_feature("box_syntax",
607                                   e.span,
608                                   "box expression syntax is experimental; \
609                                    you can call `Box::new` instead.");
610             }
611             _ => {}
612         }
613         visit::walk_expr(self, e);
614     }
615
616     fn visit_pat(&mut self, pattern: &ast::Pat) {
617         match pattern.node {
618             ast::PatVec(_, Some(_), ref last) if !last.is_empty() => {
619                 self.gate_feature("advanced_slice_patterns",
620                                   pattern.span,
621                                   "multiple-element slice matches anywhere \
622                                    but at the end of a slice (e.g. \
623                                    `[0, ..xs, 0]` are experimental")
624             }
625             ast::PatVec(..) => {
626                 self.gate_feature("slice_patterns",
627                                   pattern.span,
628                                   "slice pattern syntax is experimental");
629             }
630             ast::PatBox(..) => {
631                 self.gate_feature("box_patterns",
632                                   pattern.span,
633                                   "box pattern syntax is experimental");
634             }
635             _ => {}
636         }
637         visit::walk_pat(self, pattern)
638     }
639
640     fn visit_fn(&mut self,
641                 fn_kind: visit::FnKind<'v>,
642                 fn_decl: &'v ast::FnDecl,
643                 block: &'v ast::Block,
644                 span: Span,
645                 _node_id: NodeId) {
646         match fn_kind {
647             visit::FkItemFn(_, _, _, abi, _) if abi == Abi::RustIntrinsic => {
648                 self.gate_feature("intrinsics",
649                                   span,
650                                   "intrinsics are subject to change")
651             }
652             visit::FkItemFn(_, _, _, abi, _) |
653             visit::FkMethod(_, &ast::MethodSig { abi, .. }, _) if abi == Abi::RustCall => {
654                 self.gate_feature("unboxed_closures",
655                                   span,
656                                   "rust-call ABI is subject to change")
657             }
658             _ => {}
659         }
660         visit::walk_fn(self, fn_kind, fn_decl, block, span);
661     }
662 }
663
664 fn check_crate_inner<F>(cm: &CodeMap, span_handler: &SpanHandler,
665                         krate: &ast::Crate,
666                         check: F)
667                        -> Features
668     where F: FnOnce(&mut Context, &ast::Crate)
669 {
670     let mut cx = Context {
671         features: Vec::new(),
672         span_handler: span_handler,
673         cm: cm,
674     };
675
676     let mut accepted_features = Vec::new();
677     let mut unknown_features = Vec::new();
678
679     for attr in &krate.attrs {
680         if !attr.check_name("feature") {
681             continue
682         }
683
684         match attr.meta_item_list() {
685             None => {
686                 span_handler.span_err(attr.span, "malformed feature attribute, \
687                                                   expected #![feature(...)]");
688             }
689             Some(list) => {
690                 for mi in list {
691                     let name = match mi.node {
692                         ast::MetaWord(ref word) => (*word).clone(),
693                         _ => {
694                             span_handler.span_err(mi.span,
695                                                   "malformed feature, expected just \
696                                                    one word");
697                             continue
698                         }
699                     };
700                     match KNOWN_FEATURES.iter()
701                                         .find(|& &(n, _, _)| name == n) {
702                         Some(&(name, _, Active)) => {
703                             cx.features.push(name);
704                         }
705                         Some(&(_, _, Removed)) => {
706                             span_handler.span_err(mi.span, "feature has been removed");
707                         }
708                         Some(&(_, _, Accepted)) => {
709                             accepted_features.push(mi.span);
710                         }
711                         None => {
712                             unknown_features.push((name, mi.span));
713                         }
714                     }
715                 }
716             }
717         }
718     }
719
720     check(&mut cx, krate);
721
722     // FIXME (pnkfelix): Before adding the 99th entry below, change it
723     // to a single-pass (instead of N calls to `.has_feature`).
724
725     Features {
726         unboxed_closures: cx.has_feature("unboxed_closures"),
727         rustc_diagnostic_macros: cx.has_feature("rustc_diagnostic_macros"),
728         visible_private_types: cx.has_feature("visible_private_types"),
729         allow_quote: cx.has_feature("quote"),
730         allow_asm: cx.has_feature("asm"),
731         allow_log_syntax: cx.has_feature("log_syntax"),
732         allow_concat_idents: cx.has_feature("concat_idents"),
733         allow_trace_macros: cx.has_feature("trace_macros"),
734         allow_internal_unstable: cx.has_feature("allow_internal_unstable"),
735         allow_custom_derive: cx.has_feature("custom_derive"),
736         simd_ffi: cx.has_feature("simd_ffi"),
737         unmarked_api: cx.has_feature("unmarked_api"),
738         negate_unsigned: cx.has_feature("negate_unsigned"),
739         declared_stable_lang_features: accepted_features,
740         declared_lib_features: unknown_features
741     }
742 }
743
744 pub fn check_crate_macros(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::Crate)
745 -> Features {
746     check_crate_inner(cm, span_handler, krate,
747                       |ctx, krate| visit::walk_crate(&mut MacroVisitor { context: ctx }, krate))
748 }
749
750 pub fn check_crate(cm: &CodeMap, span_handler: &SpanHandler, krate: &ast::Crate)
751                    -> Features
752 {
753     check_crate_inner(cm, span_handler, krate,
754                       |ctx, krate| visit::walk_crate(&mut PostExpansionVisitor { context: ctx },
755                                                      krate))
756 }