]> git.lizzy.rs Git - rust.git/blob - src/librustc_ast_passes/ast_validation.rs
Rollup merge of #68224 - GuillaumeGomez:prevent-urls-in-headings, r=ollie27
[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 {
616                 unsafety,
617                 polarity,
618                 defaultness: _,
619                 generics: _,
620                 of_trait: Some(_),
621                 ref self_ty,
622                 ref items,
623             } => {
624                 self.invalid_visibility(&item.vis, None);
625                 if let TyKind::Err = self_ty.kind {
626                     self.err_handler()
627                         .struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
628                         .help("use `auto trait Trait {}` instead")
629                         .emit();
630                 }
631                 if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
632                     struct_span_err!(
633                         self.session,
634                         item.span,
635                         E0198,
636                         "negative impls cannot be unsafe"
637                     )
638                     .emit();
639                 }
640                 for impl_item in items {
641                     self.invalid_visibility(&impl_item.vis, None);
642                     if let AssocItemKind::Fn(ref sig, _) = impl_item.kind {
643                         self.check_trait_fn_not_const(sig.header.constness);
644                         self.check_trait_fn_not_async(impl_item.span, sig.header.asyncness.node);
645                     }
646                 }
647             }
648             ItemKind::Impl {
649                 unsafety,
650                 polarity,
651                 defaultness,
652                 generics: _,
653                 of_trait: None,
654                 self_ty: _,
655                 items: _,
656             } => {
657                 self.invalid_visibility(
658                     &item.vis,
659                     Some("place qualifiers on individual impl items instead"),
660                 );
661                 if unsafety == Unsafety::Unsafe {
662                     struct_span_err!(
663                         self.session,
664                         item.span,
665                         E0197,
666                         "inherent impls cannot be unsafe"
667                     )
668                     .emit();
669                 }
670                 if polarity == ImplPolarity::Negative {
671                     self.err_handler().span_err(item.span, "inherent impls cannot be negative");
672                 }
673                 if defaultness == Defaultness::Default {
674                     self.err_handler()
675                         .struct_span_err(item.span, "inherent impls cannot be default")
676                         .note("only trait implementations may be annotated with default")
677                         .emit();
678                 }
679             }
680             ItemKind::Fn(ref sig, ref generics, _) => {
681                 self.visit_fn_header(&sig.header);
682                 self.check_fn_decl(&sig.decl);
683                 // We currently do not permit const generics in `const fn`, as
684                 // this is tantamount to allowing compile-time dependent typing.
685                 if sig.header.constness.node == Constness::Const {
686                     // Look for const generics and error if we find any.
687                     for param in &generics.params {
688                         match param.kind {
689                             GenericParamKind::Const { .. } => {
690                                 self.err_handler()
691                                     .struct_span_err(
692                                         item.span,
693                                         "const parameters are not permitted in `const fn`",
694                                     )
695                                     .emit();
696                             }
697                             _ => {}
698                         }
699                     }
700                 }
701                 // Reject C-varadic type unless the function is `unsafe extern "C"` semantically.
702                 match sig.header.ext {
703                     Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. })
704                     | Extern::Implicit
705                         if sig.header.unsafety == Unsafety::Unsafe => {}
706                     _ => self.check_c_varadic_type(&sig.decl),
707                 }
708             }
709             ItemKind::ForeignMod(..) => {
710                 self.invalid_visibility(
711                     &item.vis,
712                     Some("place qualifiers on individual foreign items instead"),
713                 );
714             }
715             ItemKind::Enum(ref def, _) => {
716                 for variant in &def.variants {
717                     self.invalid_visibility(&variant.vis, None);
718                     for field in variant.data.fields() {
719                         self.invalid_visibility(&field.vis, None);
720                     }
721                 }
722             }
723             ItemKind::Trait(is_auto, _, ref generics, ref bounds, ref trait_items) => {
724                 if is_auto == IsAuto::Yes {
725                     // Auto traits cannot have generics, super traits nor contain items.
726                     if !generics.params.is_empty() {
727                         struct_span_err!(
728                             self.session,
729                             item.span,
730                             E0567,
731                             "auto traits cannot have generic parameters"
732                         )
733                         .emit();
734                     }
735                     if !bounds.is_empty() {
736                         struct_span_err!(
737                             self.session,
738                             item.span,
739                             E0568,
740                             "auto traits cannot have super traits"
741                         )
742                         .emit();
743                     }
744                     if !trait_items.is_empty() {
745                         struct_span_err!(
746                             self.session,
747                             item.span,
748                             E0380,
749                             "auto traits cannot have methods or associated items"
750                         )
751                         .emit();
752                     }
753                 }
754                 self.no_questions_in_bounds(bounds, "supertraits", true);
755
756                 // Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
757                 // context for the supertraits.
758                 self.visit_vis(&item.vis);
759                 self.visit_ident(item.ident);
760                 self.visit_generics(generics);
761                 self.with_bound_context(BoundContext::TraitBounds, |this| {
762                     walk_list!(this, visit_param_bound, bounds);
763                 });
764                 walk_list!(self, visit_trait_item, trait_items);
765                 walk_list!(self, visit_attribute, &item.attrs);
766                 return;
767             }
768             ItemKind::Mod(_) => {
769                 // Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
770                 attr::first_attr_value_str_by_name(&item.attrs, sym::path);
771             }
772             ItemKind::Union(ref vdata, _) => {
773                 if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {
774                     self.err_handler()
775                         .span_err(item.span, "tuple and unit unions are not permitted");
776                 }
777                 if vdata.fields().is_empty() {
778                     self.err_handler().span_err(item.span, "unions cannot have zero fields");
779                 }
780             }
781             _ => {}
782         }
783
784         visit::walk_item(self, item)
785     }
786
787     fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
788         match fi.kind {
789             ForeignItemKind::Fn(ref decl, _) => {
790                 self.check_fn_decl(decl);
791                 Self::check_decl_no_pat(decl, |span, _| {
792                     struct_span_err!(
793                         self.session,
794                         span,
795                         E0130,
796                         "patterns aren't allowed in foreign function declarations"
797                     )
798                     .span_label(span, "pattern not allowed in foreign function")
799                     .emit();
800                 });
801             }
802             ForeignItemKind::Static(..) | ForeignItemKind::Ty | ForeignItemKind::Macro(..) => {}
803         }
804
805         visit::walk_foreign_item(self, fi)
806     }
807
808     // Mirrors `visit::walk_generic_args`, but tracks relevant state.
809     fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
810         match *generic_args {
811             GenericArgs::AngleBracketed(ref data) => {
812                 walk_list!(self, visit_generic_arg, &data.args);
813                 validate_generics_order(
814                     self.session,
815                     self.err_handler(),
816                     data.args.iter().map(|arg| {
817                         (
818                             match arg {
819                                 GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
820                                 GenericArg::Type(..) => ParamKindOrd::Type,
821                                 GenericArg::Const(..) => ParamKindOrd::Const,
822                             },
823                             None,
824                             arg.span(),
825                             None,
826                         )
827                     }),
828                     GenericPosition::Arg,
829                     generic_args.span(),
830                 );
831
832                 // Type bindings such as `Item = impl Debug` in `Iterator<Item = Debug>`
833                 // are allowed to contain nested `impl Trait`.
834                 self.with_impl_trait(None, |this| {
835                     walk_list!(
836                         this,
837                         visit_assoc_ty_constraint_from_generic_args,
838                         &data.constraints
839                     );
840                 });
841             }
842             GenericArgs::Parenthesized(ref data) => {
843                 walk_list!(self, visit_ty, &data.inputs);
844                 if let FunctionRetTy::Ty(ty) = &data.output {
845                     // `-> Foo` syntax is essentially an associated type binding,
846                     // so it is also allowed to contain nested `impl Trait`.
847                     self.with_impl_trait(None, |this| this.visit_ty(ty));
848                 }
849             }
850         }
851     }
852
853     fn visit_generics(&mut self, generics: &'a Generics) {
854         let mut prev_ty_default = None;
855         for param in &generics.params {
856             if let GenericParamKind::Type { ref default, .. } = param.kind {
857                 if default.is_some() {
858                     prev_ty_default = Some(param.ident.span);
859                 } else if let Some(span) = prev_ty_default {
860                     self.err_handler()
861                         .span_err(span, "type parameters with a default must be trailing");
862                     break;
863                 }
864             }
865         }
866
867         validate_generics_order(
868             self.session,
869             self.err_handler(),
870             generics.params.iter().map(|param| {
871                 let ident = Some(param.ident.to_string());
872                 let (kind, ident) = match &param.kind {
873                     GenericParamKind::Lifetime { .. } => (ParamKindOrd::Lifetime, ident),
874                     GenericParamKind::Type { .. } => (ParamKindOrd::Type, ident),
875                     GenericParamKind::Const { ref ty } => {
876                         let ty = pprust::ty_to_string(ty);
877                         (ParamKindOrd::Const, Some(format!("const {}: {}", param.ident, ty)))
878                     }
879                 };
880                 (kind, Some(&*param.bounds), param.ident.span, ident)
881             }),
882             GenericPosition::Param,
883             generics.span,
884         );
885
886         for predicate in &generics.where_clause.predicates {
887             if let WherePredicate::EqPredicate(ref predicate) = *predicate {
888                 self.err_handler()
889                     .struct_span_err(
890                         predicate.span,
891                         "equality constraints are not yet supported in `where` clauses",
892                     )
893                     .span_label(predicate.span, "not supported")
894                     .note(
895                         "for more information, see https://github.com/rust-lang/rust/issues/20041",
896                     )
897                     .emit();
898             }
899         }
900
901         visit::walk_generics(self, generics)
902     }
903
904     fn visit_generic_param(&mut self, param: &'a GenericParam) {
905         if let GenericParamKind::Lifetime { .. } = param.kind {
906             self.check_lifetime(param.ident);
907         }
908         visit::walk_generic_param(self, param);
909     }
910
911     fn visit_param_bound(&mut self, bound: &'a GenericBound) {
912         if let GenericBound::Trait(poly, maybe_bound) = bound {
913             match poly.trait_ref.constness {
914                 Some(Constness::NotConst) => {
915                     if *maybe_bound == TraitBoundModifier::Maybe {
916                         self.err_handler()
917                             .span_err(bound.span(), "`?const` and `?` are mutually exclusive");
918                     }
919
920                     if let Some(ctx) = self.bound_context {
921                         let msg = format!("`?const` is not permitted in {}", ctx.description());
922                         self.err_handler().span_err(bound.span(), &msg);
923                     }
924                 }
925
926                 Some(Constness::Const) => panic!("Parser should reject bare `const` on bounds"),
927                 None => {}
928             }
929         }
930
931         visit::walk_param_bound(self, bound)
932     }
933
934     fn visit_pat(&mut self, pat: &'a Pat) {
935         match pat.kind {
936             PatKind::Lit(ref expr) => {
937                 self.check_expr_within_pat(expr, false);
938             }
939             PatKind::Range(ref start, ref end, _) => {
940                 if let Some(expr) = start {
941                     self.check_expr_within_pat(expr, true);
942                 }
943                 if let Some(expr) = end {
944                     self.check_expr_within_pat(expr, true);
945                 }
946             }
947             _ => {}
948         }
949
950         visit::walk_pat(self, pat)
951     }
952
953     fn visit_where_predicate(&mut self, p: &'a WherePredicate) {
954         if let &WherePredicate::BoundPredicate(ref bound_predicate) = p {
955             // A type binding, eg `for<'c> Foo: Send+Clone+'c`
956             self.check_late_bound_lifetime_defs(&bound_predicate.bound_generic_params);
957         }
958         visit::walk_where_predicate(self, p);
959     }
960
961     fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef, m: &'a TraitBoundModifier) {
962         self.check_late_bound_lifetime_defs(&t.bound_generic_params);
963         visit::walk_poly_trait_ref(self, t, m);
964     }
965
966     fn visit_variant_data(&mut self, s: &'a VariantData) {
967         self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s))
968     }
969
970     fn visit_enum_def(
971         &mut self,
972         enum_definition: &'a EnumDef,
973         generics: &'a Generics,
974         item_id: NodeId,
975         _: Span,
976     ) {
977         self.with_banned_assoc_ty_bound(|this| {
978             visit::walk_enum_def(this, enum_definition, generics, item_id)
979         })
980     }
981
982     fn visit_impl_item(&mut self, ii: &'a AssocItem) {
983         match &ii.kind {
984             AssocItemKind::Const(_, body) => {
985                 self.check_impl_item_provided(ii.span, body, "constant", " = <expr>;");
986             }
987             AssocItemKind::Fn(sig, body) => {
988                 self.check_impl_item_provided(ii.span, body, "function", " { <body> }");
989                 self.check_fn_decl(&sig.decl);
990             }
991             AssocItemKind::TyAlias(bounds, body) => {
992                 self.check_impl_item_provided(ii.span, body, "type", " = <type>;");
993                 self.check_impl_assoc_type_no_bounds(bounds);
994             }
995             _ => {}
996         }
997         visit::walk_impl_item(self, ii);
998     }
999
1000     fn visit_trait_item(&mut self, ti: &'a AssocItem) {
1001         self.invalid_visibility(&ti.vis, None);
1002         self.check_defaultness(ti.span, ti.defaultness);
1003
1004         if let AssocItemKind::Fn(sig, block) = &ti.kind {
1005             self.check_fn_decl(&sig.decl);
1006             self.check_trait_fn_not_async(ti.span, sig.header.asyncness.node);
1007             self.check_trait_fn_not_const(sig.header.constness);
1008             if block.is_none() {
1009                 Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
1010                     if mut_ident {
1011                         self.lint_buffer.buffer_lint(
1012                             PATTERNS_IN_FNS_WITHOUT_BODY,
1013                             ti.id,
1014                             span,
1015                             "patterns aren't allowed in methods without bodies",
1016                         );
1017                     } else {
1018                         struct_span_err!(
1019                             self.session,
1020                             span,
1021                             E0642,
1022                             "patterns aren't allowed in methods without bodies"
1023                         )
1024                         .emit();
1025                     }
1026                 });
1027             }
1028         }
1029
1030         visit::walk_trait_item(self, ti);
1031     }
1032
1033     fn visit_assoc_item(&mut self, item: &'a AssocItem) {
1034         if let AssocItemKind::Fn(sig, _) = &item.kind {
1035             self.check_c_varadic_type(&sig.decl);
1036         }
1037         visit::walk_assoc_item(self, item);
1038     }
1039 }
1040
1041 pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool {
1042     let mut validator = AstValidator {
1043         session,
1044         has_proc_macro_decls: false,
1045         outer_impl_trait: None,
1046         bound_context: None,
1047         is_impl_trait_banned: false,
1048         is_assoc_ty_bound_banned: false,
1049         lint_buffer: lints,
1050     };
1051     visit::walk_crate(&mut validator, krate);
1052
1053     validator.has_proc_macro_decls
1054 }