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