|
| 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 syntax::ast; |
| 22 | +use syntax::attr::AttrMetaMethods; |
| 23 | +use syntax::codemap::Span; |
| 24 | +use syntax::visit; |
| 25 | +use syntax::visit::Visitor; |
| 26 | + |
| 27 | +use driver::session::Session; |
| 28 | + |
| 29 | +/// This is a list of all known features since the beginning of time. This list |
| 30 | +/// can never shrink, it may only be expanded (in order to prevent old programs |
| 31 | +/// from failing to compile). The status of each feature may change, however. |
| 32 | +static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[ |
| 33 | + ("globs", Active), |
| 34 | + ("macro_rules", Active), |
| 35 | + ("struct_variant", Active), |
| 36 | + |
| 37 | + // These are used to test this portion of the compiler, they don't actually |
| 38 | + // mean anything |
| 39 | + ("test_accepted_feature", Accepted), |
| 40 | + ("test_removed_feature", Removed), |
| 41 | +]; |
| 42 | + |
| 43 | +enum Status { |
| 44 | + /// Represents an active feature that is currently being implemented or |
| 45 | + /// currently being considered for addition/removal. |
| 46 | + Active, |
| 47 | + |
| 48 | + /// Represents a feature which has since been removed (it was once Active) |
| 49 | + Removed, |
| 50 | + |
| 51 | + /// This language feature has since been Accepted (it was once Active) |
| 52 | + Accepted, |
| 53 | +} |
| 54 | + |
| 55 | +struct Context { |
| 56 | + features: ~[&'static str], |
| 57 | + sess: Session, |
| 58 | +} |
| 59 | + |
| 60 | +impl Context { |
| 61 | + fn gate_feature(&self, feature: &str, span: Span, explain: &str) { |
| 62 | + if !self.has_feature(feature) { |
| 63 | + self.sess.span_err(span, explain); |
| 64 | + self.sess.span_note(span, format!("add \\#[feature({})] to the \ |
| 65 | + crate attributes to enable", |
| 66 | + feature)); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + fn has_feature(&self, feature: &str) -> bool { |
| 71 | + self.features.iter().any(|n| n.as_slice() == feature) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +impl Visitor<()> for Context { |
| 76 | + fn visit_view_item(&mut self, i: &ast::view_item, _: ()) { |
| 77 | + match i.node { |
| 78 | + ast::view_item_use(ref paths) => { |
| 79 | + for path in paths.iter() { |
| 80 | + match path.node { |
| 81 | + ast::view_path_glob(*) => { |
| 82 | + self.gate_feature("globs", path.span, |
| 83 | + "glob import statements are \ |
| 84 | + experimental and possibly buggy"); |
| 85 | + } |
| 86 | + _ => {} |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + _ => {} |
| 91 | + } |
| 92 | + visit::walk_view_item(self, i, ()) |
| 93 | + } |
| 94 | + |
| 95 | + fn visit_item(&mut self, i: @ast::item, _:()) { |
| 96 | + match i.node { |
| 97 | + ast::item_enum(ref def, _) => { |
| 98 | + for variant in def.variants.iter() { |
| 99 | + match variant.node.kind { |
| 100 | + ast::struct_variant_kind(*) => { |
| 101 | + self.gate_feature("struct_variant", variant.span, |
| 102 | + "enum struct variants are \ |
| 103 | + experimental and possibly buggy"); |
| 104 | + } |
| 105 | + _ => {} |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + ast::item_mac(ref mac) => { |
| 111 | + match mac.node { |
| 112 | + ast::mac_invoc_tt(ref path, _, _) => { |
| 113 | + let rules = self.sess.ident_of("macro_rules"); |
| 114 | + if path.segments.last().identifier == rules { |
| 115 | + self.gate_feature("macro_rules", i.span, |
| 116 | + "macro definitions are not \ |
| 117 | + stable enough for use and are \ |
| 118 | + subject to change"); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + _ => {} |
| 125 | + } |
| 126 | + |
| 127 | + visit::walk_item(self, i, ()); |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +pub fn check_crate(sess: Session, crate: &ast::Crate) { |
| 132 | + let mut cx = Context { |
| 133 | + features: ~[], |
| 134 | + sess: sess, |
| 135 | + }; |
| 136 | + |
| 137 | + for attr in crate.attrs.iter() { |
| 138 | + if "feature" != attr.name() { continue } |
| 139 | + |
| 140 | + match attr.meta_item_list() { |
| 141 | + None => { |
| 142 | + sess.span_err(attr.span, "malformed feature attribute, \ |
| 143 | + expected #[feature(...)]"); |
| 144 | + } |
| 145 | + Some(list) => { |
| 146 | + for &mi in list.iter() { |
| 147 | + let name = match mi.node { |
| 148 | + ast::MetaWord(word) => word, |
| 149 | + _ => { |
| 150 | + sess.span_err(mi.span, "malformed feature, expected \ |
| 151 | + just one word"); |
| 152 | + continue |
| 153 | + } |
| 154 | + }; |
| 155 | + match KNOWN_FEATURES.iter().find(|& &(n, _)| n == name) { |
| 156 | + Some(&(name, Active)) => { cx.features.push(name); } |
| 157 | + Some(&(_, Removed)) => { |
| 158 | + sess.span_err(mi.span, "feature has been removed"); |
| 159 | + } |
| 160 | + Some(&(_, Accepted)) => { |
| 161 | + sess.span_warn(mi.span, "feature has added to rust, \ |
| 162 | + directive not necessary"); |
| 163 | + } |
| 164 | + None => { |
| 165 | + sess.span_err(mi.span, "unknown feature"); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + visit::walk_crate(&mut cx, crate, ()); |
| 174 | + |
| 175 | + sess.abort_if_errors(); |
| 176 | +} |
0 commit comments