]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/feature_gate.rs
Rollup merge of #86750 - fee1-dead:impl-const-test, r=jonas-schievink
[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::{AssocTyConstraint, AssocTyConstraintKind, NodeId};
4 use rustc_ast::{PatKind, RangeEnd, VariantData};
5 use rustc_errors::struct_span_err;
6 use rustc_feature::{AttributeGate, 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                         min_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 =
305             attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
306         // Check feature gates for built-in attributes.
307         if let Some((.., AttributeGate::Gated(_, name, descr, has_feature))) = attr_info {
308             gate_feature_fn!(self, has_feature, attr.span, name, descr);
309         }
310         // Check unstable flavors of the `#[doc]` attribute.
311         if self.sess.check_name(attr, sym::doc) {
312             for nested_meta in attr.meta_item_list().unwrap_or_default() {
313                 macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => {
314                     $(if nested_meta.has_name(sym::$name) {
315                         let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental");
316                         gate_feature_post!(self, $feature, attr.span, msg);
317                     })*
318                 }}
319
320                 gate_doc!(
321                     cfg => doc_cfg
322                     masked => doc_masked
323                     notable_trait => doc_notable_trait
324                     keyword => doc_keyword
325                 );
326             }
327         }
328
329         // Check for unstable modifiers on `#[link(..)]` attribute
330         if self.sess.check_name(attr, sym::link) {
331             for nested_meta in attr.meta_item_list().unwrap_or_default() {
332                 if nested_meta.has_name(sym::modifiers) {
333                     gate_feature_post!(
334                         self,
335                         native_link_modifiers,
336                         nested_meta.span(),
337                         "native link modifiers are experimental"
338                     );
339
340                     if let Some(modifiers) = nested_meta.value_str() {
341                         for modifier in modifiers.as_str().split(',') {
342                             if let Some(modifier) = modifier.strip_prefix(&['+', '-'][..]) {
343                                 macro_rules! gate_modifier { ($($name:literal => $feature:ident)*) => {
344                                     $(if modifier == $name {
345                                         let msg = concat!("`#[link(modifiers=\"", $name, "\")]` is unstable");
346                                         gate_feature_post!(
347                                             self,
348                                             $feature,
349                                             nested_meta.name_value_literal_span().unwrap(),
350                                             msg
351                                         );
352                                     })*
353                                 }}
354
355                                 gate_modifier!(
356                                     "bundle" => native_link_modifiers_bundle
357                                     "verbatim" => native_link_modifiers_verbatim
358                                     "whole-archive" => native_link_modifiers_whole_archive
359                                     "as-needed" => native_link_modifiers_as_needed
360                                 );
361                             }
362                         }
363                     }
364                 }
365             }
366         }
367     }
368
369     fn visit_item(&mut self, i: &'a ast::Item) {
370         match i.kind {
371             ast::ItemKind::ForeignMod(ref foreign_module) => {
372                 if let Some(abi) = foreign_module.abi {
373                     self.check_abi(abi);
374                 }
375             }
376
377             ast::ItemKind::Fn(..) => {
378                 if self.sess.contains_name(&i.attrs[..], sym::plugin_registrar) {
379                     gate_feature_post!(
380                         &self,
381                         plugin_registrar,
382                         i.span,
383                         "compiler plugins are experimental and possibly buggy"
384                     );
385                 }
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::ImplKind {
434                 polarity, defaultness, ref of_trait, ..
435             }) => {
436                 if let ast::ImplPolarity::Negative(span) = polarity {
437                     gate_feature_post!(
438                         &self,
439                         negative_impls,
440                         span.to(of_trait.as_ref().map_or(span, |t| t.path.span)),
441                         "negative trait bounds are not yet fully implemented; \
442                          use marker types for now"
443                     );
444                 }
445
446                 if let ast::Defaultness::Default(_) = defaultness {
447                     gate_feature_post!(&self, specialization, i.span, "specialization is unstable");
448                 }
449             }
450
451             ast::ItemKind::Trait(box ast::TraitKind(ast::IsAuto::Yes, ..)) => {
452                 gate_feature_post!(
453                     &self,
454                     auto_traits,
455                     i.span,
456                     "auto traits are experimental and possibly buggy"
457                 );
458             }
459
460             ast::ItemKind::TraitAlias(..) => {
461                 gate_feature_post!(&self, trait_alias, i.span, "trait aliases are experimental");
462             }
463
464             ast::ItemKind::MacroDef(ast::MacroDef { macro_rules: false, .. }) => {
465                 let msg = "`macro` is experimental";
466                 gate_feature_post!(&self, decl_macro, i.span, msg);
467             }
468
469             ast::ItemKind::TyAlias(box ast::TyAliasKind(_, _, _, Some(ref ty))) => {
470                 self.check_impl_trait(&ty)
471             }
472
473             _ => {}
474         }
475
476         visit::walk_item(self, i);
477     }
478
479     fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
480         match i.kind {
481             ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
482                 let link_name = self.sess.first_attr_value_str_by_name(&i.attrs, sym::link_name);
483                 let links_to_llvm =
484                     link_name.map_or(false, |val| val.as_str().starts_with("llvm."));
485                 if links_to_llvm {
486                     gate_feature_post!(
487                         &self,
488                         link_llvm_intrinsics,
489                         i.span,
490                         "linking to LLVM intrinsics is experimental"
491                     );
492                 }
493             }
494             ast::ForeignItemKind::TyAlias(..) => {
495                 gate_feature_post!(&self, extern_types, i.span, "extern types are experimental");
496             }
497             ast::ForeignItemKind::MacCall(..) => {}
498         }
499
500         visit::walk_foreign_item(self, i)
501     }
502
503     fn visit_ty(&mut self, ty: &'a ast::Ty) {
504         match ty.kind {
505             ast::TyKind::BareFn(ref bare_fn_ty) => {
506                 self.check_extern(bare_fn_ty.ext);
507             }
508             ast::TyKind::Never => {
509                 gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental");
510             }
511             _ => {}
512         }
513         visit::walk_ty(self, ty)
514     }
515
516     fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) {
517         if let ast::FnRetTy::Ty(ref output_ty) = *ret_ty {
518             if let ast::TyKind::Never = output_ty.kind {
519                 // Do nothing.
520             } else {
521                 self.visit_ty(output_ty)
522             }
523         }
524     }
525
526     fn visit_expr(&mut self, e: &'a ast::Expr) {
527         match e.kind {
528             ast::ExprKind::Box(_) => {
529                 gate_feature_post!(
530                     &self,
531                     box_syntax,
532                     e.span,
533                     "box expression syntax is experimental; you can call `Box::new` instead"
534                 );
535             }
536             ast::ExprKind::Type(..) => {
537                 // To avoid noise about type ascription in common syntax errors, only emit if it
538                 // is the *only* error.
539                 if self.sess.parse_sess.span_diagnostic.err_count() == 0 {
540                     gate_feature_post!(
541                         &self,
542                         type_ascription,
543                         e.span,
544                         "type ascription is experimental"
545                     );
546                 }
547             }
548             ast::ExprKind::TryBlock(_) => {
549                 gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
550             }
551             ast::ExprKind::Block(_, opt_label) => {
552                 if let Some(label) = opt_label {
553                     gate_feature_post!(
554                         &self,
555                         label_break_value,
556                         label.ident.span,
557                         "labels on blocks are unstable"
558                     );
559                 }
560             }
561             _ => {}
562         }
563         visit::walk_expr(self, e)
564     }
565
566     fn visit_pat(&mut self, pattern: &'a ast::Pat) {
567         match &pattern.kind {
568             PatKind::Box(..) => {
569                 gate_feature_post!(
570                     &self,
571                     box_patterns,
572                     pattern.span,
573                     "box pattern syntax is experimental"
574                 );
575             }
576             PatKind::Range(_, _, Spanned { node: RangeEnd::Excluded, .. }) => {
577                 gate_feature_post!(
578                     &self,
579                     exclusive_range_pattern,
580                     pattern.span,
581                     "exclusive range pattern syntax is experimental"
582                 );
583             }
584             _ => {}
585         }
586         visit::walk_pat(self, pattern)
587     }
588
589     fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
590         if let Some(header) = fn_kind.header() {
591             // Stability of const fn methods are covered in `visit_assoc_item` below.
592             self.check_extern(header.ext);
593
594             if let (ast::Const::Yes(_), ast::Extern::Implicit)
595             | (ast::Const::Yes(_), ast::Extern::Explicit(_)) = (header.constness, header.ext)
596             {
597                 gate_feature_post!(
598                     &self,
599                     const_extern_fn,
600                     span,
601                     "`const extern fn` definitions are unstable"
602                 );
603             }
604         }
605
606         if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() {
607             gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable");
608         }
609
610         visit::walk_fn(self, fn_kind, span)
611     }
612
613     fn visit_assoc_ty_constraint(&mut self, constraint: &'a AssocTyConstraint) {
614         if let AssocTyConstraintKind::Bound { .. } = constraint.kind {
615             gate_feature_post!(
616                 &self,
617                 associated_type_bounds,
618                 constraint.span,
619                 "associated type bounds are unstable"
620             )
621         }
622         visit::walk_assoc_ty_constraint(self, constraint)
623     }
624
625     fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
626         let is_fn = match i.kind {
627             ast::AssocItemKind::Fn(_) => true,
628             ast::AssocItemKind::TyAlias(box ast::TyAliasKind(_, ref generics, _, ref ty)) => {
629                 if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
630                     gate_feature_post!(
631                         &self,
632                         associated_type_defaults,
633                         i.span,
634                         "associated type defaults are unstable"
635                     );
636                 }
637                 if let Some(ty) = ty {
638                     self.check_impl_trait(ty);
639                 }
640                 self.check_gat(generics, i.span);
641                 false
642             }
643             _ => false,
644         };
645         if let ast::Defaultness::Default(_) = i.kind.defaultness() {
646             // Limit `min_specialization` to only specializing functions.
647             gate_feature_fn!(
648                 &self,
649                 |x: &Features| x.specialization || (is_fn && x.min_specialization),
650                 i.span,
651                 sym::specialization,
652                 "specialization is unstable"
653             );
654         }
655         visit::walk_assoc_item(self, i, ctxt)
656     }
657
658     fn visit_vis(&mut self, vis: &'a ast::Visibility) {
659         if let ast::VisibilityKind::Crate(ast::CrateSugar::JustCrate) = vis.kind {
660             gate_feature_post!(
661                 &self,
662                 crate_visibility_modifier,
663                 vis.span,
664                 "`crate` visibility modifier is experimental"
665             );
666         }
667         visit::walk_vis(self, vis)
668     }
669 }
670
671 pub fn check_crate(krate: &ast::Crate, sess: &Session) {
672     maybe_stage_features(sess, krate);
673     check_incompatible_features(sess);
674     let mut visitor = PostExpansionVisitor { sess, features: &sess.features_untracked() };
675
676     let spans = sess.parse_sess.gated_spans.spans.borrow();
677     macro_rules! gate_all {
678         ($gate:ident, $msg:literal, $help:literal) => {
679             if let Some(spans) = spans.get(&sym::$gate) {
680                 for span in spans {
681                     gate_feature_post!(&visitor, $gate, *span, $msg, $help);
682                 }
683             }
684         };
685         ($gate:ident, $msg:literal) => {
686             if let Some(spans) = spans.get(&sym::$gate) {
687                 for span in spans {
688                     gate_feature_post!(&visitor, $gate, *span, $msg);
689                 }
690             }
691         };
692     }
693     gate_all!(
694         if_let_guard,
695         "`if let` guards are experimental",
696         "you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`"
697     );
698     gate_all!(
699         let_chains,
700         "`let` expressions in this position are experimental",
701         "you can write `matches!(<expr>, <pattern>)` instead of `let <pattern> = <expr>`"
702     );
703     gate_all!(
704         async_closure,
705         "async closures are unstable",
706         "to use an async block, remove the `||`: `async {`"
707     );
708     gate_all!(more_qualified_paths, "usage of qualified paths in this context is experimental");
709     gate_all!(generators, "yield syntax is experimental");
710     gate_all!(raw_ref_op, "raw address of syntax is experimental");
711     gate_all!(const_trait_bound_opt_out, "`?const` on trait bounds is experimental");
712     gate_all!(const_trait_impl, "const trait impls are experimental");
713     gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
714     gate_all!(inline_const, "inline-const is experimental");
715     gate_all!(
716         const_generics_defaults,
717         "default values for const generic parameters are experimental"
718     );
719     if sess.parse_sess.span_diagnostic.err_count() == 0 {
720         // Errors for `destructuring_assignment` can get quite noisy, especially where `_` is
721         // involved, so we only emit errors where there are no other parsing errors.
722         gate_all!(destructuring_assignment, "destructuring assignments are unstable");
723     }
724     gate_all!(unnamed_fields, "unnamed fields are not yet fully implemented");
725
726     // All uses of `gate_all!` below this point were added in #65742,
727     // and subsequently disabled (with the non-early gating readded).
728     macro_rules! gate_all {
729         ($gate:ident, $msg:literal) => {
730             // FIXME(eddyb) do something more useful than always
731             // disabling these uses of early feature-gatings.
732             if false {
733                 for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
734                     gate_feature_post!(&visitor, $gate, *span, $msg);
735                 }
736             }
737         };
738     }
739
740     gate_all!(trait_alias, "trait aliases are experimental");
741     gate_all!(associated_type_bounds, "associated type bounds are unstable");
742     gate_all!(crate_visibility_modifier, "`crate` visibility modifier is experimental");
743     gate_all!(const_generics, "const generics are unstable");
744     gate_all!(decl_macro, "`macro` is experimental");
745     gate_all!(box_patterns, "box pattern syntax is experimental");
746     gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental");
747     gate_all!(try_blocks, "`try` blocks are unstable");
748     gate_all!(label_break_value, "labels on blocks are unstable");
749     gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead");
750     // To avoid noise about type ascription in common syntax errors,
751     // only emit if it is the *only* error. (Also check it last.)
752     if sess.parse_sess.span_diagnostic.err_count() == 0 {
753         gate_all!(type_ascription, "type ascription is experimental");
754     }
755
756     visit::walk_crate(&mut visitor, krate);
757 }
758
759 fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
760     use rustc_errors::Applicability;
761
762     if !sess.opts.unstable_features.is_nightly_build() {
763         let lang_features = &sess.features_untracked().declared_lang_features;
764         for attr in krate.attrs.iter().filter(|attr| sess.check_name(attr, sym::feature)) {
765             let mut err = struct_span_err!(
766                 sess.parse_sess.span_diagnostic,
767                 attr.span,
768                 E0554,
769                 "`#![feature]` may not be used on the {} release channel",
770                 option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)")
771             );
772             let mut all_stable = true;
773             for ident in
774                 attr.meta_item_list().into_iter().flatten().map(|nested| nested.ident()).flatten()
775             {
776                 let name = ident.name;
777                 let stable_since = lang_features
778                     .iter()
779                     .flat_map(|&(feature, _, since)| if feature == name { since } else { None })
780                     .next();
781                 if let Some(since) = stable_since {
782                     err.help(&format!(
783                         "the feature `{}` has been stable since {} and no longer requires \
784                                   an attribute to enable",
785                         name, since
786                     ));
787                 } else {
788                     all_stable = false;
789                 }
790             }
791             if all_stable {
792                 err.span_suggestion(
793                     attr.span,
794                     "remove the attribute",
795                     String::new(),
796                     Applicability::MachineApplicable,
797                 );
798             }
799             err.emit();
800         }
801     }
802 }
803
804 fn check_incompatible_features(sess: &Session) {
805     let features = sess.features_untracked();
806
807     let declared_features = features
808         .declared_lang_features
809         .iter()
810         .copied()
811         .map(|(name, span, _)| (name, span))
812         .chain(features.declared_lib_features.iter().copied());
813
814     for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
815         .iter()
816         .filter(|&&(f1, f2)| features.enabled(f1) && features.enabled(f2))
817     {
818         if let Some((f1_name, f1_span)) = declared_features.clone().find(|(name, _)| name == f1) {
819             if let Some((f2_name, f2_span)) = declared_features.clone().find(|(name, _)| name == f2)
820             {
821                 let spans = vec![f1_span, f2_span];
822                 sess.struct_span_err(
823                     spans.clone(),
824                     &format!(
825                         "features `{}` and `{}` are incompatible, using them at the same time \
826                         is not allowed",
827                         f1_name, f2_name
828                     ),
829                 )
830                 .help("remove one of these features")
831                 .emit();
832             }
833         }
834     }
835 }