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;
624 self.invalid_visibility(&item.vis, None);
625 if let TyKind::Err = self_ty.kind {
627 .struct_span_err(item.span, "`impl Trait for .. {}` is an obsolete syntax")
628 .help("use `auto trait Trait {}` instead")
631 if unsafety == Unsafety::Unsafe && polarity == ImplPolarity::Negative {
636 "negative impls cannot be unsafe"
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);
657 self.invalid_visibility(
659 Some("place qualifiers on individual impl items instead"),
661 if unsafety == Unsafety::Unsafe {
666 "inherent impls cannot be unsafe"
670 if polarity == ImplPolarity::Negative {
671 self.err_handler().span_err(item.span, "inherent impls cannot be negative");
673 if defaultness == Defaultness::Default {
675 .struct_span_err(item.span, "inherent impls cannot be default")
676 .note("only trait implementations may be annotated with default")
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 {
689 GenericParamKind::Const { .. } => {
693 "const parameters are not permitted in `const fn`",
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, .. })
705 if sig.header.unsafety == Unsafety::Unsafe => {}
706 _ => self.check_c_varadic_type(&sig.decl),
709 ItemKind::ForeignMod(..) => {
710 self.invalid_visibility(
712 Some("place qualifiers on individual foreign items instead"),
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);
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() {
731 "auto traits cannot have generic parameters"
735 if !bounds.is_empty() {
740 "auto traits cannot have super traits"
744 if !trait_items.is_empty() {
749 "auto traits cannot have methods or associated items"
754 self.no_questions_in_bounds(bounds, "supertraits", true);
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);
764 walk_list!(self, visit_trait_item, trait_items);
765 walk_list!(self, visit_attribute, &item.attrs);
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);
772 ItemKind::Union(ref vdata, _) => {
773 if let VariantData::Tuple(..) | VariantData::Unit(..) = vdata {
775 .span_err(item.span, "tuple and unit unions are not permitted");
777 if vdata.fields().is_empty() {
778 self.err_handler().span_err(item.span, "unions cannot have zero fields");
784 visit::walk_item(self, item)
787 fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
789 ForeignItemKind::Fn(ref decl, _) => {
790 self.check_fn_decl(decl);
791 Self::check_decl_no_pat(decl, |span, _| {
796 "patterns aren't allowed in foreign function declarations"
798 .span_label(span, "pattern not allowed in foreign function")
802 ForeignItemKind::Static(..) | ForeignItemKind::Ty | ForeignItemKind::Macro(..) => {}
805 visit::walk_foreign_item(self, fi)
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(
816 data.args.iter().map(|arg| {
819 GenericArg::Lifetime(..) => ParamKindOrd::Lifetime,
820 GenericArg::Type(..) => ParamKindOrd::Type,
821 GenericArg::Const(..) => ParamKindOrd::Const,
828 GenericPosition::Arg,
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| {
837 visit_assoc_ty_constraint_from_generic_args,
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));
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 {
861 .span_err(span, "type parameters with a default must be trailing");
867 validate_generics_order(
870 generics.params.iter().map(|param| {
871 let ident = Some(param.ident.to_string());
872 let (kind, ident) = match ¶m.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)))
880 (kind, Some(&*param.bounds), param.ident.span, ident)
882 GenericPosition::Param,
886 for predicate in &generics.where_clause.predicates {
887 if let WherePredicate::EqPredicate(ref predicate) = *predicate {
891 "equality constraints are not yet supported in `where` clauses",
893 .span_label(predicate.span, "not supported")
895 "for more information, see https://github.com/rust-lang/rust/issues/20041",
901 visit::walk_generics(self, generics)
904 fn visit_generic_param(&mut self, param: &'a GenericParam) {
905 if let GenericParamKind::Lifetime { .. } = param.kind {
906 self.check_lifetime(param.ident);
908 visit::walk_generic_param(self, param);
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 {
917 .span_err(bound.span(), "`?const` and `?` are mutually exclusive");
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);
926 Some(Constness::Const) => panic!("Parser should reject bare `const` on bounds"),
931 visit::walk_param_bound(self, bound)
934 fn visit_pat(&mut self, pat: &'a Pat) {
936 PatKind::Lit(ref expr) => {
937 self.check_expr_within_pat(expr, false);
939 PatKind::Range(ref start, ref end, _) => {
940 if let Some(expr) = start {
941 self.check_expr_within_pat(expr, true);
943 if let Some(expr) = end {
944 self.check_expr_within_pat(expr, true);
950 visit::walk_pat(self, pat)
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);
958 visit::walk_where_predicate(self, p);
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);
966 fn visit_variant_data(&mut self, s: &'a VariantData) {
967 self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s))
972 enum_definition: &'a EnumDef,
973 generics: &'a Generics,
977 self.with_banned_assoc_ty_bound(|this| {
978 visit::walk_enum_def(this, enum_definition, generics, item_id)
982 fn visit_impl_item(&mut self, ii: &'a AssocItem) {
984 AssocItemKind::Const(_, body) => {
985 self.check_impl_item_provided(ii.span, body, "constant", " = <expr>;");
987 AssocItemKind::Fn(sig, body) => {
988 self.check_impl_item_provided(ii.span, body, "function", " { <body> }");
989 self.check_fn_decl(&sig.decl);
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);
997 visit::walk_impl_item(self, ii);
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);
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| {
1011 self.lint_buffer.buffer_lint(
1012 PATTERNS_IN_FNS_WITHOUT_BODY,
1015 "patterns aren't allowed in methods without bodies",
1022 "patterns aren't allowed in methods without bodies"
1030 visit::walk_trait_item(self, ti);
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);
1037 visit::walk_assoc_item(self, item);
1041 pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool {
1042 let mut validator = AstValidator {
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,
1051 visit::walk_crate(&mut validator, krate);
1053 validator.has_proc_macro_decls