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.
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.
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
18 //! Features are enabled in programs via the crate-level attributes of
19 //! #[feature(...)] with a comma-separated list of features.
25 use syntax::attr::AttrMetaMethods;
26 use syntax::codemap::Span;
28 use syntax::visit::Visitor;
29 use syntax::parse::token;
31 use driver::session::Session;
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)] = &[
38 ("macro_rules", Active),
39 ("struct_variant", Active),
42 ("managed_boxes", Active),
43 ("non_ascii_idents", Active),
44 ("thread_local", Active),
45 ("link_args", Active),
47 ("macro_registrar", Active),
48 ("log_syntax", Active),
49 ("trace_macros", Active),
51 ("default_type_params", Active),
54 // These are used to test this portion of the compiler, they don't actually
56 ("test_accepted_feature", Accepted),
57 ("test_removed_feature", Removed),
61 /// Represents an active feature that is currently being implemented or
62 /// currently being considered for addition/removal.
65 /// Represents a feature which has since been removed (it was once Active)
68 /// This language feature has since been Accepted (it was once Active)
73 features: ~[&'static str],
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",
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.");
95 fn has_feature(&self, feature: &str) -> bool {
96 self.features.iter().any(|n| n.as_slice() == feature)
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.");
108 fn visit_view_item(&mut self, i: &ast::ViewItem, _: ()) {
110 ast::ViewItemUse(ref paths) => {
111 for path in paths.iter() {
113 ast::ViewPathGlob(..) => {
114 self.gate_feature("globs", path.span,
115 "glob import statements are \
116 experimental and possibly buggy");
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");
132 visit::walk_view_item(self, i, ())
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");
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");
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")
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");
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");
185 visit::walk_item(self, i, ());
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";
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");
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");
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");
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");
217 for "e in quotes.iter() {
218 if id == token::str_to_ident(quote) {
219 self.gate_feature("quote", path.span, quote + msg);
225 fn visit_ty(&mut self, t: &ast::Ty, _: ()) {
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");
234 ast::TyBox(_) => { self.gate_box(t.span); }
238 visit::walk_ty(self, t, ());
241 fn visit_expr(&mut self, e: &ast::Expr, _: ()) {
243 ast::ExprUnary(_, ast::UnBox, _) => {
244 self.gate_box(e.span);
248 visit::walk_expr(self, e, ());
251 fn visit_generics(&mut self, generics: &ast::Generics, _: ()) {
252 for type_parameter in generics.ty_params.iter() {
253 match type_parameter.default {
255 self.gate_feature("default_type_params", ty.span,
256 "default type parameters are \
257 experimental and possibly buggy");
262 visit::walk_generics(self, generics, ());
266 pub fn check_crate(sess: Session, krate: &ast::Crate) {
267 let mut cx = Context {
272 for attr in krate.attrs.iter() {
273 if !attr.name().equiv(&("feature")) {
277 match attr.meta_item_list() {
279 sess.span_err(attr.span, "malformed feature attribute, \
280 expected #[feature(...)]");
283 for &mi in list.iter() {
284 let name = match mi.node {
285 ast::MetaWord(ref word) => (*word).clone(),
287 sess.span_err(mi.span,
288 "malformed feature, expected just \
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");
299 Some(&(_, Accepted)) => {
300 sess.span_warn(mi.span, "feature has added to rust, \
301 directive not necessary");
304 sess.add_lint(lint::UnknownFeatures,
315 visit::walk_crate(&mut cx, krate, ());
317 sess.abort_if_errors();