feature_gate.rs 16.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Feature gating
//!
//! This modules implements the gating necessary for preventing certain compiler
//! features from being used by default. This module will crawl a pre-expanded
//! AST to ensure that there are no features which are used that are not
//! enabled.
//!
//! Features are enabled in programs via the crate-level attributes of
19
//! `#![feature(...)]` with a comma-separated list of features.
20

N
Nick Cameron 已提交
21 22 23 24 25 26 27 28 29 30
use abi::RustIntrinsic;
use ast::NodeId;
use ast;
use attr;
use attr::AttrMetaMethods;
use codemap::Span;
use diagnostic::SpanHandler;
use visit;
use visit::Visitor;
use parse::token;
31

32
use std::slice;
33

34 35 36 37 38 39
/// This is a list of all known features since the beginning of time. This list
/// can never shrink, it may only be expanded (in order to prevent old programs
/// from failing to compile). The status of each feature may change, however.
static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[
    ("globs", Active),
    ("macro_rules", Active),
S
Steven Fackler 已提交
40
    ("struct_variant", Accepted),
L
Léo Testard 已提交
41
    ("asm", Active),
42
    ("managed_boxes", Removed),
43
    ("non_ascii_idents", Active),
D
Daniel Micay 已提交
44
    ("thread_local", Active),
45
    ("link_args", Active),
46
    ("phase", Active),
47
    ("plugin_registrar", Active),
48
    ("log_syntax", Active),
X
xales 已提交
49
    ("trace_macros", Active),
50
    ("concat_idents", Active),
51
    ("unsafe_destructor", Active),
52 53
    ("intrinsics", Active),
    ("lang_items", Active),
54

D
David Manescu 已提交
55
    ("simd", Active),
56
    ("default_type_params", Active),
57
    ("quote", Active),
58
    ("linkage", Active),
59
    ("struct_inherit", Removed),
60
    ("overloaded_calls", Active),
61
    ("unboxed_closure_sugar", Active),
62

63
    ("quad_precision_float", Removed),
64

65
    ("rustc_diagnostic_macros", Active),
66
    ("unboxed_closures", Active),
67
    ("import_shadowing", Active),
68
    ("advanced_slice_patterns", Active),
69
    ("tuple_indexing", Active),
70
    ("associated_types", Active),
71
    ("visible_private_types", Active),
72
    ("slicing_syntax", Active),
73

74
    ("if_let", Active),
75
    ("while_let", Active),
76

77
    // if you change this list without updating src/doc/reference.md, cmr will be sad
78

79 80
    // A temporary feature gate used to enable parser extensions needed
    // to bootstrap fix for #5723.
81
    ("issue_5723_bootstrap", Accepted),
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
    // These are used to test this portion of the compiler, they don't actually
    // mean anything
    ("test_accepted_feature", Accepted),
    ("test_removed_feature", Removed),
];

enum Status {
    /// Represents an active feature that is currently being implemented or
    /// currently being considered for addition/removal.
    Active,

    /// Represents a feature which has since been removed (it was once Active)
    Removed,

    /// This language feature has since been Accepted (it was once Active)
    Accepted,
}

101 102
/// A set of features to be used by later passes.
pub struct Features {
N
Nick Cameron 已提交
103 104 105 106
    pub default_type_params: bool,
    pub overloaded_calls: bool,
    pub rustc_diagnostic_macros: bool,
    pub import_shadowing: bool,
107
    pub visible_private_types: bool,
108
    pub quote: bool,
109 110 111 112 113
}

impl Features {
    pub fn new() -> Features {
        Features {
N
Nick Cameron 已提交
114 115 116 117
            default_type_params: false,
            overloaded_calls: false,
            rustc_diagnostic_macros: false,
            import_shadowing: false,
118
            visible_private_types: false,
119
            quote: false,
120 121 122 123
        }
    }
}

E
Eduard Burtescu 已提交
124 125
struct Context<'a> {
    features: Vec<&'static str>,
N
Nick Cameron 已提交
126
    span_handler: &'a SpanHandler,
127 128
}

E
Eduard Burtescu 已提交
129
impl<'a> Context<'a> {
130 131
    fn gate_feature(&self, feature: &str, span: Span, explain: &str) {
        if !self.has_feature(feature) {
N
Nick Cameron 已提交
132
            self.span_handler.span_err(span, explain);
P
P1start 已提交
133
            self.span_handler.span_help(span, format!("add #![feature({})] to the \
N
Nick Cameron 已提交
134 135
                                                       crate attributes to enable",
                                                      feature).as_slice());
136 137
        }
    }
138 139 140 141 142 143

    fn has_feature(&self, feature: &str) -> bool {
        self.features.iter().any(|n| n.as_slice() == feature)
    }
}

144
impl<'a, 'v> Visitor<'v> for Context<'a> {
145
    fn visit_ident(&mut self, sp: Span, id: ast::Ident) {
146
        if !token::get_ident(id).get().is_ascii() {
147 148 149 150 151
            self.gate_feature("non_ascii_idents", sp,
                              "non-ascii idents are not fully supported.");
        }
    }

152
    fn visit_view_item(&mut self, i: &ast::ViewItem) {
153
        match i.node {
154 155 156 157 158 159
            ast::ViewItemUse(ref path) => {
                match path.node {
                    ast::ViewPathGlob(..) => {
                        self.gate_feature("globs", path.span,
                                          "glob import statements are \
                                           experimental and possibly buggy");
160
                    }
161
                    _ => {}
162 163
                }
            }
164
            ast::ViewItemExternCrate(..) => {
165
                for attr in i.attrs.iter() {
P
Patrick Walton 已提交
166
                    if attr.name().get() == "phase"{
167 168 169 170 171 172
                        self.gate_feature("phase", attr.span,
                                          "compile time crate loading is \
                                           experimental and possibly buggy");
                    }
                }
            }
173
        }
174
        visit::walk_view_item(self, i)
175 176
    }

177
    fn visit_item(&mut self, i: &ast::Item) {
D
Daniel Micay 已提交
178
        for attr in i.attrs.iter() {
179
            if attr.name().equiv(&("thread_local")) {
D
Daniel Micay 已提交
180 181 182 183 184 185
                self.gate_feature("thread_local", i.span,
                                  "`#[thread_local]` is an experimental feature, and does not \
                                  currently handle destructors. There is no corresponding \
                                  `#[task_local]` mapping to the task model");
            }
        }
186
        match i.node {
187
            ast::ItemForeignMod(ref foreign_module) => {
188
                if attr::contains_name(i.attrs.as_slice(), "link_args") {
189 190 191 192 193
                    self.gate_feature("link_args", i.span,
                                      "the `link_args` attribute is not portable \
                                       across platforms, it is recommended to \
                                       use `#[link(name = \"foo\")]` instead")
                }
194 195 196 197 198
                if foreign_module.abi == RustIntrinsic {
                    self.gate_feature("intrinsics",
                                      i.span,
                                      "intrinsics are subject to change")
                }
199 200
            }

201
            ast::ItemFn(..) => {
202 203 204
                if attr::contains_name(i.attrs.as_slice(), "plugin_registrar") {
                    self.gate_feature("plugin_registrar", i.span,
                                      "compiler plugins are experimental and possibly buggy");
205 206 207
                }
            }

208
            ast::ItemStruct(..) => {
209
                if attr::contains_name(i.attrs.as_slice(), "simd") {
D
David Manescu 已提交
210 211
                    self.gate_feature("simd", i.span,
                                      "SIMD types are experimental and possibly buggy");
212
                }
D
David Manescu 已提交
213 214
            }

215
            ast::ItemImpl(_, _, _, ref items) => {
216 217 218 219 220 221 222 223
                if attr::contains_name(i.attrs.as_slice(),
                                       "unsafe_destructor") {
                    self.gate_feature("unsafe_destructor",
                                      i.span,
                                      "`#[unsafe_destructor]` allows too \
                                       many unsafe patterns and may be \
                                       removed in the future");
                }
224 225 226 227 228 229 230 231 232 233 234 235

                for item in items.iter() {
                    match *item {
                        ast::MethodImplItem(_) => {}
                        ast::TypeImplItem(ref typedef) => {
                            self.gate_feature("associated_types",
                                              typedef.span,
                                              "associated types are \
                                               experimental")
                        }
                    }
                }
236 237
            }

238 239 240
            _ => {}
        }

241
        visit::walk_item(self, i);
242
    }
243

244 245 246 247 248
    fn visit_trait_item(&mut self, trait_item: &ast::TraitItem) {
        match *trait_item {
            ast::RequiredMethod(_) | ast::ProvidedMethod(_) => {}
            ast::TypeTraitItem(ref ti) => {
                self.gate_feature("associated_types",
249
                                  ti.ty_param.span,
250 251 252 253 254
                                  "associated types are experimental")
            }
        }
    }

255
    fn visit_mac(&mut self, macro: &ast::Mac) {
256
        let ast::MacInvocTT(ref path, _, _) = macro.node;
257 258
        let id = path.segments.last().unwrap().identifier;

259
        if id == token::str_to_ident("macro_rules") {
L
Léo Testard 已提交
260 261 262 263
            self.gate_feature("macro_rules", path.span, "macro definitions are \
                not stable enough for use and are subject to change");
        }

264
        else if id == token::str_to_ident("asm") {
265 266
            self.gate_feature("asm", path.span, "inline assembly is not \
                stable enough for use and is subject to change");
L
Léo Testard 已提交
267
        }
268

269
        else if id == token::str_to_ident("log_syntax") {
270 271 272
            self.gate_feature("log_syntax", path.span, "`log_syntax!` is not \
                stable enough for use and is subject to change");
        }
273

274
        else if id == token::str_to_ident("trace_macros") {
X
xales 已提交
275 276
            self.gate_feature("trace_macros", path.span, "`trace_macros` is not \
                stable enough for use and is subject to change");
277 278
        }

279 280 281 282
        else if id == token::str_to_ident("concat_idents") {
            self.gate_feature("concat_idents", path.span, "`concat_idents` is not \
                stable enough for use and is subject to change");
        }
L
Léo Testard 已提交
283 284
    }

285
    fn visit_foreign_item(&mut self, i: &ast::ForeignItem) {
286 287 288 289
        if attr::contains_name(i.attrs.as_slice(), "linkage") {
            self.gate_feature("linkage", i.span,
                              "the `linkage` attribute is experimental \
                               and not portable across platforms")
290
        }
291
        visit::walk_foreign_item(self, i)
292 293
    }

294
    fn visit_ty(&mut self, t: &ast::Ty) {
295
        match t.node {
296 297 298 299
            ast::TyClosure(ref closure) => {
                // this used to be blocked by a feature gate, but it should just
                // be plain impossible right now
                assert!(closure.onceness != ast::Once);
300 301 302 303
            },
            _ => {}
        }

304
        visit::walk_ty(self, t);
305
    }
306

307
    fn visit_expr(&mut self, e: &ast::Expr) {
308
        match e.node {
309 310 311 312 313 314
            ast::ExprUnboxedFn(..) => {
                self.gate_feature("unboxed_closures",
                                  e.span,
                                  "unboxed closures are a work-in-progress \
                                   feature with known bugs");
            }
315 316 317 318 319
            ast::ExprTupField(..) => {
                self.gate_feature("tuple_indexing",
                                  e.span,
                                  "tuple indexing is experimental");
            }
320 321
            ast::ExprIfLet(..) => {
                self.gate_feature("if_let", e.span,
K
Kevin Ballard 已提交
322
                                  "`if let` syntax is experimental");
323
            }
324 325 326 327 328
            ast::ExprSlice(..) => {
                self.gate_feature("slicing_syntax",
                                  e.span,
                                  "slicing syntax is experimental");
            }
329 330 331 332
            ast::ExprWhileLet(..) => {
                self.gate_feature("while_let", e.span,
                                  "`while let` syntax is experimental");
            }
333 334
            _ => {}
        }
335
        visit::walk_expr(self, e);
336
    }
337

338
    fn visit_generics(&mut self, generics: &ast::Generics) {
339 340
        for type_parameter in generics.ty_params.iter() {
            match type_parameter.default {
341
                Some(ref ty) => {
342 343 344 345 346 347 348
                    self.gate_feature("default_type_params", ty.span,
                                      "default type parameters are \
                                       experimental and possibly buggy");
                }
                None => {}
            }
        }
349
        visit::walk_generics(self, generics);
350
    }
351

352
    fn visit_attribute(&mut self, attr: &ast::Attribute) {
353
        if attr::contains_name(slice::ref_slice(attr), "lang") {
354 355 356 357 358 359
            self.gate_feature("lang_items",
                              attr.span,
                              "language items are subject to change");
        }
    }

360
    fn visit_pat(&mut self, pattern: &ast::Pat) {
361 362 363 364 365 366 367 368 369 370
        match pattern.node {
            ast::PatVec(_, Some(_), ref last) if !last.is_empty() => {
                self.gate_feature("advanced_slice_patterns",
                                  pattern.span,
                                  "multiple-element slice matches anywhere \
                                   but at the end of a slice (e.g. \
                                   `[0, ..xs, 0]` are experimental")
            }
            _ => {}
        }
371
        visit::walk_pat(self, pattern)
372 373
    }

374
    fn visit_fn(&mut self,
375 376 377
                fn_kind: visit::FnKind<'v>,
                fn_decl: &'v ast::FnDecl,
                block: &'v ast::Block,
378
                span: Span,
379
                _: NodeId) {
380 381
        match fn_kind {
            visit::FkItemFn(_, _, _, abi) if abi == RustIntrinsic => {
382 383 384 385 386 387
                self.gate_feature("intrinsics",
                                  span,
                                  "intrinsics are subject to change")
            }
            _ => {}
        }
388
        visit::walk_fn(self, fn_kind, fn_decl, block, span);
389
    }
390 391
}

N
Nick Cameron 已提交
392
pub fn check_crate(span_handler: &SpanHandler, krate: &ast::Crate) -> (Features, Vec<Span>) {
393
    let mut cx = Context {
394
        features: Vec::new(),
N
Nick Cameron 已提交
395
        span_handler: span_handler,
396 397
    };

N
Nick Cameron 已提交
398 399
    let mut unknown_features = Vec::new();

400
    for attr in krate.attrs.iter() {
S
Steven Fackler 已提交
401
        if !attr.check_name("feature") {
402 403
            continue
        }
404 405 406

        match attr.meta_item_list() {
            None => {
N
Nick Cameron 已提交
407 408
                span_handler.span_err(attr.span, "malformed feature attribute, \
                                                  expected #![feature(...)]");
409 410
            }
            Some(list) => {
411
                for mi in list.iter() {
412
                    let name = match mi.node {
413
                        ast::MetaWord(ref word) => (*word).clone(),
414
                        _ => {
N
Nick Cameron 已提交
415 416 417
                            span_handler.span_err(mi.span,
                                                  "malformed feature, expected just \
                                                   one word");
418 419 420
                            continue
                        }
                    };
421 422
                    match KNOWN_FEATURES.iter()
                                        .find(|& &(n, _)| name.equiv(&n)) {
423 424
                        Some(&(name, Active)) => { cx.features.push(name); }
                        Some(&(_, Removed)) => {
N
Nick Cameron 已提交
425
                            span_handler.span_err(mi.span, "feature has been removed");
426 427
                        }
                        Some(&(_, Accepted)) => {
N
Nick Cameron 已提交
428 429
                            span_handler.span_warn(mi.span, "feature has been added to Rust, \
                                                             directive not necessary");
430 431
                        }
                        None => {
N
Nick Cameron 已提交
432
                            unknown_features.push(mi.span);
433 434 435 436 437 438 439
                        }
                    }
                }
            }
        }
    }

440
    visit::walk_crate(&mut cx, krate);
441

N
Nick Cameron 已提交
442 443 444 445 446
    (Features {
        default_type_params: cx.has_feature("default_type_params"),
        overloaded_calls: cx.has_feature("overloaded_calls"),
        rustc_diagnostic_macros: cx.has_feature("rustc_diagnostic_macros"),
        import_shadowing: cx.has_feature("import_shadowing"),
447
        visible_private_types: cx.has_feature("visible_private_types"),
448
        quote: cx.has_feature("quote"),
N
Nick Cameron 已提交
449 450
    },
    unknown_features)
451
}
452