]> git.proxmox.com Git - rustc.git/blob - src/libsyntax/feature_gate.rs
New upstream version 1.25.0+dfsg1
[rustc.git] / src / libsyntax / feature_gate.rs
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 module 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 //! For the purpose of future feature-tracking, once code for detection of feature
22 //! gate usage is added, *do not remove it again* even once the feature
23 //! becomes stable.
24
25 use self::AttributeType::*;
26 use self::AttributeGate::*;
27
28 use abi::Abi;
29 use ast::{self, NodeId, PatKind, RangeEnd, RangeSyntax};
30 use attr;
31 use codemap::Spanned;
32 use syntax_pos::Span;
33 use errors::{DiagnosticBuilder, Handler, FatalError};
34 use visit::{self, FnKind, Visitor};
35 use parse::ParseSess;
36 use symbol::{keywords, Symbol};
37
38 use std::{env, path};
39
40 macro_rules! set {
41 (proc_macro) => {{
42 fn f(features: &mut Features, span: Span) {
43 features.declared_lib_features.push((Symbol::intern("proc_macro"), span));
44 features.proc_macro = true;
45 }
46 f as fn(&mut Features, Span)
47 }};
48 ($field: ident) => {{
49 fn f(features: &mut Features, _: Span) {
50 features.$field = true;
51 }
52 f as fn(&mut Features, Span)
53 }}
54 }
55
56 macro_rules! declare_features {
57 ($((active, $feature: ident, $ver: expr, $issue: expr),)+) => {
58 /// Represents active features that are currently being implemented or
59 /// currently being considered for addition/removal.
60 const ACTIVE_FEATURES:
61 &'static [(&'static str, &'static str, Option<u32>, fn(&mut Features, Span))] =
62 &[$((stringify!($feature), $ver, $issue, set!($feature))),+];
63
64 /// A set of features to be used by later passes.
65 pub struct Features {
66 /// `#![feature]` attrs for stable language features, for error reporting
67 pub declared_stable_lang_features: Vec<(Symbol, Span)>,
68 /// `#![feature]` attrs for non-language (library) features
69 pub declared_lib_features: Vec<(Symbol, Span)>,
70 $(pub $feature: bool),+
71 }
72
73 impl Features {
74 pub fn new() -> Features {
75 Features {
76 declared_stable_lang_features: Vec::new(),
77 declared_lib_features: Vec::new(),
78 $($feature: false),+
79 }
80 }
81 }
82 };
83
84 ($((removed, $feature: ident, $ver: expr, $issue: expr),)+) => {
85 /// Represents unstable features which have since been removed (it was once Active)
86 const REMOVED_FEATURES: &'static [(&'static str, &'static str, Option<u32>)] = &[
87 $((stringify!($feature), $ver, $issue)),+
88 ];
89 };
90
91 ($((stable_removed, $feature: ident, $ver: expr, $issue: expr),)+) => {
92 /// Represents stable features which have since been removed (it was once Accepted)
93 const STABLE_REMOVED_FEATURES: &'static [(&'static str, &'static str, Option<u32>)] = &[
94 $((stringify!($feature), $ver, $issue)),+
95 ];
96 };
97
98 ($((accepted, $feature: ident, $ver: expr, $issue: expr),)+) => {
99 /// Those language feature has since been Accepted (it was once Active)
100 const ACCEPTED_FEATURES: &'static [(&'static str, &'static str, Option<u32>)] = &[
101 $((stringify!($feature), $ver, $issue)),+
102 ];
103 }
104 }
105
106 // If you change this, please modify src/doc/unstable-book as well.
107 //
108 // Don't ever remove anything from this list; set them to 'Removed'.
109 //
110 // The version numbers here correspond to the version in which the current status
111 // was set. This is most important for knowing when a particular feature became
112 // stable (active).
113 //
114 // NB: tools/tidy/src/features.rs parses this information directly out of the
115 // source, so take care when modifying it.
116
117 declare_features! (
118 (active, asm, "1.0.0", Some(29722)),
119 (active, concat_idents, "1.0.0", Some(29599)),
120 (active, link_args, "1.0.0", Some(29596)),
121 (active, log_syntax, "1.0.0", Some(29598)),
122 (active, non_ascii_idents, "1.0.0", Some(28979)),
123 (active, plugin_registrar, "1.0.0", Some(29597)),
124 (active, thread_local, "1.0.0", Some(29594)),
125 (active, trace_macros, "1.0.0", Some(29598)),
126
127 // rustc internal, for now:
128 (active, intrinsics, "1.0.0", None),
129 (active, lang_items, "1.0.0", None),
130
131 (active, link_llvm_intrinsics, "1.0.0", Some(29602)),
132 (active, linkage, "1.0.0", Some(29603)),
133 (active, quote, "1.0.0", Some(29601)),
134
135
136 // rustc internal
137 (active, rustc_diagnostic_macros, "1.0.0", None),
138 (active, rustc_const_unstable, "1.0.0", None),
139 (active, advanced_slice_patterns, "1.0.0", Some(23121)),
140 (active, box_syntax, "1.0.0", Some(27779)),
141 (active, placement_in_syntax, "1.0.0", Some(27779)),
142 (active, unboxed_closures, "1.0.0", Some(29625)),
143
144 (active, fundamental, "1.0.0", Some(29635)),
145 (active, main, "1.0.0", Some(29634)),
146 (active, needs_allocator, "1.4.0", Some(27389)),
147 (active, on_unimplemented, "1.0.0", Some(29628)),
148 (active, plugin, "1.0.0", Some(29597)),
149 (active, simd_ffi, "1.0.0", Some(27731)),
150 (active, start, "1.0.0", Some(29633)),
151 (active, structural_match, "1.8.0", Some(31434)),
152 (active, panic_runtime, "1.10.0", Some(32837)),
153 (active, needs_panic_runtime, "1.10.0", Some(32837)),
154
155 // OIBIT specific features
156 (active, optin_builtin_traits, "1.0.0", Some(13231)),
157
158 // macro re-export needs more discussion and stabilization
159 (active, macro_reexport, "1.0.0", Some(29638)),
160
161 // Allows use of #[staged_api]
162 // rustc internal
163 (active, staged_api, "1.0.0", None),
164
165 // Allows using #![no_core]
166 (active, no_core, "1.3.0", Some(29639)),
167
168 // Allows using `box` in patterns; RFC 469
169 (active, box_patterns, "1.0.0", Some(29641)),
170
171 // Allows using the unsafe_destructor_blind_to_params attribute;
172 // RFC 1238
173 (active, dropck_parametricity, "1.3.0", Some(28498)),
174
175 // Allows using the may_dangle attribute; RFC 1327
176 (active, dropck_eyepatch, "1.10.0", Some(34761)),
177
178 // Allows the use of custom attributes; RFC 572
179 (active, custom_attribute, "1.0.0", Some(29642)),
180
181 // Allows the use of #[derive(Anything)] as sugar for
182 // #[derive_Anything].
183 (active, custom_derive, "1.0.0", Some(29644)),
184
185 // Allows the use of rustc_* attributes; RFC 572
186 (active, rustc_attrs, "1.0.0", Some(29642)),
187
188 // Allows the use of non lexical lifetimes; RFC 2094
189 (active, nll, "1.0.0", Some(44928)),
190
191 // Allows the use of #[allow_internal_unstable]. This is an
192 // attribute on macro_rules! and can't use the attribute handling
193 // below (it has to be checked before expansion possibly makes
194 // macros disappear).
195 //
196 // rustc internal
197 (active, allow_internal_unstable, "1.0.0", None),
198
199 // Allows the use of #[allow_internal_unsafe]. This is an
200 // attribute on macro_rules! and can't use the attribute handling
201 // below (it has to be checked before expansion possibly makes
202 // macros disappear).
203 //
204 // rustc internal
205 (active, allow_internal_unsafe, "1.0.0", None),
206
207 // #23121. Array patterns have some hazards yet.
208 (active, slice_patterns, "1.0.0", Some(23121)),
209
210 // Allows the definition of `const fn` functions.
211 (active, const_fn, "1.2.0", Some(24111)),
212
213 // Allows indexing into constant arrays.
214 (active, const_indexing, "1.4.0", Some(29947)),
215
216 // Allows using #[prelude_import] on glob `use` items.
217 //
218 // rustc internal
219 (active, prelude_import, "1.2.0", None),
220
221 // Allows default type parameters to influence type inference.
222 (active, default_type_parameter_fallback, "1.3.0", Some(27336)),
223
224 // Allows associated type defaults
225 (active, associated_type_defaults, "1.2.0", Some(29661)),
226
227 // allow `repr(simd)`, and importing the various simd intrinsics
228 (active, repr_simd, "1.4.0", Some(27731)),
229
230 // Allows cfg(target_feature = "...").
231 (active, cfg_target_feature, "1.4.0", Some(29717)),
232
233 // allow `extern "platform-intrinsic" { ... }`
234 (active, platform_intrinsics, "1.4.0", Some(27731)),
235
236 // allow `#[unwind]`
237 // rust runtime internal
238 (active, unwind_attributes, "1.4.0", None),
239
240 // allow the use of `#[naked]` on functions.
241 (active, naked_functions, "1.9.0", Some(32408)),
242
243 // allow `#[no_debug]`
244 (active, no_debug, "1.5.0", Some(29721)),
245
246 // allow `#[omit_gdb_pretty_printer_section]`
247 // rustc internal.
248 (active, omit_gdb_pretty_printer_section, "1.5.0", None),
249
250 // Allows cfg(target_vendor = "...").
251 (active, cfg_target_vendor, "1.5.0", Some(29718)),
252
253 // Allow attributes on expressions and non-item statements
254 (active, stmt_expr_attributes, "1.6.0", Some(15701)),
255
256 // allow using type ascription in expressions
257 (active, type_ascription, "1.6.0", Some(23416)),
258
259 // Allows cfg(target_thread_local)
260 (active, cfg_target_thread_local, "1.7.0", Some(29594)),
261
262 // rustc internal
263 (active, abi_vectorcall, "1.7.0", None),
264
265 // a..=b and ..=b
266 (active, inclusive_range_syntax, "1.7.0", Some(28237)),
267
268 // X..Y patterns
269 (active, exclusive_range_pattern, "1.11.0", Some(37854)),
270
271 // impl specialization (RFC 1210)
272 (active, specialization, "1.7.0", Some(31844)),
273
274 // Allows cfg(target_has_atomic = "...").
275 (active, cfg_target_has_atomic, "1.9.0", Some(32976)),
276
277 // Allows `impl Trait` in function return types.
278 (active, conservative_impl_trait, "1.12.0", Some(34511)),
279
280 // Allows `impl Trait` in function arguments.
281 (active, universal_impl_trait, "1.23.0", Some(34511)),
282
283 // The `!` type
284 (active, never_type, "1.13.0", Some(35121)),
285
286 // Allows all literals in attribute lists and values of key-value pairs.
287 (active, attr_literals, "1.13.0", Some(34981)),
288
289 // Allows untagged unions `union U { ... }`
290 (active, untagged_unions, "1.13.0", Some(32836)),
291
292 // Used to identify the `compiler_builtins` crate
293 // rustc internal
294 (active, compiler_builtins, "1.13.0", None),
295
296 // Allows attributes on lifetime/type formal parameters in generics (RFC 1327)
297 (active, generic_param_attrs, "1.11.0", Some(34761)),
298
299 // Allows #[link(..., cfg(..))]
300 (active, link_cfg, "1.14.0", Some(37406)),
301
302 (active, use_extern_macros, "1.15.0", Some(35896)),
303
304 // Allows #[target_feature(...)]
305 (active, target_feature, "1.15.0", None),
306
307 // `extern "ptx-*" fn()`
308 (active, abi_ptx, "1.15.0", None),
309
310 // The `i128` type
311 (active, i128_type, "1.16.0", Some(35118)),
312
313 // The `repr(i128)` annotation for enums
314 (active, repr128, "1.16.0", Some(35118)),
315
316 // The `unadjusted` ABI. Perma unstable.
317 (active, abi_unadjusted, "1.16.0", None),
318
319 // Procedural macros 2.0.
320 (active, proc_macro, "1.16.0", Some(38356)),
321
322 // Declarative macros 2.0 (`macro`).
323 (active, decl_macro, "1.17.0", Some(39412)),
324
325 // Allows #[link(kind="static-nobundle"...]
326 (active, static_nobundle, "1.16.0", Some(37403)),
327
328 // `extern "msp430-interrupt" fn()`
329 (active, abi_msp430_interrupt, "1.16.0", Some(38487)),
330
331 // Used to identify crates that contain sanitizer runtimes
332 // rustc internal
333 (active, sanitizer_runtime, "1.17.0", None),
334
335 // Used to identify crates that contain the profiler runtime
336 // rustc internal
337 (active, profiler_runtime, "1.18.0", None),
338
339 // `extern "x86-interrupt" fn()`
340 (active, abi_x86_interrupt, "1.17.0", Some(40180)),
341
342
343 // Allows the `catch {...}` expression
344 (active, catch_expr, "1.17.0", Some(31436)),
345
346 // Used to preserve symbols (see llvm.used)
347 (active, used, "1.18.0", Some(40289)),
348
349 // Allows module-level inline assembly by way of global_asm!()
350 (active, global_asm, "1.18.0", Some(35119)),
351
352 // Allows overlapping impls of marker traits
353 (active, overlapping_marker_traits, "1.18.0", Some(29864)),
354
355 // Allows use of the :vis macro fragment specifier
356 (active, macro_vis_matcher, "1.18.0", Some(41022)),
357
358 // rustc internal
359 (active, abi_thiscall, "1.19.0", None),
360
361 // Allows a test to fail without failing the whole suite
362 (active, allow_fail, "1.19.0", Some(42219)),
363
364 // Allows unsized tuple coercion.
365 (active, unsized_tuple_coercion, "1.20.0", Some(42877)),
366
367 // Generators
368 (active, generators, "1.21.0", None),
369
370 // Trait aliases
371 (active, trait_alias, "1.24.0", Some(41517)),
372
373 // global allocators and their internals
374 (active, global_allocator, "1.20.0", None),
375 (active, allocator_internals, "1.20.0", None),
376
377 // #[doc(cfg(...))]
378 (active, doc_cfg, "1.21.0", Some(43781)),
379 // #[doc(masked)]
380 (active, doc_masked, "1.21.0", Some(44027)),
381 // #[doc(spotlight)]
382 (active, doc_spotlight, "1.22.0", Some(45040)),
383 // #[doc(include="some-file")]
384 (active, external_doc, "1.22.0", Some(44732)),
385
386 // allow `#[must_use]` on functions and comparison operators (RFC 1940)
387 (active, fn_must_use, "1.21.0", Some(43302)),
388
389 // Future-proofing enums/structs with #[non_exhaustive] attribute (RFC 2008)
390 (active, non_exhaustive, "1.22.0", Some(44109)),
391
392 // Copy/Clone closures (RFC 2132)
393 (active, clone_closures, "1.22.0", Some(44490)),
394 (active, copy_closures, "1.22.0", Some(44490)),
395
396 // allow `'_` placeholder lifetimes
397 (active, underscore_lifetimes, "1.22.0", Some(44524)),
398
399 // allow `..=` in patterns (RFC 1192)
400 (active, dotdoteq_in_patterns, "1.22.0", Some(28237)),
401
402 // Default match binding modes (RFC 2005)
403 (active, match_default_bindings, "1.22.0", Some(42640)),
404
405 // Trait object syntax with `dyn` prefix
406 (active, dyn_trait, "1.22.0", Some(44662)),
407
408 // `crate` as visibility modifier, synonymous to `pub(crate)`
409 (active, crate_visibility_modifier, "1.23.0", Some(45388)),
410
411 // extern types
412 (active, extern_types, "1.23.0", Some(43467)),
413
414 // Allow trait methods with arbitrary self types
415 (active, arbitrary_self_types, "1.23.0", Some(44874)),
416
417 // #![wasm_import_memory] attribute
418 (active, wasm_import_memory, "1.22.0", None),
419
420 // `crate` in paths
421 (active, crate_in_paths, "1.23.0", Some(45477)),
422
423 // In-band lifetime bindings (e.g. `fn foo(x: &'a u8) -> &'a u8`)
424 (active, in_band_lifetimes, "1.23.0", Some(44524)),
425
426 // generic associated types (RFC 1598)
427 (active, generic_associated_types, "1.23.0", Some(44265)),
428
429 // Resolve absolute paths as paths from other crates
430 (active, extern_absolute_paths, "1.24.0", Some(44660)),
431
432 // `foo.rs` as an alternative to `foo/mod.rs`
433 (active, non_modrs_mods, "1.24.0", Some(44660)),
434
435 // Nested `impl Trait`
436 (active, nested_impl_trait, "1.24.0", Some(34511)),
437
438 // Termination trait in main (RFC 1937)
439 (active, termination_trait, "1.24.0", Some(43301)),
440
441 // Allows use of the :lifetime macro fragment specifier
442 (active, macro_lifetime_matcher, "1.24.0", Some(46895)),
443
444 // `extern` in paths
445 (active, extern_in_paths, "1.23.0", Some(44660)),
446
447 // Allows `#[repr(transparent)]` attribute on newtype structs
448 (active, repr_transparent, "1.25.0", Some(43036)),
449
450 // Use `?` as the Kleene "at most one" operator
451 (active, macro_at_most_once_rep, "1.25.0", Some(48075)),
452 );
453
454 declare_features! (
455 (removed, import_shadowing, "1.0.0", None),
456 (removed, managed_boxes, "1.0.0", None),
457 // Allows use of unary negate on unsigned integers, e.g. -e for e: u8
458 (removed, negate_unsigned, "1.0.0", Some(29645)),
459 (removed, reflect, "1.0.0", Some(27749)),
460 // A way to temporarily opt out of opt in copy. This will *never* be accepted.
461 (removed, opt_out_copy, "1.0.0", None),
462 (removed, quad_precision_float, "1.0.0", None),
463 (removed, struct_inherit, "1.0.0", None),
464 (removed, test_removed_feature, "1.0.0", None),
465 (removed, visible_private_types, "1.0.0", None),
466 (removed, unsafe_no_drop_flag, "1.0.0", None),
467 // Allows using items which are missing stability attributes
468 // rustc internal
469 (removed, unmarked_api, "1.0.0", None),
470 (removed, pushpop_unsafe, "1.2.0", None),
471 (removed, allocator, "1.0.0", None),
472 // Allows the `#[simd]` attribute -- removed in favor of `#[repr(simd)]`
473 (removed, simd, "1.0.0", Some(27731)),
474 );
475
476 declare_features! (
477 (stable_removed, no_stack_check, "1.0.0", None),
478 );
479
480 declare_features! (
481 (accepted, associated_types, "1.0.0", None),
482 // allow overloading augmented assignment operations like `a += b`
483 (accepted, augmented_assignments, "1.8.0", Some(28235)),
484 // allow empty structs and enum variants with braces
485 (accepted, braced_empty_structs, "1.8.0", Some(29720)),
486 (accepted, default_type_params, "1.0.0", None),
487 (accepted, globs, "1.0.0", None),
488 (accepted, if_let, "1.0.0", None),
489 // A temporary feature gate used to enable parser extensions needed
490 // to bootstrap fix for #5723.
491 (accepted, issue_5723_bootstrap, "1.0.0", None),
492 (accepted, macro_rules, "1.0.0", None),
493 // Allows using #![no_std]
494 (accepted, no_std, "1.6.0", None),
495 (accepted, slicing_syntax, "1.0.0", None),
496 (accepted, struct_variant, "1.0.0", None),
497 // These are used to test this portion of the compiler, they don't actually
498 // mean anything
499 (accepted, test_accepted_feature, "1.0.0", None),
500 (accepted, tuple_indexing, "1.0.0", None),
501 // Allows macros to appear in the type position.
502 (accepted, type_macros, "1.13.0", Some(27245)),
503 (accepted, while_let, "1.0.0", None),
504 // Allows `#[deprecated]` attribute
505 (accepted, deprecated, "1.9.0", Some(29935)),
506 // `expr?`
507 (accepted, question_mark, "1.13.0", Some(31436)),
508 // Allows `..` in tuple (struct) patterns
509 (accepted, dotdot_in_tuple_patterns, "1.14.0", Some(33627)),
510 (accepted, item_like_imports, "1.15.0", Some(35120)),
511 // Allows using `Self` and associated types in struct expressions and patterns.
512 (accepted, more_struct_aliases, "1.16.0", Some(37544)),
513 // elide `'static` lifetimes in `static`s and `const`s
514 (accepted, static_in_const, "1.17.0", Some(35897)),
515 // Allows field shorthands (`x` meaning `x: x`) in struct literal expressions.
516 (accepted, field_init_shorthand, "1.17.0", Some(37340)),
517 // Allows the definition recursive static items.
518 (accepted, static_recursion, "1.17.0", Some(29719)),
519 // pub(restricted) visibilities (RFC 1422)
520 (accepted, pub_restricted, "1.18.0", Some(32409)),
521 // The #![windows_subsystem] attribute
522 (accepted, windows_subsystem, "1.18.0", Some(37499)),
523 // Allows `break {expr}` with a value inside `loop`s.
524 (accepted, loop_break_value, "1.19.0", Some(37339)),
525 // Permits numeric fields in struct expressions and patterns.
526 (accepted, relaxed_adts, "1.19.0", Some(35626)),
527 // Coerces non capturing closures to function pointers
528 (accepted, closure_to_fn_coercion, "1.19.0", Some(39817)),
529 // Allows attributes on struct literal fields.
530 (accepted, struct_field_attributes, "1.20.0", Some(38814)),
531 // Allows the definition of associated constants in `trait` or `impl`
532 // blocks.
533 (accepted, associated_consts, "1.20.0", Some(29646)),
534 // Usage of the `compile_error!` macro
535 (accepted, compile_error, "1.20.0", Some(40872)),
536 // See rust-lang/rfcs#1414. Allows code like `let x: &'static u32 = &42` to work.
537 (accepted, rvalue_static_promotion, "1.21.0", Some(38865)),
538 // Allow Drop types in constants (RFC 1440)
539 (accepted, drop_types_in_const, "1.22.0", Some(33156)),
540 // Allows the sysV64 ABI to be specified on all platforms
541 // instead of just the platforms on which it is the C ABI
542 (accepted, abi_sysv64, "1.24.0", Some(36167)),
543 // Allows `repr(align(16))` struct attribute (RFC 1358)
544 (accepted, repr_align, "1.24.0", Some(33626)),
545 // allow '|' at beginning of match arms (RFC 1925)
546 (accepted, match_beginning_vert, "1.25.0", Some(44101)),
547 // Nested groups in `use` (RFC 2128)
548 (accepted, use_nested_groups, "1.25.0", Some(44494)),
549 );
550
551 // If you change this, please modify src/doc/unstable-book as well. You must
552 // move that documentation into the relevant place in the other docs, and
553 // remove the chapter on the flag.
554
555 #[derive(PartialEq, Copy, Clone, Debug)]
556 pub enum AttributeType {
557 /// Normal, builtin attribute that is consumed
558 /// by the compiler before the unused_attribute check
559 Normal,
560
561 /// Builtin attribute that may not be consumed by the compiler
562 /// before the unused_attribute check. These attributes
563 /// will be ignored by the unused_attribute lint
564 Whitelisted,
565
566 /// Builtin attribute that is only allowed at the crate level
567 CrateLevel,
568 }
569
570 pub enum AttributeGate {
571 /// Is gated by a given feature gate, reason
572 /// and function to check if enabled
573 Gated(Stability, &'static str, &'static str, fn(&Features) -> bool),
574
575 /// Ungated attribute, can be used on all release channels
576 Ungated,
577 }
578
579 impl AttributeGate {
580 fn is_deprecated(&self) -> bool {
581 match *self {
582 Gated(Stability::Deprecated(_), ..) => true,
583 _ => false,
584 }
585 }
586 }
587
588 #[derive(Copy, Clone, PartialEq, Eq, Debug)]
589 pub enum Stability {
590 Unstable,
591 // Argument is tracking issue link.
592 Deprecated(&'static str),
593 }
594
595 // fn() is not Debug
596 impl ::std::fmt::Debug for AttributeGate {
597 fn fmt(&self, fmt: &mut ::std::fmt::Formatter) -> ::std::fmt::Result {
598 match *self {
599 Gated(ref stab, name, expl, _) =>
600 write!(fmt, "Gated({:?}, {}, {})", stab, name, expl),
601 Ungated => write!(fmt, "Ungated")
602 }
603 }
604 }
605
606 macro_rules! cfg_fn {
607 ($field: ident) => {{
608 fn f(features: &Features) -> bool {
609 features.$field
610 }
611 f as fn(&Features) -> bool
612 }}
613 }
614
615 pub fn deprecated_attributes() -> Vec<&'static (&'static str, AttributeType, AttributeGate)> {
616 BUILTIN_ATTRIBUTES.iter().filter(|a| a.2.is_deprecated()).collect()
617 }
618
619 pub fn is_builtin_attr(attr: &ast::Attribute) -> bool {
620 BUILTIN_ATTRIBUTES.iter().any(|&(builtin_name, _, _)| attr.check_name(builtin_name))
621 }
622
623 // Attributes that have a special meaning to rustc or rustdoc
624 pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeGate)] = &[
625 // Normal attributes
626
627 ("warn", Normal, Ungated),
628 ("allow", Normal, Ungated),
629 ("forbid", Normal, Ungated),
630 ("deny", Normal, Ungated),
631
632 ("macro_reexport", Normal, Ungated),
633 ("macro_use", Normal, Ungated),
634 ("macro_export", Normal, Ungated),
635 ("plugin_registrar", Normal, Ungated),
636
637 ("cfg", Normal, Ungated),
638 ("cfg_attr", Normal, Ungated),
639 ("main", Normal, Ungated),
640 ("start", Normal, Ungated),
641 ("test", Normal, Ungated),
642 ("bench", Normal, Ungated),
643 ("repr", Normal, Ungated),
644 ("path", Normal, Ungated),
645 ("abi", Normal, Ungated),
646 ("automatically_derived", Normal, Ungated),
647 ("no_mangle", Normal, Ungated),
648 ("no_link", Normal, Ungated),
649 ("derive", Normal, Ungated),
650 ("should_panic", Normal, Ungated),
651 ("ignore", Normal, Ungated),
652 ("no_implicit_prelude", Normal, Ungated),
653 ("reexport_test_harness_main", Normal, Ungated),
654 ("link_args", Normal, Gated(Stability::Unstable,
655 "link_args",
656 "the `link_args` attribute is experimental and not \
657 portable across platforms, it is recommended to \
658 use `#[link(name = \"foo\")] instead",
659 cfg_fn!(link_args))),
660 ("macro_escape", Normal, Ungated),
661
662 // RFC #1445.
663 ("structural_match", Whitelisted, Gated(Stability::Unstable,
664 "structural_match",
665 "the semantics of constant patterns is \
666 not yet settled",
667 cfg_fn!(structural_match))),
668
669 // RFC #2008
670 ("non_exhaustive", Whitelisted, Gated(Stability::Unstable,
671 "non_exhaustive",
672 "non exhaustive is an experimental feature",
673 cfg_fn!(non_exhaustive))),
674
675 ("plugin", CrateLevel, Gated(Stability::Unstable,
676 "plugin",
677 "compiler plugins are experimental \
678 and possibly buggy",
679 cfg_fn!(plugin))),
680
681 ("no_std", CrateLevel, Ungated),
682 ("no_core", CrateLevel, Gated(Stability::Unstable,
683 "no_core",
684 "no_core is experimental",
685 cfg_fn!(no_core))),
686 ("lang", Normal, Gated(Stability::Unstable,
687 "lang_items",
688 "language items are subject to change",
689 cfg_fn!(lang_items))),
690 ("linkage", Whitelisted, Gated(Stability::Unstable,
691 "linkage",
692 "the `linkage` attribute is experimental \
693 and not portable across platforms",
694 cfg_fn!(linkage))),
695 ("thread_local", Whitelisted, Gated(Stability::Unstable,
696 "thread_local",
697 "`#[thread_local]` is an experimental feature, and does \
698 not currently handle destructors.",
699 cfg_fn!(thread_local))),
700
701 ("rustc_on_unimplemented", Normal, Gated(Stability::Unstable,
702 "on_unimplemented",
703 "the `#[rustc_on_unimplemented]` attribute \
704 is an experimental feature",
705 cfg_fn!(on_unimplemented))),
706 ("rustc_const_unstable", Normal, Gated(Stability::Unstable,
707 "rustc_const_unstable",
708 "the `#[rustc_const_unstable]` attribute \
709 is an internal feature",
710 cfg_fn!(rustc_const_unstable))),
711 ("global_allocator", Normal, Gated(Stability::Unstable,
712 "global_allocator",
713 "the `#[global_allocator]` attribute is \
714 an experimental feature",
715 cfg_fn!(global_allocator))),
716 ("default_lib_allocator", Whitelisted, Gated(Stability::Unstable,
717 "allocator_internals",
718 "the `#[default_lib_allocator]` \
719 attribute is an experimental feature",
720 cfg_fn!(allocator_internals))),
721 ("needs_allocator", Normal, Gated(Stability::Unstable,
722 "allocator_internals",
723 "the `#[needs_allocator]` \
724 attribute is an experimental \
725 feature",
726 cfg_fn!(allocator_internals))),
727 ("panic_runtime", Whitelisted, Gated(Stability::Unstable,
728 "panic_runtime",
729 "the `#[panic_runtime]` attribute is \
730 an experimental feature",
731 cfg_fn!(panic_runtime))),
732 ("needs_panic_runtime", Whitelisted, Gated(Stability::Unstable,
733 "needs_panic_runtime",
734 "the `#[needs_panic_runtime]` \
735 attribute is an experimental \
736 feature",
737 cfg_fn!(needs_panic_runtime))),
738 ("rustc_variance", Normal, Gated(Stability::Unstable,
739 "rustc_attrs",
740 "the `#[rustc_variance]` attribute \
741 is just used for rustc unit tests \
742 and will never be stable",
743 cfg_fn!(rustc_attrs))),
744 ("rustc_regions", Normal, Gated(Stability::Unstable,
745 "rustc_attrs",
746 "the `#[rustc_regions]` attribute \
747 is just used for rustc unit tests \
748 and will never be stable",
749 cfg_fn!(rustc_attrs))),
750 ("rustc_error", Whitelisted, Gated(Stability::Unstable,
751 "rustc_attrs",
752 "the `#[rustc_error]` attribute \
753 is just used for rustc unit tests \
754 and will never be stable",
755 cfg_fn!(rustc_attrs))),
756 ("rustc_if_this_changed", Whitelisted, Gated(Stability::Unstable,
757 "rustc_attrs",
758 "the `#[rustc_if_this_changed]` attribute \
759 is just used for rustc unit tests \
760 and will never be stable",
761 cfg_fn!(rustc_attrs))),
762 ("rustc_then_this_would_need", Whitelisted, Gated(Stability::Unstable,
763 "rustc_attrs",
764 "the `#[rustc_if_this_changed]` attribute \
765 is just used for rustc unit tests \
766 and will never be stable",
767 cfg_fn!(rustc_attrs))),
768 ("rustc_dirty", Whitelisted, Gated(Stability::Unstable,
769 "rustc_attrs",
770 "the `#[rustc_dirty]` attribute \
771 is just used for rustc unit tests \
772 and will never be stable",
773 cfg_fn!(rustc_attrs))),
774 ("rustc_clean", Whitelisted, Gated(Stability::Unstable,
775 "rustc_attrs",
776 "the `#[rustc_clean]` attribute \
777 is just used for rustc unit tests \
778 and will never be stable",
779 cfg_fn!(rustc_attrs))),
780 ("rustc_partition_reused", Whitelisted, Gated(Stability::Unstable,
781 "rustc_attrs",
782 "this attribute \
783 is just used for rustc unit tests \
784 and will never be stable",
785 cfg_fn!(rustc_attrs))),
786 ("rustc_partition_translated", Whitelisted, Gated(Stability::Unstable,
787 "rustc_attrs",
788 "this attribute \
789 is just used for rustc unit tests \
790 and will never be stable",
791 cfg_fn!(rustc_attrs))),
792 ("rustc_serialize_exclude_null", Normal, Gated(Stability::Unstable,
793 "rustc_attrs",
794 "the `#[rustc_serialize_exclude_null]` attribute \
795 is an internal-only feature",
796 cfg_fn!(rustc_attrs))),
797 ("rustc_synthetic", Whitelisted, Gated(Stability::Unstable,
798 "rustc_attrs",
799 "this attribute \
800 is just used for rustc unit tests \
801 and will never be stable",
802 cfg_fn!(rustc_attrs))),
803 ("rustc_symbol_name", Whitelisted, Gated(Stability::Unstable,
804 "rustc_attrs",
805 "internal rustc attributes will never be stable",
806 cfg_fn!(rustc_attrs))),
807 ("rustc_item_path", Whitelisted, Gated(Stability::Unstable,
808 "rustc_attrs",
809 "internal rustc attributes will never be stable",
810 cfg_fn!(rustc_attrs))),
811 ("rustc_mir", Whitelisted, Gated(Stability::Unstable,
812 "rustc_attrs",
813 "the `#[rustc_mir]` attribute \
814 is just used for rustc unit tests \
815 and will never be stable",
816 cfg_fn!(rustc_attrs))),
817 ("rustc_inherit_overflow_checks", Whitelisted, Gated(Stability::Unstable,
818 "rustc_attrs",
819 "the `#[rustc_inherit_overflow_checks]` \
820 attribute is just used to control \
821 overflow checking behavior of several \
822 libcore functions that are inlined \
823 across crates and will never be stable",
824 cfg_fn!(rustc_attrs))),
825
826 // RFC #2094
827 ("nll", Whitelisted, Gated(Stability::Unstable,
828 "nll",
829 "Non lexical lifetimes",
830 cfg_fn!(nll))),
831 ("compiler_builtins", Whitelisted, Gated(Stability::Unstable,
832 "compiler_builtins",
833 "the `#[compiler_builtins]` attribute is used to \
834 identify the `compiler_builtins` crate which \
835 contains compiler-rt intrinsics and will never be \
836 stable",
837 cfg_fn!(compiler_builtins))),
838 ("sanitizer_runtime", Whitelisted, Gated(Stability::Unstable,
839 "sanitizer_runtime",
840 "the `#[sanitizer_runtime]` attribute is used to \
841 identify crates that contain the runtime of a \
842 sanitizer and will never be stable",
843 cfg_fn!(sanitizer_runtime))),
844 ("profiler_runtime", Whitelisted, Gated(Stability::Unstable,
845 "profiler_runtime",
846 "the `#[profiler_runtime]` attribute is used to \
847 identify the `profiler_builtins` crate which \
848 contains the profiler runtime and will never be \
849 stable",
850 cfg_fn!(profiler_runtime))),
851
852 ("allow_internal_unstable", Normal, Gated(Stability::Unstable,
853 "allow_internal_unstable",
854 EXPLAIN_ALLOW_INTERNAL_UNSTABLE,
855 cfg_fn!(allow_internal_unstable))),
856
857 ("allow_internal_unsafe", Normal, Gated(Stability::Unstable,
858 "allow_internal_unsafe",
859 EXPLAIN_ALLOW_INTERNAL_UNSAFE,
860 cfg_fn!(allow_internal_unsafe))),
861
862 ("fundamental", Whitelisted, Gated(Stability::Unstable,
863 "fundamental",
864 "the `#[fundamental]` attribute \
865 is an experimental feature",
866 cfg_fn!(fundamental))),
867
868 ("proc_macro_derive", Normal, Ungated),
869
870 ("rustc_copy_clone_marker", Whitelisted, Gated(Stability::Unstable,
871 "rustc_attrs",
872 "internal implementation detail",
873 cfg_fn!(rustc_attrs))),
874
875 // FIXME: #14408 whitelist docs since rustdoc looks at them
876 ("doc", Whitelisted, Ungated),
877
878 // FIXME: #14406 these are processed in trans, which happens after the
879 // lint pass
880 ("cold", Whitelisted, Ungated),
881 ("naked", Whitelisted, Gated(Stability::Unstable,
882 "naked_functions",
883 "the `#[naked]` attribute \
884 is an experimental feature",
885 cfg_fn!(naked_functions))),
886 ("target_feature", Whitelisted, Gated(
887 Stability::Unstable, "target_feature",
888 "the `#[target_feature]` attribute is an experimental feature",
889 cfg_fn!(target_feature))),
890 ("export_name", Whitelisted, Ungated),
891 ("inline", Whitelisted, Ungated),
892 ("link", Whitelisted, Ungated),
893 ("link_name", Whitelisted, Ungated),
894 ("link_section", Whitelisted, Ungated),
895 ("no_builtins", Whitelisted, Ungated),
896 ("no_mangle", Whitelisted, Ungated),
897 ("no_debug", Whitelisted, Gated(
898 Stability::Deprecated("https://github.com/rust-lang/rust/issues/29721"),
899 "no_debug",
900 "the `#[no_debug]` attribute was an experimental feature that has been \
901 deprecated due to lack of demand",
902 cfg_fn!(no_debug))),
903 ("omit_gdb_pretty_printer_section", Whitelisted, Gated(Stability::Unstable,
904 "omit_gdb_pretty_printer_section",
905 "the `#[omit_gdb_pretty_printer_section]` \
906 attribute is just used for the Rust test \
907 suite",
908 cfg_fn!(omit_gdb_pretty_printer_section))),
909 ("unsafe_destructor_blind_to_params",
910 Normal,
911 Gated(Stability::Deprecated("https://github.com/rust-lang/rust/issues/34761"),
912 "dropck_parametricity",
913 "unsafe_destructor_blind_to_params has been replaced by \
914 may_dangle and will be removed in the future",
915 cfg_fn!(dropck_parametricity))),
916 ("may_dangle",
917 Normal,
918 Gated(Stability::Unstable,
919 "dropck_eyepatch",
920 "may_dangle has unstable semantics and may be removed in the future",
921 cfg_fn!(dropck_eyepatch))),
922 ("unwind", Whitelisted, Gated(Stability::Unstable,
923 "unwind_attributes",
924 "#[unwind] is experimental",
925 cfg_fn!(unwind_attributes))),
926 ("used", Whitelisted, Gated(
927 Stability::Unstable, "used",
928 "the `#[used]` attribute is an experimental feature",
929 cfg_fn!(used))),
930
931 // used in resolve
932 ("prelude_import", Whitelisted, Gated(Stability::Unstable,
933 "prelude_import",
934 "`#[prelude_import]` is for use by rustc only",
935 cfg_fn!(prelude_import))),
936
937 // FIXME: #14407 these are only looked at on-demand so we can't
938 // guarantee they'll have already been checked
939 ("rustc_deprecated", Whitelisted, Ungated),
940 ("must_use", Whitelisted, Ungated),
941 ("stable", Whitelisted, Ungated),
942 ("unstable", Whitelisted, Ungated),
943 ("deprecated", Normal, Ungated),
944
945 ("rustc_paren_sugar", Normal, Gated(Stability::Unstable,
946 "unboxed_closures",
947 "unboxed_closures are still evolving",
948 cfg_fn!(unboxed_closures))),
949
950 ("windows_subsystem", Whitelisted, Ungated),
951
952 ("proc_macro_attribute", Normal, Gated(Stability::Unstable,
953 "proc_macro",
954 "attribute proc macros are currently unstable",
955 cfg_fn!(proc_macro))),
956
957 ("proc_macro", Normal, Gated(Stability::Unstable,
958 "proc_macro",
959 "function-like proc macros are currently unstable",
960 cfg_fn!(proc_macro))),
961
962 ("rustc_derive_registrar", Normal, Gated(Stability::Unstable,
963 "rustc_derive_registrar",
964 "used internally by rustc",
965 cfg_fn!(rustc_attrs))),
966
967 ("allow_fail", Normal, Gated(Stability::Unstable,
968 "allow_fail",
969 "allow_fail attribute is currently unstable",
970 cfg_fn!(allow_fail))),
971
972 ("rustc_std_internal_symbol", Whitelisted, Gated(Stability::Unstable,
973 "rustc_attrs",
974 "this is an internal attribute that will \
975 never be stable",
976 cfg_fn!(rustc_attrs))),
977
978 // whitelists "identity-like" conversion methods to suggest on type mismatch
979 ("rustc_conversion_suggestion", Whitelisted, Gated(Stability::Unstable,
980 "rustc_attrs",
981 "this is an internal attribute that will \
982 never be stable",
983 cfg_fn!(rustc_attrs))),
984
985 ("wasm_import_memory", Whitelisted, Gated(Stability::Unstable,
986 "wasm_import_memory",
987 "wasm_import_memory attribute is currently unstable",
988 cfg_fn!(wasm_import_memory))),
989
990 ("rustc_args_required_const", Whitelisted, Gated(Stability::Unstable,
991 "rustc_attrs",
992 "never will be stable",
993 cfg_fn!(rustc_attrs))),
994
995 // Crate level attributes
996 ("crate_name", CrateLevel, Ungated),
997 ("crate_type", CrateLevel, Ungated),
998 ("crate_id", CrateLevel, Ungated),
999 ("feature", CrateLevel, Ungated),
1000 ("no_start", CrateLevel, Ungated),
1001 ("no_main", CrateLevel, Ungated),
1002 ("no_builtins", CrateLevel, Ungated),
1003 ("recursion_limit", CrateLevel, Ungated),
1004 ("type_length_limit", CrateLevel, Ungated),
1005 ];
1006
1007 // cfg(...)'s that are feature gated
1008 const GATED_CFGS: &[(&str, &str, fn(&Features) -> bool)] = &[
1009 // (name in cfg, feature, function to check if the feature is enabled)
1010 ("target_feature", "cfg_target_feature", cfg_fn!(cfg_target_feature)),
1011 ("target_vendor", "cfg_target_vendor", cfg_fn!(cfg_target_vendor)),
1012 ("target_thread_local", "cfg_target_thread_local", cfg_fn!(cfg_target_thread_local)),
1013 ("target_has_atomic", "cfg_target_has_atomic", cfg_fn!(cfg_target_has_atomic)),
1014 ];
1015
1016 #[derive(Debug, Eq, PartialEq)]
1017 pub struct GatedCfg {
1018 span: Span,
1019 index: usize,
1020 }
1021
1022 impl GatedCfg {
1023 pub fn gate(cfg: &ast::MetaItem) -> Option<GatedCfg> {
1024 let name = cfg.name().as_str();
1025 GATED_CFGS.iter()
1026 .position(|info| info.0 == name)
1027 .map(|idx| {
1028 GatedCfg {
1029 span: cfg.span,
1030 index: idx
1031 }
1032 })
1033 }
1034
1035 pub fn check_and_emit(&self, sess: &ParseSess, features: &Features) {
1036 let (cfg, feature, has_feature) = GATED_CFGS[self.index];
1037 if !has_feature(features) && !self.span.allows_unstable() {
1038 let explain = format!("`cfg({})` is experimental and subject to change", cfg);
1039 emit_feature_err(sess, feature, self.span, GateIssue::Language, &explain);
1040 }
1041 }
1042 }
1043
1044 struct Context<'a> {
1045 features: &'a Features,
1046 parse_sess: &'a ParseSess,
1047 plugin_attributes: &'a [(String, AttributeType)],
1048 }
1049
1050 macro_rules! gate_feature_fn {
1051 ($cx: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr, $level: expr) => {{
1052 let (cx, has_feature, span,
1053 name, explain, level) = ($cx, $has_feature, $span, $name, $explain, $level);
1054 let has_feature: bool = has_feature(&$cx.features);
1055 debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature);
1056 if !has_feature && !span.allows_unstable() {
1057 leveled_feature_err(cx.parse_sess, name, span, GateIssue::Language, explain, level)
1058 .emit();
1059 }
1060 }}
1061 }
1062
1063 macro_rules! gate_feature {
1064 ($cx: expr, $feature: ident, $span: expr, $explain: expr) => {
1065 gate_feature_fn!($cx, |x:&Features| x.$feature, $span,
1066 stringify!($feature), $explain, GateStrength::Hard)
1067 };
1068 ($cx: expr, $feature: ident, $span: expr, $explain: expr, $level: expr) => {
1069 gate_feature_fn!($cx, |x:&Features| x.$feature, $span,
1070 stringify!($feature), $explain, $level)
1071 };
1072 }
1073
1074 impl<'a> Context<'a> {
1075 fn check_attribute(&self, attr: &ast::Attribute, is_macro: bool) {
1076 debug!("check_attribute(attr = {:?})", attr);
1077 let name = unwrap_or!(attr.name(), return).as_str();
1078 for &(n, ty, ref gateage) in BUILTIN_ATTRIBUTES {
1079 if name == n {
1080 if let Gated(_, name, desc, ref has_feature) = *gateage {
1081 gate_feature_fn!(self, has_feature, attr.span, name, desc, GateStrength::Hard);
1082 } else if name == "doc" {
1083 if let Some(content) = attr.meta_item_list() {
1084 if content.iter().any(|c| c.check_name("include")) {
1085 gate_feature!(self, external_doc, attr.span,
1086 "#[doc(include = \"...\")] is experimental"
1087 );
1088 }
1089 }
1090 }
1091 debug!("check_attribute: {:?} is builtin, {:?}, {:?}", attr.path, ty, gateage);
1092 return;
1093 }
1094 }
1095 for &(ref n, ref ty) in self.plugin_attributes {
1096 if attr.path == &**n {
1097 // Plugins can't gate attributes, so we don't check for it
1098 // unlike the code above; we only use this loop to
1099 // short-circuit to avoid the checks below
1100 debug!("check_attribute: {:?} is registered by a plugin, {:?}", attr.path, ty);
1101 return;
1102 }
1103 }
1104 if name.starts_with("rustc_") {
1105 gate_feature!(self, rustc_attrs, attr.span,
1106 "unless otherwise specified, attributes \
1107 with the prefix `rustc_` \
1108 are reserved for internal compiler diagnostics");
1109 } else if name.starts_with("derive_") {
1110 gate_feature!(self, custom_derive, attr.span, EXPLAIN_DERIVE_UNDERSCORE);
1111 } else if !attr::is_known(attr) {
1112 // Only run the custom attribute lint during regular
1113 // feature gate checking. Macro gating runs
1114 // before the plugin attributes are registered
1115 // so we skip this then
1116 if !is_macro {
1117 gate_feature!(self, custom_attribute, attr.span,
1118 &format!("The attribute `{}` is currently \
1119 unknown to the compiler and \
1120 may have meaning \
1121 added to it in the future",
1122 attr.path));
1123 }
1124 }
1125 }
1126 }
1127
1128 pub fn check_attribute(attr: &ast::Attribute, parse_sess: &ParseSess, features: &Features) {
1129 let cx = Context { features: features, parse_sess: parse_sess, plugin_attributes: &[] };
1130 cx.check_attribute(attr, true);
1131 }
1132
1133 pub fn find_lang_feature_accepted_version(feature: &str) -> Option<&'static str> {
1134 ACCEPTED_FEATURES.iter().find(|t| t.0 == feature).map(|t| t.1)
1135 }
1136
1137 fn find_lang_feature_issue(feature: &str) -> Option<u32> {
1138 if let Some(info) = ACTIVE_FEATURES.iter().find(|t| t.0 == feature) {
1139 let issue = info.2;
1140 // FIXME (#28244): enforce that active features have issue numbers
1141 // assert!(issue.is_some())
1142 issue
1143 } else {
1144 // search in Accepted, Removed, or Stable Removed features
1145 let found = ACCEPTED_FEATURES.iter().chain(REMOVED_FEATURES).chain(STABLE_REMOVED_FEATURES)
1146 .find(|t| t.0 == feature);
1147 match found {
1148 Some(&(_, _, issue)) => issue,
1149 None => panic!("Feature `{}` is not declared anywhere", feature),
1150 }
1151 }
1152 }
1153
1154 pub enum GateIssue {
1155 Language,
1156 Library(Option<u32>)
1157 }
1158
1159 #[derive(Debug, Copy, Clone, PartialEq, Eq)]
1160 pub enum GateStrength {
1161 /// A hard error. (Most feature gates should use this.)
1162 Hard,
1163 /// Only a warning. (Use this only as backwards-compatibility demands.)
1164 Soft,
1165 }
1166
1167 pub fn emit_feature_err(sess: &ParseSess, feature: &str, span: Span, issue: GateIssue,
1168 explain: &str) {
1169 feature_err(sess, feature, span, issue, explain).emit();
1170 }
1171
1172 pub fn feature_err<'a>(sess: &'a ParseSess, feature: &str, span: Span, issue: GateIssue,
1173 explain: &str) -> DiagnosticBuilder<'a> {
1174 leveled_feature_err(sess, feature, span, issue, explain, GateStrength::Hard)
1175 }
1176
1177 fn leveled_feature_err<'a>(sess: &'a ParseSess, feature: &str, span: Span, issue: GateIssue,
1178 explain: &str, level: GateStrength) -> DiagnosticBuilder<'a> {
1179 let diag = &sess.span_diagnostic;
1180
1181 let issue = match issue {
1182 GateIssue::Language => find_lang_feature_issue(feature),
1183 GateIssue::Library(lib) => lib,
1184 };
1185
1186 let explanation = if let Some(n) = issue {
1187 format!("{} (see issue #{})", explain, n)
1188 } else {
1189 explain.to_owned()
1190 };
1191
1192 let mut err = match level {
1193 GateStrength::Hard => {
1194 diag.struct_span_err_with_code(span, &explanation, stringify_error_code!(E0658))
1195 }
1196 GateStrength::Soft => diag.struct_span_warn(span, &explanation),
1197 };
1198
1199 // #23973: do not suggest `#![feature(...)]` if we are in beta/stable
1200 if sess.unstable_features.is_nightly_build() {
1201 err.help(&format!("add #![feature({})] to the \
1202 crate attributes to enable",
1203 feature));
1204 }
1205
1206 // If we're on stable and only emitting a "soft" warning, add a note to
1207 // clarify that the feature isn't "on" (rather than being on but
1208 // warning-worthy).
1209 if !sess.unstable_features.is_nightly_build() && level == GateStrength::Soft {
1210 err.help("a nightly build of the compiler is required to enable this feature");
1211 }
1212
1213 err
1214
1215 }
1216
1217 const EXPLAIN_BOX_SYNTAX: &'static str =
1218 "box expression syntax is experimental; you can call `Box::new` instead.";
1219
1220 pub const EXPLAIN_STMT_ATTR_SYNTAX: &'static str =
1221 "attributes on non-item statements and expressions are experimental.";
1222
1223 pub const EXPLAIN_ASM: &'static str =
1224 "inline assembly is not stable enough for use and is subject to change";
1225
1226 pub const EXPLAIN_GLOBAL_ASM: &'static str =
1227 "`global_asm!` is not stable enough for use and is subject to change";
1228
1229 pub const EXPLAIN_LOG_SYNTAX: &'static str =
1230 "`log_syntax!` is not stable enough for use and is subject to change";
1231
1232 pub const EXPLAIN_CONCAT_IDENTS: &'static str =
1233 "`concat_idents` is not stable enough for use and is subject to change";
1234
1235 pub const EXPLAIN_TRACE_MACROS: &'static str =
1236 "`trace_macros` is not stable enough for use and is subject to change";
1237 pub const EXPLAIN_ALLOW_INTERNAL_UNSTABLE: &'static str =
1238 "allow_internal_unstable side-steps feature gating and stability checks";
1239 pub const EXPLAIN_ALLOW_INTERNAL_UNSAFE: &'static str =
1240 "allow_internal_unsafe side-steps the unsafe_code lint";
1241
1242 pub const EXPLAIN_CUSTOM_DERIVE: &'static str =
1243 "`#[derive]` for custom traits is deprecated and will be removed in the future.";
1244
1245 pub const EXPLAIN_DEPR_CUSTOM_DERIVE: &'static str =
1246 "`#[derive]` for custom traits is deprecated and will be removed in the future. \
1247 Prefer using procedural macro custom derive.";
1248
1249 pub const EXPLAIN_DERIVE_UNDERSCORE: &'static str =
1250 "attributes of the form `#[derive_*]` are reserved for the compiler";
1251
1252 pub const EXPLAIN_VIS_MATCHER: &'static str =
1253 ":vis fragment specifier is experimental and subject to change";
1254
1255 pub const EXPLAIN_LIFETIME_MATCHER: &'static str =
1256 ":lifetime fragment specifier is experimental and subject to change";
1257
1258 pub const EXPLAIN_PLACEMENT_IN: &'static str =
1259 "placement-in expression syntax is experimental and subject to change.";
1260
1261 pub const EXPLAIN_UNSIZED_TUPLE_COERCION: &'static str =
1262 "Unsized tuple coercion is not stable enough for use and is subject to change";
1263
1264 pub const EXPLAIN_MACRO_AT_MOST_ONCE_REP: &'static str =
1265 "Using the `?` macro Kleene operator for \"at most one\" repetition is unstable";
1266
1267 struct PostExpansionVisitor<'a> {
1268 context: &'a Context<'a>,
1269 }
1270
1271 macro_rules! gate_feature_post {
1272 ($cx: expr, $feature: ident, $span: expr, $explain: expr) => {{
1273 let (cx, span) = ($cx, $span);
1274 if !span.allows_unstable() {
1275 gate_feature!(cx.context, $feature, span, $explain)
1276 }
1277 }};
1278 ($cx: expr, $feature: ident, $span: expr, $explain: expr, $level: expr) => {{
1279 let (cx, span) = ($cx, $span);
1280 if !span.allows_unstable() {
1281 gate_feature!(cx.context, $feature, span, $explain, $level)
1282 }
1283 }}
1284 }
1285
1286 impl<'a> PostExpansionVisitor<'a> {
1287 fn check_abi(&self, abi: Abi, span: Span) {
1288 match abi {
1289 Abi::RustIntrinsic => {
1290 gate_feature_post!(&self, intrinsics, span,
1291 "intrinsics are subject to change");
1292 },
1293 Abi::PlatformIntrinsic => {
1294 gate_feature_post!(&self, platform_intrinsics, span,
1295 "platform intrinsics are experimental and possibly buggy");
1296 },
1297 Abi::Vectorcall => {
1298 gate_feature_post!(&self, abi_vectorcall, span,
1299 "vectorcall is experimental and subject to change");
1300 },
1301 Abi::Thiscall => {
1302 gate_feature_post!(&self, abi_thiscall, span,
1303 "thiscall is experimental and subject to change");
1304 },
1305 Abi::RustCall => {
1306 gate_feature_post!(&self, unboxed_closures, span,
1307 "rust-call ABI is subject to change");
1308 },
1309 Abi::PtxKernel => {
1310 gate_feature_post!(&self, abi_ptx, span,
1311 "PTX ABIs are experimental and subject to change");
1312 },
1313 Abi::Unadjusted => {
1314 gate_feature_post!(&self, abi_unadjusted, span,
1315 "unadjusted ABI is an implementation detail and perma-unstable");
1316 },
1317 Abi::Msp430Interrupt => {
1318 gate_feature_post!(&self, abi_msp430_interrupt, span,
1319 "msp430-interrupt ABI is experimental and subject to change");
1320 },
1321 Abi::X86Interrupt => {
1322 gate_feature_post!(&self, abi_x86_interrupt, span,
1323 "x86-interrupt ABI is experimental and subject to change");
1324 },
1325 // Stable
1326 Abi::Cdecl |
1327 Abi::Stdcall |
1328 Abi::Fastcall |
1329 Abi::Aapcs |
1330 Abi::Win64 |
1331 Abi::SysV64 |
1332 Abi::Rust |
1333 Abi::C |
1334 Abi::System => {}
1335 }
1336 }
1337 }
1338
1339 fn contains_novel_literal(item: &ast::MetaItem) -> bool {
1340 use ast::MetaItemKind::*;
1341 use ast::NestedMetaItemKind::*;
1342
1343 match item.node {
1344 Word => false,
1345 NameValue(ref lit) => !lit.node.is_str(),
1346 List(ref list) => list.iter().any(|li| {
1347 match li.node {
1348 MetaItem(ref mi) => contains_novel_literal(mi),
1349 Literal(_) => true,
1350 }
1351 }),
1352 }
1353 }
1354
1355 // Bans nested `impl Trait`, e.g. `impl Into<impl Debug>`.
1356 // Nested `impl Trait` _is_ allowed in associated type position,
1357 // e.g `impl Iterator<Item=impl Debug>`
1358 struct NestedImplTraitVisitor<'a> {
1359 context: &'a Context<'a>,
1360 is_in_impl_trait: bool,
1361 }
1362
1363 impl<'a> NestedImplTraitVisitor<'a> {
1364 fn with_impl_trait<F>(&mut self, is_in_impl_trait: bool, f: F)
1365 where F: FnOnce(&mut NestedImplTraitVisitor<'a>)
1366 {
1367 let old_is_in_impl_trait = self.is_in_impl_trait;
1368 self.is_in_impl_trait = is_in_impl_trait;
1369 f(self);
1370 self.is_in_impl_trait = old_is_in_impl_trait;
1371 }
1372 }
1373
1374
1375 impl<'a> Visitor<'a> for NestedImplTraitVisitor<'a> {
1376 fn visit_ty(&mut self, t: &'a ast::Ty) {
1377 if let ast::TyKind::ImplTrait(_) = t.node {
1378 if self.is_in_impl_trait {
1379 gate_feature_post!(&self, nested_impl_trait, t.span,
1380 "nested `impl Trait` is experimental"
1381 );
1382 }
1383 self.with_impl_trait(true, |this| visit::walk_ty(this, t));
1384 } else {
1385 visit::walk_ty(self, t);
1386 }
1387 }
1388 fn visit_path_parameters(&mut self, _: Span, path_parameters: &'a ast::PathParameters) {
1389 match *path_parameters {
1390 ast::PathParameters::AngleBracketed(ref params) => {
1391 for type_ in &params.types {
1392 self.visit_ty(type_);
1393 }
1394 for type_binding in &params.bindings {
1395 // Type bindings such as `Item=impl Debug` in `Iterator<Item=Debug>`
1396 // are allowed to contain nested `impl Trait`.
1397 self.with_impl_trait(false, |this| visit::walk_ty(this, &type_binding.ty));
1398 }
1399 }
1400 ast::PathParameters::Parenthesized(ref params) => {
1401 for type_ in &params.inputs {
1402 self.visit_ty(type_);
1403 }
1404 if let Some(ref type_) = params.output {
1405 // `-> Foo` syntax is essentially an associated type binding,
1406 // so it is also allowed to contain nested `impl Trait`.
1407 self.with_impl_trait(false, |this| visit::walk_ty(this, type_));
1408 }
1409 }
1410 }
1411 }
1412 }
1413
1414 impl<'a> PostExpansionVisitor<'a> {
1415 fn whole_crate_feature_gates(&mut self, krate: &ast::Crate) {
1416 visit::walk_crate(
1417 &mut NestedImplTraitVisitor {
1418 context: self.context,
1419 is_in_impl_trait: false,
1420 }, krate);
1421
1422 for &(ident, span) in &*self.context.parse_sess.non_modrs_mods.borrow() {
1423 if !span.allows_unstable() {
1424 let cx = &self.context;
1425 let level = GateStrength::Hard;
1426 let has_feature = cx.features.non_modrs_mods;
1427 let name = "non_modrs_mods";
1428 debug!("gate_feature(feature = {:?}, span = {:?}); has? {}",
1429 name, span, has_feature);
1430
1431 if !has_feature && !span.allows_unstable() {
1432 leveled_feature_err(
1433 cx.parse_sess, name, span, GateIssue::Language,
1434 "mod statements in non-mod.rs files are unstable", level
1435 )
1436 .help(&format!("on stable builds, rename this file to {}{}mod.rs",
1437 ident, path::MAIN_SEPARATOR))
1438 .emit();
1439 }
1440 }
1441 }
1442 }
1443 }
1444
1445 impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
1446 fn visit_attribute(&mut self, attr: &ast::Attribute) {
1447 if !attr.span.allows_unstable() {
1448 // check for gated attributes
1449 self.context.check_attribute(attr, false);
1450 }
1451
1452 if attr.check_name("doc") {
1453 if let Some(content) = attr.meta_item_list() {
1454 if content.len() == 1 && content[0].check_name("cfg") {
1455 gate_feature_post!(&self, doc_cfg, attr.span,
1456 "#[doc(cfg(...))] is experimental"
1457 );
1458 } else if content.iter().any(|c| c.check_name("masked")) {
1459 gate_feature_post!(&self, doc_masked, attr.span,
1460 "#[doc(masked)] is experimental"
1461 );
1462 } else if content.iter().any(|c| c.check_name("spotlight")) {
1463 gate_feature_post!(&self, doc_spotlight, attr.span,
1464 "#[doc(spotlight)] is experimental"
1465 );
1466 }
1467 }
1468 }
1469
1470 // allow attr_literals in #[repr(align(x))]
1471 let mut is_repr_align = false;
1472 if attr.path == "repr" {
1473 if let Some(content) = attr.meta_item_list() {
1474 is_repr_align = content.iter().any(|c| c.check_name("align"));
1475 }
1476 }
1477
1478 if self.context.features.proc_macro && attr::is_known(attr) {
1479 return
1480 }
1481
1482 if !is_repr_align {
1483 let meta = panictry!(attr.parse_meta(self.context.parse_sess));
1484 if contains_novel_literal(&meta) {
1485 gate_feature_post!(&self, attr_literals, attr.span,
1486 "non-string literals in attributes, or string \
1487 literals in top-level positions, are experimental");
1488 }
1489 }
1490 }
1491
1492 fn visit_name(&mut self, sp: Span, name: ast::Name) {
1493 if !name.as_str().is_ascii() {
1494 gate_feature_post!(&self,
1495 non_ascii_idents,
1496 self.context.parse_sess.codemap().def_span(sp),
1497 "non-ascii idents are not fully supported.");
1498 }
1499 }
1500
1501 fn visit_item(&mut self, i: &'a ast::Item) {
1502 match i.node {
1503 ast::ItemKind::ExternCrate(_) => {
1504 if let Some(attr) = attr::find_by_name(&i.attrs[..], "macro_reexport") {
1505 gate_feature_post!(&self, macro_reexport, attr.span,
1506 "macros re-exports are experimental \
1507 and possibly buggy");
1508 }
1509 }
1510
1511 ast::ItemKind::ForeignMod(ref foreign_module) => {
1512 self.check_abi(foreign_module.abi, i.span);
1513 }
1514
1515 ast::ItemKind::Fn(..) => {
1516 if attr::contains_name(&i.attrs[..], "plugin_registrar") {
1517 gate_feature_post!(&self, plugin_registrar, i.span,
1518 "compiler plugins are experimental and possibly buggy");
1519 }
1520 if attr::contains_name(&i.attrs[..], "start") {
1521 gate_feature_post!(&self, start, i.span,
1522 "a #[start] function is an experimental \
1523 feature whose signature may change \
1524 over time");
1525 }
1526 if attr::contains_name(&i.attrs[..], "main") {
1527 gate_feature_post!(&self, main, i.span,
1528 "declaration of a nonstandard #[main] \
1529 function may change over time, for now \
1530 a top-level `fn main()` is required");
1531 }
1532 if let Some(attr) = attr::find_by_name(&i.attrs[..], "must_use") {
1533 gate_feature_post!(&self, fn_must_use, attr.span,
1534 "`#[must_use]` on functions is experimental",
1535 GateStrength::Soft);
1536 }
1537 }
1538
1539 ast::ItemKind::Struct(..) => {
1540 if let Some(attr) = attr::find_by_name(&i.attrs[..], "repr") {
1541 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
1542 if item.check_name("simd") {
1543 gate_feature_post!(&self, repr_simd, attr.span,
1544 "SIMD types are experimental and possibly buggy");
1545 }
1546 if item.check_name("transparent") {
1547 gate_feature_post!(&self, repr_transparent, attr.span,
1548 "the `#[repr(transparent)]` attribute \
1549 is experimental");
1550 }
1551 }
1552 }
1553 }
1554
1555 ast::ItemKind::TraitAlias(..) => {
1556 gate_feature_post!(&self, trait_alias,
1557 i.span,
1558 "trait aliases are not yet fully implemented");
1559 }
1560
1561 ast::ItemKind::Impl(_, polarity, defaultness, _, _, _, ref impl_items) => {
1562 if polarity == ast::ImplPolarity::Negative {
1563 gate_feature_post!(&self, optin_builtin_traits,
1564 i.span,
1565 "negative trait bounds are not yet fully implemented; \
1566 use marker types for now");
1567 }
1568
1569 if let ast::Defaultness::Default = defaultness {
1570 gate_feature_post!(&self, specialization,
1571 i.span,
1572 "specialization is unstable");
1573 }
1574
1575 for impl_item in impl_items {
1576 if let ast::ImplItemKind::Method(..) = impl_item.node {
1577 if let Some(attr) = attr::find_by_name(&impl_item.attrs[..], "must_use") {
1578 gate_feature_post!(&self, fn_must_use, attr.span,
1579 "`#[must_use]` on methods is experimental",
1580 GateStrength::Soft);
1581 }
1582 }
1583 }
1584 }
1585
1586 ast::ItemKind::Trait(ast::IsAuto::Yes, ..) => {
1587 gate_feature_post!(&self, optin_builtin_traits,
1588 i.span,
1589 "auto traits are experimental and possibly buggy");
1590 }
1591
1592 ast::ItemKind::MacroDef(ast::MacroDef { legacy: false, .. }) => {
1593 let msg = "`macro` is experimental";
1594 gate_feature_post!(&self, decl_macro, i.span, msg);
1595 }
1596
1597 _ => {}
1598 }
1599
1600 visit::walk_item(self, i);
1601 }
1602
1603 fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
1604 match i.node {
1605 ast::ForeignItemKind::Fn(..) |
1606 ast::ForeignItemKind::Static(..) => {
1607 let link_name = attr::first_attr_value_str_by_name(&i.attrs, "link_name");
1608 let links_to_llvm = match link_name {
1609 Some(val) => val.as_str().starts_with("llvm."),
1610 _ => false
1611 };
1612 if links_to_llvm {
1613 gate_feature_post!(&self, link_llvm_intrinsics, i.span,
1614 "linking to LLVM intrinsics is experimental");
1615 }
1616 }
1617 ast::ForeignItemKind::Ty => {
1618 gate_feature_post!(&self, extern_types, i.span,
1619 "extern types are experimental");
1620 }
1621 }
1622
1623 visit::walk_foreign_item(self, i)
1624 }
1625
1626 fn visit_ty(&mut self, ty: &'a ast::Ty) {
1627 match ty.node {
1628 ast::TyKind::BareFn(ref bare_fn_ty) => {
1629 self.check_abi(bare_fn_ty.abi, ty.span);
1630 }
1631 ast::TyKind::Never => {
1632 gate_feature_post!(&self, never_type, ty.span,
1633 "The `!` type is experimental");
1634 },
1635 ast::TyKind::TraitObject(_, ast::TraitObjectSyntax::Dyn) => {
1636 gate_feature_post!(&self, dyn_trait, ty.span,
1637 "`dyn Trait` syntax is unstable");
1638 }
1639 _ => {}
1640 }
1641 visit::walk_ty(self, ty)
1642 }
1643
1644 fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FunctionRetTy) {
1645 if let ast::FunctionRetTy::Ty(ref output_ty) = *ret_ty {
1646 if output_ty.node != ast::TyKind::Never {
1647 self.visit_ty(output_ty)
1648 }
1649 }
1650 }
1651
1652 fn visit_expr(&mut self, e: &'a ast::Expr) {
1653 match e.node {
1654 ast::ExprKind::Box(_) => {
1655 gate_feature_post!(&self, box_syntax, e.span, EXPLAIN_BOX_SYNTAX);
1656 }
1657 ast::ExprKind::Type(..) => {
1658 gate_feature_post!(&self, type_ascription, e.span,
1659 "type ascription is experimental");
1660 }
1661 ast::ExprKind::Range(_, _, ast::RangeLimits::Closed) => {
1662 gate_feature_post!(&self, inclusive_range_syntax,
1663 e.span,
1664 "inclusive range syntax is experimental");
1665 }
1666 ast::ExprKind::InPlace(..) => {
1667 gate_feature_post!(&self, placement_in_syntax, e.span, EXPLAIN_PLACEMENT_IN);
1668 }
1669 ast::ExprKind::Yield(..) => {
1670 gate_feature_post!(&self, generators,
1671 e.span,
1672 "yield syntax is experimental");
1673 }
1674 ast::ExprKind::Lit(ref lit) => {
1675 if let ast::LitKind::Int(_, ref ty) = lit.node {
1676 match *ty {
1677 ast::LitIntType::Signed(ast::IntTy::I128) |
1678 ast::LitIntType::Unsigned(ast::UintTy::U128) => {
1679 gate_feature_post!(&self, i128_type, e.span,
1680 "128-bit integers are not stable");
1681 }
1682 _ => {}
1683 }
1684 }
1685 }
1686 ast::ExprKind::Catch(_) => {
1687 gate_feature_post!(&self, catch_expr, e.span, "`catch` expression is experimental");
1688 }
1689 _ => {}
1690 }
1691 visit::walk_expr(self, e);
1692 }
1693
1694 fn visit_arm(&mut self, arm: &'a ast::Arm) {
1695 visit::walk_arm(self, arm)
1696 }
1697
1698 fn visit_pat(&mut self, pattern: &'a ast::Pat) {
1699 match pattern.node {
1700 PatKind::Slice(_, Some(_), ref last) if !last.is_empty() => {
1701 gate_feature_post!(&self, advanced_slice_patterns,
1702 pattern.span,
1703 "multiple-element slice matches anywhere \
1704 but at the end of a slice (e.g. \
1705 `[0, ..xs, 0]`) are experimental")
1706 }
1707 PatKind::Slice(..) => {
1708 gate_feature_post!(&self, slice_patterns,
1709 pattern.span,
1710 "slice pattern syntax is experimental");
1711 }
1712 PatKind::Box(..) => {
1713 gate_feature_post!(&self, box_patterns,
1714 pattern.span,
1715 "box pattern syntax is experimental");
1716 }
1717 PatKind::Range(_, _, RangeEnd::Excluded) => {
1718 gate_feature_post!(&self, exclusive_range_pattern, pattern.span,
1719 "exclusive range pattern syntax is experimental");
1720 }
1721 PatKind::Range(_, _, RangeEnd::Included(RangeSyntax::DotDotEq)) => {
1722 gate_feature_post!(&self, dotdoteq_in_patterns, pattern.span,
1723 "`..=` syntax in patterns is experimental");
1724 }
1725 _ => {}
1726 }
1727 visit::walk_pat(self, pattern)
1728 }
1729
1730 fn visit_fn(&mut self,
1731 fn_kind: FnKind<'a>,
1732 fn_decl: &'a ast::FnDecl,
1733 span: Span,
1734 _node_id: NodeId) {
1735 // check for const fn declarations
1736 if let FnKind::ItemFn(_, _, Spanned { node: ast::Constness::Const, .. }, _, _, _) =
1737 fn_kind {
1738 gate_feature_post!(&self, const_fn, span, "const fn is unstable");
1739 }
1740 // stability of const fn methods are covered in
1741 // visit_trait_item and visit_impl_item below; this is
1742 // because default methods don't pass through this
1743 // point.
1744
1745 match fn_kind {
1746 FnKind::ItemFn(_, _, _, abi, _, _) |
1747 FnKind::Method(_, &ast::MethodSig { abi, .. }, _, _) => {
1748 self.check_abi(abi, span);
1749 }
1750 _ => {}
1751 }
1752 visit::walk_fn(self, fn_kind, fn_decl, span);
1753 }
1754
1755 fn visit_trait_item(&mut self, ti: &'a ast::TraitItem) {
1756 match ti.node {
1757 ast::TraitItemKind::Method(ref sig, ref block) => {
1758 if block.is_none() {
1759 self.check_abi(sig.abi, ti.span);
1760 }
1761 if sig.constness.node == ast::Constness::Const {
1762 gate_feature_post!(&self, const_fn, ti.span, "const fn is unstable");
1763 }
1764 }
1765 ast::TraitItemKind::Type(_, ref default) => {
1766 // We use two if statements instead of something like match guards so that both
1767 // of these errors can be emitted if both cases apply.
1768 if default.is_some() {
1769 gate_feature_post!(&self, associated_type_defaults, ti.span,
1770 "associated type defaults are unstable");
1771 }
1772 if ti.generics.is_parameterized() {
1773 gate_feature_post!(&self, generic_associated_types, ti.span,
1774 "generic associated types are unstable");
1775 }
1776 }
1777 _ => {}
1778 }
1779 visit::walk_trait_item(self, ti);
1780 }
1781
1782 fn visit_impl_item(&mut self, ii: &'a ast::ImplItem) {
1783 if ii.defaultness == ast::Defaultness::Default {
1784 gate_feature_post!(&self, specialization,
1785 ii.span,
1786 "specialization is unstable");
1787 }
1788
1789 match ii.node {
1790 ast::ImplItemKind::Method(ref sig, _) => {
1791 if sig.constness.node == ast::Constness::Const {
1792 gate_feature_post!(&self, const_fn, ii.span, "const fn is unstable");
1793 }
1794 }
1795 ast::ImplItemKind::Type(_) if ii.generics.is_parameterized() => {
1796 gate_feature_post!(&self, generic_associated_types, ii.span,
1797 "generic associated types are unstable");
1798 }
1799 _ => {}
1800 }
1801 visit::walk_impl_item(self, ii);
1802 }
1803
1804 fn visit_path(&mut self, path: &'a ast::Path, _id: NodeId) {
1805 for segment in &path.segments {
1806 if segment.identifier.name == keywords::Crate.name() {
1807 gate_feature_post!(&self, crate_in_paths, segment.span,
1808 "`crate` in paths is experimental");
1809 } else if segment.identifier.name == keywords::Extern.name() {
1810 gate_feature_post!(&self, extern_in_paths, segment.span,
1811 "`extern` in paths is experimental");
1812 }
1813 }
1814
1815 visit::walk_path(self, path);
1816 }
1817
1818 fn visit_vis(&mut self, vis: &'a ast::Visibility) {
1819 if let ast::Visibility::Crate(span, ast::CrateSugar::JustCrate) = *vis {
1820 gate_feature_post!(&self, crate_visibility_modifier, span,
1821 "`crate` visibility modifier is experimental");
1822 }
1823 visit::walk_vis(self, vis);
1824 }
1825
1826 fn visit_generic_param(&mut self, param: &'a ast::GenericParam) {
1827 let (attrs, explain) = match *param {
1828 ast::GenericParam::Lifetime(ref ld) =>
1829 (&ld.attrs, "attributes on lifetime bindings are experimental"),
1830 ast::GenericParam::Type(ref t) =>
1831 (&t.attrs, "attributes on type parameter bindings are experimental"),
1832 };
1833
1834 if !attrs.is_empty() {
1835 gate_feature_post!(&self, generic_param_attrs, attrs[0].span, explain);
1836 }
1837
1838 visit::walk_generic_param(self, param)
1839 }
1840
1841 fn visit_lifetime(&mut self, lt: &'a ast::Lifetime) {
1842 if lt.ident.name == "'_" {
1843 gate_feature_post!(&self, underscore_lifetimes, lt.span,
1844 "underscore lifetimes are unstable");
1845 }
1846 visit::walk_lifetime(self, lt)
1847 }
1848 }
1849
1850 pub fn get_features(span_handler: &Handler, krate_attrs: &[ast::Attribute]) -> Features {
1851 let mut features = Features::new();
1852
1853 let mut feature_checker = FeatureChecker::default();
1854
1855 for attr in krate_attrs {
1856 if !attr.check_name("feature") {
1857 continue
1858 }
1859
1860 match attr.meta_item_list() {
1861 None => {
1862 span_err!(span_handler, attr.span, E0555,
1863 "malformed feature attribute, expected #![feature(...)]");
1864 }
1865 Some(list) => {
1866 for mi in list {
1867 let name = if let Some(word) = mi.word() {
1868 word.name()
1869 } else {
1870 span_err!(span_handler, mi.span, E0556,
1871 "malformed feature, expected just one word");
1872 continue
1873 };
1874
1875 if let Some(&(_, _, _, set)) = ACTIVE_FEATURES.iter()
1876 .find(|& &(n, _, _, _)| name == n) {
1877 set(&mut features, mi.span);
1878 feature_checker.collect(&features, mi.span);
1879 }
1880 else if let Some(&(_, _, _)) = REMOVED_FEATURES.iter()
1881 .find(|& &(n, _, _)| name == n)
1882 .or_else(|| STABLE_REMOVED_FEATURES.iter()
1883 .find(|& &(n, _, _)| name == n)) {
1884 span_err!(span_handler, mi.span, E0557, "feature has been removed");
1885 }
1886 else if let Some(&(_, _, _)) = ACCEPTED_FEATURES.iter()
1887 .find(|& &(n, _, _)| name == n) {
1888 features.declared_stable_lang_features.push((name, mi.span));
1889 } else {
1890 features.declared_lib_features.push((name, mi.span));
1891 }
1892 }
1893 }
1894 }
1895 }
1896
1897 feature_checker.check(span_handler);
1898
1899 features
1900 }
1901
1902 /// A collector for mutually exclusive and interdependent features and their flag spans.
1903 #[derive(Default)]
1904 struct FeatureChecker {
1905 proc_macro: Option<Span>,
1906 custom_attribute: Option<Span>,
1907 copy_closures: Option<Span>,
1908 clone_closures: Option<Span>,
1909 }
1910
1911 impl FeatureChecker {
1912 // If this method turns out to be a hotspot due to branching,
1913 // the branching can be eliminated by modifying `set!()` to set these spans
1914 // only for the features that need to be checked for mutual exclusion.
1915 fn collect(&mut self, features: &Features, span: Span) {
1916 if features.proc_macro {
1917 // If self.proc_macro is None, set to Some(span)
1918 self.proc_macro = self.proc_macro.or(Some(span));
1919 }
1920
1921 if features.custom_attribute {
1922 self.custom_attribute = self.custom_attribute.or(Some(span));
1923 }
1924
1925 if features.copy_closures {
1926 self.copy_closures = self.copy_closures.or(Some(span));
1927 }
1928
1929 if features.clone_closures {
1930 self.clone_closures = self.clone_closures.or(Some(span));
1931 }
1932 }
1933
1934 fn check(self, handler: &Handler) {
1935 if let (Some(pm_span), Some(ca_span)) = (self.proc_macro, self.custom_attribute) {
1936 handler.struct_span_err(pm_span, "Cannot use `#![feature(proc_macro)]` and \
1937 `#![feature(custom_attribute)] at the same time")
1938 .span_note(ca_span, "`#![feature(custom_attribute)]` declared here")
1939 .emit();
1940
1941 FatalError.raise();
1942 }
1943
1944 if let (Some(span), None) = (self.copy_closures, self.clone_closures) {
1945 handler.struct_span_err(span, "`#![feature(copy_closures)]` can only be used with \
1946 `#![feature(clone_closures)]`")
1947 .span_note(span, "`#![feature(copy_closures)]` declared here")
1948 .emit();
1949
1950 FatalError.raise();
1951 }
1952 }
1953 }
1954
1955 pub fn check_crate(krate: &ast::Crate,
1956 sess: &ParseSess,
1957 features: &Features,
1958 plugin_attributes: &[(String, AttributeType)],
1959 unstable: UnstableFeatures) {
1960 maybe_stage_features(&sess.span_diagnostic, krate, unstable);
1961 let ctx = Context {
1962 features,
1963 parse_sess: sess,
1964 plugin_attributes,
1965 };
1966 let visitor = &mut PostExpansionVisitor { context: &ctx };
1967 visitor.whole_crate_feature_gates(krate);
1968 visit::walk_crate(visitor, krate);
1969 }
1970
1971 #[derive(Clone, Copy, PartialEq, Eq, Hash)]
1972 pub enum UnstableFeatures {
1973 /// Hard errors for unstable features are active, as on
1974 /// beta/stable channels.
1975 Disallow,
1976 /// Allow features to be activated, as on nightly.
1977 Allow,
1978 /// Errors are bypassed for bootstrapping. This is required any time
1979 /// during the build that feature-related lints are set to warn or above
1980 /// because the build turns on warnings-as-errors and uses lots of unstable
1981 /// features. As a result, this is always required for building Rust itself.
1982 Cheat
1983 }
1984
1985 impl UnstableFeatures {
1986 pub fn from_environment() -> UnstableFeatures {
1987 // Whether this is a feature-staged build, i.e. on the beta or stable channel
1988 let disable_unstable_features = option_env!("CFG_DISABLE_UNSTABLE_FEATURES").is_some();
1989 // Whether we should enable unstable features for bootstrapping
1990 let bootstrap = env::var("RUSTC_BOOTSTRAP").is_ok();
1991 match (disable_unstable_features, bootstrap) {
1992 (_, true) => UnstableFeatures::Cheat,
1993 (true, _) => UnstableFeatures::Disallow,
1994 (false, _) => UnstableFeatures::Allow
1995 }
1996 }
1997
1998 pub fn is_nightly_build(&self) -> bool {
1999 match *self {
2000 UnstableFeatures::Allow | UnstableFeatures::Cheat => true,
2001 _ => false,
2002 }
2003 }
2004 }
2005
2006 fn maybe_stage_features(span_handler: &Handler, krate: &ast::Crate,
2007 unstable: UnstableFeatures) {
2008 let allow_features = match unstable {
2009 UnstableFeatures::Allow => true,
2010 UnstableFeatures::Disallow => false,
2011 UnstableFeatures::Cheat => true
2012 };
2013 if !allow_features {
2014 for attr in &krate.attrs {
2015 if attr.check_name("feature") {
2016 let release_channel = option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)");
2017 span_err!(span_handler, attr.span, E0554,
2018 "#![feature] may not be used on the {} release channel",
2019 release_channel);
2020 }
2021 }
2022 }
2023 }