feature_gate.rs 17.0 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.
S
Steven Fackler 已提交
20
use self::Status::*;
21

N
Nick Cameron 已提交
22 23 24 25 26 27 28 29 30 31
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;
32

33
use std::slice;
34

35 36 37 38 39 40
/// 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 已提交
41
    ("struct_variant", Accepted),
L
Léo Testard 已提交
42
    ("asm", Active),
43
    ("managed_boxes", Removed),
44
    ("non_ascii_idents", Active),
D
Daniel Micay 已提交
45
    ("thread_local", Active),
46
    ("link_args", Active),
47
    ("phase", Active),
48
    ("plugin_registrar", Active),
49
    ("log_syntax", Active),
X
xales 已提交
50
    ("trace_macros", Active),
51
    ("concat_idents", Active),
52
    ("unsafe_destructor", Active),
53 54
    ("intrinsics", Active),
    ("lang_items", Active),
55

D
David Manescu 已提交
56
    ("simd", Active),
57
    ("default_type_params", Active),
58
    ("quote", Active),
59
    ("linkage", Active),
60
    ("struct_inherit", Removed),
61
    ("overloaded_calls", 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
                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");
184 185 186 187
            } else if attr.name().equiv(&("linkage")) {
                self.gate_feature("linkage", i.span,
                                  "the `linkage` attribute is experimental \
                                   and not portable across platforms")
D
Daniel Micay 已提交
188 189
            }
        }
190
        match i.node {
191
            ast::ItemForeignMod(ref foreign_module) => {
192
                if attr::contains_name(i.attrs.as_slice(), "link_args") {
193 194 195 196 197
                    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")
                }
198 199 200 201 202
                if foreign_module.abi == RustIntrinsic {
                    self.gate_feature("intrinsics",
                                      i.span,
                                      "intrinsics are subject to change")
                }
203 204
            }

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

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

219
            ast::ItemImpl(_, _, _, ref items) => {
220 221 222 223 224 225 226 227
                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");
                }
228 229 230 231 232 233 234 235 236 237 238 239

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

242 243 244
            _ => {}
        }

245
        visit::walk_item(self, i);
246
    }
247

248 249 250 251 252
    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",
253
                                  ti.ty_param.span,
254 255 256 257 258
                                  "associated types are experimental")
            }
        }
    }

259
    fn visit_mac(&mut self, macro: &ast::Mac) {
260
        let ast::MacInvocTT(ref path, _, _) = macro.node;
261 262
        let id = path.segments.last().unwrap().identifier;

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

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

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

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

283 284 285 286
        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 已提交
287 288
    }

289
    fn visit_foreign_item(&mut self, i: &ast::ForeignItem) {
290 291 292 293
        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")
294
        }
295
        visit::walk_foreign_item(self, i)
296 297
    }

298
    fn visit_ty(&mut self, t: &ast::Ty) {
299
        match t.node {
300 301 302 303
            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);
304 305 306 307
            },
            _ => {}
        }

308
        visit::walk_ty(self, t);
309
    }
310

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

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

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

364
    fn visit_pat(&mut self, pattern: &ast::Pat) {
365 366 367 368 369 370 371 372 373 374
        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")
            }
            _ => {}
        }
375
        visit::walk_pat(self, pattern)
376 377
    }

378
    fn visit_fn(&mut self,
379 380 381
                fn_kind: visit::FnKind<'v>,
                fn_decl: &'v ast::FnDecl,
                block: &'v ast::Block,
382
                span: Span,
383
                _node_id: NodeId) {
384 385
        match fn_kind {
            visit::FkItemFn(_, _, _, abi) if abi == RustIntrinsic => {
386 387 388 389 390 391
                self.gate_feature("intrinsics",
                                  span,
                                  "intrinsics are subject to change")
            }
            _ => {}
        }
392
        visit::walk_fn(self, fn_kind, fn_decl, block, span);
393
    }
394 395 396 397 398 399 400 401 402 403 404 405 406

    fn visit_path_parameters(&mut self, path_span: Span, parameters: &'v ast::PathParameters) {
        match *parameters {
            ast::ParenthesizedParameters(..) => {
                self.gate_feature("unboxed_closures",
                                  path_span,
                                  "parenthetical parameter notation is subject to change");
            }
            ast::AngleBracketedParameters(..) => { }
        }

        visit::walk_path_parameters(self, path_span, parameters)
    }
407 408
}

N
Nick Cameron 已提交
409
pub fn check_crate(span_handler: &SpanHandler, krate: &ast::Crate) -> (Features, Vec<Span>) {
410
    let mut cx = Context {
411
        features: Vec::new(),
N
Nick Cameron 已提交
412
        span_handler: span_handler,
413 414
    };

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

417
    for attr in krate.attrs.iter() {
S
Steven Fackler 已提交
418
        if !attr.check_name("feature") {
419 420
            continue
        }
421 422 423

        match attr.meta_item_list() {
            None => {
N
Nick Cameron 已提交
424 425
                span_handler.span_err(attr.span, "malformed feature attribute, \
                                                  expected #![feature(...)]");
426 427
            }
            Some(list) => {
428
                for mi in list.iter() {
429
                    let name = match mi.node {
430
                        ast::MetaWord(ref word) => (*word).clone(),
431
                        _ => {
N
Nick Cameron 已提交
432 433 434
                            span_handler.span_err(mi.span,
                                                  "malformed feature, expected just \
                                                   one word");
435 436 437
                            continue
                        }
                    };
438 439
                    match KNOWN_FEATURES.iter()
                                        .find(|& &(n, _)| name.equiv(&n)) {
440 441
                        Some(&(name, Active)) => { cx.features.push(name); }
                        Some(&(_, Removed)) => {
N
Nick Cameron 已提交
442
                            span_handler.span_err(mi.span, "feature has been removed");
443 444
                        }
                        Some(&(_, Accepted)) => {
N
Nick Cameron 已提交
445 446
                            span_handler.span_warn(mi.span, "feature has been added to Rust, \
                                                             directive not necessary");
447 448
                        }
                        None => {
N
Nick Cameron 已提交
449
                            unknown_features.push(mi.span);
450 451 452 453 454 455 456
                        }
                    }
                }
            }
        }
    }

457
    visit::walk_crate(&mut cx, krate);
458

N
Nick Cameron 已提交
459 460 461 462 463
    (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"),
464
        visible_private_types: cx.has_feature("visible_private_types"),
465
        quote: cx.has_feature("quote"),
N
Nick Cameron 已提交
466 467
    },
    unknown_features)
468
}
469