]> git.lizzy.rs Git - rust.git/blob - src/librustc_ast_passes/ast_validation.rs
Rollup merge of #68036 - euclio:libterm-ncurses6-fix, r=KodrAus
[rust.git] / src / librustc_ast_passes / ast_validation.rs
1 // Validate AST before lowering it to HIR.
2 //
3 // This pass is supposed to catch things that fit into AST data structures,
4 // but not permitted by the language. It runs after expansion when AST is frozen,
5 // so it can check for erroneous constructions produced by syntax extensions.
6 // This pass is supposed to perform only simple checks not requiring name resolution
7 // or type checking or some other kind of complex analysis.
8
9 use rustc_data_structures::fx::FxHashMap;
10 use rustc_errors::{struct_span_err, Applicability, FatalError};
11 use rustc_parse::validate_attr;
12 use rustc_session::lint::builtin::PATTERNS_IN_FNS_WITHOUT_BODY;
13 use rustc_session::lint::LintBuffer;
14 use rustc_session::Session;
15 use rustc_span::source_map::Spanned;
16 use rustc_span::symbol::{kw, sym};
17 use rustc_span::Span;
18 use std::mem;
19 use syntax::ast::*;
20 use syntax::attr;
21 use syntax::expand::is_proc_macro_attr;
22 use syntax::print::pprust;
23 use syntax::visit::{self, Visitor};
24 use syntax::walk_list;
25
26 use rustc_error_codes::*;
27
28 /// A syntactic context that disallows certain kinds of bounds (e.g., `?Trait` or `?const Trait`).
29 #[derive(Clone, Copy)]
30 enum BoundContext {
31     ImplTrait,
32     TraitBounds,
33     TraitObject,
34 }
35
36 impl BoundContext {
37     fn description(&self) -> &'static str {
38         match self {
39             Self::ImplTrait => "`impl Trait`",
40             Self::TraitBounds => "supertraits",
41             Self::TraitObject => "trait objects",
42         }
43     }
44 }
45
46 struct AstValidator<'a> {
47     session: &'a Session,
48     has_proc_macro_decls: bool,
49
50     /// Used to ban nested `impl Trait`, e.g., `impl Into<impl Debug>`.
51     /// Nested `impl Trait` _is_ allowed in associated type position,
52     /// e.g., `impl Iterator<Item = impl Debug>`.
53     outer_impl_trait: Option<Span>,
54
55     /// Keeps track of the `BoundContext` as we recurse.
56     ///
57     /// This is used to forbid `?const Trait` bounds in, e.g.,
58     /// `impl Iterator<Item = Box<dyn ?const Trait>`.
59     bound_context: Option<BoundContext>,
60
61     /// Used to ban `impl Trait` in path projections like `<impl Iterator>::Item`
62     /// or `Foo::Bar<impl Trait>`
63     is_impl_trait_banned: bool,
64
65     /// Used to ban associated type bounds (i.e., `Type<AssocType: Bounds>`) in
66     /// certain positions.
67     is_assoc_ty_bound_banned: bool,
68
69     lint_buffer: &'a mut LintBuffer,
70 }
71
72 impl<'a> AstValidator<'a> {
73     fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) {
74         let old = mem::replace(&mut self.is_impl_trait_banned, true);
75         f(self);
76         self.is_impl_trait_banned = old;
77     }
78
79     fn with_banned_assoc_ty_bound(&mut self, f: impl FnOnce(&mut Self)) {
80         let old = mem::replace(&mut self.is_assoc_ty_bound_banned, true);
81         f(self);
82         self.is_assoc_ty_bound_banned = old;
83     }
84
85     fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
86         let old = mem::replace(&mut self.outer_impl_trait, outer);
87         if outer.is_some() {
88             self.with_bound_context(BoundContext::ImplTrait, |this| f(this));
89         } else {
90             f(self)
91         }
92         self.outer_impl_trait = old;
93     }
94
95     fn with_bound_context(&mut self, ctx: BoundContext, f: impl FnOnce(&mut Self)) {
96         let old = self.bound_context.replace(ctx);
97         f(self);
98         self.bound_context = old;
99     }
100
101     fn visit_assoc_ty_constraint_from_generic_args(&mut self, constraint: &'a AssocTyConstraint) {
102         match constraint.kind {
103             AssocTyConstraintKind::Equality { .. } => {}
104             AssocTyConstraintKind::Bound { .. } => {
105                 if self.is_assoc_ty_bound_banned {
106                     self.err_handler().span_err(
107                         constraint.span,
108                         "associated type bounds are not allowed within structs, enums, or unions",
109                     );
110                 }
111             }
112         }
113         self.visit_assoc_ty_constraint(constraint);
114     }
115
116     // Mirrors `visit::walk_ty`, but tracks relevant state.
117     fn walk_ty(&mut self, t: &'a Ty) {
118         match t.kind {
119             TyKind::ImplTrait(..) => {
120                 self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t))
121             }
122             TyKind::TraitObject(..) => {
123                 self.with_bound_context(BoundContext::TraitObject, |this| visit::walk_ty(this, t));
124             }
125             TyKind::Path(ref qself, ref path) => {
126                 // We allow these:
127                 //  - `Option<impl Trait>`
128                 //  - `option::Option<impl Trait>`
129                 //  - `option::Option<T>::Foo<impl Trait>
130                 //
131                 // But not these:
132                 //  - `<impl Trait>::Foo`
133                 //  - `option::Option<impl Trait>::Foo`.
134                 //
135                 // To implement this, we disallow `impl Trait` from `qself`
136                 // (for cases like `<impl Trait>::Foo>`)
137                 // but we allow `impl Trait` in `GenericArgs`
138                 // iff there are no more PathSegments.
139                 if let Some(ref qself) = *qself {
140                     // `impl Trait` in `qself` is always illegal
141                     self.with_banned_impl_trait(|this| this.visit_ty(&qself.ty));
142                 }
143
144                 // Note that there should be a call to visit_path here,
145                 // so if any logic is added to process `Path`s a call to it should be
146                 // added both in visit_path and here. This code mirrors visit::walk_path.
147                 for (i, segment) in path.segments.iter().enumerate() {
148                     // Allow `impl Trait` iff we're on the final path segment
149                     if i == path.segments.len() - 1 {
150                         self.visit_path_segment(path.span, segment);
151                     } else {
152                         self.with_banned_impl_trait(|this| {
153                             this.visit_path_segment(path.span, segment)
154                         });
155                     }
156                 }
157             }
158             _ => visit::walk_ty(self, t),
159         }
160     }
161
162     fn err_handler(&self) -> &rustc_errors::Handler {
163         &self.session.diagnostic()
164     }
165
166     fn check_lifetime(&self, ident: Ident) {
167         let valid_names = [kw::UnderscoreLifetime, kw::StaticLifetime, kw::Invalid];
168         if !valid_names.contains(&ident.name) && ident.without_first_quote().is_reserved() {
169             self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names");
170         }
171     }
172
173     fn check_label(&self, ident: Ident) {
174         if ident.without_first_quote().is_reserved() {
175             self.err_handler()
176                 .span_err(ident.span, &format!("invalid label name `{}`", ident.name));
177         }
178     }
179
180     fn invalid_visibility(&self, vis: &Visibility, note: Option<&str>) {
181         if let VisibilityKind::Inherited = vis.node {
182             return;
183         }
184
185         let mut err =
186             struct_span_err!(self.session, vis.span, E0449, "unnecessary visibility qualifier");
187         if vis.node.is_pub() {
188             err.span_label(vis.span, "`pub` not permitted here because it's implied");
189         }
190         if let Some(note) = note {
191             err.note(note);
192         }
193         err.emit();
194     }
195
196     fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, bool)) {
197         for Param { pat, .. } in &decl.inputs {
198             match pat.kind {
199                 PatKind::Ident(BindingMode::ByValue(Mutability::Not), _, None) | PatKind::Wild => {}
200                 PatKind::Ident(BindingMode::ByValue(Mutability::Mut), _, None) => {
201                     report_err(pat.span, true)
202                 }
203                 _ => report_err(pat.span, false),
204             }
205         }
206     }
207
208     fn check_trait_fn_not_async(&self, span: Span, asyncness: IsAsync) {
209         if asyncness.is_async() {
210             struct_span_err!(self.session, span, E0706, "trait fns cannot be declared `async`")
211                 .note("`async` trait functions are not currently supported")
212                 .note(
213                     "consider using the `async-trait` crate: \
214                        https://crates.io/crates/async-trait",
215                 )
216                 .emit();
217         }
218     }
219
220     fn check_trait_fn_not_const(&self, constness: Spanned<Constness>) {
221         if constness.node == Constness::Const {
222             struct_span_err!(
223                 self.session,
224                 constness.span,
225                 E0379,
226                 "trait fns cannot be declared const"
227             )
228             .span_label(constness.span, "trait fns cannot be const")
229             .emit();
230         }
231     }
232
233     // FIXME(ecstaticmorse): Instead, use `bound_context` to check this in `visit_param_bound`.
234     fn no_questions_in_bounds(&self, bounds: &GenericBounds, where_: &str, is_trait: bool) {
235         for bound in bounds {
236             if let GenericBound::Trait(ref poly, TraitBoundModifier::Maybe) = *bound {
237                 let mut err = self.err_handler().struct_span_err(
238                     poly.span,
239                     &format!("`?Trait` is not permitted in {}", where_),
240                 );
241                 if is_trait {
242                     let path_str = pprust::path_to_string(&poly.trait_ref.path);
243                     err.note(&format!("traits are `?{}` by default", path_str));
244                 }
245                 err.emit();
246             }
247         }
248     }
249
250     /// Matches `'-' lit | lit (cf. parser::Parser::parse_literal_maybe_minus)`,
251     /// or paths for ranges.
252     //
253     // FIXME: do we want to allow `expr -> pattern` conversion to create path expressions?
254     // That means making this work:
255     //
256     // ```rust,ignore (FIXME)
257     // struct S;
258     // macro_rules! m {
259     //     ($a:expr) => {
260     //         let $a = S;
261     //     }
262     // }
263     // m!(S);
264     // ```
265     fn check_expr_within_pat(&self, expr: &Expr, allow_paths: bool) {
266         match expr.kind {
267             ExprKind::Lit(..) | ExprKind::Err => {}
268             ExprKind::Path(..) if allow_paths => {}
269             ExprKind::Unary(UnOp::Neg, ref inner)
270                 if match inner.kind {
271                     ExprKind::Lit(_) => true,
272                     _ => false,
273                 } => {}
274             _ => self.err_handler().span_err(
275                 expr.span,
276                 "arbitrary expressions aren't allowed \
277                                                          in patterns",
278             ),
279         }
280     }
281
282     fn check_late_bound_lifetime_defs(&self, params: &[GenericParam]) {
283         // Check only lifetime parameters are present and that the lifetime
284         // parameters that are present have no bounds.
285         let non_lt_param_spans: Vec<_> = params
286             .iter()
287             .filter_map(|param| match param.kind {
288                 GenericParamKind::Lifetime { .. } => {
289                     if !param.bounds.is_empty() {
290                         let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
291                         self.err_handler()
292                             .span_err(spans, "lifetime bounds cannot be used in this context");
293                     }
294                     None
295                 }
296                 _ => Some(param.ident.span),
297             })
298             .collect();
299         if !non_lt_param_spans.is_empty() {
300             self.err_handler().span_err(
301                 non_lt_param_spans,
302                 "only lifetime parameters can be used in this context",
303             );
304         }
305     }
306
307     fn check_fn_decl(&self, fn_decl: &FnDecl) {
308         match &*fn_decl.inputs {
309             [Param { ty, span, .. }] => {
310                 if let TyKind::CVarArgs = ty.kind {
311                     self.err_handler().span_err(
312                         *span,
313                         "C-variadic function must be declared with at least one named argument",
314                     );
315                 }
316             }
317             [ps @ .., _] => {
318                 for Param { ty, span, .. } in ps {
319                     if let TyKind::CVarArgs = ty.kind {
320                         self.err_handler().span_err(
321                             *span,
322                             "`...` must be the last argument of a C-variadic function",
323                         );
324                     }
325                 }
326             }
327             _ => {}
328         }
329
330         fn_decl
331             .inputs
332             .iter()
333             .flat_map(|i| i.attrs.as_ref())
334             .filter(|attr| {
335                 let arr = [sym::allow, sym::cfg, sym::cfg_attr, sym::deny, sym::forbid, sym::warn];
336                 !arr.contains(&attr.name_or_empty()) && attr::is_builtin_attr(attr)
337             })
338             .for_each(|attr| {
339                 if attr.is_doc_comment() {
340                     self.err_handler()
341                         .struct_span_err(
342                             attr.span,
343                             "documentation comments cannot be applied to function parameters",
344                         )
345                         .span_label(attr.span, "doc comments are not allowed here")
346                         .emit();
347                 } else {
348                     self.err_handler().span_err(
349                         attr.span,
350                         "allow, cfg, cfg_attr, deny, \
351                 forbid, and warn are the only allowed built-in attributes in function parameters",
352                     )
353                 }
354             });
355     }
356
357     fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
358         if let Defaultness::Default = defaultness {
359             self.err_handler()
360                 .struct_span_err(span, "`default` is only allowed on items in `impl` definitions")
361                 .emit();
362         }
363     }
364
365     fn check_impl_item_provided<T>(&self, sp: Span, body: &Option<T>, ctx: &str, sugg: &str) {
366         if body.is_some() {
367             return;
368         }
369
370         self.err_handler()
371             .struct_span_err(sp, &format!("associated {} in `impl` without body", ctx))
372             .span_suggestion(
373                 self.session.source_map().end_point(sp),
374                 &format!("provide a definition for the {}", ctx),
375                 sugg.to_string(),
376                 Applicability::HasPlaceholders,
377             )
378             .emit();
379     }
380
381     fn check_impl_assoc_type_no_bounds(&self, bounds: &[GenericBound]) {
382         let span = match bounds {
383             [] => return,
384             [b0] => b0.span(),
385             [b0, .., bl] => b0.span().to(bl.span()),
386         };
387         self.err_handler()
388             .struct_span_err(span, "bounds on associated `type`s in `impl`s have no effect")
389             .emit();
390     }
391
392     fn check_c_varadic_type(&self, decl: &FnDecl) {
393         for Param { ty, span, .. } in &decl.inputs {
394             if let TyKind::CVarArgs = ty.kind {
395                 self.err_handler()
396                     .struct_span_err(
397                         *span,
398                         "only foreign or `unsafe extern \"C\" functions may be C-variadic",
399                     )
400                     .emit();
401             }
402         }
403     }
404 }
405
406 enum GenericPosition {
407     Param,
408     Arg,
409 }
410
411 fn validate_generics_order<'a>(
412     sess: &Session,
413     handler: &rustc_errors::Handler,
414     generics: impl Iterator<Item = (ParamKindOrd, Option<&'a [GenericBound]>, Span, Option<String>)>,
415     pos: GenericPosition,
416     span: Span,
417 ) {
418     let mut max_param: Option<ParamKindOrd> = None;
419     let mut out_of_order = FxHashMap::default();
420     let mut param_idents = vec![];
421     let mut found_type = false;
422     let mut found_const = false;
423
424     for (kind, bounds, span, ident) in generics {
425         if let Some(ident) = ident {
426             param_idents.push((kind, bounds, param_idents.len(), ident));
427         }
428         let max_param = &mut max_param;
429         match max_param {
430             Some(max_param) if *max_param > kind => {
431                 let entry = out_of_order.entry(kind).or_insert((*max_param, vec![]));
432                 entry.1.push(span);
433             }
434             Some(_) | None => *max_param = Some(kind),
435         };
436         match kind {
437             ParamKindOrd::Type => found_type = true,
438             ParamKindOrd::Const => found_const = true,
439             _ => {}
440         }
441     }
442
443     let mut ordered_params = "<".to_string();
444     if !out_of_order.is_empty() {
445         param_idents.sort_by_key(|&(po, _, i, _)| (po, i));
446         let mut first = true;
447         for (_, bounds, _, ident) in param_idents {
448             if !first {
449                 ordered_params += ", ";
450             }
451             ordered_params += &ident;
452             if let Some(bounds) = bounds {
453                 if !bounds.is_empty() {
454                     ordered_params += ": ";
455                     ordered_params += &pprust::bounds_to_string(&bounds);
456                 }
457             }
458             first = false;
459         }
460     }
461     ordered_params += ">";
462
463     let pos_str = match pos {
464         GenericPosition::Param => "parameter",
465         GenericPosition::Arg => "argument",
466     };
467
468     for (param_ord, (max_param, spans)) in &out_of_order {
469         let mut err = handler.struct_span_err(
470             spans.clone(),
471             &format!(
472                 "{} {pos}s must be declared prior to {} {pos}s",
473                 param_ord,
474                 max_param,
475                 pos = pos_str,
476             ),
477         );
478         if let GenericPosition::Param = pos {
479             err.span_suggestion(
480                 span,
481                 &format!(
482                     "reorder the {}s: lifetimes, then types{}",
483                     pos_str,
484                     if sess.features_untracked().const_generics { ", then consts" } else { "" },
485                 ),
486                 ordered_params.clone(),
487                 Applicability::MachineApplicable,
488             );
489         }
490         err.emit();
491     }
492
493     // FIXME(const_generics): we shouldn't have to abort here at all, but we currently get ICEs
494     // if we don't. Const parameters and type parameters can currently conflict if they
495     // are out-of-order.
496     if !out_of_order.is_empty() && found_type && found_const {
497         FatalError.raise();
498     }
499 }
500
501 impl<'a> Visitor<'a> for AstValidator<'a> {
502     fn visit_attribute(&mut self, attr: &Attribute) {
503         validate_attr::check_meta(&self.session.parse_sess, attr);
504     }
505
506     fn visit_expr(&mut self, expr: &'a Expr) {
507         match &expr.kind {
508             ExprKind::Closure(_, _, _, fn_decl, _, _) => {
509                 self.check_fn_decl(fn_decl);
510             }
511             ExprKind::InlineAsm(..) if !self.session.target.target.options.allow_asm => {
512                 struct_span_err!(
513                     self.session,
514                     expr.span,
515                     E0472,
516                     "asm! is unsupported on this target"
517                 )
518                 .emit();
519             }
520             _ => {}
521         }
522
523         visit::walk_expr(self, expr);
524     }
525
526     fn visit_ty(&mut self, ty: &'a Ty) {
527         match ty.kind {
528             TyKind::BareFn(ref bfty) => {
529                 self.check_fn_decl(&bfty.decl);
530                 Self::check_decl_no_pat(&bfty.decl, |span, _| {
531                     struct_span_err!(
532                         self.session,
533                         span,
534                         E0561,
535                         "patterns aren't allowed in function pointer types"
536                     )
537                     .emit();
538                 });
539                 self.check_late_bound_lifetime_defs(&bfty.generic_params);
540             }
541             TyKind::TraitObject(ref bounds, ..) => {
542                 let mut any_lifetime_bounds = false;
543                 for bound in bounds {
544                     if let GenericBound::Outlives(ref lifetime) = *bound {
545                         if any_lifetime_bounds {
546                             struct_span_err!(
547                                 self.session,
548                                 lifetime.ident.span,
549                                 E0226,
550                                 "only a single explicit lifetime bound is permitted"
551                             )
552                             .emit();
553                             break;
554                         }
555                         any_lifetime_bounds = true;
556                     }
557                 }
558                 self.no_questions_in_bounds(bounds, "trait object types", false);
559             }
560             TyKind::ImplTrait(_, ref bounds) => {
561                 if self.is_impl_trait_banned {
562                     struct_span_err!(
563                         self.session,
564                         ty.span,
565                         E0667,
566                         "`impl Trait` is not allowed in path parameters"
567                     )
568                     .emit();
569                 }
570
571                 if let Some(outer_impl_trait_sp) = self.outer_impl_trait {
572                     struct_span_err!(
573                         self.session,
574                         ty.span,
575                         E0666,
576                         "nested `impl Trait` is not allowed"
577                     )
578                     .span_label(outer_impl_trait_sp, "outer `impl Trait`")
579                     .span_label(ty.span, "nested `impl Trait` here")
580                     .emit();
581                 }
582
583                 if !bounds
584                     .iter()
585                     .any(|b| if let GenericBound::Trait(..) = *b { true } else { false })
586                 {
587                     self.err_handler().span_err(ty.span, "at least one trait must be specified");
588                 }
589
590                 self.walk_ty(ty);
591                 return;
592             }
593             _ => {}
594         }
595
596         self.walk_ty(ty)
597     }
598
599     fn visit_label(&mut self, label: &'a Label) {
600         self.check_label(label.ident);
601         visit::walk_label(self, label);
602     }
603
604     fn visit_lifetime(&mut self, lifetime: &'a Lifetime) {
605         self.check_lifetime(lifetime.ident);
606         visit::walk_lifetime(self, lifetime);
607     }
608
609     fn visit_item(&mut self, item: &'a Item) {
610         if item.attrs.iter().any(|attr| is_proc_macro_attr(attr)) {
611             self.has_proc_macro_decls = true;
612         }
613
614         match item.kind {
615             ItemKind::Impl(unsafety, polarity, _, _, Some(..), ref ty, ref impl_items) => {
616                 self.invalid_visibility(&item.vis, None);
617                 if let TyKind::Err = ty.kind {
618                     self.err_handler()
619                         .struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
620                         .help("use `auto trait Trait {}` instead")
621                         .emit();
622                 }
623                 if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
624                     struct_span_err!(
625                         self.session,
626                         item.span,
627                         E0198,
628                         "negative impls cannot be unsafe"
629                     )
630                     .emit();
631                 }
632                 for impl_item in impl_items {
633                     self.invalid_visibility(&impl_item.vis, None);
634                     if let AssocItemKind::Fn(ref sig, _) = impl_item.kind {
635                         self.check_trait_fn_not_const(sig.header.constness);
636                         self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node);
637                     }
638                 }
639             }
640             ItemKind::Impl(unsafety, polarity, defaultness, _, None, _, _) => {
641                 self.invalid_visibility(
642                     &item.vis,
643                     Some("place qualifiers on individual impl items instead"),
644                 );
645                 if unsafety == Unsafety::Unsafe {
646                     struct_span_err!(
647                         self.session,
648                         item.span,
649                         E0197,
650                         "inherent impls cannot be unsafe"
651                     )
652                     .emit();
653                 }
654                 if polarity == ImplPolarity::Negative {
655                     self.err_handler().span_err(item.span, "inherent impls cannot be negative");
656                 }
657                 if defaultness == Defaultness::Default {
658                     self.err_handler()
659                         .struct_span_err(item.span, "inherent impls cannot be default")
660                         .note("only trait implementations may be annotated with default")
661                         .emit();
662                 }
663             }
664             ItemKind::Fn(ref sig, ref generics, _) => {
665                 self.visit_fn_header(&sig.header);
666                 self.check_fn_decl(&sig.decl);
667                 // We currently do not permit const generics in `const fn`, as
668                 // this is tantamount to allowing compile-time dependent typing.
669                 if sig.header.constness.node == Constness::Const {
670                     // Look for const generics and error if we find any.
671                     for param in &generics.params {
672                         match param.kind {
673                             GenericParamKind::Const { .. } => {
674                                 self.err_handler()
675                                     .struct_span_err(
676                                         item.span,
677                                         "const parameters are not permitted in `const fn`",
678                                     )
679                                     .emit();
680                             }
681                             _ => {}
682                         }
683                     }
684                 }
685                 // Reject C-varadic type unless the function is `unsafe extern "C"` semantically.
686                 match sig.header.ext {
687                     Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. })
688                     | Extern::Implicit
689                         if sig.header.unsafety == Unsafety::Unsafe => {}
690                     _ => self.check_c_varadic_type(&sig.decl),
691                 }
692             }
693             ItemKind::ForeignMod(..) => {
694                 self.invalid_visibility(
695                     &item.vis,
696                     Some("place qualifiers on individual foreign items instead"),
697                 );
698             }
699             ItemKind::Enum(ref def, _) => {
700                 for variant in &def.variants {
701                     self.invalid_visibility(&variant.vis, None);
702                     for field in variant.data.fields() {
703                         self.invalid_visibility(&field.vis, None);
704                     }
705                 }
706             }
707             ItemKind::Trait(is_auto, _, ref generics, ref bounds, ref trait_items) => {
708                 if is_auto == IsAuto::Yes {
709                     // Auto traits cannot have generics, super traits nor contain items.
710                     if !generics.params.is_empty() {
711                         struct_span_err!(
712                             self.session,
713                             item.span,
714                             E0567,
715                             "auto traits cannot have generic parameters"
716                         )
717                         .emit();
718                     }
719                     if !bounds.is_empty() {
720                         struct_span_err!(
721                             self.session,
722                             item.span,
723                             E0568,
724                             "auto traits cannot have super traits"
725                         )
726                         .emit();
727                     }
728                     if !trait_items.is_empty() {
729                         struct_span_err!(
730                             self.session,
731                             item.span,
732                             E0380,
733                             "auto traits cannot have methods or associated items"
734                         )
735                         .emit();
736                     }
737                 }
738                 self.no_questions_in_bounds(bounds, "supertraits", true);
739
740                 // Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
741                 // context for the supertraits.
742                 self.visit_vis(&item.vis);
743                 self.visit_ident(item.ident);
744                 self.visit_generics(generics);
745                 self.with_bound_context(BoundContext::TraitBounds, |this| {
746                     walk_list!(this, visit_param_bound, bounds);
747                 });
748                 walk_list!(self, visit_trait_item, trait_items);
749                 walk_list!(self, visit_attribute, &item.attrs);
750                 return;
751             }
752             ItemKind::Mod(_) => {
753                 // Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
754                 attr::first_attr_value_str_by_name(&item.attrs, sym::path);
755             }
756             ItemKind::Union(ref vdata, _) => {
757                 if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {
758                     self.err_handler()
759                         .span_err(item.span, "tuple and unit unions are not permitted");
760                 }
761                 if vdata.fields().is_empty() {
762                     self.err_handler().span_err(item.span, "unions cannot have zero fields");
763                 }
764             }
765             _ => {}
766         }
767
768         visit::walk_item(self, item)
769     }
770
771     fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
772         match fi.kind {
773             ForeignItemKind::Fn(ref decl, _) => {
774                 self.check_fn_decl(decl);
775                 Self::check_decl_no_pat(decl, |span, _| {
776                     struct_span_err!(
777                         self.session,
778                         span,
779                         E0130,
780                         "patterns aren't allowed in foreign function declarations"
781                     )
782                     .span_label(span, "pattern not allowed in foreign function")
783                     .emit();
784                 });
785             }
786             ForeignItemKind::Static(..) | ForeignItemKind::Ty | ForeignItemKind::Macro(..) => {}
787         }
788
789         visit::walk_foreign_item(self, fi)
790     }
791
792     // Mirrors `visit::walk_generic_args`, but tracks relevant state.
793     fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
794         match *generic_args {
795             GenericArgs::AngleBracketed(ref data) => {
796                 walk_list!(self, visit_generic_arg, &data.args);
797                 validate_generics_order(
798                     self.session,
799                     self.err_handler(),
800                     data.args.iter().map(|arg| {
801                         (
802                             match arg {
803                                 GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
804                                 GenericArg::Type(..) => ParamKindOrd::Type,
805                                 GenericArg::Const(..) => ParamKindOrd::Const,
806                             },
807                             None,
808                             arg.span(),
809                             None,
810                         )
811                     }),
812                     GenericPosition::Arg,
813                     generic_args.span(),
814                 );
815
816                 // Type bindings such as `Item = impl Debug` in `Iterator<Item = Debug>`
817                 // are allowed to contain nested `impl Trait`.
818                 self.with_impl_trait(None, |this| {
819                     walk_list!(
820                         this,
821                         visit_assoc_ty_constraint_from_generic_args,
822                         &data.constraints
823                     );
824                 });
825             }
826             GenericArgs::Parenthesized(ref data) => {
827                 walk_list!(self, visit_ty, &data.inputs);
828                 if let FunctionRetTy::Ty(ty) = &data.output {
829                     // `-> Foo` syntax is essentially an associated type binding,
830                     // so it is also allowed to contain nested `impl Trait`.
831                     self.with_impl_trait(None, |this| this.visit_ty(ty));
832                 }
833             }
834         }
835     }
836
837     fn visit_generics(&mut self, generics: &'a Generics) {
838         let mut prev_ty_default = None;
839         for param in &generics.params {
840             if let GenericParamKind::Type { ref default, .. } = param.kind {
841                 if default.is_some() {
842                     prev_ty_default = Some(param.ident.span);
843                 } else if let Some(span) = prev_ty_default {
844                     self.err_handler()
845                         .span_err(span, "type parameters with a default must be trailing");
846                     break;
847                 }
848             }
849         }
850
851         validate_generics_order(
852             self.session,
853             self.err_handler(),
854             generics.params.iter().map(|param| {
855                 let ident = Some(param.ident.to_string());
856                 let (kind, ident) = match &param.kind {
857                     GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
858                     GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
859                     GenericParamKind::Const { ref ty } => {
860                         let ty = pprust::ty_to_string(ty);
861                         (ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
862                     }
863                 };
864                 (kind, Some(&*param.bounds), param.ident.span, ident)
865             }),
866             GenericPosition::Param,
867             generics.span,
868         );
869
870         for predicate in &generics.where_clause.predicates {
871             if let WherePredicate::EqPredicate(ref predicate) = *predicate {
872                 self.err_handler()
873                     .struct_span_err(
874                         predicate.span,
875                         "equality constraints are not yet supported in `where` clauses",
876                     )
877                     .span_label(predicate.span, "not supported")
878                     .note(
879                         "for more information, see https://github.com/rust-lang/rust/issues/20041",
880                     )
881                     .emit();
882             }
883         }
884
885         visit::walk_generics(self, generics)
886     }
887
888     fn visit_generic_param(&mut self, param: &'a GenericParam) {
889         if let GenericParamKind::Lifetime { .. } = param.kind {
890             self.check_lifetime(param.ident);
891         }
892         visit::walk_generic_param(self, param);
893     }
894
895     fn visit_param_bound(&mut self, bound: &'a GenericBound) {
896         if let GenericBound::Trait(poly, maybe_bound) = bound {
897             match poly.trait_ref.constness {
898                 Some(Constness::NotConst) => {
899                     if *maybe_bound == TraitBoundModifier::Maybe {
900                         self.err_handler()
901                             .span_err(bound.span(), "`?const` and `?` are mutually exclusive");
902                     }
903
904                     if let Some(ctx) = self.bound_context {
905                         let msg = format!("`?const` is not permitted in {}", ctx.description());
906                         self.err_handler().span_err(bound.span(), &msg);
907                     }
908                 }
909
910                 Some(Constness::Const) => panic!("Parser should reject bare `const` on bounds"),
911                 None => {}
912             }
913         }
914
915         visit::walk_param_bound(self, bound)
916     }
917
918     fn visit_pat(&mut self, pat: &'a Pat) {
919         match pat.kind {
920             PatKind::Lit(ref expr) => {
921                 self.check_expr_within_pat(expr, false);
922             }
923             PatKind::Range(ref start, ref end, _) => {
924                 if let Some(expr) = start {
925                     self.check_expr_within_pat(expr, true);
926                 }
927                 if let Some(expr) = end {
928                     self.check_expr_within_pat(expr, true);
929                 }
930             }
931             _ => {}
932         }
933
934         visit::walk_pat(self, pat)
935     }
936
937     fn visit_where_predicate(&mut self, p: &'a WherePredicate) {
938         if let &WherePredicate::BoundPredicate(ref bound_predicate) = p {
939             // A type binding, eg `for<'c> Foo: Send+Clone+'c`
940             self.check_late_bound_lifetime_defs(&bound_predicate.bound_generic_params);
941         }
942         visit::walk_where_predicate(self, p);
943     }
944
945     fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef, m: &'a TraitBoundModifier) {
946         self.check_late_bound_lifetime_defs(&t.bound_generic_params);
947         visit::walk_poly_trait_ref(self, t, m);
948     }
949
950     fn visit_variant_data(&mut self, s: &'a VariantData) {
951         self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s))
952     }
953
954     fn visit_enum_def(
955         &mut self,
956         enum_definition: &'a EnumDef,
957         generics: &'a Generics,
958         item_id: NodeId,
959         _: Span,
960     ) {
961         self.with_banned_assoc_ty_bound(|this| {
962             visit::walk_enum_def(this, enum_definition, generics, item_id)
963         })
964     }
965
966     fn visit_impl_item(&mut self, ii: &'a AssocItem) {
967         match &ii.kind {
968             AssocItemKind::Const(_, body) => {
969                 self.check_impl_item_provided(ii.span, body, "constant", " = <expr>;");
970             }
971             AssocItemKind::Fn(sig, body) => {
972                 self.check_impl_item_provided(ii.span, body, "function", " { <body> }");
973                 self.check_fn_decl(&sig.decl);
974             }
975             AssocItemKind::TyAlias(bounds, body) => {
976                 self.check_impl_item_provided(ii.span, body, "type", " = <type>;");
977                 self.check_impl_assoc_type_no_bounds(bounds);
978             }
979             _ => {}
980         }
981         visit::walk_impl_item(self, ii);
982     }
983
984     fn visit_trait_item(&mut self, ti: &'a AssocItem) {
985         self.invalid_visibility(&ti.vis, None);
986         self.check_defaultness(ti.span, ti.defaultness);
987
988         if let AssocItemKind::Fn(sig, block) = &ti.kind {
989             self.check_fn_decl(&sig.decl);
990             self.check_trait_fn_not_async(ti.span, sig.header.asyncness.node);
991             self.check_trait_fn_not_const(sig.header.constness);
992             if block.is_none() {
993                 Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
994                     if mut_ident {
995                         self.lint_buffer.buffer_lint(
996                             PATTERNS_IN_FNS_WITHOUT_BODY,
997                             ti.id,
998                             span,
999                             "patterns aren't allowed in methods without bodies",
1000                         );
1001                     } else {
1002                         struct_span_err!(
1003                             self.session,
1004                             span,
1005                             E0642,
1006                             "patterns aren't allowed in methods without bodies"
1007                         )
1008                         .emit();
1009                     }
1010                 });
1011             }
1012         }
1013
1014         visit::walk_trait_item(self, ti);
1015     }
1016
1017     fn visit_assoc_item(&mut self, item: &'a AssocItem) {
1018         if let AssocItemKind::Fn(sig, _) = &item.kind {
1019             self.check_c_varadic_type(&sig.decl);
1020         }
1021         visit::walk_assoc_item(self, item);
1022     }
1023 }
1024
1025 pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool {
1026     let mut validator = AstValidator {
1027         session,
1028         has_proc_macro_decls: false,
1029         outer_impl_trait: None,
1030         bound_context: None,
1031         is_impl_trait_banned: false,
1032         is_assoc_ty_bound_banned: false,
1033         lint_buffer: lints,
1034     };
1035     visit::walk_crate(&mut validator, krate);
1036
1037     validator.has_proc_macro_decls
1038 }