]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/feature_gate.rs
Rollup merge of #102158 - notriddle:notriddle/stab-p, r=GuillaumeGomez
[rust.git] / compiler / rustc_ast_passes / src / feature_gate.rs
1 use rustc_ast as ast;
2 use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
3 use rustc_ast::{AssocConstraint, AssocConstraintKind, NodeId};
4 use rustc_ast::{PatKind, RangeEnd, VariantData};
5 use rustc_errors::{struct_span_err, Applicability, StashKey};
6 use rustc_feature::{AttributeGate, BuiltinAttribute, Features, GateIssue, BUILTIN_ATTRIBUTE_MAP};
7 use rustc_session::parse::{feature_err, feature_err_issue, feature_warn};
8 use rustc_session::Session;
9 use rustc_span::source_map::Spanned;
10 use rustc_span::symbol::sym;
11 use rustc_span::Span;
12 use rustc_target::spec::abi;
13
14 macro_rules! gate_feature_fn {
15     ($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr, $help: expr) => {{
16         let (visitor, has_feature, span, name, explain, help) =
17             (&*$visitor, $has_feature, $span, $name, $explain, $help);
18         let has_feature: bool = has_feature(visitor.features);
19         debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature);
20         if !has_feature && !span.allows_unstable($name) {
21             feature_err(&visitor.sess.parse_sess, name, span, explain).help(help).emit();
22         }
23     }};
24     ($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{
25         let (visitor, has_feature, span, name, explain) =
26             (&*$visitor, $has_feature, $span, $name, $explain);
27         let has_feature: bool = has_feature(visitor.features);
28         debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature);
29         if !has_feature && !span.allows_unstable($name) {
30             feature_err(&visitor.sess.parse_sess, name, span, explain).emit();
31         }
32     }};
33     (future_incompatible; $visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{
34         let (visitor, has_feature, span, name, explain) =
35             (&*$visitor, $has_feature, $span, $name, $explain);
36         let has_feature: bool = has_feature(visitor.features);
37         debug!(
38             "gate_feature(feature = {:?}, span = {:?}); has? {} (future_incompatible)",
39             name, span, has_feature
40         );
41         if !has_feature && !span.allows_unstable($name) {
42             feature_warn(&visitor.sess.parse_sess, name, span, explain);
43         }
44     }};
45 }
46
47 macro_rules! gate_feature_post {
48     ($visitor: expr, $feature: ident, $span: expr, $explain: expr, $help: expr) => {
49         gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain, $help)
50     };
51     ($visitor: expr, $feature: ident, $span: expr, $explain: expr) => {
52         gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain)
53     };
54     (future_incompatible; $visitor: expr, $feature: ident, $span: expr, $explain: expr) => {
55         gate_feature_fn!(future_incompatible; $visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain)
56     };
57 }
58
59 pub fn check_attribute(attr: &ast::Attribute, sess: &Session, features: &Features) {
60     PostExpansionVisitor { sess, features }.visit_attribute(attr)
61 }
62
63 struct PostExpansionVisitor<'a> {
64     sess: &'a Session,
65
66     // `sess` contains a `Features`, but this might not be that one.
67     features: &'a Features,
68 }
69
70 impl<'a> PostExpansionVisitor<'a> {
71     fn check_abi(&self, abi: ast::StrLit, constness: ast::Const) {
72         let ast::StrLit { symbol_unescaped, span, .. } = abi;
73
74         if let ast::Const::Yes(_) = constness {
75             match symbol_unescaped {
76                 // Stable
77                 sym::Rust | sym::C => {}
78                 abi => gate_feature_post!(
79                     &self,
80                     const_extern_fn,
81                     span,
82                     &format!("`{}` as a `const fn` ABI is unstable", abi)
83                 ),
84             }
85         }
86
87         match abi::is_enabled(&self.features, span, symbol_unescaped.as_str()) {
88             Ok(()) => (),
89             Err(abi::AbiDisabled::Unstable { feature, explain }) => {
90                 feature_err_issue(
91                     &self.sess.parse_sess,
92                     feature,
93                     span,
94                     GateIssue::Language,
95                     explain,
96                 )
97                 .emit();
98             }
99             Err(abi::AbiDisabled::Unrecognized) => {
100                 if self.sess.opts.pretty.map_or(true, |ppm| ppm.needs_hir()) {
101                     self.sess.parse_sess.span_diagnostic.delay_span_bug(
102                         span,
103                         &format!(
104                             "unrecognized ABI not caught in lowering: {}",
105                             symbol_unescaped.as_str()
106                         ),
107                     );
108                 }
109             }
110         }
111     }
112
113     fn check_extern(&self, ext: ast::Extern, constness: ast::Const) {
114         if let ast::Extern::Explicit(abi, _) = ext {
115             self.check_abi(abi, constness);
116         }
117     }
118
119     fn maybe_report_invalid_custom_discriminants(&self, variants: &[ast::Variant]) {
120         let has_fields = variants.iter().any(|variant| match variant.data {
121             VariantData::Tuple(..) | VariantData::Struct(..) => true,
122             VariantData::Unit(..) => false,
123         });
124
125         let discriminant_spans = variants
126             .iter()
127             .filter(|variant| match variant.data {
128                 VariantData::Tuple(..) | VariantData::Struct(..) => false,
129                 VariantData::Unit(..) => true,
130             })
131             .filter_map(|variant| variant.disr_expr.as_ref().map(|c| c.value.span))
132             .collect::<Vec<_>>();
133
134         if !discriminant_spans.is_empty() && has_fields {
135             let mut err = feature_err(
136                 &self.sess.parse_sess,
137                 sym::arbitrary_enum_discriminant,
138                 discriminant_spans.clone(),
139                 "custom discriminant values are not allowed in enums with tuple or struct variants",
140             );
141             for sp in discriminant_spans {
142                 err.span_label(sp, "disallowed custom discriminant");
143             }
144             for variant in variants.iter() {
145                 match &variant.data {
146                     VariantData::Struct(..) => {
147                         err.span_label(variant.span, "struct variant defined here");
148                     }
149                     VariantData::Tuple(..) => {
150                         err.span_label(variant.span, "tuple variant defined here");
151                     }
152                     VariantData::Unit(..) => {}
153                 }
154             }
155             err.emit();
156         }
157     }
158
159     /// Feature gate `impl Trait` inside `type Alias = $type_expr;`.
160     fn check_impl_trait(&self, ty: &ast::Ty) {
161         struct ImplTraitVisitor<'a> {
162             vis: &'a PostExpansionVisitor<'a>,
163         }
164         impl Visitor<'_> for ImplTraitVisitor<'_> {
165             fn visit_ty(&mut self, ty: &ast::Ty) {
166                 if let ast::TyKind::ImplTrait(..) = ty.kind {
167                     gate_feature_post!(
168                         &self.vis,
169                         type_alias_impl_trait,
170                         ty.span,
171                         "`impl Trait` in type aliases is unstable"
172                     );
173                 }
174                 visit::walk_ty(self, ty);
175             }
176         }
177         ImplTraitVisitor { vis: self }.visit_ty(ty);
178     }
179 }
180
181 impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
182     fn visit_attribute(&mut self, attr: &ast::Attribute) {
183         let attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
184         // Check feature gates for built-in attributes.
185         if let Some(BuiltinAttribute {
186             gate: AttributeGate::Gated(_, name, descr, has_feature),
187             ..
188         }) = attr_info
189         {
190             gate_feature_fn!(self, has_feature, attr.span, *name, descr);
191         }
192         // Check unstable flavors of the `#[doc]` attribute.
193         if attr.has_name(sym::doc) {
194             for nested_meta in attr.meta_item_list().unwrap_or_default() {
195                 macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => {
196                     $(if nested_meta.has_name(sym::$name) {
197                         let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental");
198                         gate_feature_post!(self, $feature, attr.span, msg);
199                     })*
200                 }}
201
202                 gate_doc!(
203                     cfg => doc_cfg
204                     cfg_hide => doc_cfg_hide
205                     masked => doc_masked
206                     notable_trait => doc_notable_trait
207                 );
208
209                 if nested_meta.has_name(sym::keyword) {
210                     let msg = "`#[doc(keyword)]` is meant for internal use only";
211                     gate_feature_post!(self, rustdoc_internals, attr.span, msg);
212                 }
213
214                 if nested_meta.has_name(sym::fake_variadic) {
215                     let msg = "`#[doc(fake_variadic)]` is meant for internal use only";
216                     gate_feature_post!(self, rustdoc_internals, attr.span, msg);
217                 }
218             }
219         }
220
221         // Emit errors for non-staged-api crates.
222         if !self.features.staged_api {
223             if attr.has_name(sym::unstable)
224                 || attr.has_name(sym::stable)
225                 || attr.has_name(sym::rustc_const_unstable)
226                 || attr.has_name(sym::rustc_const_stable)
227                 || attr.has_name(sym::rustc_default_body_unstable)
228             {
229                 struct_span_err!(
230                     self.sess,
231                     attr.span,
232                     E0734,
233                     "stability attributes may not be used outside of the standard library",
234                 )
235                 .emit();
236             }
237         }
238     }
239
240     fn visit_item(&mut self, i: &'a ast::Item) {
241         match i.kind {
242             ast::ItemKind::ForeignMod(ref foreign_module) => {
243                 if let Some(abi) = foreign_module.abi {
244                     self.check_abi(abi, ast::Const::No);
245                 }
246             }
247
248             ast::ItemKind::Fn(..) => {
249                 if self.sess.contains_name(&i.attrs, sym::start) {
250                     gate_feature_post!(
251                         &self,
252                         start,
253                         i.span,
254                         "`#[start]` functions are experimental \
255                          and their signature may change \
256                          over time"
257                     );
258                 }
259             }
260
261             ast::ItemKind::Struct(..) => {
262                 for attr in self.sess.filter_by_name(&i.attrs, sym::repr) {
263                     for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
264                         if item.has_name(sym::simd) {
265                             gate_feature_post!(
266                                 &self,
267                                 repr_simd,
268                                 attr.span,
269                                 "SIMD types are experimental and possibly buggy"
270                             );
271                         }
272                     }
273                 }
274             }
275
276             ast::ItemKind::Enum(ast::EnumDef { ref variants, .. }, ..) => {
277                 for variant in variants {
278                     match (&variant.data, &variant.disr_expr) {
279                         (ast::VariantData::Unit(..), _) => {}
280                         (_, Some(disr_expr)) => gate_feature_post!(
281                             &self,
282                             arbitrary_enum_discriminant,
283                             disr_expr.value.span,
284                             "discriminants on non-unit variants are experimental"
285                         ),
286                         _ => {}
287                     }
288                 }
289
290                 let has_feature = self.features.arbitrary_enum_discriminant;
291                 if !has_feature && !i.span.allows_unstable(sym::arbitrary_enum_discriminant) {
292                     self.maybe_report_invalid_custom_discriminants(&variants);
293                 }
294             }
295
296             ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, ref of_trait, .. }) => {
297                 if let ast::ImplPolarity::Negative(span) = polarity {
298                     gate_feature_post!(
299                         &self,
300                         negative_impls,
301                         span.to(of_trait.as_ref().map_or(span, |t| t.path.span)),
302                         "negative trait bounds are not yet fully implemented; \
303                          use marker types for now"
304                     );
305                 }
306
307                 if let ast::Defaultness::Default(_) = defaultness {
308                     gate_feature_post!(&self, specialization, i.span, "specialization is unstable");
309                 }
310             }
311
312             ast::ItemKind::Trait(box ast::Trait { is_auto: ast::IsAuto::Yes, .. }) => {
313                 gate_feature_post!(
314                     &self,
315                     auto_traits,
316                     i.span,
317                     "auto traits are experimental and possibly buggy"
318                 );
319             }
320
321             ast::ItemKind::TraitAlias(..) => {
322                 gate_feature_post!(&self, trait_alias, i.span, "trait aliases are experimental");
323             }
324
325             ast::ItemKind::MacroDef(ast::MacroDef { macro_rules: false, .. }) => {
326                 let msg = "`macro` is experimental";
327                 gate_feature_post!(&self, decl_macro, i.span, msg);
328             }
329
330             ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ref ty), .. }) => {
331                 self.check_impl_trait(&ty)
332             }
333
334             _ => {}
335         }
336
337         visit::walk_item(self, i);
338     }
339
340     fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
341         match i.kind {
342             ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
343                 let link_name = self.sess.first_attr_value_str_by_name(&i.attrs, sym::link_name);
344                 let links_to_llvm =
345                     link_name.map_or(false, |val| val.as_str().starts_with("llvm."));
346                 if links_to_llvm {
347                     gate_feature_post!(
348                         &self,
349                         link_llvm_intrinsics,
350                         i.span,
351                         "linking to LLVM intrinsics is experimental"
352                     );
353                 }
354             }
355             ast::ForeignItemKind::TyAlias(..) => {
356                 gate_feature_post!(&self, extern_types, i.span, "extern types are experimental");
357             }
358             ast::ForeignItemKind::MacCall(..) => {}
359         }
360
361         visit::walk_foreign_item(self, i)
362     }
363
364     fn visit_ty(&mut self, ty: &'a ast::Ty) {
365         match ty.kind {
366             ast::TyKind::BareFn(ref bare_fn_ty) => {
367                 // Function pointers cannot be `const`
368                 self.check_extern(bare_fn_ty.ext, ast::Const::No);
369             }
370             ast::TyKind::Never => {
371                 gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental");
372             }
373             ast::TyKind::TraitObject(_, ast::TraitObjectSyntax::DynStar, ..) => {
374                 gate_feature_post!(&self, dyn_star, ty.span, "dyn* trait objects are unstable");
375             }
376             _ => {}
377         }
378         visit::walk_ty(self, ty)
379     }
380
381     fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) {
382         if let ast::FnRetTy::Ty(ref output_ty) = *ret_ty {
383             if let ast::TyKind::Never = output_ty.kind {
384                 // Do nothing.
385             } else {
386                 self.visit_ty(output_ty)
387             }
388         }
389     }
390
391     fn visit_stmt(&mut self, stmt: &'a ast::Stmt) {
392         if let ast::StmtKind::Semi(expr) = &stmt.kind
393             && let ast::ExprKind::Assign(lhs, _, _) = &expr.kind
394             && let ast::ExprKind::Type(..) = lhs.kind
395             && self.sess.parse_sess.span_diagnostic.err_count() == 0
396             && !self.features.type_ascription
397             && !lhs.span.allows_unstable(sym::type_ascription)
398         {
399             // When we encounter a statement of the form `foo: Ty = val;`, this will emit a type
400             // ascription error, but the likely intention was to write a `let` statement. (#78907).
401             feature_err(
402                 &self.sess.parse_sess,
403                 sym::type_ascription,
404                 lhs.span,
405                 "type ascription is experimental",
406             ).span_suggestion_verbose(
407                 lhs.span.shrink_to_lo(),
408                 "you might have meant to introduce a new binding",
409                 "let ".to_string(),
410                 Applicability::MachineApplicable,
411             ).emit();
412         }
413         visit::walk_stmt(self, stmt);
414     }
415
416     fn visit_expr(&mut self, e: &'a ast::Expr) {
417         match e.kind {
418             ast::ExprKind::Box(_) => {
419                 gate_feature_post!(
420                     &self,
421                     box_syntax,
422                     e.span,
423                     "box expression syntax is experimental; you can call `Box::new` instead"
424                 );
425             }
426             ast::ExprKind::Type(..) => {
427                 if self.sess.parse_sess.span_diagnostic.err_count() == 0 {
428                     // To avoid noise about type ascription in common syntax errors,
429                     // only emit if it is the *only* error.
430                     gate_feature_post!(
431                         &self,
432                         type_ascription,
433                         e.span,
434                         "type ascription is experimental"
435                     );
436                 } else {
437                     // And if it isn't, cancel the early-pass warning.
438                     self.sess
439                         .parse_sess
440                         .span_diagnostic
441                         .steal_diagnostic(e.span, StashKey::EarlySyntaxWarning)
442                         .map(|err| err.cancel());
443                 }
444             }
445             ast::ExprKind::TryBlock(_) => {
446                 gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
447             }
448             _ => {}
449         }
450         visit::walk_expr(self, e)
451     }
452
453     fn visit_pat(&mut self, pattern: &'a ast::Pat) {
454         match &pattern.kind {
455             PatKind::Slice(pats) => {
456                 for pat in pats {
457                     let inner_pat = match &pat.kind {
458                         PatKind::Ident(.., Some(pat)) => pat,
459                         _ => pat,
460                     };
461                     if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
462                         gate_feature_post!(
463                             &self,
464                             half_open_range_patterns,
465                             pat.span,
466                             "`X..` patterns in slices are experimental"
467                         );
468                     }
469                 }
470             }
471             PatKind::Box(..) => {
472                 gate_feature_post!(
473                     &self,
474                     box_patterns,
475                     pattern.span,
476                     "box pattern syntax is experimental"
477                 );
478             }
479             PatKind::Range(_, Some(_), Spanned { node: RangeEnd::Excluded, .. }) => {
480                 gate_feature_post!(
481                     &self,
482                     exclusive_range_pattern,
483                     pattern.span,
484                     "exclusive range pattern syntax is experimental"
485                 );
486             }
487             _ => {}
488         }
489         visit::walk_pat(self, pattern)
490     }
491
492     fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
493         if let Some(header) = fn_kind.header() {
494             // Stability of const fn methods are covered in `visit_assoc_item` below.
495             self.check_extern(header.ext, header.constness);
496         }
497
498         if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() {
499             gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable");
500         }
501
502         visit::walk_fn(self, fn_kind)
503     }
504
505     fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) {
506         if let AssocConstraintKind::Bound { .. } = constraint.kind {
507             gate_feature_post!(
508                 &self,
509                 associated_type_bounds,
510                 constraint.span,
511                 "associated type bounds are unstable"
512             )
513         }
514         visit::walk_assoc_constraint(self, constraint)
515     }
516
517     fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
518         let is_fn = match i.kind {
519             ast::AssocItemKind::Fn(_) => true,
520             ast::AssocItemKind::TyAlias(box ast::TyAlias { ref ty, .. }) => {
521                 if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
522                     gate_feature_post!(
523                         &self,
524                         associated_type_defaults,
525                         i.span,
526                         "associated type defaults are unstable"
527                     );
528                 }
529                 if let Some(ty) = ty {
530                     self.check_impl_trait(ty);
531                 }
532                 false
533             }
534             _ => false,
535         };
536         if let ast::Defaultness::Default(_) = i.kind.defaultness() {
537             // Limit `min_specialization` to only specializing functions.
538             gate_feature_fn!(
539                 &self,
540                 |x: &Features| x.specialization || (is_fn && x.min_specialization),
541                 i.span,
542                 sym::specialization,
543                 "specialization is unstable"
544             );
545         }
546         visit::walk_assoc_item(self, i, ctxt)
547     }
548 }
549
550 pub fn check_crate(krate: &ast::Crate, sess: &Session) {
551     maybe_stage_features(sess, krate);
552     check_incompatible_features(sess);
553     let mut visitor = PostExpansionVisitor { sess, features: &sess.features_untracked() };
554
555     let spans = sess.parse_sess.gated_spans.spans.borrow();
556     macro_rules! gate_all {
557         ($gate:ident, $msg:literal, $help:literal) => {
558             if let Some(spans) = spans.get(&sym::$gate) {
559                 for span in spans {
560                     gate_feature_post!(&visitor, $gate, *span, $msg, $help);
561                 }
562             }
563         };
564         ($gate:ident, $msg:literal) => {
565             if let Some(spans) = spans.get(&sym::$gate) {
566                 for span in spans {
567                     gate_feature_post!(&visitor, $gate, *span, $msg);
568                 }
569             }
570         };
571     }
572     gate_all!(
573         if_let_guard,
574         "`if let` guards are experimental",
575         "you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`"
576     );
577     gate_all!(let_chains, "`let` expressions in this position are unstable");
578     gate_all!(
579         async_closure,
580         "async closures are unstable",
581         "to use an async block, remove the `||`: `async {`"
582     );
583     gate_all!(
584         closure_lifetime_binder,
585         "`for<...>` binders for closures are experimental",
586         "consider removing `for<...>`"
587     );
588     gate_all!(more_qualified_paths, "usage of qualified paths in this context is experimental");
589     gate_all!(generators, "yield syntax is experimental");
590     gate_all!(raw_ref_op, "raw address of syntax is experimental");
591     gate_all!(const_trait_impl, "const trait impls are experimental");
592     gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
593     gate_all!(inline_const, "inline-const is experimental");
594     gate_all!(inline_const_pat, "inline-const in pattern position is experimental");
595     gate_all!(associated_const_equality, "associated const equality is incomplete");
596     gate_all!(yeet_expr, "`do yeet` expression is experimental");
597
598     // All uses of `gate_all!` below this point were added in #65742,
599     // and subsequently disabled (with the non-early gating readded).
600     // We emit an early future-incompatible warning for these.
601     // New syntax gates should go above here to get a hard error gate.
602     macro_rules! gate_all {
603         ($gate:ident, $msg:literal) => {
604             for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
605                 gate_feature_post!(future_incompatible; &visitor, $gate, *span, $msg);
606             }
607         };
608     }
609
610     gate_all!(trait_alias, "trait aliases are experimental");
611     gate_all!(associated_type_bounds, "associated type bounds are unstable");
612     gate_all!(decl_macro, "`macro` is experimental");
613     gate_all!(box_patterns, "box pattern syntax is experimental");
614     gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental");
615     gate_all!(try_blocks, "`try` blocks are unstable");
616     gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead");
617     gate_all!(type_ascription, "type ascription is experimental");
618
619     visit::walk_crate(&mut visitor, krate);
620 }
621
622 fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
623     // checks if `#![feature]` has been used to enable any lang feature
624     // does not check the same for lib features unless there's at least one
625     // declared lang feature
626     if !sess.opts.unstable_features.is_nightly_build() {
627         let lang_features = &sess.features_untracked().declared_lang_features;
628         if lang_features.len() == 0 {
629             return;
630         }
631         for attr in krate.attrs.iter().filter(|attr| attr.has_name(sym::feature)) {
632             let mut err = struct_span_err!(
633                 sess.parse_sess.span_diagnostic,
634                 attr.span,
635                 E0554,
636                 "`#![feature]` may not be used on the {} release channel",
637                 option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)")
638             );
639             let mut all_stable = true;
640             for ident in
641                 attr.meta_item_list().into_iter().flatten().flat_map(|nested| nested.ident())
642             {
643                 let name = ident.name;
644                 let stable_since = lang_features
645                     .iter()
646                     .flat_map(|&(feature, _, since)| if feature == name { since } else { None })
647                     .next();
648                 if let Some(since) = stable_since {
649                     err.help(&format!(
650                         "the feature `{}` has been stable since {} and no longer requires \
651                                   an attribute to enable",
652                         name, since
653                     ));
654                 } else {
655                     all_stable = false;
656                 }
657             }
658             if all_stable {
659                 err.span_suggestion(
660                     attr.span,
661                     "remove the attribute",
662                     "",
663                     Applicability::MachineApplicable,
664                 );
665             }
666             err.emit();
667         }
668     }
669 }
670
671 fn check_incompatible_features(sess: &Session) {
672     let features = sess.features_untracked();
673
674     let declared_features = features
675         .declared_lang_features
676         .iter()
677         .copied()
678         .map(|(name, span, _)| (name, span))
679         .chain(features.declared_lib_features.iter().copied());
680
681     for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
682         .iter()
683         .filter(|&&(f1, f2)| features.enabled(f1) && features.enabled(f2))
684     {
685         if let Some((f1_name, f1_span)) = declared_features.clone().find(|(name, _)| name == f1) {
686             if let Some((f2_name, f2_span)) = declared_features.clone().find(|(name, _)| name == f2)
687             {
688                 let spans = vec![f1_span, f2_span];
689                 sess.struct_span_err(
690                     spans.clone(),
691                     &format!(
692                         "features `{}` and `{}` are incompatible, using them at the same time \
693                         is not allowed",
694                         f1_name, f2_name
695                     ),
696                 )
697                 .help("remove one of these features")
698                 .emit();
699             }
700         }
701     }
702 }