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