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