feature_gate.rs 16.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

S
Steve Klabnik 已提交
35
// if you change this list without updating src/doc/reference.md, @cmr will be sad
36 37 38
static KNOWN_FEATURES: &'static [(&'static str, Status)] = &[
    ("globs", Active),
    ("macro_rules", Active),
S
Steven Fackler 已提交
39
    ("struct_variant", Accepted),
L
Léo Testard 已提交
40
    ("asm", Active),
41
    ("managed_boxes", Removed),
42
    ("non_ascii_idents", Active),
D
Daniel Micay 已提交
43
    ("thread_local", Active),
44
    ("link_args", Active),
45
    ("phase", Active),
46
    ("plugin_registrar", Active),
47
    ("log_syntax", Active),
X
xales 已提交
48
    ("trace_macros", Active),
49
    ("concat_idents", Active),
50
    ("unsafe_destructor", Active),
51 52
    ("intrinsics", Active),
    ("lang_items", Active),
53

D
David Manescu 已提交
54
    ("simd", Active),
55
    ("default_type_params", Active),
56
    ("quote", Active),
57
    ("linkage", Active),
58
    ("struct_inherit", Removed),
59

60
    ("quad_precision_float", Removed),
61

62
    ("rustc_diagnostic_macros", Active),
63
    ("unboxed_closures", Active),
64
    ("import_shadowing", Active),
65
    ("advanced_slice_patterns", Active),
66
    ("tuple_indexing", Accepted),
67
    ("associated_types", Active),
68
    ("visible_private_types", Active),
69
    ("slicing_syntax", Active),
70

71 72
    ("if_let", Accepted),
    ("while_let", Accepted),
73

74 75
    // A temporary feature gate used to enable parser extensions needed
    // to bootstrap fix for #5723.
76
    ("issue_5723_bootstrap", Accepted),
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    // 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,
}

96 97
/// A set of features to be used by later passes.
pub struct Features {
N
Nick Cameron 已提交
98
    pub default_type_params: bool,
99
    pub unboxed_closures: bool,
N
Nick Cameron 已提交
100 101
    pub rustc_diagnostic_macros: bool,
    pub import_shadowing: bool,
102
    pub visible_private_types: bool,
103
    pub quote: bool,
104 105 106 107 108
}

impl Features {
    pub fn new() -> Features {
        Features {
N
Nick Cameron 已提交
109
            default_type_params: false,
110
            unboxed_closures: false,
N
Nick Cameron 已提交
111 112
            rustc_diagnostic_macros: false,
            import_shadowing: false,
113
            visible_private_types: false,
114
            quote: false,
115 116 117 118
        }
    }
}

E
Eduard Burtescu 已提交
119 120
struct Context<'a> {
    features: Vec<&'static str>,
N
Nick Cameron 已提交
121
    span_handler: &'a SpanHandler,
122 123
}

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

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

139
impl<'a, 'v> Visitor<'v> for Context<'a> {
140 141
    fn visit_name(&mut self, sp: Span, name: ast::Name) {
        if !token::get_name(name).get().is_ascii() {
142 143 144 145 146
            self.gate_feature("non_ascii_idents", sp,
                              "non-ascii idents are not fully supported.");
        }
    }

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

169
    fn visit_item(&mut self, i: &ast::Item) {
D
Daniel Micay 已提交
170
        for attr in i.attrs.iter() {
171
            if attr.name() == "thread_local" {
D
Daniel Micay 已提交
172 173 174 175
                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");
176
            } else if attr.name() == "linkage" {
177 178 179
                self.gate_feature("linkage", i.span,
                                  "the `linkage` attribute is experimental \
                                   and not portable across platforms")
D
Daniel Micay 已提交
180 181
            }
        }
182
        match i.node {
183
            ast::ItemForeignMod(ref foreign_module) => {
184
                if attr::contains_name(i.attrs.as_slice(), "link_args") {
185 186 187 188 189
                    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")
                }
190 191 192 193 194
                if foreign_module.abi == RustIntrinsic {
                    self.gate_feature("intrinsics",
                                      i.span,
                                      "intrinsics are subject to change")
                }
195 196
            }

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

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

211
            ast::ItemImpl(_, _, _, ref items) => {
212 213 214 215 216 217 218 219
                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");
                }
220 221 222 223 224 225 226 227 228 229 230 231

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

234 235 236
            _ => {}
        }

237
        visit::walk_item(self, i);
238
    }
239

240 241 242 243 244
    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",
245
                                  ti.ty_param.span,
246 247 248 249 250
                                  "associated types are experimental")
            }
        }
    }

251
    fn visit_mac(&mut self, macro: &ast::Mac) {
252
        let ast::MacInvocTT(ref path, _, _) = macro.node;
253 254
        let id = path.segments.last().unwrap().identifier;

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

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

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

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

275 276 277 278
        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 已提交
279 280
    }

281
    fn visit_foreign_item(&mut self, i: &ast::ForeignItem) {
282 283 284 285
        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")
286
        }
287
        visit::walk_foreign_item(self, i)
288 289
    }

290
    fn visit_ty(&mut self, t: &ast::Ty) {
291 292 293 294
        if let ast::TyClosure(ref closure) =  t.node {
            // this used to be blocked by a feature gate, but it should just
            // be plain impossible right now
            assert!(closure.onceness != ast::Once);
295 296
        }

297
        visit::walk_ty(self, t);
298
    }
299

300
    fn visit_expr(&mut self, e: &ast::Expr) {
301
        match e.node {
302
            ast::ExprClosure(_, Some(_), _, _) => {
303 304 305 306 307
                self.gate_feature("unboxed_closures",
                                  e.span,
                                  "unboxed closures are a work-in-progress \
                                   feature with known bugs");
            }
308 309 310 311 312
            ast::ExprSlice(..) => {
                self.gate_feature("slicing_syntax",
                                  e.span,
                                  "slicing syntax is experimental");
            }
313 314
            _ => {}
        }
315
        visit::walk_expr(self, e);
316
    }
317

318
    fn visit_generics(&mut self, generics: &ast::Generics) {
319 320
        for type_parameter in generics.ty_params.iter() {
            match type_parameter.default {
321
                Some(ref ty) => {
322 323 324 325 326 327 328
                    self.gate_feature("default_type_params", ty.span,
                                      "default type parameters are \
                                       experimental and possibly buggy");
                }
                None => {}
            }
        }
329
        visit::walk_generics(self, generics);
330
    }
331

332
    fn visit_attribute(&mut self, attr: &ast::Attribute) {
333
        if attr::contains_name(slice::ref_slice(attr), "lang") {
334 335 336 337 338 339
            self.gate_feature("lang_items",
                              attr.span,
                              "language items are subject to change");
        }
    }

340
    fn visit_pat(&mut self, pattern: &ast::Pat) {
341 342 343 344 345 346 347 348 349 350
        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")
            }
            _ => {}
        }
351
        visit::walk_pat(self, pattern)
352 353
    }

354
    fn visit_fn(&mut self,
355 356 357
                fn_kind: visit::FnKind<'v>,
                fn_decl: &'v ast::FnDecl,
                block: &'v ast::Block,
358
                span: Span,
359
                _node_id: NodeId) {
360 361
        match fn_kind {
            visit::FkItemFn(_, _, _, abi) if abi == RustIntrinsic => {
362 363 364 365 366 367
                self.gate_feature("intrinsics",
                                  span,
                                  "intrinsics are subject to change")
            }
            _ => {}
        }
368
        visit::walk_fn(self, fn_kind, fn_decl, block, span);
369
    }
370 371 372 373 374 375 376 377 378 379 380 381 382

    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)
    }
383 384
}

N
Nick Cameron 已提交
385
pub fn check_crate(span_handler: &SpanHandler, krate: &ast::Crate) -> (Features, Vec<Span>) {
386
    let mut cx = Context {
387
        features: Vec::new(),
N
Nick Cameron 已提交
388
        span_handler: span_handler,
389 390
    };

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

393
    for attr in krate.attrs.iter() {
S
Steven Fackler 已提交
394
        if !attr.check_name("feature") {
395 396
            continue
        }
397 398 399

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

433
    visit::walk_crate(&mut cx, krate);
434

N
Nick Cameron 已提交
435 436
    (Features {
        default_type_params: cx.has_feature("default_type_params"),
437
        unboxed_closures: cx.has_feature("unboxed_closures"),
N
Nick Cameron 已提交
438 439
        rustc_diagnostic_macros: cx.has_feature("rustc_diagnostic_macros"),
        import_shadowing: cx.has_feature("import_shadowing"),
440
        visible_private_types: cx.has_feature("visible_private_types"),
441
        quote: cx.has_feature("quote"),
N
Nick Cameron 已提交
442 443
    },
    unknown_features)
444
}