]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/feature_gate.rs
Rollup merge of #81008 - tmiasko:generator-layout-err, 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::{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, Symbol};
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             abi => self
160                 .sess
161                 .parse_sess
162                 .span_diagnostic
163                 .delay_span_bug(span, &format!("unrecognized ABI not caught in lowering: {}", abi)),
164         }
165     }
166
167     fn check_extern(&self, ext: ast::Extern) {
168         if let ast::Extern::Explicit(abi) = ext {
169             self.check_abi(abi);
170         }
171     }
172
173     fn maybe_report_invalid_custom_discriminants(&self, variants: &[ast::Variant]) {
174         let has_fields = variants.iter().any(|variant| match variant.data {
175             VariantData::Tuple(..) | VariantData::Struct(..) => true,
176             VariantData::Unit(..) => false,
177         });
178
179         let discriminant_spans = variants
180             .iter()
181             .filter(|variant| match variant.data {
182                 VariantData::Tuple(..) | VariantData::Struct(..) => false,
183                 VariantData::Unit(..) => true,
184             })
185             .filter_map(|variant| variant.disr_expr.as_ref().map(|c| c.value.span))
186             .collect::<Vec<_>>();
187
188         if !discriminant_spans.is_empty() && has_fields {
189             let mut err = feature_err(
190                 &self.sess.parse_sess,
191                 sym::arbitrary_enum_discriminant,
192                 discriminant_spans.clone(),
193                 "custom discriminant values are not allowed in enums with tuple or struct variants",
194             );
195             for sp in discriminant_spans {
196                 err.span_label(sp, "disallowed custom discriminant");
197             }
198             for variant in variants.iter() {
199                 match &variant.data {
200                     VariantData::Struct(..) => {
201                         err.span_label(variant.span, "struct variant defined here");
202                     }
203                     VariantData::Tuple(..) => {
204                         err.span_label(variant.span, "tuple variant defined here");
205                     }
206                     VariantData::Unit(..) => {}
207                 }
208             }
209             err.emit();
210         }
211     }
212
213     fn check_gat(&self, generics: &ast::Generics, span: Span) {
214         if !generics.params.is_empty() {
215             gate_feature_post!(
216                 &self,
217                 generic_associated_types,
218                 span,
219                 "generic associated types are unstable"
220             );
221         }
222         if !generics.where_clause.predicates.is_empty() {
223             gate_feature_post!(
224                 &self,
225                 generic_associated_types,
226                 span,
227                 "where clauses on associated types are unstable"
228             );
229         }
230     }
231
232     /// Feature gate `impl Trait` inside `type Alias = $type_expr;`.
233     fn check_impl_trait(&self, ty: &ast::Ty) {
234         struct ImplTraitVisitor<'a> {
235             vis: &'a PostExpansionVisitor<'a>,
236         }
237         impl Visitor<'_> for ImplTraitVisitor<'_> {
238             fn visit_ty(&mut self, ty: &ast::Ty) {
239                 if let ast::TyKind::ImplTrait(..) = ty.kind {
240                     gate_feature_post!(
241                         &self.vis,
242                         type_alias_impl_trait,
243                         ty.span,
244                         "`impl Trait` in type aliases is unstable"
245                     );
246                 }
247                 visit::walk_ty(self, ty);
248             }
249         }
250         ImplTraitVisitor { vis: self }.visit_ty(ty);
251     }
252 }
253
254 impl<'a> Visitor<'a> for PostExpansionVisitor<'a> {
255     fn visit_attribute(&mut self, attr: &ast::Attribute) {
256         let attr_info =
257             attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)).map(|a| **a);
258         // Check feature gates for built-in attributes.
259         if let Some((.., AttributeGate::Gated(_, name, descr, has_feature))) = attr_info {
260             gate_feature_fn!(self, has_feature, attr.span, name, descr);
261         }
262         // Check unstable flavors of the `#[doc]` attribute.
263         if self.sess.check_name(attr, sym::doc) {
264             for nested_meta in attr.meta_item_list().unwrap_or_default() {
265                 macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => {
266                     $(if nested_meta.has_name(sym::$name) {
267                         let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental");
268                         gate_feature_post!(self, $feature, attr.span, msg);
269                     })*
270                 }}
271
272                 gate_doc!(
273                     include => external_doc
274                     cfg => doc_cfg
275                     masked => doc_masked
276                     spotlight => doc_spotlight
277                     keyword => doc_keyword
278                 );
279             }
280         }
281     }
282
283     fn visit_name(&mut self, sp: Span, name: Symbol) {
284         if !name.as_str().is_ascii() {
285             gate_feature_post!(
286                 &self,
287                 non_ascii_idents,
288                 self.sess.parse_sess.source_map().guess_head_span(sp),
289                 "non-ascii idents are not fully supported"
290             );
291         }
292     }
293
294     fn visit_item(&mut self, i: &'a ast::Item) {
295         match i.kind {
296             ast::ItemKind::ForeignMod(ref foreign_module) => {
297                 if let Some(abi) = foreign_module.abi {
298                     self.check_abi(abi);
299                 }
300             }
301
302             ast::ItemKind::Fn(..) => {
303                 if self.sess.contains_name(&i.attrs[..], sym::plugin_registrar) {
304                     gate_feature_post!(
305                         &self,
306                         plugin_registrar,
307                         i.span,
308                         "compiler plugins are experimental and possibly buggy"
309                     );
310                 }
311                 if self.sess.contains_name(&i.attrs[..], sym::start) {
312                     gate_feature_post!(
313                         &self,
314                         start,
315                         i.span,
316                         "`#[start]` functions are experimental \
317                          and their signature may change \
318                          over time"
319                     );
320                 }
321                 if self.sess.contains_name(&i.attrs[..], sym::main) {
322                     gate_feature_post!(
323                         &self,
324                         main,
325                         i.span,
326                         "declaration of a non-standard `#[main]` \
327                          function may change over time, for now \
328                          a top-level `fn main()` is required"
329                     );
330                 }
331             }
332
333             ast::ItemKind::Struct(..) => {
334                 for attr in self.sess.filter_by_name(&i.attrs[..], sym::repr) {
335                     for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
336                         if item.has_name(sym::simd) {
337                             gate_feature_post!(
338                                 &self,
339                                 repr_simd,
340                                 attr.span,
341                                 "SIMD types are experimental and possibly buggy"
342                             );
343                         }
344                     }
345                 }
346             }
347
348             ast::ItemKind::Enum(ast::EnumDef { ref variants, .. }, ..) => {
349                 for variant in variants {
350                     match (&variant.data, &variant.disr_expr) {
351                         (ast::VariantData::Unit(..), _) => {}
352                         (_, Some(disr_expr)) => gate_feature_post!(
353                             &self,
354                             arbitrary_enum_discriminant,
355                             disr_expr.value.span,
356                             "discriminants on non-unit variants are experimental"
357                         ),
358                         _ => {}
359                     }
360                 }
361
362                 let has_feature = self.features.arbitrary_enum_discriminant;
363                 if !has_feature && !i.span.allows_unstable(sym::arbitrary_enum_discriminant) {
364                     self.maybe_report_invalid_custom_discriminants(&variants);
365                 }
366             }
367
368             ast::ItemKind::Impl { polarity, defaultness, ref of_trait, .. } => {
369                 if let ast::ImplPolarity::Negative(span) = polarity {
370                     gate_feature_post!(
371                         &self,
372                         negative_impls,
373                         span.to(of_trait.as_ref().map_or(span, |t| t.path.span)),
374                         "negative trait bounds are not yet fully implemented; \
375                          use marker types for now"
376                     );
377                 }
378
379                 if let ast::Defaultness::Default(_) = defaultness {
380                     gate_feature_post!(&self, specialization, i.span, "specialization is unstable");
381                 }
382             }
383
384             ast::ItemKind::Trait(ast::IsAuto::Yes, ..) => {
385                 gate_feature_post!(
386                     &self,
387                     auto_traits,
388                     i.span,
389                     "auto traits are experimental and possibly buggy"
390                 );
391             }
392
393             ast::ItemKind::TraitAlias(..) => {
394                 gate_feature_post!(&self, trait_alias, i.span, "trait aliases are experimental");
395             }
396
397             ast::ItemKind::MacroDef(ast::MacroDef { macro_rules: false, .. }) => {
398                 let msg = "`macro` is experimental";
399                 gate_feature_post!(&self, decl_macro, i.span, msg);
400             }
401
402             ast::ItemKind::TyAlias(_, _, _, Some(ref ty)) => self.check_impl_trait(&ty),
403
404             _ => {}
405         }
406
407         visit::walk_item(self, i);
408     }
409
410     fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
411         match i.kind {
412             ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
413                 let link_name = self.sess.first_attr_value_str_by_name(&i.attrs, sym::link_name);
414                 let links_to_llvm =
415                     link_name.map_or(false, |val| val.as_str().starts_with("llvm."));
416                 if links_to_llvm {
417                     gate_feature_post!(
418                         &self,
419                         link_llvm_intrinsics,
420                         i.span,
421                         "linking to LLVM intrinsics is experimental"
422                     );
423                 }
424             }
425             ast::ForeignItemKind::TyAlias(..) => {
426                 gate_feature_post!(&self, extern_types, i.span, "extern types are experimental");
427             }
428             ast::ForeignItemKind::MacCall(..) => {}
429         }
430
431         visit::walk_foreign_item(self, i)
432     }
433
434     fn visit_ty(&mut self, ty: &'a ast::Ty) {
435         match ty.kind {
436             ast::TyKind::BareFn(ref bare_fn_ty) => {
437                 self.check_extern(bare_fn_ty.ext);
438             }
439             ast::TyKind::Never => {
440                 gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental");
441             }
442             _ => {}
443         }
444         visit::walk_ty(self, ty)
445     }
446
447     fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) {
448         if let ast::FnRetTy::Ty(ref output_ty) = *ret_ty {
449             if let ast::TyKind::Never = output_ty.kind {
450                 // Do nothing.
451             } else {
452                 self.visit_ty(output_ty)
453             }
454         }
455     }
456
457     fn visit_expr(&mut self, e: &'a ast::Expr) {
458         match e.kind {
459             ast::ExprKind::Box(_) => {
460                 gate_feature_post!(
461                     &self,
462                     box_syntax,
463                     e.span,
464                     "box expression syntax is experimental; you can call `Box::new` instead"
465                 );
466             }
467             ast::ExprKind::Type(..) => {
468                 // To avoid noise about type ascription in common syntax errors, only emit if it
469                 // is the *only* error.
470                 if self.sess.parse_sess.span_diagnostic.err_count() == 0 {
471                     gate_feature_post!(
472                         &self,
473                         type_ascription,
474                         e.span,
475                         "type ascription is experimental"
476                     );
477                 }
478             }
479             ast::ExprKind::TryBlock(_) => {
480                 gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
481             }
482             ast::ExprKind::Block(_, opt_label) => {
483                 if let Some(label) = opt_label {
484                     gate_feature_post!(
485                         &self,
486                         label_break_value,
487                         label.ident.span,
488                         "labels on blocks are unstable"
489                     );
490                 }
491             }
492             _ => {}
493         }
494         visit::walk_expr(self, e)
495     }
496
497     fn visit_pat(&mut self, pattern: &'a ast::Pat) {
498         match &pattern.kind {
499             PatKind::Box(..) => {
500                 gate_feature_post!(
501                     &self,
502                     box_patterns,
503                     pattern.span,
504                     "box pattern syntax is experimental"
505                 );
506             }
507             PatKind::Range(_, _, Spanned { node: RangeEnd::Excluded, .. }) => {
508                 gate_feature_post!(
509                     &self,
510                     exclusive_range_pattern,
511                     pattern.span,
512                     "exclusive range pattern syntax is experimental"
513                 );
514             }
515             _ => {}
516         }
517         visit::walk_pat(self, pattern)
518     }
519
520     fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
521         if let Some(header) = fn_kind.header() {
522             // Stability of const fn methods are covered in `visit_assoc_item` below.
523             self.check_extern(header.ext);
524
525             if let (ast::Const::Yes(_), ast::Extern::Implicit)
526             | (ast::Const::Yes(_), ast::Extern::Explicit(_)) = (header.constness, header.ext)
527             {
528                 gate_feature_post!(
529                     &self,
530                     const_extern_fn,
531                     span,
532                     "`const extern fn` definitions are unstable"
533                 );
534             }
535         }
536
537         if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() {
538             gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable");
539         }
540
541         visit::walk_fn(self, fn_kind, span)
542     }
543
544     fn visit_assoc_ty_constraint(&mut self, constraint: &'a AssocTyConstraint) {
545         if let AssocTyConstraintKind::Bound { .. } = constraint.kind {
546             gate_feature_post!(
547                 &self,
548                 associated_type_bounds,
549                 constraint.span,
550                 "associated type bounds are unstable"
551             )
552         }
553         visit::walk_assoc_ty_constraint(self, constraint)
554     }
555
556     fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
557         let is_fn = match i.kind {
558             ast::AssocItemKind::Fn(_, ref sig, _, _) => {
559                 if let (ast::Const::Yes(_), AssocCtxt::Trait) = (sig.header.constness, ctxt) {
560                     gate_feature_post!(&self, const_fn, i.span, "const fn is unstable");
561                 }
562                 true
563             }
564             ast::AssocItemKind::TyAlias(_, ref generics, _, ref ty) => {
565                 if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
566                     gate_feature_post!(
567                         &self,
568                         associated_type_defaults,
569                         i.span,
570                         "associated type defaults are unstable"
571                     );
572                 }
573                 if let Some(ty) = ty {
574                     self.check_impl_trait(ty);
575                 }
576                 self.check_gat(generics, i.span);
577                 false
578             }
579             _ => false,
580         };
581         if let ast::Defaultness::Default(_) = i.kind.defaultness() {
582             // Limit `min_specialization` to only specializing functions.
583             gate_feature_fn!(
584                 &self,
585                 |x: &Features| x.specialization || (is_fn && x.min_specialization),
586                 i.span,
587                 sym::specialization,
588                 "specialization is unstable"
589             );
590         }
591         visit::walk_assoc_item(self, i, ctxt)
592     }
593
594     fn visit_vis(&mut self, vis: &'a ast::Visibility) {
595         if let ast::VisibilityKind::Crate(ast::CrateSugar::JustCrate) = vis.kind {
596             gate_feature_post!(
597                 &self,
598                 crate_visibility_modifier,
599                 vis.span,
600                 "`crate` visibility modifier is experimental"
601             );
602         }
603         visit::walk_vis(self, vis)
604     }
605 }
606
607 pub fn check_crate(krate: &ast::Crate, sess: &Session) {
608     maybe_stage_features(sess, krate);
609     check_incompatible_features(sess);
610     let mut visitor = PostExpansionVisitor { sess, features: &sess.features_untracked() };
611
612     let spans = sess.parse_sess.gated_spans.spans.borrow();
613     macro_rules! gate_all {
614         ($gate:ident, $msg:literal, $help:literal) => {
615             if let Some(spans) = spans.get(&sym::$gate) {
616                 for span in spans {
617                     gate_feature_post!(&visitor, $gate, *span, $msg, $help);
618                 }
619             }
620         };
621         ($gate:ident, $msg:literal) => {
622             if let Some(spans) = spans.get(&sym::$gate) {
623                 for span in spans {
624                     gate_feature_post!(&visitor, $gate, *span, $msg);
625                 }
626             }
627         };
628     }
629     gate_all!(if_let_guard, "`if let` guards are experimental");
630     gate_all!(let_chains, "`let` expressions in this position are experimental");
631     gate_all!(
632         async_closure,
633         "async closures are unstable",
634         "to use an async block, remove the `||`: `async {`"
635     );
636     gate_all!(generators, "yield syntax is experimental");
637     gate_all!(or_patterns, "or-patterns syntax is experimental");
638     gate_all!(raw_ref_op, "raw address of syntax is experimental");
639     gate_all!(const_trait_bound_opt_out, "`?const` on trait bounds is experimental");
640     gate_all!(const_trait_impl, "const trait impls are experimental");
641     gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
642     gate_all!(inline_const, "inline-const is experimental");
643     gate_all!(
644         extended_key_value_attributes,
645         "arbitrary expressions in key-value attributes are unstable"
646     );
647     gate_all!(
648         const_generics_defaults,
649         "default values for const generic parameters are experimental"
650     );
651     if sess.parse_sess.span_diagnostic.err_count() == 0 {
652         // Errors for `destructuring_assignment` can get quite noisy, especially where `_` is
653         // involved, so we only emit errors where there are no other parsing errors.
654         gate_all!(destructuring_assignment, "destructuring assignments are unstable");
655     }
656
657     // All uses of `gate_all!` below this point were added in #65742,
658     // and subsequently disabled (with the non-early gating readded).
659     macro_rules! gate_all {
660         ($gate:ident, $msg:literal) => {
661             // FIXME(eddyb) do something more useful than always
662             // disabling these uses of early feature-gatings.
663             if false {
664                 for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
665                     gate_feature_post!(&visitor, $gate, *span, $msg);
666                 }
667             }
668         };
669     }
670
671     gate_all!(trait_alias, "trait aliases are experimental");
672     gate_all!(associated_type_bounds, "associated type bounds are unstable");
673     gate_all!(crate_visibility_modifier, "`crate` visibility modifier is experimental");
674     gate_all!(const_generics, "const generics are unstable");
675     gate_all!(decl_macro, "`macro` is experimental");
676     gate_all!(box_patterns, "box pattern syntax is experimental");
677     gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental");
678     gate_all!(try_blocks, "`try` blocks are unstable");
679     gate_all!(label_break_value, "labels on blocks are unstable");
680     gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead");
681     // To avoid noise about type ascription in common syntax errors,
682     // only emit if it is the *only* error. (Also check it last.)
683     if sess.parse_sess.span_diagnostic.err_count() == 0 {
684         gate_all!(type_ascription, "type ascription is experimental");
685     }
686
687     visit::walk_crate(&mut visitor, krate);
688 }
689
690 fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
691     if !sess.opts.unstable_features.is_nightly_build() {
692         for attr in krate.attrs.iter().filter(|attr| sess.check_name(attr, sym::feature)) {
693             struct_span_err!(
694                 sess.parse_sess.span_diagnostic,
695                 attr.span,
696                 E0554,
697                 "`#![feature]` may not be used on the {} release channel",
698                 option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)")
699             )
700             .emit();
701         }
702     }
703 }
704
705 fn check_incompatible_features(sess: &Session) {
706     let features = sess.features_untracked();
707
708     let declared_features = features
709         .declared_lang_features
710         .iter()
711         .copied()
712         .map(|(name, span, _)| (name, span))
713         .chain(features.declared_lib_features.iter().copied());
714
715     for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
716         .iter()
717         .filter(|&&(f1, f2)| features.enabled(f1) && features.enabled(f2))
718     {
719         if let Some((f1_name, f1_span)) = declared_features.clone().find(|(name, _)| name == f1) {
720             if let Some((f2_name, f2_span)) = declared_features.clone().find(|(name, _)| name == f2)
721             {
722                 let spans = vec![f1_span, f2_span];
723                 sess.struct_span_err(
724                     spans.clone(),
725                     &format!(
726                         "features `{}` and `{}` are incompatible, using them at the same time \
727                         is not allowed",
728                         f1_name, f2_name
729                     ),
730                 )
731                 .help("remove one of these features")
732                 .emit();
733             }
734         }
735     }
736 }