]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_ast_passes/src/feature_gate.rs
New upstream version 1.53.0+dfsg1
[rustc.git] / compiler / rustc_ast_passes / src / feature_gate.rs
CommitLineData
3dfed10e 1use rustc_ast as ast;
74b04a01 2use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
3dfed10e 3use rustc_ast::{AssocTyConstraint, AssocTyConstraintKind, NodeId};
5869c6ff 4use rustc_ast::{PatKind, RangeEnd, VariantData};
3dfed10e 5use rustc_errors::struct_span_err;
dfeec247 6use rustc_feature::{AttributeGate, BUILTIN_ATTRIBUTE_MAP};
3dfed10e
XL
7use rustc_feature::{Features, GateIssue};
8use rustc_session::parse::{feature_err, feature_err_issue};
9use rustc_session::Session;
dfeec247 10use rustc_span::source_map::Spanned;
cdc7bbd5 11use rustc_span::symbol::sym;
dfeec247 12use rustc_span::Span;
dfeec247 13
3dfed10e 14use tracing::debug;
dfeec247
XL
15
16macro_rules! gate_feature_fn {
5869c6ff
XL
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 }};
3dfed10e
XL
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);
dfeec247
XL
32 debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature);
33 if !has_feature && !span.allows_unstable($name) {
3dfed10e
XL
34 feature_err_issue(&visitor.sess.parse_sess, name, span, GateIssue::Language, explain)
35 .emit();
dfeec247
XL
36 }
37 }};
38}
39
40macro_rules! gate_feature_post {
5869c6ff
XL
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 };
3dfed10e
XL
44 ($visitor: expr, $feature: ident, $span: expr, $explain: expr) => {
45 gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain)
dfeec247
XL
46 };
47}
48
3dfed10e
XL
49pub fn check_attribute(attr: &ast::Attribute, sess: &Session, features: &Features) {
50 PostExpansionVisitor { sess, features }.visit_attribute(attr)
dfeec247
XL
51}
52
53struct PostExpansionVisitor<'a> {
3dfed10e
XL
54 sess: &'a Session,
55
56 // `sess` contains a `Features`, but this might not be that one.
dfeec247
XL
57 features: &'a Features,
58}
59
60impl<'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 }
f035d41b
XL
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 }
dfeec247
XL
151 "efiapi" => {
152 gate_feature_post!(
153 &self,
154 abi_efiapi,
155 span,
156 "efiapi ABI is experimental and subject to change"
157 );
158 }
5869c6ff
XL
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 }
6a06907d
XL
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 }
cdc7bbd5
XL
199 "wasm" => {
200 gate_feature_post!(
201 &self,
202 wasm_abi,
203 span,
204 "wasm ABI is experimental and subject to change"
205 );
206 }
dfeec247 207 abi => self
3dfed10e 208 .sess
dfeec247
XL
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(
3dfed10e 238 &self.sess.parse_sess,
dfeec247
XL
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,
6a06907d 290 min_type_alias_impl_trait,
dfeec247
XL
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
302impl<'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.
3dfed10e 311 if self.sess.check_name(attr, sym::doc) {
dfeec247
XL
312 for nested_meta in attr.meta_item_list().unwrap_or_default() {
313 macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => {
3dfed10e 314 $(if nested_meta.has_name(sym::$name) {
dfeec247
XL
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 include => external_doc
322 cfg => doc_cfg
323 masked => doc_masked
cdc7bbd5 324 notable_trait => doc_notable_trait
dfeec247
XL
325 keyword => doc_keyword
326 );
327 }
328 }
329 }
330
dfeec247
XL
331 fn visit_item(&mut self, i: &'a ast::Item) {
332 match i.kind {
333 ast::ItemKind::ForeignMod(ref foreign_module) => {
334 if let Some(abi) = foreign_module.abi {
335 self.check_abi(abi);
336 }
337 }
338
339 ast::ItemKind::Fn(..) => {
3dfed10e 340 if self.sess.contains_name(&i.attrs[..], sym::plugin_registrar) {
dfeec247
XL
341 gate_feature_post!(
342 &self,
343 plugin_registrar,
344 i.span,
345 "compiler plugins are experimental and possibly buggy"
346 );
347 }
3dfed10e 348 if self.sess.contains_name(&i.attrs[..], sym::start) {
dfeec247
XL
349 gate_feature_post!(
350 &self,
351 start,
352 i.span,
353 "`#[start]` functions are experimental \
ba9703b0
XL
354 and their signature may change \
355 over time"
dfeec247
XL
356 );
357 }
dfeec247
XL
358 }
359
360 ast::ItemKind::Struct(..) => {
3dfed10e 361 for attr in self.sess.filter_by_name(&i.attrs[..], sym::repr) {
dfeec247 362 for item in attr.meta_item_list().unwrap_or_else(Vec::new) {
3dfed10e 363 if item.has_name(sym::simd) {
dfeec247
XL
364 gate_feature_post!(
365 &self,
366 repr_simd,
367 attr.span,
368 "SIMD types are experimental and possibly buggy"
369 );
370 }
371 }
372 }
373 }
374
375 ast::ItemKind::Enum(ast::EnumDef { ref variants, .. }, ..) => {
376 for variant in variants {
377 match (&variant.data, &variant.disr_expr) {
378 (ast::VariantData::Unit(..), _) => {}
379 (_, Some(disr_expr)) => gate_feature_post!(
380 &self,
381 arbitrary_enum_discriminant,
382 disr_expr.value.span,
383 "discriminants on non-unit variants are experimental"
384 ),
385 _ => {}
386 }
387 }
388
389 let has_feature = self.features.arbitrary_enum_discriminant;
390 if !has_feature && !i.span.allows_unstable(sym::arbitrary_enum_discriminant) {
391 self.maybe_report_invalid_custom_discriminants(&variants);
392 }
393 }
394
5869c6ff
XL
395 ast::ItemKind::Impl(box ast::ImplKind {
396 polarity, defaultness, ref of_trait, ..
397 }) => {
ba9703b0 398 if let ast::ImplPolarity::Negative(span) = polarity {
dfeec247
XL
399 gate_feature_post!(
400 &self,
ba9703b0 401 negative_impls,
5869c6ff 402 span.to(of_trait.as_ref().map_or(span, |t| t.path.span)),
dfeec247 403 "negative trait bounds are not yet fully implemented; \
ba9703b0 404 use marker types for now"
dfeec247
XL
405 );
406 }
407
74b04a01 408 if let ast::Defaultness::Default(_) = defaultness {
dfeec247
XL
409 gate_feature_post!(&self, specialization, i.span, "specialization is unstable");
410 }
411 }
412
5869c6ff 413 ast::ItemKind::Trait(box ast::TraitKind(ast::IsAuto::Yes, ..)) => {
dfeec247
XL
414 gate_feature_post!(
415 &self,
fc512014 416 auto_traits,
dfeec247
XL
417 i.span,
418 "auto traits are experimental and possibly buggy"
419 );
420 }
421
422 ast::ItemKind::TraitAlias(..) => {
423 gate_feature_post!(&self, trait_alias, i.span, "trait aliases are experimental");
424 }
425
ba9703b0 426 ast::ItemKind::MacroDef(ast::MacroDef { macro_rules: false, .. }) => {
dfeec247
XL
427 let msg = "`macro` is experimental";
428 gate_feature_post!(&self, decl_macro, i.span, msg);
429 }
430
5869c6ff
XL
431 ast::ItemKind::TyAlias(box ast::TyAliasKind(_, _, _, Some(ref ty))) => {
432 self.check_impl_trait(&ty)
433 }
dfeec247
XL
434
435 _ => {}
436 }
437
438 visit::walk_item(self, i);
439 }
440
441 fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) {
442 match i.kind {
443 ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => {
3dfed10e 444 let link_name = self.sess.first_attr_value_str_by_name(&i.attrs, sym::link_name);
5869c6ff
XL
445 let links_to_llvm =
446 link_name.map_or(false, |val| val.as_str().starts_with("llvm."));
dfeec247
XL
447 if links_to_llvm {
448 gate_feature_post!(
449 &self,
450 link_llvm_intrinsics,
451 i.span,
452 "linking to LLVM intrinsics is experimental"
453 );
454 }
455 }
74b04a01 456 ast::ForeignItemKind::TyAlias(..) => {
dfeec247
XL
457 gate_feature_post!(&self, extern_types, i.span, "extern types are experimental");
458 }
ba9703b0 459 ast::ForeignItemKind::MacCall(..) => {}
dfeec247
XL
460 }
461
462 visit::walk_foreign_item(self, i)
463 }
464
465 fn visit_ty(&mut self, ty: &'a ast::Ty) {
466 match ty.kind {
467 ast::TyKind::BareFn(ref bare_fn_ty) => {
468 self.check_extern(bare_fn_ty.ext);
469 }
470 ast::TyKind::Never => {
471 gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental");
472 }
473 _ => {}
474 }
475 visit::walk_ty(self, ty)
476 }
477
74b04a01
XL
478 fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) {
479 if let ast::FnRetTy::Ty(ref output_ty) = *ret_ty {
dfeec247
XL
480 if let ast::TyKind::Never = output_ty.kind {
481 // Do nothing.
482 } else {
483 self.visit_ty(output_ty)
484 }
485 }
486 }
487
488 fn visit_expr(&mut self, e: &'a ast::Expr) {
489 match e.kind {
490 ast::ExprKind::Box(_) => {
491 gate_feature_post!(
492 &self,
493 box_syntax,
494 e.span,
495 "box expression syntax is experimental; you can call `Box::new` instead"
496 );
497 }
498 ast::ExprKind::Type(..) => {
499 // To avoid noise about type ascription in common syntax errors, only emit if it
500 // is the *only* error.
3dfed10e 501 if self.sess.parse_sess.span_diagnostic.err_count() == 0 {
dfeec247
XL
502 gate_feature_post!(
503 &self,
504 type_ascription,
505 e.span,
506 "type ascription is experimental"
507 );
508 }
509 }
510 ast::ExprKind::TryBlock(_) => {
511 gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental");
512 }
513 ast::ExprKind::Block(_, opt_label) => {
514 if let Some(label) = opt_label {
515 gate_feature_post!(
516 &self,
517 label_break_value,
518 label.ident.span,
519 "labels on blocks are unstable"
520 );
521 }
522 }
523 _ => {}
524 }
525 visit::walk_expr(self, e)
526 }
527
528 fn visit_pat(&mut self, pattern: &'a ast::Pat) {
529 match &pattern.kind {
530 PatKind::Box(..) => {
531 gate_feature_post!(
532 &self,
533 box_patterns,
534 pattern.span,
535 "box pattern syntax is experimental"
536 );
537 }
538 PatKind::Range(_, _, Spanned { node: RangeEnd::Excluded, .. }) => {
539 gate_feature_post!(
540 &self,
541 exclusive_range_pattern,
542 pattern.span,
543 "exclusive range pattern syntax is experimental"
544 );
545 }
546 _ => {}
547 }
548 visit::walk_pat(self, pattern)
549 }
550
74b04a01 551 fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) {
dfeec247 552 if let Some(header) = fn_kind.header() {
74b04a01 553 // Stability of const fn methods are covered in `visit_assoc_item` below.
dfeec247 554 self.check_extern(header.ext);
74b04a01
XL
555
556 if let (ast::Const::Yes(_), ast::Extern::Implicit)
557 | (ast::Const::Yes(_), ast::Extern::Explicit(_)) = (header.constness, header.ext)
558 {
559 gate_feature_post!(
560 &self,
561 const_extern_fn,
562 span,
563 "`const extern fn` definitions are unstable"
564 );
565 }
dfeec247
XL
566 }
567
74b04a01 568 if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() {
dfeec247
XL
569 gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable");
570 }
571
74b04a01 572 visit::walk_fn(self, fn_kind, span)
dfeec247
XL
573 }
574
dfeec247 575 fn visit_assoc_ty_constraint(&mut self, constraint: &'a AssocTyConstraint) {
ba9703b0
XL
576 if let AssocTyConstraintKind::Bound { .. } = constraint.kind {
577 gate_feature_post!(
dfeec247
XL
578 &self,
579 associated_type_bounds,
580 constraint.span,
581 "associated type bounds are unstable"
ba9703b0 582 )
dfeec247
XL
583 }
584 visit::walk_assoc_ty_constraint(self, constraint)
585 }
586
74b04a01 587 fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) {
ba9703b0 588 let is_fn = match i.kind {
cdc7bbd5 589 ast::AssocItemKind::Fn(_) => true,
5869c6ff 590 ast::AssocItemKind::TyAlias(box ast::TyAliasKind(_, ref generics, _, ref ty)) => {
74b04a01 591 if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) {
dfeec247
XL
592 gate_feature_post!(
593 &self,
594 associated_type_defaults,
74b04a01 595 i.span,
dfeec247
XL
596 "associated type defaults are unstable"
597 );
598 }
dfeec247
XL
599 if let Some(ty) = ty {
600 self.check_impl_trait(ty);
601 }
74b04a01 602 self.check_gat(generics, i.span);
ba9703b0 603 false
dfeec247 604 }
ba9703b0
XL
605 _ => false,
606 };
607 if let ast::Defaultness::Default(_) = i.kind.defaultness() {
608 // Limit `min_specialization` to only specializing functions.
609 gate_feature_fn!(
610 &self,
611 |x: &Features| x.specialization || (is_fn && x.min_specialization),
612 i.span,
613 sym::specialization,
614 "specialization is unstable"
615 );
dfeec247 616 }
74b04a01 617 visit::walk_assoc_item(self, i, ctxt)
dfeec247
XL
618 }
619
620 fn visit_vis(&mut self, vis: &'a ast::Visibility) {
1b1a35ee 621 if let ast::VisibilityKind::Crate(ast::CrateSugar::JustCrate) = vis.kind {
dfeec247
XL
622 gate_feature_post!(
623 &self,
624 crate_visibility_modifier,
625 vis.span,
626 "`crate` visibility modifier is experimental"
627 );
628 }
629 visit::walk_vis(self, vis)
630 }
631}
632
3dfed10e
XL
633pub fn check_crate(krate: &ast::Crate, sess: &Session) {
634 maybe_stage_features(sess, krate);
1b1a35ee 635 check_incompatible_features(sess);
3dfed10e 636 let mut visitor = PostExpansionVisitor { sess, features: &sess.features_untracked() };
dfeec247 637
3dfed10e 638 let spans = sess.parse_sess.gated_spans.spans.borrow();
dfeec247 639 macro_rules! gate_all {
5869c6ff
XL
640 ($gate:ident, $msg:literal, $help:literal) => {
641 if let Some(spans) = spans.get(&sym::$gate) {
642 for span in spans {
643 gate_feature_post!(&visitor, $gate, *span, $msg, $help);
644 }
645 }
646 };
dfeec247 647 ($gate:ident, $msg:literal) => {
3dfed10e
XL
648 if let Some(spans) = spans.get(&sym::$gate) {
649 for span in spans {
650 gate_feature_post!(&visitor, $gate, *span, $msg);
651 }
dfeec247
XL
652 }
653 };
654 }
6a06907d
XL
655 gate_all!(
656 if_let_guard,
657 "`if let` guards are experimental",
658 "you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`"
659 );
660 gate_all!(
661 let_chains,
662 "`let` expressions in this position are experimental",
663 "you can write `matches!(<expr>, <pattern>)` instead of `let <pattern> = <expr>`"
664 );
5869c6ff
XL
665 gate_all!(
666 async_closure,
667 "async closures are unstable",
668 "to use an async block, remove the `||`: `async {`"
669 );
dfeec247 670 gate_all!(generators, "yield syntax is experimental");
dfeec247
XL
671 gate_all!(raw_ref_op, "raw address of syntax is experimental");
672 gate_all!(const_trait_bound_opt_out, "`?const` on trait bounds is experimental");
673 gate_all!(const_trait_impl, "const trait impls are experimental");
674 gate_all!(half_open_range_patterns, "half-open range patterns are unstable");
29967ef6 675 gate_all!(inline_const, "inline-const is experimental");
fc512014
XL
676 gate_all!(
677 extended_key_value_attributes,
678 "arbitrary expressions in key-value attributes are unstable"
679 );
5869c6ff
XL
680 gate_all!(
681 const_generics_defaults,
682 "default values for const generic parameters are experimental"
683 );
fc512014
XL
684 if sess.parse_sess.span_diagnostic.err_count() == 0 {
685 // Errors for `destructuring_assignment` can get quite noisy, especially where `_` is
686 // involved, so we only emit errors where there are no other parsing errors.
687 gate_all!(destructuring_assignment, "destructuring assignments are unstable");
688 }
dfeec247
XL
689
690 // All uses of `gate_all!` below this point were added in #65742,
691 // and subsequently disabled (with the non-early gating readded).
692 macro_rules! gate_all {
693 ($gate:ident, $msg:literal) => {
694 // FIXME(eddyb) do something more useful than always
695 // disabling these uses of early feature-gatings.
696 if false {
697 for span in spans.get(&sym::$gate).unwrap_or(&vec![]) {
698 gate_feature_post!(&visitor, $gate, *span, $msg);
699 }
700 }
701 };
702 }
703
704 gate_all!(trait_alias, "trait aliases are experimental");
705 gate_all!(associated_type_bounds, "associated type bounds are unstable");
706 gate_all!(crate_visibility_modifier, "`crate` visibility modifier is experimental");
707 gate_all!(const_generics, "const generics are unstable");
708 gate_all!(decl_macro, "`macro` is experimental");
709 gate_all!(box_patterns, "box pattern syntax is experimental");
710 gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental");
711 gate_all!(try_blocks, "`try` blocks are unstable");
712 gate_all!(label_break_value, "labels on blocks are unstable");
713 gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead");
714 // To avoid noise about type ascription in common syntax errors,
715 // only emit if it is the *only* error. (Also check it last.)
3dfed10e 716 if sess.parse_sess.span_diagnostic.err_count() == 0 {
dfeec247
XL
717 gate_all!(type_ascription, "type ascription is experimental");
718 }
719
720 visit::walk_crate(&mut visitor, krate);
721}
722
3dfed10e 723fn maybe_stage_features(sess: &Session, krate: &ast::Crate) {
cdc7bbd5
XL
724 use rustc_errors::Applicability;
725
3dfed10e 726 if !sess.opts.unstable_features.is_nightly_build() {
cdc7bbd5 727 let lang_features = &sess.features_untracked().declared_lang_features;
3dfed10e 728 for attr in krate.attrs.iter().filter(|attr| sess.check_name(attr, sym::feature)) {
cdc7bbd5 729 let mut err = struct_span_err!(
3dfed10e 730 sess.parse_sess.span_diagnostic,
dfeec247
XL
731 attr.span,
732 E0554,
733 "`#![feature]` may not be used on the {} release channel",
734 option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)")
cdc7bbd5
XL
735 );
736 let mut all_stable = true;
737 for ident in
738 attr.meta_item_list().into_iter().flatten().map(|nested| nested.ident()).flatten()
739 {
740 let name = ident.name;
741 let stable_since = lang_features
742 .iter()
743 .flat_map(|&(feature, _, since)| if feature == name { since } else { None })
744 .next();
745 if let Some(since) = stable_since {
746 err.help(&format!(
747 "the feature `{}` has been stable since {} and no longer requires \
748 an attribute to enable",
749 name, since
750 ));
751 } else {
752 all_stable = false;
753 }
754 }
755 if all_stable {
756 err.span_suggestion(
757 attr.span,
758 "remove the attribute",
759 String::new(),
760 Applicability::MachineApplicable,
761 );
762 }
763 err.emit();
dfeec247
XL
764 }
765 }
766}
1b1a35ee
XL
767
768fn check_incompatible_features(sess: &Session) {
769 let features = sess.features_untracked();
770
771 let declared_features = features
772 .declared_lang_features
773 .iter()
774 .copied()
775 .map(|(name, span, _)| (name, span))
776 .chain(features.declared_lib_features.iter().copied());
777
778 for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES
779 .iter()
780 .filter(|&&(f1, f2)| features.enabled(f1) && features.enabled(f2))
781 {
782 if let Some((f1_name, f1_span)) = declared_features.clone().find(|(name, _)| name == f1) {
783 if let Some((f2_name, f2_span)) = declared_features.clone().find(|(name, _)| name == f2)
784 {
785 let spans = vec![f1_span, f2_span];
786 sess.struct_span_err(
787 spans.clone(),
788 &format!(
789 "features `{}` and `{}` are incompatible, using them at the same time \
790 is not allowed",
791 f1_name, f2_name
792 ),
793 )
794 .help("remove one of these features")
795 .emit();
796 }
797 }
798 }
799}