]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/feature_gate.rs
Do not ICE when we have -Zunpretty=expand with invalid ABI
[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;
6 use rustc_feature::{AttributeGate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP};
7 use rustc_feature::{Features, GateIssue};
8 use rustc_session::parse::{feature_err, feature_err_issue};
9 use rustc_session::Session;
10 use rustc_span::source_map::Spanned;
11 use rustc_span::symbol::sym;
12 use rustc_span::Span;
13
14 use tracing::debug;
15
16 macro_rules! gate_feature_fn {
17     ($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr, $help: expr) => {{
18         let (visitor, has_feature, span, name, explain, help) =
19             (&*$visitor, $has_feature, $span, $name, $explain, $help);
20         let has_feature: bool = has_feature(visitor.features);
21         debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature);
22         if !has_feature && !span.allows_unstable($name) {
23             feature_err_issue(&visitor.sess.parse_sess, name, span, GateIssue::Language, explain)
24                 .help(help)
25                 .emit();
26         }
27     }};
28     ($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{
29         let (visitor, has_feature, span, name, explain) =
30             (&*$visitor, $has_feature, $span, $name, $explain);
31         let has_feature: bool = has_feature(visitor.features);
32         debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature);
33         if !has_feature && !span.allows_unstable($name) {
34             feature_err_issue(&visitor.sess.parse_sess, name, span, GateIssue::Language, explain)
35                 .emit();
36         }
37     }};
38 }
39
40 macro_rules! gate_feature_post {
41     ($visitor: expr, $feature: ident, $span: expr, $explain: expr, $help: expr) => {
42         gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain, $help)
43     };
44     ($visitor: expr, $feature: ident, $span: expr, $explain: expr) => {
45         gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain)
46     };
47 }
48
49 pub fn check_attribute(attr: &ast::Attribute, sess: &Session, features: &Features) {
50     PostExpansionVisitor { sess, features }.visit_attribute(attr)
51 }
52
53 struct PostExpansionVisitor<'a> {
54     sess: &'a Session,
55
56     // `sess` contains a `Features`, but this might not be that one.
57     features: &'a Features,
58 }
59
60 impl<'a> PostExpansionVisitor<'a> {
61     fn check_abi(&self, abi: ast::StrLit, constness: ast::Const) {
62         let ast::StrLit { symbol_unescaped, span, .. } = abi;
63
64         if let ast::Const::Yes(_) = constness {
65             match symbol_unescaped.as_str() {
66                 // Stable
67                 "Rust" | "C" => {}
68                 abi => gate_feature_post!(
69                     &self,
70                     const_extern_fn,
71                     span,
72                     &format!("`{}` as a `const fn` ABI is unstable", abi)
73                 ),
74             }
75         }
76
77         match symbol_unescaped.as_str() {
78             // Stable
79             "Rust" | "C" | "cdecl" | "stdcall" | "fastcall" | "aapcs" | "win64" | "sysv64"
80             | "system" => {}
81             "rust-intrinsic" => {
82                 gate_feature_post!(&self, intrinsics, span, "intrinsics are subject to change");
83             }
84             "platform-intrinsic" => {
85                 gate_feature_post!(
86                     &self,
87                     platform_intrinsics,
88                     span,
89                     "platform intrinsics are experimental and possibly buggy"
90                 );
91             }
92             "vectorcall" => {
93                 gate_feature_post!(
94                     &self,
95                     abi_vectorcall,
96                     span,
97                     "vectorcall is experimental and subject to change"
98                 );
99             }
100             "thiscall" => {
101                 gate_feature_post!(
102                     &self,
103                     abi_thiscall,
104                     span,
105                     "thiscall is experimental and subject to change"
106                 );
107             }
108             "rust-call" => {
109                 gate_feature_post!(
110                     &self,
111                     unboxed_closures,
112                     span,
113                     "rust-call ABI is subject to change"
114                 );
115             }
116             "rust-cold" => {
117                 gate_feature_post!(
118                     &self,
119                     rust_cold_cc,
120                     span,
121                     "rust-cold is experimental and subject to change"
122                 );
123             }
124             "ptx-kernel" => {
125                 gate_feature_post!(
126                     &self,
127                     abi_ptx,
128                     span,
129                     "PTX ABIs are experimental and subject to change"
130                 );
131             }
132             "unadjusted" => {
133                 gate_feature_post!(
134                     &self,
135                     abi_unadjusted,
136                     span,
137                     "unadjusted ABI is an implementation detail and perma-unstable"
138                 );
139             }
140             "msp430-interrupt" => {
141                 gate_feature_post!(
142                     &self,
143                     abi_msp430_interrupt,
144                     span,
145                     "msp430-interrupt ABI is experimental and subject to change"
146                 );
147             }
148             "x86-interrupt" => {
149                 gate_feature_post!(
150                     &self,
151                     abi_x86_interrupt,
152                     span,
153                     "x86-interrupt ABI is experimental and subject to change"
154                 );
155             }
156             "amdgpu-kernel" => {
157                 gate_feature_post!(
158                     &self,
159                     abi_amdgpu_kernel,
160                     span,
161                     "amdgpu-kernel ABI is experimental and subject to change"
162                 );
163             }
164             "avr-interrupt" | "avr-non-blocking-interrupt" => {
165                 gate_feature_post!(
166                     &self,
167                     abi_avr_interrupt,
168                     span,
169                     "avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change"
170                 );
171             }
172             "efiapi" => {
173                 gate_feature_post!(
174                     &self,
175                     abi_efiapi,
176                     span,
177                     "efiapi ABI is experimental and subject to change"
178                 );
179             }
180             "C-cmse-nonsecure-call" => {
181                 gate_feature_post!(
182                     &self,
183                     abi_c_cmse_nonsecure_call,
184                     span,
185                     "C-cmse-nonsecure-call ABI is experimental and subject to change"
186                 );
187             }
188             "C-unwind" => {
189                 gate_feature_post!(
190                     &self,
191                     c_unwind,
192                     span,
193                     "C-unwind ABI is experimental and subject to change"
194                 );
195             }
196             "stdcall-unwind" => {
197                 gate_feature_post!(
198                     &self,
199                     c_unwind,
200                     span,
201                     "stdcall-unwind ABI is experimental and subject to change"
202                 );
203             }
204             "system-unwind" => {
205                 gate_feature_post!(
206                     &self,
207                     c_unwind,
208                     span,
209                     "system-unwind ABI is experimental and subject to change"
210                 );
211             }
212             "thiscall-unwind" => {
213                 gate_feature_post!(
214                     &self,
215                     c_unwind,
216                     span,
217                     "thiscall-unwind ABI is experimental and subject to change"
218                 );
219             }
220             "cdecl-unwind" => {
221                 gate_feature_post!(
222                     &self,
223                     c_unwind,
224                     span,
225                     "cdecl-unwind ABI is experimental and subject to change"
226                 );
227             }
228             "fastcall-unwind" => {
229                 gate_feature_post!(
230                     &self,
231                     c_unwind,
232                     span,
233                     "fastcall-unwind ABI is experimental and subject to change"
234                 );
235             }
236             "vectorcall-unwind" => {
237                 gate_feature_post!(
238                     &self,
239                     c_unwind,
240                     span,
241                     "vectorcall-unwind ABI is experimental and subject to change"
242                 );
243             }
244             "aapcs-unwind" => {
245                 gate_feature_post!(
246                     &self,
247                     c_unwind,
248                     span,
249                     "aapcs-unwind ABI is experimental and subject to change"
250                 );
251             }
252             "win64-unwind" => {
253                 gate_feature_post!(
254                     &self,
255                     c_unwind,
256                     span,
257                     "win64-unwind ABI is experimental and subject to change"
258                 );
259             }
260             "sysv64-unwind" => {
261                 gate_feature_post!(
262                     &self,
263                     c_unwind,
264                     span,
265                     "sysv64-unwind ABI is experimental and subject to change"
266                 );
267             }
268             "wasm" => {
269                 gate_feature_post!(
270                     &self,
271                     wasm_abi,
272                     span,
273                     "wasm ABI is experimental and subject to change"
274                 );
275             }
276             abi => {
277                 if self.sess.opts.pretty.map_or(true, |ppm| ppm.needs_hir()) {
278                     self.sess.parse_sess.span_diagnostic.delay_span_bug(
279                         span,
280                         &format!("unrecognized ABI not caught in lowering: {}", abi),
281                     );
282                 }
283             }
284         }
285     }
286
287     fn check_extern(&self, ext: ast::Extern, constness: ast::Const) {
288         if let ast::Extern::Explicit(abi, _) = ext {
289             self.check_abi(abi, constness);
290         }
291     }
292
293     fn maybe_report_invalid_custom_discriminants(&self, variants: &[ast::Variant]) {
294         let has_fields = variants.iter().any(|variant| match variant.data {
295             VariantData::Tuple(..) | VariantData::Struct(..) => true,
296             VariantData::Unit(..) => false,
297         });
298
299         let discriminant_spans = variants
300             .iter()
301             .filter(|variant| match variant.data {
302                 VariantData::Tuple(..) | VariantData::Struct(..) => false,
303                 VariantData::Unit(..) => true,
304             })
305             .filter_map(|variant| variant.disr_expr.as_ref().map(|c| c.value.span))
306             .collect::<Vec<_>>();
307
308         if !discriminant_spans.is_empty() && has_fields {
309             let mut err = feature_err(
310                 &self.sess.parse_sess,
311                 sym::arbitrary_enum_discriminant,
312                 discriminant_spans.clone(),
313                 "custom discriminant values are not allowed in enums with tuple or struct variants",
314             );
315             for sp in discriminant_spans {
316                 err.span_label(sp, "disallowed custom discriminant");
317             }
318             for variant in variants.iter() {
319                 match &variant.data {
320                     VariantData::Struct(..) => {
321                         err.span_label(variant.span, "struct variant defined here");
322                     }
323                     VariantData::Tuple(..) => {
324                         err.span_label(variant.span, "tuple variant defined here");
325                     }
326                     VariantData::Unit(..) => {}
327                 }
328             }
329             err.emit();
330         }
331     }
332
333     fn check_gat(&self, generics: &ast::Generics, span: Span) {
334         if !generics.params.is_empty() {
335             gate_feature_post!(
336                 &self,
337                 generic_associated_types,
338                 span,
339                 "generic associated types are unstable"
340             );
341         }
342         if !generics.where_clause.predicates.is_empty() {
343             gate_feature_post!(
344                 &self,
345                 generic_associated_types,
346                 span,
347                 "where clauses on associated types are unstable"
348             );
349         }
350     }
351
352     /// Feature gate `impl Trait` inside `type Alias = $type_expr;`.
353     fn check_impl_trait(&self, ty: &ast::Ty) {
354         struct ImplTraitVisitor<'a> {
355             vis: &'a PostExpansionVisitor<'a>,
356         }
357         impl Visitor<'_> for ImplTraitVisitor<'_> {
358             fn visit_ty(&mut self, ty: &ast::Ty) {
359                 if let ast::TyKind::ImplTrait(..) = ty.kind {
360                     gate_feature_post!(
361                         &self.vis,
362                         type_alias_impl_trait,
363                         ty.span,
364                         "`impl Trait` in type aliases is unstable"
365                     );
366                 }
367                 visit::walk_ty(self, ty);
368             }
369         }
370         ImplTraitVisitor { vis: self }.visit_ty(ty);
371     }
372 }
373
374 impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
375     fn visit_attribute(&mut self, attr: &ast::Attribute) {
376         let attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
377         // Check feature gates for built-in attributes.
378         if let Some(BuiltinAttribute {
379             gate: AttributeGate::Gated(_, name, descr, has_feature),
380             ..
381         }) = attr_info
382         {
383             gate_feature_fn!(self, has_feature, attr.span, *name, descr);
384         }
385         // Check unstable flavors of the `#[doc]` attribute.
386         if attr.has_name(sym::doc) {
387             for nested_meta in attr.meta_item_list().unwrap_or_default() {
388                 macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => {
389                     $(if nested_meta.has_name(sym::$name) {
390                         let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental");
391                         gate_feature_post!(self, $feature, attr.span, msg);
392                     })*
393                 }}
394
395                 gate_doc!(
396                     cfg => doc_cfg
397                     cfg_hide => doc_cfg_hide
398                     masked => doc_masked
399                     notable_trait => doc_notable_trait
400                 );
401
402                 if nested_meta.has_name(sym::keyword) {
403                     let msg = "`#[doc(keyword)]` is meant for internal use only";
404                     gate_feature_post!(self, rustdoc_internals, attr.span, msg);
405                 }
406
407                 if nested_meta.has_name(sym::tuple_variadic) {
408                     let msg = "`#[doc(tuple_variadic)]` is meant for internal use only";
409                     gate_feature_post!(self, rustdoc_internals, attr.span, msg);
410                 }
411             }
412         }
413
414         // Emit errors for non-staged-api crates.
415         if !self.features.staged_api {
416             if attr.has_name(sym::unstable)
417                 || attr.has_name(sym::stable)
418                 || attr.has_name(sym::rustc_const_unstable)
419                 || attr.has_name(sym::rustc_const_stable)
420             {
421                 struct_span_err!(
422                     self.sess,
423                     attr.span,
424                     E0734,
425                     "stability attributes may not be used outside of the standard library",
426                 )
427                 .emit();
428             }
429         }
430     }
431
432     fn visit_item(&mut self, i: &'a ast::Item) {
433         match i.kind {
434             ast::ItemKind::ForeignMod(ref foreign_module) => {
435                 if let Some(abi) = foreign_module.abi {
436                     self.check_abi(abi, ast::Const::No);
437                 }
438             }
439
440             ast::ItemKind::Fn(..) => {
441                 if self.sess.contains_name(&i.attrs, sym::start) {
442                     gate_feature_post!(
443                         &self,
444                         start,
445                         i.span,
446                         "`#[start]` functions are experimental \
447                          and their signature may change \
448                          over time"
449                     );
450                 }
451             }
452
453             ast::ItemKind::Struct(..) => {
454                 for attr in self.sess.filter_by_name(&i.attrs, sym::repr) {
455                     for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
456                         if item.has_name(sym::simd) {
457                             gate_feature_post!(
458                                 &self,
459                                 repr_simd,
460                                 attr.span,
461                                 "SIMD types are experimental and possibly buggy"
462                             );
463                         }
464                     }
465                 }
466             }
467
468             ast::ItemKind::Enum(ast::EnumDef { ref variants, .. }, ..) => {
469                 for variant in variants {
470                     match (&variant.data, &variant.disr_expr) {
471                         (ast::VariantData::Unit(..), _) => {}
472                         (_, Some(disr_expr)) => gate_feature_post!(
473                             &self,
474                             arbitrary_enum_discriminant,
475                             disr_expr.value.span,
476                             "discriminants on non-unit variants are experimental"
477                         ),
478                         _ => {}
479                     }
480                 }
481
482                 let has_feature = self.features.arbitrary_enum_discriminant;
483                 if !has_feature && !i.span.allows_unstable(sym::arbitrary_enum_discriminant) {
484                     self.maybe_report_invalid_custom_discriminants(&variants);
485                 }
486             }
487
488             ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, ref of_trait, .. }) => {
489                 if let ast::ImplPolarity::Negative(span) = polarity {
490                     gate_feature_post!(
491                         &self,
492                         negative_impls,
493                         span.to(of_trait.as_ref().map_or(span, |t| t.path.span)),
494                         "negative trait bounds are not yet fully implemented; \
495                          use marker types for now"
496                     );
497                 }
498
499                 if let ast::Defaultness::Default(_) = defaultness {
500                     gate_feature_post!(&self, specialization, i.span, "specialization is unstable");
501                 }
502             }
503
504             ast::ItemKind::Trait(box ast::Trait { is_auto: ast::IsAuto::Yes, .. }) => {
505                 gate_feature_post!(
506                     &self,
507                     auto_traits,
508                     i.span,
509                     "auto traits are experimental and possibly buggy"
510                 );
511             }
512
513             ast::ItemKind::TraitAlias(..) => {
514                 gate_feature_post!(&self, trait_alias, i.span, "trait aliases are experimental");
515             }
516
517             ast::ItemKind::MacroDef(ast::MacroDef { macro_rules: false, .. }) => {
518                 let msg = "`macro` is experimental";
519                 gate_feature_post!(&self, decl_macro, i.span, msg);
520             }
521
522             ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ref ty), .. }) => {
523                 self.check_impl_trait(&ty)
524             }
525
526             _ => {}
527         }
528
529         visit::walk_item(self, i);
530     }
531
532     fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
533         match i.kind {
534             ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
535                 let link_name = self.sess.first_attr_value_str_by_name(&i.attrs, sym::link_name);
536                 let links_to_llvm =
537                     link_name.map_or(false, |val| val.as_str().starts_with("llvm."));
538                 if links_to_llvm {
539                     gate_feature_post!(
540                         &self,
541                         link_llvm_intrinsics,
542                         i.span,
543                         "linking to LLVM intrinsics is experimental"
544                     );
545                 }
546             }
547             ast::ForeignItemKind::TyAlias(..) => {
548                 gate_feature_post!(&self, extern_types, i.span, "extern types are experimental");
549             }
550             ast::ForeignItemKind::MacCall(..) => {}
551         }
552
553         visit::walk_foreign_item(self, i)
554     }
555
556     fn visit_ty(&mut self, ty: &'a ast::Ty) {
557         match ty.kind {
558             ast::TyKind::BareFn(ref bare_fn_ty) => {
559                 // Function pointers cannot be `const`
560                 self.check_extern(bare_fn_ty.ext, ast::Const::No);
561             }
562             ast::TyKind::Never => {
563                 gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental");
564             }
565             _ => {}
566         }
567         visit::walk_ty(self, ty)
568     }
569
570     fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) {
571         if let ast::FnRetTy::Ty(ref output_ty) = *ret_ty {
572             if let ast::TyKind::Never = output_ty.kind {
573                 // Do nothing.
574             } else {
575                 self.visit_ty(output_ty)
576             }
577         }
578     }
579
580     fn visit_expr(&mut self, e: &'a ast::Expr) {
581         match e.kind {
582             ast::ExprKind::Box(_) => {
583                 gate_feature_post!(
584                     &self,
585                     box_syntax,
586                     e.span,
587                     "box expression syntax is experimental; you can call `Box::new` instead"
588                 );
589             }
590             ast::ExprKind::Type(..) => {
591                 // To avoid noise about type ascription in common syntax errors, only emit if it
592                 // is the *only* error.
593                 if self.sess.parse_sess.span_diagnostic.err_count() == 0 {
594                     gate_feature_post!(
595                         &self,
596                         type_ascription,
597                         e.span,
598                         "type ascription is experimental"
599                     );
600                 }
601             }
602             ast::ExprKind::TryBlock(_) => {
603                 gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
604             }
605             ast::ExprKind::Block(_, Some(label)) => {
606                 gate_feature_post!(
607                     &self,
608                     label_break_value,
609                     label.ident.span,
610                     "labels on blocks are unstable"
611                 );
612             }
613             _ => {}
614         }
615         visit::walk_expr(self, e)
616     }
617
618     fn visit_pat(&mut self, pattern: &'a ast::Pat) {
619         match &pattern.kind {
620             PatKind::Slice(pats) => {
621                 for pat in pats {
622                     let inner_pat = match &pat.kind {
623                         PatKind::Ident(.., Some(pat)) => pat,
624                         _ => pat,
625                     };
626                     if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind {
627                         gate_feature_post!(
628                             &self,
629                             half_open_range_patterns,
630                             pat.span,
631                             "`X..` patterns in slices are experimental"
632                         );
633                     }
634                 }
635             }
636             PatKind::Box(..) => {
637                 gate_feature_post!(
638                     &self,
639                     box_patterns,
640                     pattern.span,
641                     "box pattern syntax is experimental"
642                 );
643             }
644             PatKind::Range(_, Some(_), Spanned { node: RangeEnd::Excluded, .. }) => {
645                 gate_feature_post!(
646                     &self,
647                     exclusive_range_pattern,
648                     pattern.span,
649                     "exclusive range pattern syntax is experimental"
650                 );
651             }
652             _ => {}
653         }
654         visit::walk_pat(self, pattern)
655     }
656
657     fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
658         if let Some(header) = fn_kind.header() {
659             // Stability of const fn methods are covered in `visit_assoc_item` below.
660             self.check_extern(header.ext, header.constness);
661         }
662
663         if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() {
664             gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable");
665         }
666
667         visit::walk_fn(self, fn_kind, span)
668     }
669
670     fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) {
671         if let AssocConstraintKind::Bound { .. } = constraint.kind {
672             gate_feature_post!(
673                 &self,
674                 associated_type_bounds,
675                 constraint.span,
676                 "associated type bounds are unstable"
677             )
678         }
679         visit::walk_assoc_constraint(self, constraint)
680     }
681
682     fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
683         let is_fn = match i.kind {
684             ast::AssocItemKind::Fn(_) => true,
685             ast::AssocItemKind::TyAlias(box ast::TyAlias { ref generics, ref ty, .. }) => {
686                 if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
687                     gate_feature_post!(
688                         &self,
689                         associated_type_defaults,
690                         i.span,
691                         "associated type defaults are unstable"
692                     );
693                 }
694                 if let Some(ty) = ty {
695                     self.check_impl_trait(ty);
696                 }
697                 self.check_gat(generics, i.span);
698                 false
699             }
700             _ => false,
701         };
702         if let ast::Defaultness::Default(_) = i.kind.defaultness() {
703             // Limit `min_specialization` to only specializing functions.
704             gate_feature_fn!(
705                 &self,
706                 |x: &Features| x.specialization || (is_fn && x.min_specialization),
707                 i.span,
708                 sym::specialization,
709                 "specialization is unstable"
710             );
711         }
712         visit::walk_assoc_item(self, i, ctxt)
713     }
714 }
715
716 pub fn check_crate(krate: &ast::Crate, sess: &Session) {
717     maybe_stage_features(sess, krate);
718     check_incompatible_features(sess);
719     let mut visitor = PostExpansionVisitor { sess, features: &sess.features_untracked() };
720
721     let spans = sess.parse_sess.gated_spans.spans.borrow();
722     macro_rules! gate_all {
723         ($gate:ident, $msg:literal, $help:literal) => {
724             if let Some(spans) = spans.get(&sym::$gate) {
725                 for span in spans {
726                     gate_feature_post!(&visitor, $gate, *span, $msg, $help);
727                 }
728             }
729         };
730         ($gate:ident, $msg:literal) => {
731             if let Some(spans) = spans.get(&sym::$gate) {
732                 for span in spans {
733                     gate_feature_post!(&visitor, $gate, *span, $msg);
734                 }
735             }
736         };
737     }
738     gate_all!(
739         if_let_guard,
740         "`if let` guards are experimental",
741         "you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`"
742     );
743     gate_all!(let_chains, "`let` expressions in this position are unstable");
744     gate_all!(
745         async_closure,
746         "async closures are unstable",
747         "to use an async block, remove the `||`: `async {`"
748     );
749     gate_all!(
750         closure_lifetime_binder,
751         "`for<...>` binders for closures are experimental",
752         "consider removing `for<...>`"
753     );
754     gate_all!(more_qualified_paths, "usage of qualified paths in this context is experimental");
755     gate_all!(generators, "yield syntax is experimental");
756     gate_all!(raw_ref_op, "raw address of syntax is experimental");
757     gate_all!(const_trait_impl, "const trait impls are experimental");
758     gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
759     gate_all!(inline_const, "inline-const is experimental");
760     gate_all!(inline_const_pat, "inline-const in pattern position is experimental");
761     gate_all!(associated_const_equality, "associated const equality is incomplete");
762     gate_all!(yeet_expr, "`do yeet` expression is experimental");
763
764     // All uses of `gate_all!` below this point were added in #65742,
765     // and subsequently disabled (with the non-early gating readded).
766     macro_rules! gate_all {
767         ($gate:ident, $msg:literal) => {
768             // FIXME(eddyb) do something more useful than always
769             // disabling these uses of early feature-gatings.
770             if false {
771                 for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
772                     gate_feature_post!(&visitor, $gate, *span, $msg);
773                 }
774             }
775         };
776     }
777
778     gate_all!(trait_alias, "trait aliases are experimental");
779     gate_all!(associated_type_bounds, "associated type bounds are unstable");
780     gate_all!(decl_macro, "`macro` is experimental");
781     gate_all!(box_patterns, "box pattern syntax is experimental");
782     gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental");
783     gate_all!(try_blocks, "`try` blocks are unstable");
784     gate_all!(label_break_value, "labels on blocks are unstable");
785     gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead");
786     // To avoid noise about type ascription in common syntax errors,
787     // only emit if it is the *only* error. (Also check it last.)
788     if sess.parse_sess.span_diagnostic.err_count() == 0 {
789         gate_all!(type_ascription, "type ascription is experimental");
790     }
791
792     visit::walk_crate(&mut visitor, krate);
793 }
794
795 fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
796     // checks if `#![feature]` has been used to enable any lang feature
797     // does not check the same for lib features unless there's at least one
798     // declared lang feature
799     use rustc_errors::Applicability;
800
801     if !sess.opts.unstable_features.is_nightly_build() {
802         let lang_features = &sess.features_untracked().declared_lang_features;
803         if lang_features.len() == 0 {
804             return;
805         }
806         for attr in krate.attrs.iter().filter(|attr| attr.has_name(sym::feature)) {
807             let mut err = struct_span_err!(
808                 sess.parse_sess.span_diagnostic,
809                 attr.span,
810                 E0554,
811                 "`#![feature]` may not be used on the {} release channel",
812                 option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)")
813             );
814             let mut all_stable = true;
815             for ident in
816                 attr.meta_item_list().into_iter().flatten().flat_map(|nested| nested.ident())
817             {
818                 let name = ident.name;
819                 let stable_since = lang_features
820                     .iter()
821                     .flat_map(|&(feature, _, since)| if feature == name { since } else { None })
822                     .next();
823                 if let Some(since) = stable_since {
824                     err.help(&format!(
825                         "the feature `{}` has been stable since {} and no longer requires \
826                                   an attribute to enable",
827                         name, since
828                     ));
829                 } else {
830                     all_stable = false;
831                 }
832             }
833             if all_stable {
834                 err.span_suggestion(
835                     attr.span,
836                     "remove the attribute",
837                     "",
838                     Applicability::MachineApplicable,
839                 );
840             }
841             err.emit();
842         }
843     }
844 }
845
846 fn check_incompatible_features(sess: &Session) {
847     let features = sess.features_untracked();
848
849     let declared_features = features
850         .declared_lang_features
851         .iter()
852         .copied()
853         .map(|(name, span, _)| (name, span))
854         .chain(features.declared_lib_features.iter().copied());
855
856     for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
857         .iter()
858         .filter(|&&(f1, f2)| features.enabled(f1) && features.enabled(f2))
859     {
860         if let Some((f1_name, f1_span)) = declared_features.clone().find(|(name, _)| name == f1) {
861             if let Some((f2_name, f2_span)) = declared_features.clone().find(|(name, _)| name == f2)
862             {
863                 let spans = vec![f1_span, f2_span];
864                 sess.struct_span_err(
865                     spans.clone(),
866                     &format!(
867                         "features `{}` and `{}` are incompatible, using them at the same time \
868                         is not allowed",
869                         f1_name, f2_name
870                     ),
871                 )
872                 .help("remove one of these features")
873                 .emit();
874             }
875         }
876     }
877 }