1 // Validate AST before lowering it to HIR.
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.
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};
21 use syntax::expand::is_proc_macro_attr;
22 use syntax::print::pprust;
23 use syntax::visit::{self, Visitor};
24 use syntax::walk_list;
26 use rustc_error_codes::*;
28 /// A syntactic context that disallows certain kinds of bounds (e.g., `?Trait` or `?const Trait`).
29 #[derive(Clone, Copy)]
37 fn description(&self) -> &'static str {
39 Self::ImplTrait => "`impl Trait`",
40 Self::TraitBounds => "supertraits",
41 Self::TraitObject => "trait objects",
46 struct AstValidator<'a> {
48 has_proc_macro_decls: bool,
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>,
55 /// Keeps track of the `BoundContext` as we recurse.
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>,
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,
65 /// Used to ban associated type bounds (i.e., `Type<AssocType: Bounds>`) in
66 /// certain positions.
67 is_assoc_ty_bound_banned: bool,
69 lint_buffer: &'a mut LintBuffer,
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);
76 self.is_impl_trait_banned = old;
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);
82 self.is_assoc_ty_bound_banned = old;
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);
88 self.with_bound_context(BoundContext::ImplTrait, |this| f(this));
92 self.outer_impl_trait = old;
95 fn with_bound_context(&mut self, ctx: BoundContext, f: impl FnOnce(&mut Self)) {
96 let old = self.bound_context.replace(ctx);
98 self.bound_context = old;
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(
108 "associated type bounds are not allowed within structs, enums, or unions",
113 self.visit_assoc_ty_constraint(constraint);
116 // Mirrors `visit::walk_ty`, but tracks relevant state.
117 fn walk_ty(&mut self, t: &'a Ty) {
119 TyKind::ImplTrait(..) => {
120 self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t))
122 TyKind::TraitObject(..) => {
123 self.with_bound_context(BoundContext::TraitObject, |this| visit::walk_ty(this, t));
125 TyKind::Path(ref qself, ref path) => {
127 // - `Option<impl Trait>`
128 // - `option::Option<impl Trait>`
129 // - `option::Option<T>::Foo<impl Trait>
132 // - `<impl Trait>::Foo`
133 // - `option::Option<impl Trait>::Foo`.
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));
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);
152 self.with_banned_impl_trait(|this| {
153 this.visit_path_segment(path.span, segment)
158 _ => visit::walk_ty(self, t),
162 fn err_handler(&self) -> &rustc_errors::Handler {
163 &self.session.diagnostic()
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");
173 fn check_label(&self, ident: Ident) {
174 if ident.without_first_quote().is_reserved() {
176 .span_err(ident.span, &format!("invalid label name `{}`", ident.name));
180 fn invalid_visibility(&self, vis: &Visibility, note: Option<&str>) {
181 if let VisibilityKind::Inherited = vis.node {
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");
190 if let Some(note) = note {
196 fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, bool)) {
197 for Param { pat, .. } in &decl.inputs {
199 PatKind::Ident(BindingMode::ByValue(Mutability::Not), _, None) | PatKind::Wild => {}
200 PatKind::Ident(BindingMode::ByValue(Mutability::Mut), _, None) => {
201 report_err(pat.span, true)
203 _ => report_err(pat.span, false),
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")
213 "consider using the `async-trait` crate: \
214 https://crates.io/crates/async-trait",
220 fn check_trait_fn_not_const(&self, constness: Spanned<Constness>) {
221 if constness.node == Constness::Const {
226 "trait fns cannot be declared const"
228 .span_label(constness.span, "trait fns cannot be const")
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(
239 &format!("`?Trait` is not permitted in {}", where_),
242 let path_str = pprust::path_to_string(&poly.trait_ref.path);
243 err.note(&format!("traits are `?{}` by default", path_str));
250 /// Matches `'-' lit | lit (cf. parser::Parser::parse_literal_maybe_minus)`,
251 /// or paths for ranges.
253 // FIXME: do we want to allow `expr -> pattern` conversion to create path expressions?
254 // That means making this work:
256 // ```rust,ignore (FIXME)
265 fn check_expr_within_pat(&self, expr: &Expr, allow_paths: bool) {
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,
274 _ => self.err_handler().span_err(
276 "arbitrary expressions aren't allowed \
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
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();
292 .span_err(spans, "lifetime bounds cannot be used in this context");
296 _ => Some(param.ident.span),
299 if !non_lt_param_spans.is_empty() {
300 self.err_handler().span_err(
302 "only lifetime parameters can be used in this context",
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(
313 "C-variadic function must be declared with at least one named argument",
318 for Param { ty, span, .. } in ps {
319 if let TyKind::CVarArgs = ty.kind {
320 self.err_handler().span_err(
322 "`...` must be the last argument of a C-variadic function",
333 .flat_map(|i| i.attrs.as_ref())
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)
339 if attr.is_doc_comment() {
343 "documentation comments cannot be applied to function parameters",
345 .span_label(attr.span, "doc comments are not allowed here")
348 self.err_handler().span_err(
350 "allow, cfg, cfg_attr, deny, \
351 forbid, and warn are the only allowed built-in attributes in function parameters",
357 fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
358 if let Defaultness::Default = defaultness {
360 .struct_span_err(span, "`default` is only allowed on items in `impl` definitions")
365 fn check_impl_item_provided<T>(&self, sp: Span, body: &Option<T>, ctx: &str, sugg: &str) {
371 .struct_span_err(sp, &format!("associated {} in `impl` without body", ctx))
373 self.session.source_map().end_point(sp),
374 &format!("provide a definition for the {}", ctx),
376 Applicability::HasPlaceholders,
381 fn check_impl_assoc_type_no_bounds(&self, bounds: &[GenericBound]) {
382 let span = match bounds {
385 [b0, .., bl] => b0.span().to(bl.span()),
388 .struct_span_err(span, "bounds on associated `type`s in `impl`s have no effect")
392 fn check_c_varadic_type(&self, decl: &FnDecl) {
393 for Param { ty, span, .. } in &decl.inputs {
394 if let TyKind::CVarArgs = ty.kind {
398 "only foreign or `unsafe extern \"C\" functions may be C-variadic",
406 enum GenericPosition {
411 fn validate_generics_order<'a>(
413 handler: &rustc_errors::Handler,
414 generics: impl Iterator<Item = (ParamKindOrd, Option<&'a [GenericBound]>, Span, Option<String>)>,
415 pos: GenericPosition,
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;
424 for (kind, bounds, span, ident) in generics {
425 if let Some(ident) = ident {
426 param_idents.push((kind, bounds, param_idents.len(), ident));
428 let max_param = &mut max_param;
430 Some(max_param) if *max_param > kind => {
431 let entry = out_of_order.entry(kind).or_insert((*max_param, vec![]));
434 Some(_) | None => *max_param = Some(kind),
437 ParamKindOrd::Type => found_type = true,
438 ParamKindOrd::Const => found_const = true,
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 {
449 ordered_params += ", ";
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);
461 ordered_params += ">";
463 let pos_str = match pos {
464 GenericPosition::Param => "parameter",
465 GenericPosition::Arg => "argument",
468 for (param_ord, (max_param, spans)) in &out_of_order {
469 let mut err = handler.struct_span_err(
472 "{} {pos}s must be declared prior to {} {pos}s",
478 if let GenericPosition::Param = pos {
482 "reorder the {}s: lifetimes, then types{}",
484 if sess.features_untracked().const_generics { ", then consts" } else { "" },
486 ordered_params.clone(),
487 Applicability::MachineApplicable,
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
496 if !out_of_order.is_empty() && found_type && found_const {
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);
506 fn visit_expr(&mut self, expr: &'a Expr) {
508 ExprKind::Closure(_, _, _, fn_decl, _, _) => {
509 self.check_fn_decl(fn_decl);
511 ExprKind::InlineAsm(..) if !self.session.target.target.options.allow_asm => {
516 "asm! is unsupported on this target"
523 visit::walk_expr(self, expr);
526 fn visit_ty(&mut self, ty: &'a Ty) {
528 TyKind::BareFn(ref bfty) => {
529 self.check_fn_decl(&bfty.decl);
530 Self::check_decl_no_pat(&bfty.decl, |span, _| {
535 "patterns aren't allowed in function pointer types"
539 self.check_late_bound_lifetime_defs(&bfty.generic_params);
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 {
550 "only a single explicit lifetime bound is permitted"
555 any_lifetime_bounds = true;
558 self.no_questions_in_bounds(bounds, "trait object types", false);
560 TyKind::ImplTrait(_, ref bounds) => {
561 if self.is_impl_trait_banned {
566 "`impl Trait` is not allowed in path parameters"
571 if let Some(outer_impl_trait_sp) = self.outer_impl_trait {
576 "nested `impl Trait` is not allowed"
578 .span_label(outer_impl_trait_sp, "outer `impl Trait`")
579 .span_label(ty.span, "nested `impl Trait` here")
585 .any(|b| if let GenericBound::Trait(..) = *b { true } else { false })
587 self.err_handler().span_err(ty.span, "at least one trait must be specified");
599 fn visit_label(&mut self, label: &'a Label) {
600 self.check_label(label.ident);
601 visit::walk_label(self, label);
604 fn visit_lifetime(&mut self, lifetime: &'a Lifetime) {
605 self.check_lifetime(lifetime.ident);
606 visit::walk_lifetime(self, lifetime);
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;
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 {
619 .struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
620 .help("use `auto trait Trait {}` instead")
623 if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
628 "negative impls cannot be unsafe"
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);
640 ItemKind::Impl(unsafety, polarity, defaultness, _, None, _, _) => {
641 self.invalid_visibility(
643 Some("place qualifiers on individual impl items instead"),
645 if unsafety == Unsafety::Unsafe {
650 "inherent impls cannot be unsafe"
654 if polarity == ImplPolarity::Negative {
655 self.err_handler().span_err(item.span, "inherent impls cannot be negative");
657 if defaultness == Defaultness::Default {
659 .struct_span_err(item.span, "inherent impls cannot be default")
660 .note("only trait implementations may be annotated with default")
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 {
673 GenericParamKind::Const { .. } => {
677 "const parameters are not permitted in `const fn`",
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, .. })
689 if sig.header.unsafety == Unsafety::Unsafe => {}
690 _ => self.check_c_varadic_type(&sig.decl),
693 ItemKind::ForeignMod(..) => {
694 self.invalid_visibility(
696 Some("place qualifiers on individual foreign items instead"),
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);
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() {
715 "auto traits cannot have generic parameters"
719 if !bounds.is_empty() {
724 "auto traits cannot have super traits"
728 if !trait_items.is_empty() {
733 "auto traits cannot have methods or associated items"
738 self.no_questions_in_bounds(bounds, "supertraits", true);
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);
748 walk_list!(self, visit_trait_item, trait_items);
749 walk_list!(self, visit_attribute, &item.attrs);
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);
756 ItemKind::Union(ref vdata, _) => {
757 if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {
759 .span_err(item.span, "tuple and unit unions are not permitted");
761 if vdata.fields().is_empty() {
762 self.err_handler().span_err(item.span, "unions cannot have zero fields");
768 visit::walk_item(self, item)
771 fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
773 ForeignItemKind::Fn(ref decl, _) => {
774 self.check_fn_decl(decl);
775 Self::check_decl_no_pat(decl, |span, _| {
780 "patterns aren't allowed in foreign function declarations"
782 .span_label(span, "pattern not allowed in foreign function")
786 ForeignItemKind::Static(..) | ForeignItemKind::Ty | ForeignItemKind::Macro(..) => {}
789 visit::walk_foreign_item(self, fi)
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(
800 data.args.iter().map(|arg| {
803 GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
804 GenericArg::Type(..) => ParamKindOrd::Type,
805 GenericArg::Const(..) => ParamKindOrd::Const,
812 GenericPosition::Arg,
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| {
821 visit_assoc_ty_constraint_from_generic_args,
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));
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 {
845 .span_err(span, "type parameters with a default must be trailing");
851 validate_generics_order(
854 generics.params.iter().map(|param| {
855 let ident = Some(param.ident.to_string());
856 let (kind, ident) = match ¶m.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)))
864 (kind, Some(&*param.bounds), param.ident.span, ident)
866 GenericPosition::Param,
870 for predicate in &generics.where_clause.predicates {
871 if let WherePredicate::EqPredicate(ref predicate) = *predicate {
875 "equality constraints are not yet supported in `where` clauses",
877 .span_label(predicate.span, "not supported")
879 "for more information, see https://github.com/rust-lang/rust/issues/20041",
885 visit::walk_generics(self, generics)
888 fn visit_generic_param(&mut self, param: &'a GenericParam) {
889 if let GenericParamKind::Lifetime { .. } = param.kind {
890 self.check_lifetime(param.ident);
892 visit::walk_generic_param(self, param);
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 {
901 .span_err(bound.span(), "`?const` and `?` are mutually exclusive");
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);
910 Some(Constness::Const) => panic!("Parser should reject bare `const` on bounds"),
915 visit::walk_param_bound(self, bound)
918 fn visit_pat(&mut self, pat: &'a Pat) {
920 PatKind::Lit(ref expr) => {
921 self.check_expr_within_pat(expr, false);
923 PatKind::Range(ref start, ref end, _) => {
924 if let Some(expr) = start {
925 self.check_expr_within_pat(expr, true);
927 if let Some(expr) = end {
928 self.check_expr_within_pat(expr, true);
934 visit::walk_pat(self, pat)
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);
942 visit::walk_where_predicate(self, p);
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);
950 fn visit_variant_data(&mut self, s: &'a VariantData) {
951 self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s))
956 enum_definition: &'a EnumDef,
957 generics: &'a Generics,
961 self.with_banned_assoc_ty_bound(|this| {
962 visit::walk_enum_def(this, enum_definition, generics, item_id)
966 fn visit_impl_item(&mut self, ii: &'a AssocItem) {
968 AssocItemKind::Const(_, body) => {
969 self.check_impl_item_provided(ii.span, body, "constant", " = <expr>;");
971 AssocItemKind::Fn(sig, body) => {
972 self.check_impl_item_provided(ii.span, body, "function", " { <body> }");
973 self.check_fn_decl(&sig.decl);
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);
981 visit::walk_impl_item(self, ii);
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);
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);
993 Self::check_decl_no_pat(&sig.decl, |span, mut_ident| {
995 self.lint_buffer.buffer_lint(
996 PATTERNS_IN_FNS_WITHOUT_BODY,
999 "patterns aren't allowed in methods without bodies",
1006 "patterns aren't allowed in methods without bodies"
1014 visit::walk_trait_item(self, ti);
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);
1021 visit::walk_assoc_item(self, item);
1025 pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool {
1026 let mut validator = AstValidator {
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,
1035 visit::walk_crate(&mut validator, krate);
1037 validator.has_proc_macro_decls