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