]>
Commit | Line | Data |
---|---|---|
3dfed10e | 1 | use rustc_ast as ast; |
74b04a01 | 2 | use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor}; |
5099ac24 | 3 | use rustc_ast::{AssocConstraint, AssocConstraintKind, NodeId}; |
dc3f5686 | 4 | use rustc_ast::{PatKind, RangeEnd, VariantData}; |
f2b60f7d FG |
5 | use rustc_errors::{struct_span_err, Applicability, StashKey}; |
6 | use rustc_feature::Features; | |
3c0e092e | 7 | use rustc_feature::{AttributeGate, BuiltinAttribute, BUILTIN_ATTRIBUTE_MAP}; |
f2b60f7d | 8 | use rustc_session::parse::{feature_err, feature_warn}; |
3dfed10e | 9 | use rustc_session::Session; |
dfeec247 | 10 | use rustc_span::source_map::Spanned; |
cdc7bbd5 | 11 | use rustc_span::symbol::sym; |
dfeec247 | 12 | use rustc_span::Span; |
dfeec247 | 13 | |
dfeec247 | 14 | macro_rules! gate_feature_fn { |
5869c6ff XL |
15 | ($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr, $help: expr) => {{ |
16 | let (visitor, has_feature, span, name, explain, help) = | |
17 | (&*$visitor, $has_feature, $span, $name, $explain, $help); | |
18 | let has_feature: bool = has_feature(visitor.features); | |
19 | debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature); | |
20 | if !has_feature && !span.allows_unstable($name) { | |
f2b60f7d | 21 | feature_err(&visitor.sess.parse_sess, name, span, explain).help(help).emit(); |
5869c6ff XL |
22 | } |
23 | }}; | |
3dfed10e XL |
24 | ($visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{ |
25 | let (visitor, has_feature, span, name, explain) = | |
26 | (&*$visitor, $has_feature, $span, $name, $explain); | |
27 | let has_feature: bool = has_feature(visitor.features); | |
dfeec247 XL |
28 | debug!("gate_feature(feature = {:?}, span = {:?}); has? {}", name, span, has_feature); |
29 | if !has_feature && !span.allows_unstable($name) { | |
f2b60f7d FG |
30 | feature_err(&visitor.sess.parse_sess, name, span, explain).emit(); |
31 | } | |
32 | }}; | |
33 | (future_incompatible; $visitor: expr, $has_feature: expr, $span: expr, $name: expr, $explain: expr) => {{ | |
34 | let (visitor, has_feature, span, name, explain) = | |
35 | (&*$visitor, $has_feature, $span, $name, $explain); | |
36 | let has_feature: bool = has_feature(visitor.features); | |
37 | debug!( | |
38 | "gate_feature(feature = {:?}, span = {:?}); has? {} (future_incompatible)", | |
39 | name, span, has_feature | |
40 | ); | |
41 | if !has_feature && !span.allows_unstable($name) { | |
42 | feature_warn(&visitor.sess.parse_sess, name, span, explain); | |
dfeec247 XL |
43 | } |
44 | }}; | |
45 | } | |
46 | ||
47 | macro_rules! gate_feature_post { | |
5869c6ff XL |
48 | ($visitor: expr, $feature: ident, $span: expr, $explain: expr, $help: expr) => { |
49 | gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain, $help) | |
50 | }; | |
3dfed10e XL |
51 | ($visitor: expr, $feature: ident, $span: expr, $explain: expr) => { |
52 | gate_feature_fn!($visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain) | |
dfeec247 | 53 | }; |
f2b60f7d FG |
54 | (future_incompatible; $visitor: expr, $feature: ident, $span: expr, $explain: expr) => { |
55 | gate_feature_fn!(future_incompatible; $visitor, |x: &Features| x.$feature, $span, sym::$feature, $explain) | |
56 | }; | |
dfeec247 XL |
57 | } |
58 | ||
3dfed10e XL |
59 | pub fn check_attribute(attr: &ast::Attribute, sess: &Session, features: &Features) { |
60 | PostExpansionVisitor { sess, features }.visit_attribute(attr) | |
dfeec247 XL |
61 | } |
62 | ||
63 | struct PostExpansionVisitor<'a> { | |
3dfed10e XL |
64 | sess: &'a Session, |
65 | ||
66 | // `sess` contains a `Features`, but this might not be that one. | |
dfeec247 XL |
67 | features: &'a Features, |
68 | } | |
69 | ||
70 | impl<'a> PostExpansionVisitor<'a> { | |
04454e1e | 71 | fn check_abi(&self, abi: ast::StrLit, constness: ast::Const) { |
dfeec247 XL |
72 | let ast::StrLit { symbol_unescaped, span, .. } = abi; |
73 | ||
04454e1e | 74 | if let ast::Const::Yes(_) = constness { |
064997fb | 75 | match symbol_unescaped { |
04454e1e | 76 | // Stable |
064997fb | 77 | sym::Rust | sym::C => {} |
04454e1e FG |
78 | abi => gate_feature_post!( |
79 | &self, | |
80 | const_extern_fn, | |
81 | span, | |
82 | &format!("`{}` as a `const fn` ABI is unstable", abi) | |
83 | ), | |
84 | } | |
85 | } | |
86 | ||
a2a8927a | 87 | match symbol_unescaped.as_str() { |
dfeec247 XL |
88 | // Stable |
89 | "Rust" | "C" | "cdecl" | "stdcall" | "fastcall" | "aapcs" | "win64" | "sysv64" | |
90 | | "system" => {} | |
91 | "rust-intrinsic" => { | |
92 | gate_feature_post!(&self, intrinsics, span, "intrinsics are subject to change"); | |
93 | } | |
94 | "platform-intrinsic" => { | |
95 | gate_feature_post!( | |
96 | &self, | |
97 | platform_intrinsics, | |
98 | span, | |
99 | "platform intrinsics are experimental and possibly buggy" | |
100 | ); | |
101 | } | |
102 | "vectorcall" => { | |
103 | gate_feature_post!( | |
104 | &self, | |
105 | abi_vectorcall, | |
106 | span, | |
107 | "vectorcall is experimental and subject to change" | |
108 | ); | |
109 | } | |
110 | "thiscall" => { | |
111 | gate_feature_post!( | |
112 | &self, | |
113 | abi_thiscall, | |
114 | span, | |
115 | "thiscall is experimental and subject to change" | |
116 | ); | |
117 | } | |
118 | "rust-call" => { | |
119 | gate_feature_post!( | |
120 | &self, | |
121 | unboxed_closures, | |
122 | span, | |
123 | "rust-call ABI is subject to change" | |
124 | ); | |
125 | } | |
923072b8 FG |
126 | "rust-cold" => { |
127 | gate_feature_post!( | |
128 | &self, | |
129 | rust_cold_cc, | |
130 | span, | |
131 | "rust-cold is experimental and subject to change" | |
132 | ); | |
133 | } | |
dfeec247 XL |
134 | "ptx-kernel" => { |
135 | gate_feature_post!( | |
136 | &self, | |
137 | abi_ptx, | |
138 | span, | |
139 | "PTX ABIs are experimental and subject to change" | |
140 | ); | |
141 | } | |
142 | "unadjusted" => { | |
143 | gate_feature_post!( | |
144 | &self, | |
145 | abi_unadjusted, | |
146 | span, | |
147 | "unadjusted ABI is an implementation detail and perma-unstable" | |
148 | ); | |
149 | } | |
150 | "msp430-interrupt" => { | |
151 | gate_feature_post!( | |
152 | &self, | |
153 | abi_msp430_interrupt, | |
154 | span, | |
155 | "msp430-interrupt ABI is experimental and subject to change" | |
156 | ); | |
157 | } | |
158 | "x86-interrupt" => { | |
159 | gate_feature_post!( | |
160 | &self, | |
161 | abi_x86_interrupt, | |
162 | span, | |
163 | "x86-interrupt ABI is experimental and subject to change" | |
164 | ); | |
165 | } | |
166 | "amdgpu-kernel" => { | |
167 | gate_feature_post!( | |
168 | &self, | |
169 | abi_amdgpu_kernel, | |
170 | span, | |
171 | "amdgpu-kernel ABI is experimental and subject to change" | |
172 | ); | |
173 | } | |
f035d41b XL |
174 | "avr-interrupt" | "avr-non-blocking-interrupt" => { |
175 | gate_feature_post!( | |
176 | &self, | |
177 | abi_avr_interrupt, | |
178 | span, | |
179 | "avr-interrupt and avr-non-blocking-interrupt ABIs are experimental and subject to change" | |
180 | ); | |
181 | } | |
dfeec247 XL |
182 | "efiapi" => { |
183 | gate_feature_post!( | |
184 | &self, | |
185 | abi_efiapi, | |
186 | span, | |
187 | "efiapi ABI is experimental and subject to change" | |
188 | ); | |
189 | } | |
5869c6ff XL |
190 | "C-cmse-nonsecure-call" => { |
191 | gate_feature_post!( | |
192 | &self, | |
193 | abi_c_cmse_nonsecure_call, | |
194 | span, | |
195 | "C-cmse-nonsecure-call ABI is experimental and subject to change" | |
196 | ); | |
197 | } | |
6a06907d XL |
198 | "C-unwind" => { |
199 | gate_feature_post!( | |
200 | &self, | |
201 | c_unwind, | |
202 | span, | |
203 | "C-unwind ABI is experimental and subject to change" | |
204 | ); | |
205 | } | |
206 | "stdcall-unwind" => { | |
207 | gate_feature_post!( | |
208 | &self, | |
209 | c_unwind, | |
210 | span, | |
211 | "stdcall-unwind ABI is experimental and subject to change" | |
212 | ); | |
213 | } | |
214 | "system-unwind" => { | |
215 | gate_feature_post!( | |
216 | &self, | |
217 | c_unwind, | |
218 | span, | |
219 | "system-unwind ABI is experimental and subject to change" | |
220 | ); | |
221 | } | |
222 | "thiscall-unwind" => { | |
223 | gate_feature_post!( | |
224 | &self, | |
225 | c_unwind, | |
226 | span, | |
227 | "thiscall-unwind ABI is experimental and subject to change" | |
228 | ); | |
229 | } | |
5099ac24 FG |
230 | "cdecl-unwind" => { |
231 | gate_feature_post!( | |
232 | &self, | |
233 | c_unwind, | |
234 | span, | |
235 | "cdecl-unwind ABI is experimental and subject to change" | |
236 | ); | |
237 | } | |
238 | "fastcall-unwind" => { | |
239 | gate_feature_post!( | |
240 | &self, | |
241 | c_unwind, | |
242 | span, | |
243 | "fastcall-unwind ABI is experimental and subject to change" | |
244 | ); | |
245 | } | |
246 | "vectorcall-unwind" => { | |
247 | gate_feature_post!( | |
248 | &self, | |
249 | c_unwind, | |
250 | span, | |
251 | "vectorcall-unwind ABI is experimental and subject to change" | |
252 | ); | |
253 | } | |
254 | "aapcs-unwind" => { | |
255 | gate_feature_post!( | |
256 | &self, | |
257 | c_unwind, | |
258 | span, | |
259 | "aapcs-unwind ABI is experimental and subject to change" | |
260 | ); | |
261 | } | |
262 | "win64-unwind" => { | |
263 | gate_feature_post!( | |
264 | &self, | |
265 | c_unwind, | |
266 | span, | |
267 | "win64-unwind ABI is experimental and subject to change" | |
268 | ); | |
269 | } | |
270 | "sysv64-unwind" => { | |
271 | gate_feature_post!( | |
272 | &self, | |
273 | c_unwind, | |
274 | span, | |
275 | "sysv64-unwind ABI is experimental and subject to change" | |
276 | ); | |
277 | } | |
cdc7bbd5 XL |
278 | "wasm" => { |
279 | gate_feature_post!( | |
280 | &self, | |
281 | wasm_abi, | |
282 | span, | |
283 | "wasm ABI is experimental and subject to change" | |
284 | ); | |
285 | } | |
5e7ed085 | 286 | abi => { |
064997fb FG |
287 | if self.sess.opts.pretty.map_or(true, |ppm| ppm.needs_hir()) { |
288 | self.sess.parse_sess.span_diagnostic.delay_span_bug( | |
289 | span, | |
290 | &format!("unrecognized ABI not caught in lowering: {}", abi), | |
291 | ); | |
292 | } | |
5e7ed085 | 293 | } |
dfeec247 XL |
294 | } |
295 | } | |
296 | ||
04454e1e | 297 | fn check_extern(&self, ext: ast::Extern, constness: ast::Const) { |
064997fb | 298 | if let ast::Extern::Explicit(abi, _) = ext { |
04454e1e | 299 | self.check_abi(abi, constness); |
dfeec247 XL |
300 | } |
301 | } | |
302 | ||
dc3f5686 XL |
303 | fn maybe_report_invalid_custom_discriminants(&self, variants: &[ast::Variant]) { |
304 | let has_fields = variants.iter().any(|variant| match variant.data { | |
305 | VariantData::Tuple(..) | VariantData::Struct(..) => true, | |
306 | VariantData::Unit(..) => false, | |
307 | }); | |
308 | ||
309 | let discriminant_spans = variants | |
310 | .iter() | |
311 | .filter(|variant| match variant.data { | |
312 | VariantData::Tuple(..) | VariantData::Struct(..) => false, | |
313 | VariantData::Unit(..) => true, | |
314 | }) | |
315 | .filter_map(|variant| variant.disr_expr.as_ref().map(|c| c.value.span)) | |
316 | .collect::<Vec<_>>(); | |
317 | ||
318 | if !discriminant_spans.is_empty() && has_fields { | |
319 | let mut err = feature_err( | |
320 | &self.sess.parse_sess, | |
321 | sym::arbitrary_enum_discriminant, | |
322 | discriminant_spans.clone(), | |
323 | "custom discriminant values are not allowed in enums with tuple or struct variants", | |
324 | ); | |
325 | for sp in discriminant_spans { | |
326 | err.span_label(sp, "disallowed custom discriminant"); | |
327 | } | |
328 | for variant in variants.iter() { | |
329 | match &variant.data { | |
330 | VariantData::Struct(..) => { | |
331 | err.span_label(variant.span, "struct variant defined here"); | |
332 | } | |
333 | VariantData::Tuple(..) => { | |
334 | err.span_label(variant.span, "tuple variant defined here"); | |
335 | } | |
336 | VariantData::Unit(..) => {} | |
337 | } | |
338 | } | |
339 | err.emit(); | |
340 | } | |
341 | } | |
342 | ||
dfeec247 XL |
343 | /// Feature gate `impl Trait` inside `type Alias = $type_expr;`. |
344 | fn check_impl_trait(&self, ty: &ast::Ty) { | |
345 | struct ImplTraitVisitor<'a> { | |
346 | vis: &'a PostExpansionVisitor<'a>, | |
347 | } | |
348 | impl Visitor<'_> for ImplTraitVisitor<'_> { | |
349 | fn visit_ty(&mut self, ty: &ast::Ty) { | |
350 | if let ast::TyKind::ImplTrait(..) = ty.kind { | |
351 | gate_feature_post!( | |
352 | &self.vis, | |
94222f64 | 353 | type_alias_impl_trait, |
dfeec247 XL |
354 | ty.span, |
355 | "`impl Trait` in type aliases is unstable" | |
356 | ); | |
357 | } | |
358 | visit::walk_ty(self, ty); | |
359 | } | |
360 | } | |
361 | ImplTraitVisitor { vis: self }.visit_ty(ty); | |
362 | } | |
363 | } | |
364 | ||
365 | impl<'a> Visitor<'a> for PostExpansionVisitor<'a> { | |
366 | fn visit_attribute(&mut self, attr: &ast::Attribute) { | |
3c0e092e | 367 | let attr_info = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name)); |
dfeec247 | 368 | // Check feature gates for built-in attributes. |
3c0e092e XL |
369 | if let Some(BuiltinAttribute { |
370 | gate: AttributeGate::Gated(_, name, descr, has_feature), | |
371 | .. | |
372 | }) = attr_info | |
373 | { | |
374 | gate_feature_fn!(self, has_feature, attr.span, *name, descr); | |
dfeec247 XL |
375 | } |
376 | // Check unstable flavors of the `#[doc]` attribute. | |
94222f64 | 377 | if attr.has_name(sym::doc) { |
dfeec247 XL |
378 | for nested_meta in attr.meta_item_list().unwrap_or_default() { |
379 | macro_rules! gate_doc { ($($name:ident => $feature:ident)*) => { | |
3dfed10e | 380 | $(if nested_meta.has_name(sym::$name) { |
dfeec247 XL |
381 | let msg = concat!("`#[doc(", stringify!($name), ")]` is experimental"); |
382 | gate_feature_post!(self, $feature, attr.span, msg); | |
383 | })* | |
384 | }} | |
385 | ||
386 | gate_doc!( | |
dfeec247 | 387 | cfg => doc_cfg |
c295e0f8 | 388 | cfg_hide => doc_cfg_hide |
dfeec247 | 389 | masked => doc_masked |
cdc7bbd5 | 390 | notable_trait => doc_notable_trait |
dfeec247 | 391 | ); |
3c0e092e XL |
392 | |
393 | if nested_meta.has_name(sym::keyword) { | |
394 | let msg = "`#[doc(keyword)]` is meant for internal use only"; | |
395 | gate_feature_post!(self, rustdoc_internals, attr.span, msg); | |
396 | } | |
17df50a5 | 397 | |
064997fb FG |
398 | if nested_meta.has_name(sym::fake_variadic) { |
399 | let msg = "`#[doc(fake_variadic)]` is meant for internal use only"; | |
923072b8 | 400 | gate_feature_post!(self, rustdoc_internals, attr.span, msg); |
17df50a5 XL |
401 | } |
402 | } | |
403 | } | |
5e7ed085 FG |
404 | |
405 | // Emit errors for non-staged-api crates. | |
406 | if !self.features.staged_api { | |
923072b8 | 407 | if attr.has_name(sym::unstable) |
5e7ed085 FG |
408 | || attr.has_name(sym::stable) |
409 | || attr.has_name(sym::rustc_const_unstable) | |
410 | || attr.has_name(sym::rustc_const_stable) | |
f2b60f7d | 411 | || attr.has_name(sym::rustc_default_body_unstable) |
5e7ed085 FG |
412 | { |
413 | struct_span_err!( | |
414 | self.sess, | |
415 | attr.span, | |
416 | E0734, | |
417 | "stability attributes may not be used outside of the standard library", | |
418 | ) | |
419 | .emit(); | |
420 | } | |
421 | } | |
dfeec247 XL |
422 | } |
423 | ||
dfeec247 XL |
424 | fn visit_item(&mut self, i: &'a ast::Item) { |
425 | match i.kind { | |
426 | ast::ItemKind::ForeignMod(ref foreign_module) => { | |
427 | if let Some(abi) = foreign_module.abi { | |
04454e1e | 428 | self.check_abi(abi, ast::Const::No); |
dfeec247 XL |
429 | } |
430 | } | |
431 | ||
432 | ast::ItemKind::Fn(..) => { | |
a2a8927a | 433 | if self.sess.contains_name(&i.attrs, sym::start) { |
dfeec247 XL |
434 | gate_feature_post!( |
435 | &self, | |
436 | start, | |
437 | i.span, | |
438 | "`#[start]` functions are experimental \ | |
ba9703b0 XL |
439 | and their signature may change \ |
440 | over time" | |
dfeec247 XL |
441 | ); |
442 | } | |
dfeec247 XL |
443 | } |
444 | ||
445 | ast::ItemKind::Struct(..) => { | |
a2a8927a | 446 | for attr in self.sess.filter_by_name(&i.attrs, sym::repr) { |
dfeec247 | 447 | for item in attr.meta_item_list().unwrap_or_else(Vec::new) { |
3dfed10e | 448 | if item.has_name(sym::simd) { |
dfeec247 XL |
449 | gate_feature_post!( |
450 | &self, | |
451 | repr_simd, | |
452 | attr.span, | |
453 | "SIMD types are experimental and possibly buggy" | |
454 | ); | |
455 | } | |
456 | } | |
457 | } | |
458 | } | |
459 | ||
dc3f5686 XL |
460 | ast::ItemKind::Enum(ast::EnumDef { ref variants, .. }, ..) => { |
461 | for variant in variants { | |
462 | match (&variant.data, &variant.disr_expr) { | |
463 | (ast::VariantData::Unit(..), _) => {} | |
464 | (_, Some(disr_expr)) => gate_feature_post!( | |
465 | &self, | |
466 | arbitrary_enum_discriminant, | |
467 | disr_expr.value.span, | |
468 | "discriminants on non-unit variants are experimental" | |
469 | ), | |
470 | _ => {} | |
471 | } | |
472 | } | |
473 | ||
474 | let has_feature = self.features.arbitrary_enum_discriminant; | |
475 | if !has_feature && !i.span.allows_unstable(sym::arbitrary_enum_discriminant) { | |
476 | self.maybe_report_invalid_custom_discriminants(&variants); | |
477 | } | |
478 | } | |
479 | ||
3c0e092e | 480 | ast::ItemKind::Impl(box ast::Impl { polarity, defaultness, ref of_trait, .. }) => { |
ba9703b0 | 481 | if let ast::ImplPolarity::Negative(span) = polarity { |
dfeec247 XL |
482 | gate_feature_post!( |
483 | &self, | |
ba9703b0 | 484 | negative_impls, |
5869c6ff | 485 | span.to(of_trait.as_ref().map_or(span, |t| t.path.span)), |
dfeec247 | 486 | "negative trait bounds are not yet fully implemented; \ |
ba9703b0 | 487 | use marker types for now" |
dfeec247 XL |
488 | ); |
489 | } | |
490 | ||
74b04a01 | 491 | if let ast::Defaultness::Default(_) = defaultness { |
dfeec247 XL |
492 | gate_feature_post!(&self, specialization, i.span, "specialization is unstable"); |
493 | } | |
494 | } | |
495 | ||
3c0e092e | 496 | ast::ItemKind::Trait(box ast::Trait { is_auto: ast::IsAuto::Yes, .. }) => { |
dfeec247 XL |
497 | gate_feature_post!( |
498 | &self, | |
fc512014 | 499 | auto_traits, |
dfeec247 XL |
500 | i.span, |
501 | "auto traits are experimental and possibly buggy" | |
502 | ); | |
503 | } | |
504 | ||
505 | ast::ItemKind::TraitAlias(..) => { | |
506 | gate_feature_post!(&self, trait_alias, i.span, "trait aliases are experimental"); | |
507 | } | |
508 | ||
ba9703b0 | 509 | ast::ItemKind::MacroDef(ast::MacroDef { macro_rules: false, .. }) => { |
dfeec247 XL |
510 | let msg = "`macro` is experimental"; |
511 | gate_feature_post!(&self, decl_macro, i.span, msg); | |
512 | } | |
513 | ||
3c0e092e | 514 | ast::ItemKind::TyAlias(box ast::TyAlias { ty: Some(ref ty), .. }) => { |
5869c6ff XL |
515 | self.check_impl_trait(&ty) |
516 | } | |
dfeec247 XL |
517 | |
518 | _ => {} | |
519 | } | |
520 | ||
521 | visit::walk_item(self, i); | |
522 | } | |
523 | ||
524 | fn visit_foreign_item(&mut self, i: &'a ast::ForeignItem) { | |
525 | match i.kind { | |
526 | ast::ForeignItemKind::Fn(..) | ast::ForeignItemKind::Static(..) => { | |
3dfed10e | 527 | let link_name = self.sess.first_attr_value_str_by_name(&i.attrs, sym::link_name); |
5869c6ff XL |
528 | let links_to_llvm = |
529 | link_name.map_or(false, |val| val.as_str().starts_with("llvm.")); | |
dfeec247 XL |
530 | if links_to_llvm { |
531 | gate_feature_post!( | |
532 | &self, | |
533 | link_llvm_intrinsics, | |
534 | i.span, | |
535 | "linking to LLVM intrinsics is experimental" | |
536 | ); | |
537 | } | |
538 | } | |
74b04a01 | 539 | ast::ForeignItemKind::TyAlias(..) => { |
dfeec247 XL |
540 | gate_feature_post!(&self, extern_types, i.span, "extern types are experimental"); |
541 | } | |
ba9703b0 | 542 | ast::ForeignItemKind::MacCall(..) => {} |
dfeec247 XL |
543 | } |
544 | ||
545 | visit::walk_foreign_item(self, i) | |
546 | } | |
547 | ||
548 | fn visit_ty(&mut self, ty: &'a ast::Ty) { | |
549 | match ty.kind { | |
550 | ast::TyKind::BareFn(ref bare_fn_ty) => { | |
04454e1e FG |
551 | // Function pointers cannot be `const` |
552 | self.check_extern(bare_fn_ty.ext, ast::Const::No); | |
dfeec247 XL |
553 | } |
554 | ast::TyKind::Never => { | |
555 | gate_feature_post!(&self, never_type, ty.span, "the `!` type is experimental"); | |
556 | } | |
f2b60f7d FG |
557 | ast::TyKind::TraitObject(_, ast::TraitObjectSyntax::DynStar, ..) => { |
558 | gate_feature_post!(&self, dyn_star, ty.span, "dyn* trait objects are unstable"); | |
559 | } | |
dfeec247 XL |
560 | _ => {} |
561 | } | |
562 | visit::walk_ty(self, ty) | |
563 | } | |
564 | ||
74b04a01 XL |
565 | fn visit_fn_ret_ty(&mut self, ret_ty: &'a ast::FnRetTy) { |
566 | if let ast::FnRetTy::Ty(ref output_ty) = *ret_ty { | |
dfeec247 XL |
567 | if let ast::TyKind::Never = output_ty.kind { |
568 | // Do nothing. | |
569 | } else { | |
570 | self.visit_ty(output_ty) | |
571 | } | |
572 | } | |
573 | } | |
574 | ||
064997fb FG |
575 | fn visit_stmt(&mut self, stmt: &'a ast::Stmt) { |
576 | if let ast::StmtKind::Semi(expr) = &stmt.kind | |
577 | && let ast::ExprKind::Assign(lhs, _, _) = &expr.kind | |
578 | && let ast::ExprKind::Type(..) = lhs.kind | |
579 | && self.sess.parse_sess.span_diagnostic.err_count() == 0 | |
580 | && !self.features.type_ascription | |
581 | && !lhs.span.allows_unstable(sym::type_ascription) | |
582 | { | |
583 | // When we encounter a statement of the form `foo: Ty = val;`, this will emit a type | |
584 | // ascription error, but the likely intention was to write a `let` statement. (#78907). | |
f2b60f7d | 585 | feature_err( |
064997fb FG |
586 | &self.sess.parse_sess, |
587 | sym::type_ascription, | |
588 | lhs.span, | |
064997fb FG |
589 | "type ascription is experimental", |
590 | ).span_suggestion_verbose( | |
591 | lhs.span.shrink_to_lo(), | |
592 | "you might have meant to introduce a new binding", | |
593 | "let ".to_string(), | |
594 | Applicability::MachineApplicable, | |
595 | ).emit(); | |
596 | } | |
597 | visit::walk_stmt(self, stmt); | |
598 | } | |
599 | ||
dfeec247 XL |
600 | fn visit_expr(&mut self, e: &'a ast::Expr) { |
601 | match e.kind { | |
602 | ast::ExprKind::Box(_) => { | |
603 | gate_feature_post!( | |
604 | &self, | |
605 | box_syntax, | |
606 | e.span, | |
607 | "box expression syntax is experimental; you can call `Box::new` instead" | |
608 | ); | |
609 | } | |
610 | ast::ExprKind::Type(..) => { | |
3dfed10e | 611 | if self.sess.parse_sess.span_diagnostic.err_count() == 0 { |
f2b60f7d FG |
612 | // To avoid noise about type ascription in common syntax errors, |
613 | // only emit if it is the *only* error. | |
dfeec247 XL |
614 | gate_feature_post!( |
615 | &self, | |
616 | type_ascription, | |
617 | e.span, | |
618 | "type ascription is experimental" | |
619 | ); | |
f2b60f7d FG |
620 | } else { |
621 | // And if it isn't, cancel the early-pass warning. | |
622 | self.sess | |
623 | .parse_sess | |
624 | .span_diagnostic | |
625 | .steal_diagnostic(e.span, StashKey::EarlySyntaxWarning) | |
626 | .map(|err| err.cancel()); | |
dfeec247 XL |
627 | } |
628 | } | |
629 | ast::ExprKind::TryBlock(_) => { | |
630 | gate_feature_post!(&self, try_blocks, e.span, "`try` expression is experimental"); | |
631 | } | |
dfeec247 XL |
632 | _ => {} |
633 | } | |
634 | visit::walk_expr(self, e) | |
635 | } | |
636 | ||
637 | fn visit_pat(&mut self, pattern: &'a ast::Pat) { | |
638 | match &pattern.kind { | |
136023e0 XL |
639 | PatKind::Slice(pats) => { |
640 | for pat in pats { | |
641 | let inner_pat = match &pat.kind { | |
642 | PatKind::Ident(.., Some(pat)) => pat, | |
643 | _ => pat, | |
644 | }; | |
645 | if let PatKind::Range(Some(_), None, Spanned { .. }) = inner_pat.kind { | |
646 | gate_feature_post!( | |
647 | &self, | |
648 | half_open_range_patterns, | |
649 | pat.span, | |
650 | "`X..` patterns in slices are experimental" | |
651 | ); | |
652 | } | |
653 | } | |
654 | } | |
dfeec247 XL |
655 | PatKind::Box(..) => { |
656 | gate_feature_post!( | |
657 | &self, | |
658 | box_patterns, | |
659 | pattern.span, | |
660 | "box pattern syntax is experimental" | |
661 | ); | |
662 | } | |
136023e0 | 663 | PatKind::Range(_, Some(_), Spanned { node: RangeEnd::Excluded, .. }) => { |
dfeec247 XL |
664 | gate_feature_post!( |
665 | &self, | |
666 | exclusive_range_pattern, | |
667 | pattern.span, | |
668 | "exclusive range pattern syntax is experimental" | |
669 | ); | |
670 | } | |
671 | _ => {} | |
672 | } | |
673 | visit::walk_pat(self, pattern) | |
674 | } | |
675 | ||
74b04a01 | 676 | fn visit_fn(&mut self, fn_kind: FnKind<'a>, span: Span, _: NodeId) { |
dfeec247 | 677 | if let Some(header) = fn_kind.header() { |
74b04a01 | 678 | // Stability of const fn methods are covered in `visit_assoc_item` below. |
04454e1e | 679 | self.check_extern(header.ext, header.constness); |
dfeec247 XL |
680 | } |
681 | ||
74b04a01 | 682 | if fn_kind.ctxt() != Some(FnCtxt::Foreign) && fn_kind.decl().c_variadic() { |
dfeec247 XL |
683 | gate_feature_post!(&self, c_variadic, span, "C-variadic functions are unstable"); |
684 | } | |
685 | ||
f2b60f7d | 686 | visit::walk_fn(self, fn_kind) |
dfeec247 XL |
687 | } |
688 | ||
5099ac24 FG |
689 | fn visit_assoc_constraint(&mut self, constraint: &'a AssocConstraint) { |
690 | if let AssocConstraintKind::Bound { .. } = constraint.kind { | |
ba9703b0 | 691 | gate_feature_post!( |
dfeec247 XL |
692 | &self, |
693 | associated_type_bounds, | |
694 | constraint.span, | |
695 | "associated type bounds are unstable" | |
ba9703b0 | 696 | ) |
dfeec247 | 697 | } |
5099ac24 | 698 | visit::walk_assoc_constraint(self, constraint) |
dfeec247 XL |
699 | } |
700 | ||
74b04a01 | 701 | fn visit_assoc_item(&mut self, i: &'a ast::AssocItem, ctxt: AssocCtxt) { |
ba9703b0 | 702 | let is_fn = match i.kind { |
cdc7bbd5 | 703 | ast::AssocItemKind::Fn(_) => true, |
f2b60f7d | 704 | ast::AssocItemKind::TyAlias(box ast::TyAlias { ref ty, .. }) => { |
74b04a01 | 705 | if let (Some(_), AssocCtxt::Trait) = (ty, ctxt) { |
dfeec247 XL |
706 | gate_feature_post!( |
707 | &self, | |
708 | associated_type_defaults, | |
74b04a01 | 709 | i.span, |
dfeec247 XL |
710 | "associated type defaults are unstable" |
711 | ); | |
712 | } | |
dfeec247 XL |
713 | if let Some(ty) = ty { |
714 | self.check_impl_trait(ty); | |
715 | } | |
ba9703b0 | 716 | false |
dfeec247 | 717 | } |
ba9703b0 XL |
718 | _ => false, |
719 | }; | |
720 | if let ast::Defaultness::Default(_) = i.kind.defaultness() { | |
721 | // Limit `min_specialization` to only specializing functions. | |
722 | gate_feature_fn!( | |
723 | &self, | |
724 | |x: &Features| x.specialization || (is_fn && x.min_specialization), | |
725 | i.span, | |
726 | sym::specialization, | |
727 | "specialization is unstable" | |
728 | ); | |
dfeec247 | 729 | } |
74b04a01 | 730 | visit::walk_assoc_item(self, i, ctxt) |
dfeec247 | 731 | } |
dfeec247 XL |
732 | } |
733 | ||
3dfed10e XL |
734 | pub fn check_crate(krate: &ast::Crate, sess: &Session) { |
735 | maybe_stage_features(sess, krate); | |
1b1a35ee | 736 | check_incompatible_features(sess); |
3dfed10e | 737 | let mut visitor = PostExpansionVisitor { sess, features: &sess.features_untracked() }; |
dfeec247 | 738 | |
3dfed10e | 739 | let spans = sess.parse_sess.gated_spans.spans.borrow(); |
dfeec247 | 740 | macro_rules! gate_all { |
5869c6ff XL |
741 | ($gate:ident, $msg:literal, $help:literal) => { |
742 | if let Some(spans) = spans.get(&sym::$gate) { | |
743 | for span in spans { | |
744 | gate_feature_post!(&visitor, $gate, *span, $msg, $help); | |
745 | } | |
746 | } | |
747 | }; | |
dfeec247 | 748 | ($gate:ident, $msg:literal) => { |
3dfed10e XL |
749 | if let Some(spans) = spans.get(&sym::$gate) { |
750 | for span in spans { | |
751 | gate_feature_post!(&visitor, $gate, *span, $msg); | |
752 | } | |
dfeec247 XL |
753 | } |
754 | }; | |
755 | } | |
6a06907d XL |
756 | gate_all!( |
757 | if_let_guard, | |
758 | "`if let` guards are experimental", | |
759 | "you can write `if matches!(<expr>, <pattern>)` instead of `if let <pattern> = <expr>`" | |
760 | ); | |
5099ac24 | 761 | gate_all!(let_chains, "`let` expressions in this position are unstable"); |
5869c6ff XL |
762 | gate_all!( |
763 | async_closure, | |
764 | "async closures are unstable", | |
765 | "to use an async block, remove the `||`: `async {`" | |
766 | ); | |
064997fb FG |
767 | gate_all!( |
768 | closure_lifetime_binder, | |
769 | "`for<...>` binders for closures are experimental", | |
770 | "consider removing `for<...>`" | |
771 | ); | |
17df50a5 | 772 | gate_all!(more_qualified_paths, "usage of qualified paths in this context is experimental"); |
dfeec247 | 773 | gate_all!(generators, "yield syntax is experimental"); |
dfeec247 | 774 | gate_all!(raw_ref_op, "raw address of syntax is experimental"); |
dfeec247 XL |
775 | gate_all!(const_trait_impl, "const trait impls are experimental"); |
776 | gate_all!(half_open_range_patterns, "half-open range patterns are unstable"); | |
29967ef6 | 777 | gate_all!(inline_const, "inline-const is experimental"); |
3c0e092e | 778 | gate_all!(inline_const_pat, "inline-const in pattern position is experimental"); |
5099ac24 | 779 | gate_all!(associated_const_equality, "associated const equality is incomplete"); |
04454e1e | 780 | gate_all!(yeet_expr, "`do yeet` expression is experimental"); |
dfeec247 XL |
781 | |
782 | // All uses of `gate_all!` below this point were added in #65742, | |
783 | // and subsequently disabled (with the non-early gating readded). | |
f2b60f7d FG |
784 | // We emit an early future-incompatible warning for these. |
785 | // New syntax gates should go above here to get a hard error gate. | |
dfeec247 XL |
786 | macro_rules! gate_all { |
787 | ($gate:ident, $msg:literal) => { | |
f2b60f7d FG |
788 | for span in spans.get(&sym::$gate).unwrap_or(&vec![]) { |
789 | gate_feature_post!(future_incompatible; &visitor, $gate, *span, $msg); | |
dfeec247 XL |
790 | } |
791 | }; | |
792 | } | |
793 | ||
794 | gate_all!(trait_alias, "trait aliases are experimental"); | |
795 | gate_all!(associated_type_bounds, "associated type bounds are unstable"); | |
dfeec247 XL |
796 | gate_all!(decl_macro, "`macro` is experimental"); |
797 | gate_all!(box_patterns, "box pattern syntax is experimental"); | |
798 | gate_all!(exclusive_range_pattern, "exclusive range pattern syntax is experimental"); | |
799 | gate_all!(try_blocks, "`try` blocks are unstable"); | |
dfeec247 | 800 | gate_all!(box_syntax, "box expression syntax is experimental; you can call `Box::new` instead"); |
f2b60f7d | 801 | gate_all!(type_ascription, "type ascription is experimental"); |
dfeec247 XL |
802 | |
803 | visit::walk_crate(&mut visitor, krate); | |
804 | } | |
805 | ||
3dfed10e | 806 | fn maybe_stage_features(sess: &Session, krate: &ast::Crate) { |
c295e0f8 XL |
807 | // checks if `#![feature]` has been used to enable any lang feature |
808 | // does not check the same for lib features unless there's at least one | |
809 | // declared lang feature | |
3dfed10e | 810 | if !sess.opts.unstable_features.is_nightly_build() { |
cdc7bbd5 | 811 | let lang_features = &sess.features_untracked().declared_lang_features; |
c295e0f8 XL |
812 | if lang_features.len() == 0 { |
813 | return; | |
814 | } | |
94222f64 | 815 | for attr in krate.attrs.iter().filter(|attr| attr.has_name(sym::feature)) { |
cdc7bbd5 | 816 | let mut err = struct_span_err!( |
3dfed10e | 817 | sess.parse_sess.span_diagnostic, |
dfeec247 XL |
818 | attr.span, |
819 | E0554, | |
820 | "`#![feature]` may not be used on the {} release channel", | |
821 | option_env!("CFG_RELEASE_CHANNEL").unwrap_or("(unknown)") | |
cdc7bbd5 XL |
822 | ); |
823 | let mut all_stable = true; | |
824 | for ident in | |
5099ac24 | 825 | attr.meta_item_list().into_iter().flatten().flat_map(|nested| nested.ident()) |
cdc7bbd5 XL |
826 | { |
827 | let name = ident.name; | |
828 | let stable_since = lang_features | |
829 | .iter() | |
830 | .flat_map(|&(feature, _, since)| if feature == name { since } else { None }) | |
831 | .next(); | |
832 | if let Some(since) = stable_since { | |
833 | err.help(&format!( | |
834 | "the feature `{}` has been stable since {} and no longer requires \ | |
835 | an attribute to enable", | |
836 | name, since | |
837 | )); | |
838 | } else { | |
839 | all_stable = false; | |
840 | } | |
841 | } | |
842 | if all_stable { | |
843 | err.span_suggestion( | |
844 | attr.span, | |
845 | "remove the attribute", | |
923072b8 | 846 | "", |
cdc7bbd5 XL |
847 | Applicability::MachineApplicable, |
848 | ); | |
849 | } | |
850 | err.emit(); | |
dfeec247 XL |
851 | } |
852 | } | |
853 | } | |
1b1a35ee XL |
854 | |
855 | fn check_incompatible_features(sess: &Session) { | |
856 | let features = sess.features_untracked(); | |
857 | ||
858 | let declared_features = features | |
859 | .declared_lang_features | |
860 | .iter() | |
861 | .copied() | |
862 | .map(|(name, span, _)| (name, span)) | |
863 | .chain(features.declared_lib_features.iter().copied()); | |
864 | ||
865 | for (f1, f2) in rustc_feature::INCOMPATIBLE_FEATURES | |
866 | .iter() | |
867 | .filter(|&&(f1, f2)| features.enabled(f1) && features.enabled(f2)) | |
868 | { | |
869 | if let Some((f1_name, f1_span)) = declared_features.clone().find(|(name, _)| name == f1) { | |
870 | if let Some((f2_name, f2_span)) = declared_features.clone().find(|(name, _)| name == f2) | |
871 | { | |
872 | let spans = vec![f1_span, f2_span]; | |
873 | sess.struct_span_err( | |
874 | spans.clone(), | |
875 | &format!( | |
876 | "features `{}` and `{}` are incompatible, using them at the same time \ | |
877 | is not allowed", | |
878 | f1_name, f2_name | |
879 | ), | |
880 | ) | |
881 | .help("remove one of these features") | |
882 | .emit(); | |
883 | } | |
884 | } | |
885 | } | |
886 | } |