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