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