]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_ast_passes/src/ast_validation.rs
Auto merge of #94515 - estebank:tweak-move-error, r=davidtwco
[rust.git] / compiler / rustc_ast_passes / src / 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 itertools::{Either, Itertools};
10 use rustc_ast::ptr::P;
11 use rustc_ast::visit::{self, AssocCtxt, FnCtxt, FnKind, Visitor};
12 use rustc_ast::walk_list;
13 use rustc_ast::*;
14 use rustc_ast_pretty::pprust::{self, State};
15 use rustc_data_structures::fx::FxHashMap;
16 use rustc_errors::{error_code, pluralize, struct_span_err, Applicability};
17 use rustc_parse::validate_attr;
18 use rustc_session::lint::builtin::{
19     DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, PATTERNS_IN_FNS_WITHOUT_BODY,
20 };
21 use rustc_session::lint::{BuiltinLintDiagnostics, LintBuffer};
22 use rustc_session::Session;
23 use rustc_span::source_map::Spanned;
24 use rustc_span::symbol::{kw, sym, Ident};
25 use rustc_span::Span;
26 use rustc_target::spec::abi;
27 use std::mem;
28 use std::ops::{Deref, DerefMut};
29
30 const MORE_EXTERN: &str =
31     "for more information, visit https://doc.rust-lang.org/std/keyword.extern.html";
32
33 /// Is `self` allowed semantically as the first parameter in an `FnDecl`?
34 enum SelfSemantic {
35     Yes,
36     No,
37 }
38
39 struct AstValidator<'a> {
40     session: &'a Session,
41
42     /// The span of the `extern` in an `extern { ... }` block, if any.
43     extern_mod: Option<&'a Item>,
44
45     /// Are we inside a trait impl?
46     in_trait_impl: bool,
47
48     in_const_trait_impl: bool,
49
50     has_proc_macro_decls: bool,
51
52     /// Used to ban nested `impl Trait`, e.g., `impl Into<impl Debug>`.
53     /// Nested `impl Trait` _is_ allowed in associated type position,
54     /// e.g., `impl Iterator<Item = impl Debug>`.
55     outer_impl_trait: Option<Span>,
56
57     is_tilde_const_allowed: bool,
58
59     /// Used to ban `impl Trait` in path projections like `<impl Iterator>::Item`
60     /// or `Foo::Bar<impl Trait>`
61     is_impl_trait_banned: bool,
62
63     /// Used to ban associated type bounds (i.e., `Type<AssocType: Bounds>`) in
64     /// certain positions.
65     is_assoc_ty_bound_banned: bool,
66
67     /// Used to allow `let` expressions in certain syntactic locations.
68     is_let_allowed: bool,
69
70     lint_buffer: &'a mut LintBuffer,
71 }
72
73 impl<'a> AstValidator<'a> {
74     fn with_in_trait_impl(
75         &mut self,
76         is_in: bool,
77         constness: Option<Const>,
78         f: impl FnOnce(&mut Self),
79     ) {
80         let old = mem::replace(&mut self.in_trait_impl, is_in);
81         let old_const =
82             mem::replace(&mut self.in_const_trait_impl, matches!(constness, Some(Const::Yes(_))));
83         f(self);
84         self.in_trait_impl = old;
85         self.in_const_trait_impl = old_const;
86     }
87
88     fn with_banned_impl_trait(&mut self, f: impl FnOnce(&mut Self)) {
89         let old = mem::replace(&mut self.is_impl_trait_banned, true);
90         f(self);
91         self.is_impl_trait_banned = old;
92     }
93
94     fn with_tilde_const_allowed(&mut self, f: impl FnOnce(&mut Self)) {
95         let old = mem::replace(&mut self.is_tilde_const_allowed, true);
96         f(self);
97         self.is_tilde_const_allowed = old;
98     }
99
100     fn with_banned_tilde_const(&mut self, f: impl FnOnce(&mut Self)) {
101         let old = mem::replace(&mut self.is_tilde_const_allowed, false);
102         f(self);
103         self.is_tilde_const_allowed = old;
104     }
105
106     fn with_let_allowed(&mut self, allowed: bool, f: impl FnOnce(&mut Self, bool)) {
107         let old = mem::replace(&mut self.is_let_allowed, allowed);
108         f(self, old);
109         self.is_let_allowed = old;
110     }
111
112     /// Emits an error banning the `let` expression provided in the given location.
113     fn ban_let_expr(&self, expr: &'a Expr) {
114         let sess = &self.session;
115         if sess.opts.unstable_features.is_nightly_build() {
116             sess.struct_span_err(expr.span, "`let` expressions are not supported here")
117                 .note("only supported directly in conditions of `if`- and `while`-expressions")
118                 .note("as well as when nested within `&&` and parentheses in those conditions")
119                 .emit();
120         } else {
121             sess.struct_span_err(expr.span, "expected expression, found statement (`let`)")
122                 .note("variable declaration using `let` is a statement")
123                 .emit();
124         }
125     }
126
127     fn check_gat_where(
128         &mut self,
129         id: NodeId,
130         before_predicates: &[WherePredicate],
131         where_clauses: (ast::TyAliasWhereClause, ast::TyAliasWhereClause),
132     ) {
133         if !before_predicates.is_empty() {
134             let mut state = State::new();
135             if !where_clauses.1.0 {
136                 state.space();
137                 state.word_space("where");
138             } else {
139                 state.word_space(",");
140             }
141             let mut first = true;
142             for p in before_predicates.iter() {
143                 if !first {
144                     state.word_space(",");
145                 }
146                 first = false;
147                 state.print_where_predicate(p);
148             }
149             let suggestion = state.s.eof();
150             self.lint_buffer.buffer_lint_with_diagnostic(
151                 DEPRECATED_WHERE_CLAUSE_LOCATION,
152                 id,
153                 where_clauses.0.1,
154                 "where clause not allowed here",
155                 BuiltinLintDiagnostics::DeprecatedWhereclauseLocation(
156                     where_clauses.1.1.shrink_to_hi(),
157                     suggestion,
158                 ),
159             );
160         }
161     }
162
163     fn with_banned_assoc_ty_bound(&mut self, f: impl FnOnce(&mut Self)) {
164         let old = mem::replace(&mut self.is_assoc_ty_bound_banned, true);
165         f(self);
166         self.is_assoc_ty_bound_banned = old;
167     }
168
169     fn with_impl_trait(&mut self, outer: Option<Span>, f: impl FnOnce(&mut Self)) {
170         let old = mem::replace(&mut self.outer_impl_trait, outer);
171         if outer.is_some() {
172             self.with_banned_tilde_const(f);
173         } else {
174             f(self);
175         }
176         self.outer_impl_trait = old;
177     }
178
179     fn visit_assoc_constraint_from_generic_args(&mut self, constraint: &'a AssocConstraint) {
180         match constraint.kind {
181             AssocConstraintKind::Equality { .. } => {}
182             AssocConstraintKind::Bound { .. } => {
183                 if self.is_assoc_ty_bound_banned {
184                     self.err_handler().span_err(
185                         constraint.span,
186                         "associated type bounds are not allowed within structs, enums, or unions",
187                     );
188                 }
189             }
190         }
191         self.visit_assoc_constraint(constraint);
192     }
193
194     // Mirrors `visit::walk_ty`, but tracks relevant state.
195     fn walk_ty(&mut self, t: &'a Ty) {
196         match t.kind {
197             TyKind::ImplTrait(..) => {
198                 self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t))
199             }
200             TyKind::TraitObject(..) => self.with_banned_tilde_const(|this| visit::walk_ty(this, t)),
201             TyKind::Path(ref qself, ref path) => {
202                 // We allow these:
203                 //  - `Option<impl Trait>`
204                 //  - `option::Option<impl Trait>`
205                 //  - `option::Option<T>::Foo<impl Trait>
206                 //
207                 // But not these:
208                 //  - `<impl Trait>::Foo`
209                 //  - `option::Option<impl Trait>::Foo`.
210                 //
211                 // To implement this, we disallow `impl Trait` from `qself`
212                 // (for cases like `<impl Trait>::Foo>`)
213                 // but we allow `impl Trait` in `GenericArgs`
214                 // iff there are no more PathSegments.
215                 if let Some(ref qself) = *qself {
216                     // `impl Trait` in `qself` is always illegal
217                     self.with_banned_impl_trait(|this| this.visit_ty(&qself.ty));
218                 }
219
220                 // Note that there should be a call to visit_path here,
221                 // so if any logic is added to process `Path`s a call to it should be
222                 // added both in visit_path and here. This code mirrors visit::walk_path.
223                 for (i, segment) in path.segments.iter().enumerate() {
224                     // Allow `impl Trait` iff we're on the final path segment
225                     if i == path.segments.len() - 1 {
226                         self.visit_path_segment(path.span, segment);
227                     } else {
228                         self.with_banned_impl_trait(|this| {
229                             this.visit_path_segment(path.span, segment)
230                         });
231                     }
232                 }
233             }
234             _ => visit::walk_ty(self, t),
235         }
236     }
237
238     fn visit_struct_field_def(&mut self, field: &'a FieldDef) {
239         if let Some(ident) = field.ident {
240             if ident.name == kw::Underscore {
241                 self.visit_vis(&field.vis);
242                 self.visit_ident(ident);
243                 self.visit_ty_common(&field.ty);
244                 self.walk_ty(&field.ty);
245                 walk_list!(self, visit_attribute, &field.attrs);
246                 return;
247             }
248         }
249         self.visit_field_def(field);
250     }
251
252     fn err_handler(&self) -> &rustc_errors::Handler {
253         &self.session.diagnostic()
254     }
255
256     fn check_lifetime(&self, ident: Ident) {
257         let valid_names = [kw::UnderscoreLifetime, kw::StaticLifetime, kw::Empty];
258         if !valid_names.contains(&ident.name) && ident.without_first_quote().is_reserved() {
259             self.err_handler().span_err(ident.span, "lifetimes cannot use keyword names");
260         }
261     }
262
263     fn check_label(&self, ident: Ident) {
264         if ident.without_first_quote().is_reserved() {
265             self.err_handler()
266                 .span_err(ident.span, &format!("invalid label name `{}`", ident.name));
267         }
268     }
269
270     fn invalid_visibility(&self, vis: &Visibility, note: Option<&str>) {
271         if let VisibilityKind::Inherited = vis.kind {
272             return;
273         }
274
275         let mut err =
276             struct_span_err!(self.session, vis.span, E0449, "unnecessary visibility qualifier");
277         if vis.kind.is_pub() {
278             err.span_label(vis.span, "`pub` not permitted here because it's implied");
279         }
280         if let Some(note) = note {
281             err.note(note);
282         }
283         err.emit();
284     }
285
286     fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
287         for Param { pat, .. } in &decl.inputs {
288             match pat.kind {
289                 PatKind::Ident(BindingMode::ByValue(Mutability::Not), _, None) | PatKind::Wild => {}
290                 PatKind::Ident(BindingMode::ByValue(Mutability::Mut), ident, None) => {
291                     report_err(pat.span, Some(ident), true)
292                 }
293                 _ => report_err(pat.span, None, false),
294             }
295         }
296     }
297
298     fn check_trait_fn_not_async(&self, fn_span: Span, asyncness: Async) {
299         if let Async::Yes { span, .. } = asyncness {
300             struct_span_err!(
301                 self.session,
302                 fn_span,
303                 E0706,
304                 "functions in traits cannot be declared `async`"
305             )
306             .span_label(span, "`async` because of this")
307             .note("`async` trait functions are not currently supported")
308             .note("consider using the `async-trait` crate: https://crates.io/crates/async-trait")
309             .emit();
310         }
311     }
312
313     fn check_trait_fn_not_const(&self, constness: Const) {
314         if let Const::Yes(span) = constness {
315             struct_span_err!(
316                 self.session,
317                 span,
318                 E0379,
319                 "functions in traits cannot be declared const"
320             )
321             .span_label(span, "functions in traits cannot be const")
322             .emit();
323         }
324     }
325
326     // FIXME(ecstaticmorse): Instead, use `bound_context` to check this in `visit_param_bound`.
327     fn no_questions_in_bounds(&self, bounds: &GenericBounds, where_: &str, is_trait: bool) {
328         for bound in bounds {
329             if let GenericBound::Trait(ref poly, TraitBoundModifier::Maybe) = *bound {
330                 let mut err = self.err_handler().struct_span_err(
331                     poly.span,
332                     &format!("`?Trait` is not permitted in {}", where_),
333                 );
334                 if is_trait {
335                     let path_str = pprust::path_to_string(&poly.trait_ref.path);
336                     err.note(&format!("traits are `?{}` by default", path_str));
337                 }
338                 err.emit();
339             }
340         }
341     }
342
343     fn check_late_bound_lifetime_defs(&self, params: &[GenericParam]) {
344         // Check only lifetime parameters are present and that the lifetime
345         // parameters that are present have no bounds.
346         let non_lt_param_spans: Vec<_> = params
347             .iter()
348             .filter_map(|param| match param.kind {
349                 GenericParamKind::Lifetime { .. } => {
350                     if !param.bounds.is_empty() {
351                         let spans: Vec<_> = param.bounds.iter().map(|b| b.span()).collect();
352                         self.err_handler()
353                             .span_err(spans, "lifetime bounds cannot be used in this context");
354                     }
355                     None
356                 }
357                 _ => Some(param.ident.span),
358             })
359             .collect();
360         if !non_lt_param_spans.is_empty() {
361             self.err_handler().span_err(
362                 non_lt_param_spans,
363                 "only lifetime parameters can be used in this context",
364             );
365         }
366     }
367
368     fn check_fn_decl(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
369         self.check_decl_num_args(fn_decl);
370         self.check_decl_cvaradic_pos(fn_decl);
371         self.check_decl_attrs(fn_decl);
372         self.check_decl_self_param(fn_decl, self_semantic);
373     }
374
375     /// Emits fatal error if function declaration has more than `u16::MAX` arguments
376     /// Error is fatal to prevent errors during typechecking
377     fn check_decl_num_args(&self, fn_decl: &FnDecl) {
378         let max_num_args: usize = u16::MAX.into();
379         if fn_decl.inputs.len() > max_num_args {
380             let Param { span, .. } = fn_decl.inputs[0];
381             self.err_handler().span_fatal(
382                 span,
383                 &format!("function can not have more than {} arguments", max_num_args),
384             );
385         }
386     }
387
388     fn check_decl_cvaradic_pos(&self, fn_decl: &FnDecl) {
389         match &*fn_decl.inputs {
390             [Param { ty, span, .. }] => {
391                 if let TyKind::CVarArgs = ty.kind {
392                     self.err_handler().span_err(
393                         *span,
394                         "C-variadic function must be declared with at least one named argument",
395                     );
396                 }
397             }
398             [ps @ .., _] => {
399                 for Param { ty, span, .. } in ps {
400                     if let TyKind::CVarArgs = ty.kind {
401                         self.err_handler().span_err(
402                             *span,
403                             "`...` must be the last argument of a C-variadic function",
404                         );
405                     }
406                 }
407             }
408             _ => {}
409         }
410     }
411
412     fn check_decl_attrs(&self, fn_decl: &FnDecl) {
413         fn_decl
414             .inputs
415             .iter()
416             .flat_map(|i| i.attrs.as_ref())
417             .filter(|attr| {
418                 let arr = [sym::allow, sym::cfg, sym::cfg_attr, sym::deny, sym::forbid, sym::warn];
419                 !arr.contains(&attr.name_or_empty()) && rustc_attr::is_builtin_attr(attr)
420             })
421             .for_each(|attr| {
422                 if attr.is_doc_comment() {
423                     self.err_handler()
424                         .struct_span_err(
425                             attr.span,
426                             "documentation comments cannot be applied to function parameters",
427                         )
428                         .span_label(attr.span, "doc comments are not allowed here")
429                         .emit();
430                 } else {
431                     self.err_handler().span_err(
432                         attr.span,
433                         "allow, cfg, cfg_attr, deny, \
434                 forbid, and warn are the only allowed built-in attributes in function parameters",
435                     )
436                 }
437             });
438     }
439
440     fn check_decl_self_param(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
441         if let (SelfSemantic::No, [param, ..]) = (self_semantic, &*fn_decl.inputs) {
442             if param.is_self() {
443                 self.err_handler()
444                     .struct_span_err(
445                         param.span,
446                         "`self` parameter is only allowed in associated functions",
447                     )
448                     .span_label(param.span, "not semantically valid as function parameter")
449                     .note("associated functions are those in `impl` or `trait` definitions")
450                     .emit();
451             }
452         }
453     }
454
455     fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
456         if let Defaultness::Default(def_span) = defaultness {
457             let span = self.session.source_map().guess_head_span(span);
458             self.err_handler()
459                 .struct_span_err(span, "`default` is only allowed on items in trait impls")
460                 .span_label(def_span, "`default` because of this")
461                 .emit();
462         }
463     }
464
465     fn error_item_without_body(&self, sp: Span, ctx: &str, msg: &str, sugg: &str) {
466         self.err_handler()
467             .struct_span_err(sp, msg)
468             .span_suggestion(
469                 self.session.source_map().end_point(sp),
470                 &format!("provide a definition for the {}", ctx),
471                 sugg.to_string(),
472                 Applicability::HasPlaceholders,
473             )
474             .emit();
475     }
476
477     fn check_impl_item_provided<T>(&self, sp: Span, body: &Option<T>, ctx: &str, sugg: &str) {
478         if body.is_none() {
479             let msg = format!("associated {} in `impl` without body", ctx);
480             self.error_item_without_body(sp, ctx, &msg, sugg);
481         }
482     }
483
484     fn check_type_no_bounds(&self, bounds: &[GenericBound], ctx: &str) {
485         let span = match bounds {
486             [] => return,
487             [b0] => b0.span(),
488             [b0, .., bl] => b0.span().to(bl.span()),
489         };
490         self.err_handler()
491             .struct_span_err(span, &format!("bounds on `type`s in {} have no effect", ctx))
492             .emit();
493     }
494
495     fn check_foreign_ty_genericless(&self, generics: &Generics, where_span: Span) {
496         let cannot_have = |span, descr, remove_descr| {
497             self.err_handler()
498                 .struct_span_err(
499                     span,
500                     &format!("`type`s inside `extern` blocks cannot have {}", descr),
501                 )
502                 .span_suggestion(
503                     span,
504                     &format!("remove the {}", remove_descr),
505                     String::new(),
506                     Applicability::MaybeIncorrect,
507                 )
508                 .span_label(self.current_extern_span(), "`extern` block begins here")
509                 .note(MORE_EXTERN)
510                 .emit();
511         };
512
513         if !generics.params.is_empty() {
514             cannot_have(generics.span, "generic parameters", "generic parameters");
515         }
516
517         if !generics.where_clause.predicates.is_empty() {
518             cannot_have(where_span, "`where` clauses", "`where` clause");
519         }
520     }
521
522     fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body: Option<Span>) {
523         let Some(body) = body else {
524             return;
525         };
526         self.err_handler()
527             .struct_span_err(ident.span, &format!("incorrect `{}` inside `extern` block", kind))
528             .span_label(ident.span, "cannot have a body")
529             .span_label(body, "the invalid body")
530             .span_label(
531                 self.current_extern_span(),
532                 format!(
533                     "`extern` blocks define existing foreign {0}s and {0}s \
534                     inside of them cannot have a body",
535                     kind
536                 ),
537             )
538             .note(MORE_EXTERN)
539             .emit();
540     }
541
542     /// An `fn` in `extern { ... }` cannot have a body `{ ... }`.
543     fn check_foreign_fn_bodyless(&self, ident: Ident, body: Option<&Block>) {
544         let Some(body) = body else {
545             return;
546         };
547         self.err_handler()
548             .struct_span_err(ident.span, "incorrect function inside `extern` block")
549             .span_label(ident.span, "cannot have a body")
550             .span_suggestion(
551                 body.span,
552                 "remove the invalid body",
553                 ";".to_string(),
554                 Applicability::MaybeIncorrect,
555             )
556             .help(
557                 "you might have meant to write a function accessible through FFI, \
558                 which can be done by writing `extern fn` outside of the `extern` block",
559             )
560             .span_label(
561                 self.current_extern_span(),
562                 "`extern` blocks define existing foreign functions and functions \
563                 inside of them cannot have a body",
564             )
565             .note(MORE_EXTERN)
566             .emit();
567     }
568
569     fn current_extern_span(&self) -> Span {
570         self.session.source_map().guess_head_span(self.extern_mod.unwrap().span)
571     }
572
573     /// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`.
574     fn check_foreign_fn_headerless(&self, ident: Ident, span: Span, header: FnHeader) {
575         if header.has_qualifiers() {
576             self.err_handler()
577                 .struct_span_err(ident.span, "functions in `extern` blocks cannot have qualifiers")
578                 .span_label(self.current_extern_span(), "in this `extern` block")
579                 .span_suggestion_verbose(
580                     span.until(ident.span.shrink_to_lo()),
581                     "remove the qualifiers",
582                     "fn ".to_string(),
583                     Applicability::MaybeIncorrect,
584                 )
585                 .emit();
586         }
587     }
588
589     /// An item in `extern { ... }` cannot use non-ascii identifier.
590     fn check_foreign_item_ascii_only(&self, ident: Ident) {
591         if !ident.as_str().is_ascii() {
592             let n = 83942;
593             self.err_handler()
594                 .struct_span_err(
595                     ident.span,
596                     "items in `extern` blocks cannot use non-ascii identifiers",
597                 )
598                 .span_label(self.current_extern_span(), "in this `extern` block")
599                 .note(&format!(
600                     "this limitation may be lifted in the future; see issue #{} <https://github.com/rust-lang/rust/issues/{}> for more information",
601                     n, n,
602                 ))
603                 .emit();
604         }
605     }
606
607     /// Reject C-varadic type unless the function is foreign,
608     /// or free and `unsafe extern "C"` semantically.
609     fn check_c_varadic_type(&self, fk: FnKind<'a>) {
610         match (fk.ctxt(), fk.header()) {
611             (Some(FnCtxt::Foreign), _) => return,
612             (Some(FnCtxt::Free), Some(header)) => match header.ext {
613                 Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }) | Extern::Implicit
614                     if matches!(header.unsafety, Unsafe::Yes(_)) =>
615                 {
616                     return;
617                 }
618                 _ => {}
619             },
620             _ => {}
621         };
622
623         for Param { ty, span, .. } in &fk.decl().inputs {
624             if let TyKind::CVarArgs = ty.kind {
625                 self.err_handler()
626                     .struct_span_err(
627                         *span,
628                         "only foreign or `unsafe extern \"C\"` functions may be C-variadic",
629                     )
630                     .emit();
631             }
632         }
633     }
634
635     fn check_item_named(&self, ident: Ident, kind: &str) {
636         if ident.name != kw::Underscore {
637             return;
638         }
639         self.err_handler()
640             .struct_span_err(ident.span, &format!("`{}` items in this context need a name", kind))
641             .span_label(ident.span, format!("`_` is not a valid name for this `{}` item", kind))
642             .emit();
643     }
644
645     fn check_nomangle_item_asciionly(&self, ident: Ident, item_span: Span) {
646         if ident.name.as_str().is_ascii() {
647             return;
648         }
649         let head_span = self.session.source_map().guess_head_span(item_span);
650         struct_span_err!(
651             self.session,
652             head_span,
653             E0754,
654             "`#[no_mangle]` requires ASCII identifier"
655         )
656         .emit();
657     }
658
659     fn check_mod_file_item_asciionly(&self, ident: Ident) {
660         if ident.name.as_str().is_ascii() {
661             return;
662         }
663         struct_span_err!(
664             self.session,
665             ident.span,
666             E0754,
667             "trying to load file for module `{}` with non-ascii identifier name",
668             ident.name
669         )
670         .help("consider using `#[path]` attribute to specify filesystem path")
671         .emit();
672     }
673
674     fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
675         if !generics.params.is_empty() {
676             struct_span_err!(
677                 self.session,
678                 generics.span,
679                 E0567,
680                 "auto traits cannot have generic parameters"
681             )
682             .span_label(ident_span, "auto trait cannot have generic parameters")
683             .span_suggestion(
684                 generics.span,
685                 "remove the parameters",
686                 String::new(),
687                 Applicability::MachineApplicable,
688             )
689             .emit();
690         }
691     }
692
693     fn emit_e0568(&self, span: Span, ident_span: Span) {
694         struct_span_err!(
695             self.session,
696             span,
697             E0568,
698             "auto traits cannot have super traits or lifetime bounds"
699         )
700         .span_label(ident_span, "auto trait cannot have super traits or lifetime bounds")
701         .span_suggestion(
702             span,
703             "remove the super traits or lifetime bounds",
704             String::new(),
705             Applicability::MachineApplicable,
706         )
707         .emit();
708     }
709
710     fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) {
711         if let [.., last] = &bounds[..] {
712             let span = ident_span.shrink_to_hi().to(last.span());
713             self.emit_e0568(span, ident_span);
714         }
715     }
716
717     fn deny_where_clause(&self, where_clause: &WhereClause, ident_span: Span) {
718         if !where_clause.predicates.is_empty() {
719             self.emit_e0568(where_clause.span, ident_span);
720         }
721     }
722
723     fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) {
724         if !trait_items.is_empty() {
725             let spans: Vec<_> = trait_items.iter().map(|i| i.ident.span).collect();
726             let total_span = trait_items.first().unwrap().span.to(trait_items.last().unwrap().span);
727             struct_span_err!(
728                 self.session,
729                 spans,
730                 E0380,
731                 "auto traits cannot have associated items"
732             )
733             .span_suggestion(
734                 total_span,
735                 "remove these associated items",
736                 String::new(),
737                 Applicability::MachineApplicable,
738             )
739             .span_label(ident_span, "auto trait cannot have associated items")
740             .emit();
741         }
742     }
743
744     fn correct_generic_order_suggestion(&self, data: &AngleBracketedArgs) -> String {
745         // Lifetimes always come first.
746         let lt_sugg = data.args.iter().filter_map(|arg| match arg {
747             AngleBracketedArg::Arg(lt @ GenericArg::Lifetime(_)) => {
748                 Some(pprust::to_string(|s| s.print_generic_arg(lt)))
749             }
750             _ => None,
751         });
752         let args_sugg = data.args.iter().filter_map(|a| match a {
753             AngleBracketedArg::Arg(GenericArg::Lifetime(_)) | AngleBracketedArg::Constraint(_) => {
754                 None
755             }
756             AngleBracketedArg::Arg(arg) => Some(pprust::to_string(|s| s.print_generic_arg(arg))),
757         });
758         // Constraints always come last.
759         let constraint_sugg = data.args.iter().filter_map(|a| match a {
760             AngleBracketedArg::Arg(_) => None,
761             AngleBracketedArg::Constraint(c) => {
762                 Some(pprust::to_string(|s| s.print_assoc_constraint(c)))
763             }
764         });
765         format!(
766             "<{}>",
767             lt_sugg.chain(args_sugg).chain(constraint_sugg).collect::<Vec<String>>().join(", ")
768         )
769     }
770
771     /// Enforce generic args coming before constraints in `<...>` of a path segment.
772     fn check_generic_args_before_constraints(&self, data: &AngleBracketedArgs) {
773         // Early exit in case it's partitioned as it should be.
774         if data.args.iter().is_partitioned(|arg| matches!(arg, AngleBracketedArg::Arg(_))) {
775             return;
776         }
777         // Find all generic argument coming after the first constraint...
778         let (constraint_spans, arg_spans): (Vec<Span>, Vec<Span>) =
779             data.args.iter().partition_map(|arg| match arg {
780                 AngleBracketedArg::Constraint(c) => Either::Left(c.span),
781                 AngleBracketedArg::Arg(a) => Either::Right(a.span()),
782             });
783         let args_len = arg_spans.len();
784         let constraint_len = constraint_spans.len();
785         // ...and then error:
786         self.err_handler()
787             .struct_span_err(
788                 arg_spans.clone(),
789                 "generic arguments must come before the first constraint",
790             )
791             .span_label(constraint_spans[0], &format!("constraint{}", pluralize!(constraint_len)))
792             .span_label(
793                 *arg_spans.iter().last().unwrap(),
794                 &format!("generic argument{}", pluralize!(args_len)),
795             )
796             .span_labels(constraint_spans, "")
797             .span_labels(arg_spans, "")
798             .span_suggestion_verbose(
799                 data.span,
800                 &format!(
801                     "move the constraint{} after the generic argument{}",
802                     pluralize!(constraint_len),
803                     pluralize!(args_len)
804                 ),
805                 self.correct_generic_order_suggestion(&data),
806                 Applicability::MachineApplicable,
807             )
808             .emit();
809     }
810
811     fn visit_ty_common(&mut self, ty: &'a Ty) {
812         match ty.kind {
813             TyKind::BareFn(ref bfty) => {
814                 self.check_fn_decl(&bfty.decl, SelfSemantic::No);
815                 Self::check_decl_no_pat(&bfty.decl, |span, _, _| {
816                     struct_span_err!(
817                         self.session,
818                         span,
819                         E0561,
820                         "patterns aren't allowed in function pointer types"
821                     )
822                     .emit();
823                 });
824                 self.check_late_bound_lifetime_defs(&bfty.generic_params);
825                 if let Extern::Implicit = bfty.ext {
826                     let sig_span = self.session.source_map().next_point(ty.span.shrink_to_lo());
827                     self.maybe_lint_missing_abi(sig_span, ty.id);
828                 }
829             }
830             TyKind::TraitObject(ref bounds, ..) => {
831                 let mut any_lifetime_bounds = false;
832                 for bound in bounds {
833                     if let GenericBound::Outlives(ref lifetime) = *bound {
834                         if any_lifetime_bounds {
835                             struct_span_err!(
836                                 self.session,
837                                 lifetime.ident.span,
838                                 E0226,
839                                 "only a single explicit lifetime bound is permitted"
840                             )
841                             .emit();
842                             break;
843                         }
844                         any_lifetime_bounds = true;
845                     }
846                 }
847                 self.no_questions_in_bounds(bounds, "trait object types", false);
848             }
849             TyKind::ImplTrait(_, ref bounds) => {
850                 if self.is_impl_trait_banned {
851                     struct_span_err!(
852                         self.session,
853                         ty.span,
854                         E0667,
855                         "`impl Trait` is not allowed in path parameters"
856                     )
857                     .emit();
858                 }
859
860                 if let Some(outer_impl_trait_sp) = self.outer_impl_trait {
861                     struct_span_err!(
862                         self.session,
863                         ty.span,
864                         E0666,
865                         "nested `impl Trait` is not allowed"
866                     )
867                     .span_label(outer_impl_trait_sp, "outer `impl Trait`")
868                     .span_label(ty.span, "nested `impl Trait` here")
869                     .emit();
870                 }
871
872                 if !bounds.iter().any(|b| matches!(b, GenericBound::Trait(..))) {
873                     self.err_handler().span_err(ty.span, "at least one trait must be specified");
874                 }
875             }
876             _ => {}
877         }
878     }
879
880     fn maybe_lint_missing_abi(&mut self, span: Span, id: NodeId) {
881         // FIXME(davidtwco): This is a hack to detect macros which produce spans of the
882         // call site which do not have a macro backtrace. See #61963.
883         let is_macro_callsite = self
884             .session
885             .source_map()
886             .span_to_snippet(span)
887             .map(|snippet| snippet.starts_with("#["))
888             .unwrap_or(true);
889         if !is_macro_callsite {
890             self.lint_buffer.buffer_lint_with_diagnostic(
891                 MISSING_ABI,
892                 id,
893                 span,
894                 "extern declarations without an explicit ABI are deprecated",
895                 BuiltinLintDiagnostics::MissingAbi(span, abi::Abi::FALLBACK),
896             )
897         }
898     }
899 }
900
901 /// Checks that generic parameters are in the correct order,
902 /// which is lifetimes, then types and then consts. (`<'a, T, const N: usize>`)
903 fn validate_generic_param_order(
904     handler: &rustc_errors::Handler,
905     generics: &[GenericParam],
906     span: Span,
907 ) {
908     let mut max_param: Option<ParamKindOrd> = None;
909     let mut out_of_order = FxHashMap::default();
910     let mut param_idents = Vec::with_capacity(generics.len());
911
912     for (idx, param) in generics.iter().enumerate() {
913         let ident = param.ident;
914         let (kind, bounds, span) = (&param.kind, &param.bounds, ident.span);
915         let (ord_kind, ident) = match &param.kind {
916             GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident.to_string()),
917             GenericParamKind::Type { default: _ } => (ParamKindOrd::Type, ident.to_string()),
918             GenericParamKind::Const { ref ty, kw_span: _, default: _ } => {
919                 let ty = pprust::ty_to_string(ty);
920                 (ParamKindOrd::Const, format!("const {}: {}", ident, ty))
921             }
922         };
923         param_idents.push((kind, ord_kind, bounds, idx, ident));
924         match max_param {
925             Some(max_param) if max_param > ord_kind => {
926                 let entry = out_of_order.entry(ord_kind).or_insert((max_param, vec![]));
927                 entry.1.push(span);
928             }
929             Some(_) | None => max_param = Some(ord_kind),
930         };
931     }
932
933     if !out_of_order.is_empty() {
934         let mut ordered_params = "<".to_string();
935         param_idents.sort_by_key(|&(_, po, _, i, _)| (po, i));
936         let mut first = true;
937         for (kind, _, bounds, _, ident) in param_idents {
938             if !first {
939                 ordered_params += ", ";
940             }
941             ordered_params += &ident;
942
943             if !bounds.is_empty() {
944                 ordered_params += ": ";
945                 ordered_params += &pprust::bounds_to_string(&bounds);
946             }
947
948             match kind {
949                 GenericParamKind::Type { default: Some(default) } => {
950                     ordered_params += " = ";
951                     ordered_params += &pprust::ty_to_string(default);
952                 }
953                 GenericParamKind::Type { default: None } => (),
954                 GenericParamKind::Lifetime => (),
955                 GenericParamKind::Const { ty: _, kw_span: _, default: Some(default) } => {
956                     ordered_params += " = ";
957                     ordered_params += &pprust::expr_to_string(&*default.value);
958                 }
959                 GenericParamKind::Const { ty: _, kw_span: _, default: None } => (),
960             }
961             first = false;
962         }
963
964         ordered_params += ">";
965
966         for (param_ord, (max_param, spans)) in &out_of_order {
967             let mut err = handler.struct_span_err(
968                 spans.clone(),
969                 &format!(
970                     "{} parameters must be declared prior to {} parameters",
971                     param_ord, max_param,
972                 ),
973             );
974             err.span_suggestion(
975                 span,
976                 "reorder the parameters: lifetimes, then consts and types",
977                 ordered_params.clone(),
978                 Applicability::MachineApplicable,
979             );
980             err.emit();
981         }
982     }
983 }
984
985 impl<'a> Visitor<'a> for AstValidator<'a> {
986     fn visit_attribute(&mut self, attr: &Attribute) {
987         validate_attr::check_meta(&self.session.parse_sess, attr);
988     }
989
990     fn visit_expr(&mut self, expr: &'a Expr) {
991         self.with_let_allowed(false, |this, let_allowed| match &expr.kind {
992             ExprKind::If(cond, then, opt_else) => {
993                 this.visit_block(then);
994                 walk_list!(this, visit_expr, opt_else);
995                 this.with_let_allowed(true, |this, _| this.visit_expr(cond));
996                 return;
997             }
998             ExprKind::Let(..) if !let_allowed => this.ban_let_expr(expr),
999             ExprKind::Match(expr, arms) => {
1000                 this.visit_expr(expr);
1001                 for arm in arms {
1002                     this.visit_expr(&arm.body);
1003                     this.visit_pat(&arm.pat);
1004                     walk_list!(this, visit_attribute, &arm.attrs);
1005                     if let Some(ref guard) = arm.guard {
1006                         if let ExprKind::Let(_, ref expr, _) = guard.kind {
1007                             this.with_let_allowed(true, |this, _| this.visit_expr(expr));
1008                             return;
1009                         }
1010                     }
1011                 }
1012             }
1013             ExprKind::Paren(_) | ExprKind::Binary(Spanned { node: BinOpKind::And, .. }, ..) => {
1014                 this.with_let_allowed(let_allowed, |this, _| visit::walk_expr(this, expr));
1015                 return;
1016             }
1017             ExprKind::While(cond, then, opt_label) => {
1018                 walk_list!(this, visit_label, opt_label);
1019                 this.visit_block(then);
1020                 this.with_let_allowed(true, |this, _| this.visit_expr(cond));
1021                 return;
1022             }
1023             _ => visit::walk_expr(this, expr),
1024         });
1025     }
1026
1027     fn visit_ty(&mut self, ty: &'a Ty) {
1028         self.visit_ty_common(ty);
1029         self.walk_ty(ty)
1030     }
1031
1032     fn visit_label(&mut self, label: &'a Label) {
1033         self.check_label(label.ident);
1034         visit::walk_label(self, label);
1035     }
1036
1037     fn visit_lifetime(&mut self, lifetime: &'a Lifetime) {
1038         self.check_lifetime(lifetime.ident);
1039         visit::walk_lifetime(self, lifetime);
1040     }
1041
1042     fn visit_field_def(&mut self, s: &'a FieldDef) {
1043         visit::walk_field_def(self, s)
1044     }
1045
1046     fn visit_item(&mut self, item: &'a Item) {
1047         if item.attrs.iter().any(|attr| self.session.is_proc_macro_attr(attr)) {
1048             self.has_proc_macro_decls = true;
1049         }
1050
1051         if self.session.contains_name(&item.attrs, sym::no_mangle) {
1052             self.check_nomangle_item_asciionly(item.ident, item.span);
1053         }
1054
1055         match item.kind {
1056             ItemKind::Impl(box Impl {
1057                 unsafety,
1058                 polarity,
1059                 defaultness: _,
1060                 constness,
1061                 ref generics,
1062                 of_trait: Some(ref t),
1063                 ref self_ty,
1064                 ref items,
1065             }) => {
1066                 self.with_in_trait_impl(true, Some(constness), |this| {
1067                     this.invalid_visibility(&item.vis, None);
1068                     if let TyKind::Err = self_ty.kind {
1069                         this.err_handler()
1070                             .struct_span_err(
1071                                 item.span,
1072                                 "`impl Trait for .. {}` is an obsolete syntax",
1073                             )
1074                             .help("use `auto trait Trait {}` instead")
1075                             .emit();
1076                     }
1077                     if let (Unsafe::Yes(span), ImplPolarity::Negative(sp)) = (unsafety, polarity) {
1078                         struct_span_err!(
1079                             this.session,
1080                             sp.to(t.path.span),
1081                             E0198,
1082                             "negative impls cannot be unsafe"
1083                         )
1084                         .span_label(sp, "negative because of this")
1085                         .span_label(span, "unsafe because of this")
1086                         .emit();
1087                     }
1088
1089                     this.visit_vis(&item.vis);
1090                     this.visit_ident(item.ident);
1091                     if let Const::Yes(_) = constness {
1092                         this.with_tilde_const_allowed(|this| this.visit_generics(generics));
1093                     } else {
1094                         this.visit_generics(generics);
1095                     }
1096                     this.visit_trait_ref(t);
1097                     this.visit_ty(self_ty);
1098
1099                     walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl);
1100                 });
1101                 return; // Avoid visiting again.
1102             }
1103             ItemKind::Impl(box Impl {
1104                 unsafety,
1105                 polarity,
1106                 defaultness,
1107                 constness,
1108                 generics: _,
1109                 of_trait: None,
1110                 ref self_ty,
1111                 items: _,
1112             }) => {
1113                 let error = |annotation_span, annotation| {
1114                     let mut err = self.err_handler().struct_span_err(
1115                         self_ty.span,
1116                         &format!("inherent impls cannot be {}", annotation),
1117                     );
1118                     err.span_label(annotation_span, &format!("{} because of this", annotation));
1119                     err.span_label(self_ty.span, "inherent impl for this type");
1120                     err
1121                 };
1122
1123                 self.invalid_visibility(
1124                     &item.vis,
1125                     Some("place qualifiers on individual impl items instead"),
1126                 );
1127                 if let Unsafe::Yes(span) = unsafety {
1128                     error(span, "unsafe").code(error_code!(E0197)).emit();
1129                 }
1130                 if let ImplPolarity::Negative(span) = polarity {
1131                     error(span, "negative").emit();
1132                 }
1133                 if let Defaultness::Default(def_span) = defaultness {
1134                     error(def_span, "`default`")
1135                         .note("only trait implementations may be annotated with `default`")
1136                         .emit();
1137                 }
1138                 if let Const::Yes(span) = constness {
1139                     error(span, "`const`")
1140                         .note("only trait implementations may be annotated with `const`")
1141                         .emit();
1142                 }
1143             }
1144             ItemKind::Fn(box Fn { defaultness, ref sig, ref generics, ref body }) => {
1145                 self.check_defaultness(item.span, defaultness);
1146
1147                 if body.is_none() {
1148                     let msg = "free function without a body";
1149                     self.error_item_without_body(item.span, "function", msg, " { <body> }");
1150                 }
1151                 self.visit_vis(&item.vis);
1152                 self.visit_ident(item.ident);
1153                 if let Const::Yes(_) = sig.header.constness {
1154                     self.with_tilde_const_allowed(|this| this.visit_generics(generics));
1155                 } else {
1156                     self.visit_generics(generics);
1157                 }
1158                 let kind = FnKind::Fn(FnCtxt::Free, item.ident, sig, &item.vis, body.as_deref());
1159                 self.visit_fn(kind, item.span, item.id);
1160                 walk_list!(self, visit_attribute, &item.attrs);
1161                 return; // Avoid visiting again.
1162             }
1163             ItemKind::ForeignMod(ForeignMod { abi, unsafety, .. }) => {
1164                 let old_item = mem::replace(&mut self.extern_mod, Some(item));
1165                 self.invalid_visibility(
1166                     &item.vis,
1167                     Some("place qualifiers on individual foreign items instead"),
1168                 );
1169                 if let Unsafe::Yes(span) = unsafety {
1170                     self.err_handler().span_err(span, "extern block cannot be declared unsafe");
1171                 }
1172                 if abi.is_none() {
1173                     self.maybe_lint_missing_abi(item.span, item.id);
1174                 }
1175                 visit::walk_item(self, item);
1176                 self.extern_mod = old_item;
1177                 return; // Avoid visiting again.
1178             }
1179             ItemKind::Enum(ref def, _) => {
1180                 for variant in &def.variants {
1181                     self.invalid_visibility(&variant.vis, None);
1182                     for field in variant.data.fields() {
1183                         self.invalid_visibility(&field.vis, None);
1184                     }
1185                 }
1186             }
1187             ItemKind::Trait(box Trait { is_auto, ref generics, ref bounds, ref items, .. }) => {
1188                 if is_auto == IsAuto::Yes {
1189                     // Auto traits cannot have generics, super traits nor contain items.
1190                     self.deny_generic_params(generics, item.ident.span);
1191                     self.deny_super_traits(bounds, item.ident.span);
1192                     self.deny_where_clause(&generics.where_clause, item.ident.span);
1193                     self.deny_items(items, item.ident.span);
1194                 }
1195                 self.no_questions_in_bounds(bounds, "supertraits", true);
1196
1197                 // Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
1198                 // context for the supertraits.
1199                 self.visit_vis(&item.vis);
1200                 self.visit_ident(item.ident);
1201                 self.visit_generics(generics);
1202                 self.with_banned_tilde_const(|this| walk_list!(this, visit_param_bound, bounds));
1203                 walk_list!(self, visit_assoc_item, items, AssocCtxt::Trait);
1204                 walk_list!(self, visit_attribute, &item.attrs);
1205                 return;
1206             }
1207             ItemKind::Mod(unsafety, ref mod_kind) => {
1208                 if let Unsafe::Yes(span) = unsafety {
1209                     self.err_handler().span_err(span, "module cannot be declared unsafe");
1210                 }
1211                 // Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
1212                 if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _))
1213                     && !self.session.contains_name(&item.attrs, sym::path)
1214                 {
1215                     self.check_mod_file_item_asciionly(item.ident);
1216                 }
1217             }
1218             ItemKind::Struct(ref vdata, ref generics) => match vdata {
1219                 // Duplicating the `Visitor` logic allows catching all cases
1220                 // of `Anonymous(Struct, Union)` outside of a field struct or union.
1221                 //
1222                 // Inside `visit_ty` the validator catches every `Anonymous(Struct, Union)` it
1223                 // encounters, and only on `ItemKind::Struct` and `ItemKind::Union`
1224                 // it uses `visit_ty_common`, which doesn't contain that specific check.
1225                 VariantData::Struct(ref fields, ..) => {
1226                     self.visit_vis(&item.vis);
1227                     self.visit_ident(item.ident);
1228                     self.visit_generics(generics);
1229                     self.with_banned_assoc_ty_bound(|this| {
1230                         walk_list!(this, visit_struct_field_def, fields);
1231                     });
1232                     walk_list!(self, visit_attribute, &item.attrs);
1233                     return;
1234                 }
1235                 _ => {}
1236             },
1237             ItemKind::Union(ref vdata, ref generics) => {
1238                 if vdata.fields().is_empty() {
1239                     self.err_handler().span_err(item.span, "unions cannot have zero fields");
1240                 }
1241                 match vdata {
1242                     VariantData::Struct(ref fields, ..) => {
1243                         self.visit_vis(&item.vis);
1244                         self.visit_ident(item.ident);
1245                         self.visit_generics(generics);
1246                         self.with_banned_assoc_ty_bound(|this| {
1247                             walk_list!(this, visit_struct_field_def, fields);
1248                         });
1249                         walk_list!(self, visit_attribute, &item.attrs);
1250                         return;
1251                     }
1252                     _ => {}
1253                 }
1254             }
1255             ItemKind::Const(def, .., None) => {
1256                 self.check_defaultness(item.span, def);
1257                 let msg = "free constant item without body";
1258                 self.error_item_without_body(item.span, "constant", msg, " = <expr>;");
1259             }
1260             ItemKind::Static(.., None) => {
1261                 let msg = "free static item without body";
1262                 self.error_item_without_body(item.span, "static", msg, " = <expr>;");
1263             }
1264             ItemKind::TyAlias(box TyAlias {
1265                 defaultness,
1266                 where_clauses,
1267                 ref bounds,
1268                 ref ty,
1269                 ..
1270             }) => {
1271                 self.check_defaultness(item.span, defaultness);
1272                 if ty.is_none() {
1273                     let msg = "free type alias without body";
1274                     self.error_item_without_body(item.span, "type", msg, " = <type>;");
1275                 }
1276                 self.check_type_no_bounds(bounds, "this context");
1277                 if where_clauses.1.0 {
1278                     let mut err = self.err_handler().struct_span_err(
1279                         where_clauses.1.1,
1280                         "where clauses are not allowed after the type for type aliases",
1281                     );
1282                     err.note(
1283                         "see issue #89122 <https://github.com/rust-lang/rust/issues/89122> for more information",
1284                     );
1285                     err.emit();
1286                 }
1287             }
1288             _ => {}
1289         }
1290
1291         visit::walk_item(self, item);
1292     }
1293
1294     fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
1295         match &fi.kind {
1296             ForeignItemKind::Fn(box Fn { defaultness, sig, body, .. }) => {
1297                 self.check_defaultness(fi.span, *defaultness);
1298                 self.check_foreign_fn_bodyless(fi.ident, body.as_deref());
1299                 self.check_foreign_fn_headerless(fi.ident, fi.span, sig.header);
1300                 self.check_foreign_item_ascii_only(fi.ident);
1301             }
1302             ForeignItemKind::TyAlias(box TyAlias {
1303                 defaultness,
1304                 generics,
1305                 where_clauses,
1306                 bounds,
1307                 ty,
1308                 ..
1309             }) => {
1310                 self.check_defaultness(fi.span, *defaultness);
1311                 self.check_foreign_kind_bodyless(fi.ident, "type", ty.as_ref().map(|b| b.span));
1312                 self.check_type_no_bounds(bounds, "`extern` blocks");
1313                 self.check_foreign_ty_genericless(generics, where_clauses.0.1);
1314                 self.check_foreign_item_ascii_only(fi.ident);
1315             }
1316             ForeignItemKind::Static(_, _, body) => {
1317                 self.check_foreign_kind_bodyless(fi.ident, "static", body.as_ref().map(|b| b.span));
1318                 self.check_foreign_item_ascii_only(fi.ident);
1319             }
1320             ForeignItemKind::MacCall(..) => {}
1321         }
1322
1323         visit::walk_foreign_item(self, fi)
1324     }
1325
1326     // Mirrors `visit::walk_generic_args`, but tracks relevant state.
1327     fn visit_generic_args(&mut self, _: Span, generic_args: &'a GenericArgs) {
1328         match *generic_args {
1329             GenericArgs::AngleBracketed(ref data) => {
1330                 self.check_generic_args_before_constraints(data);
1331
1332                 for arg in &data.args {
1333                     match arg {
1334                         AngleBracketedArg::Arg(arg) => self.visit_generic_arg(arg),
1335                         // Type bindings such as `Item = impl Debug` in `Iterator<Item = Debug>`
1336                         // are allowed to contain nested `impl Trait`.
1337                         AngleBracketedArg::Constraint(constraint) => {
1338                             self.with_impl_trait(None, |this| {
1339                                 this.visit_assoc_constraint_from_generic_args(constraint);
1340                             });
1341                         }
1342                     }
1343                 }
1344             }
1345             GenericArgs::Parenthesized(ref data) => {
1346                 walk_list!(self, visit_ty, &data.inputs);
1347                 if let FnRetTy::Ty(ty) = &data.output {
1348                     // `-> Foo` syntax is essentially an associated type binding,
1349                     // so it is also allowed to contain nested `impl Trait`.
1350                     self.with_impl_trait(None, |this| this.visit_ty(ty));
1351                 }
1352             }
1353         }
1354     }
1355
1356     fn visit_generics(&mut self, generics: &'a Generics) {
1357         let mut prev_param_default = None;
1358         for param in &generics.params {
1359             match param.kind {
1360                 GenericParamKind::Lifetime => (),
1361                 GenericParamKind::Type { default: Some(_), .. }
1362                 | GenericParamKind::Const { default: Some(_), .. } => {
1363                     prev_param_default = Some(param.ident.span);
1364                 }
1365                 GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
1366                     if let Some(span) = prev_param_default {
1367                         let mut err = self.err_handler().struct_span_err(
1368                             span,
1369                             "generic parameters with a default must be trailing",
1370                         );
1371                         err.emit();
1372                         break;
1373                     }
1374                 }
1375             }
1376         }
1377
1378         validate_generic_param_order(self.err_handler(), &generics.params, generics.span);
1379
1380         for predicate in &generics.where_clause.predicates {
1381             if let WherePredicate::EqPredicate(ref predicate) = *predicate {
1382                 deny_equality_constraints(self, predicate, generics);
1383             }
1384         }
1385         walk_list!(self, visit_generic_param, &generics.params);
1386         for predicate in &generics.where_clause.predicates {
1387             match predicate {
1388                 WherePredicate::BoundPredicate(bound_pred) => {
1389                     // A type binding, eg `for<'c> Foo: Send+Clone+'c`
1390                     self.check_late_bound_lifetime_defs(&bound_pred.bound_generic_params);
1391
1392                     // This is slightly complicated. Our representation for poly-trait-refs contains a single
1393                     // binder and thus we only allow a single level of quantification. However,
1394                     // the syntax of Rust permits quantification in two places in where clauses,
1395                     // e.g., `T: for <'a> Foo<'a>` and `for <'a, 'b> &'b T: Foo<'a>`. If both are
1396                     // defined, then error.
1397                     if !bound_pred.bound_generic_params.is_empty() {
1398                         for bound in &bound_pred.bounds {
1399                             match bound {
1400                                 GenericBound::Trait(t, _) => {
1401                                     if !t.bound_generic_params.is_empty() {
1402                                         struct_span_err!(
1403                                             self.err_handler(),
1404                                             t.span,
1405                                             E0316,
1406                                             "nested quantification of lifetimes"
1407                                         )
1408                                         .emit();
1409                                     }
1410                                 }
1411                                 GenericBound::Outlives(_) => {}
1412                             }
1413                         }
1414                     }
1415                 }
1416                 _ => {}
1417             }
1418             self.visit_where_predicate(predicate);
1419         }
1420     }
1421
1422     fn visit_generic_param(&mut self, param: &'a GenericParam) {
1423         if let GenericParamKind::Lifetime { .. } = param.kind {
1424             self.check_lifetime(param.ident);
1425         }
1426         visit::walk_generic_param(self, param);
1427     }
1428
1429     fn visit_param_bound(&mut self, bound: &'a GenericBound) {
1430         match bound {
1431             GenericBound::Trait(_, TraitBoundModifier::MaybeConst) => {
1432                 if !self.is_tilde_const_allowed {
1433                     self.err_handler()
1434                         .struct_span_err(bound.span(), "`~const` is not allowed here")
1435                         .note("only allowed on bounds on traits' associated types and functions, const fns, const impls and its associated functions")
1436                         .emit();
1437                 }
1438             }
1439
1440             GenericBound::Trait(_, TraitBoundModifier::MaybeConstMaybe) => {
1441                 self.err_handler()
1442                     .span_err(bound.span(), "`~const` and `?` are mutually exclusive");
1443             }
1444
1445             _ => {}
1446         }
1447
1448         visit::walk_param_bound(self, bound)
1449     }
1450
1451     fn visit_poly_trait_ref(&mut self, t: &'a PolyTraitRef, m: &'a TraitBoundModifier) {
1452         self.check_late_bound_lifetime_defs(&t.bound_generic_params);
1453         visit::walk_poly_trait_ref(self, t, m);
1454     }
1455
1456     fn visit_variant_data(&mut self, s: &'a VariantData) {
1457         self.with_banned_assoc_ty_bound(|this| visit::walk_struct_def(this, s))
1458     }
1459
1460     fn visit_enum_def(
1461         &mut self,
1462         enum_definition: &'a EnumDef,
1463         generics: &'a Generics,
1464         item_id: NodeId,
1465         _: Span,
1466     ) {
1467         self.with_banned_assoc_ty_bound(|this| {
1468             visit::walk_enum_def(this, enum_definition, generics, item_id)
1469         })
1470     }
1471
1472     fn visit_fn(&mut self, fk: FnKind<'a>, span: Span, id: NodeId) {
1473         // Only associated `fn`s can have `self` parameters.
1474         let self_semantic = match fk.ctxt() {
1475             Some(FnCtxt::Assoc(_)) => SelfSemantic::Yes,
1476             _ => SelfSemantic::No,
1477         };
1478         self.check_fn_decl(fk.decl(), self_semantic);
1479
1480         self.check_c_varadic_type(fk);
1481
1482         // Functions cannot both be `const async`
1483         if let Some(FnHeader {
1484             constness: Const::Yes(cspan),
1485             asyncness: Async::Yes { span: aspan, .. },
1486             ..
1487         }) = fk.header()
1488         {
1489             self.err_handler()
1490                 .struct_span_err(
1491                     vec![*cspan, *aspan],
1492                     "functions cannot be both `const` and `async`",
1493                 )
1494                 .span_label(*cspan, "`const` because of this")
1495                 .span_label(*aspan, "`async` because of this")
1496                 .span_label(span, "") // Point at the fn header.
1497                 .emit();
1498         }
1499
1500         if let FnKind::Fn(
1501             _,
1502             _,
1503             FnSig { span: sig_span, header: FnHeader { ext: Extern::Implicit, .. }, .. },
1504             _,
1505             _,
1506         ) = fk
1507         {
1508             self.maybe_lint_missing_abi(*sig_span, id);
1509         }
1510
1511         // Functions without bodies cannot have patterns.
1512         if let FnKind::Fn(ctxt, _, sig, _, None) = fk {
1513             Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
1514                 let (code, msg, label) = match ctxt {
1515                     FnCtxt::Foreign => (
1516                         error_code!(E0130),
1517                         "patterns aren't allowed in foreign function declarations",
1518                         "pattern not allowed in foreign function",
1519                     ),
1520                     _ => (
1521                         error_code!(E0642),
1522                         "patterns aren't allowed in functions without bodies",
1523                         "pattern not allowed in function without body",
1524                     ),
1525                 };
1526                 if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
1527                     if let Some(ident) = ident {
1528                         let diag = BuiltinLintDiagnostics::PatternsInFnsWithoutBody(span, ident);
1529                         self.lint_buffer.buffer_lint_with_diagnostic(
1530                             PATTERNS_IN_FNS_WITHOUT_BODY,
1531                             id,
1532                             span,
1533                             msg,
1534                             diag,
1535                         )
1536                     }
1537                 } else {
1538                     self.err_handler()
1539                         .struct_span_err(span, msg)
1540                         .span_label(span, label)
1541                         .code(code)
1542                         .emit();
1543                 }
1544             });
1545         }
1546
1547         visit::walk_fn(self, fk, span);
1548     }
1549
1550     fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
1551         if self.session.contains_name(&item.attrs, sym::no_mangle) {
1552             self.check_nomangle_item_asciionly(item.ident, item.span);
1553         }
1554
1555         if ctxt == AssocCtxt::Trait || !self.in_trait_impl {
1556             self.check_defaultness(item.span, item.kind.defaultness());
1557         }
1558
1559         if ctxt == AssocCtxt::Impl {
1560             match &item.kind {
1561                 AssocItemKind::Const(_, _, body) => {
1562                     self.check_impl_item_provided(item.span, body, "constant", " = <expr>;");
1563                 }
1564                 AssocItemKind::Fn(box Fn { body, .. }) => {
1565                     self.check_impl_item_provided(item.span, body, "function", " { <body> }");
1566                 }
1567                 AssocItemKind::TyAlias(box TyAlias {
1568                     generics,
1569                     where_clauses,
1570                     where_predicates_split,
1571                     bounds,
1572                     ty,
1573                     ..
1574                 }) => {
1575                     self.check_impl_item_provided(item.span, ty, "type", " = <type>;");
1576                     self.check_type_no_bounds(bounds, "`impl`s");
1577                     if ty.is_some() {
1578                         self.check_gat_where(
1579                             item.id,
1580                             generics.where_clause.predicates.split_at(*where_predicates_split).0,
1581                             *where_clauses,
1582                         );
1583                     }
1584                 }
1585                 _ => {}
1586             }
1587         }
1588
1589         if ctxt == AssocCtxt::Trait || self.in_trait_impl {
1590             self.invalid_visibility(&item.vis, None);
1591             if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
1592                 self.check_trait_fn_not_const(sig.header.constness);
1593                 self.check_trait_fn_not_async(item.span, sig.header.asyncness);
1594             }
1595         }
1596
1597         if let AssocItemKind::Const(..) = item.kind {
1598             self.check_item_named(item.ident, "const");
1599         }
1600
1601         match item.kind {
1602             AssocItemKind::TyAlias(box TyAlias { ref generics, ref bounds, ref ty, .. })
1603                 if ctxt == AssocCtxt::Trait =>
1604             {
1605                 self.visit_vis(&item.vis);
1606                 self.visit_ident(item.ident);
1607                 walk_list!(self, visit_attribute, &item.attrs);
1608                 self.with_tilde_const_allowed(|this| {
1609                     this.visit_generics(generics);
1610                     walk_list!(this, visit_param_bound, bounds);
1611                 });
1612                 walk_list!(self, visit_ty, ty);
1613             }
1614             AssocItemKind::Fn(box Fn { ref sig, ref generics, ref body, .. })
1615                 if self.in_const_trait_impl
1616                     || ctxt == AssocCtxt::Trait
1617                     || matches!(sig.header.constness, Const::Yes(_)) =>
1618             {
1619                 self.visit_vis(&item.vis);
1620                 self.visit_ident(item.ident);
1621                 self.with_tilde_const_allowed(|this| this.visit_generics(generics));
1622                 let kind =
1623                     FnKind::Fn(FnCtxt::Assoc(ctxt), item.ident, sig, &item.vis, body.as_deref());
1624                 self.visit_fn(kind, item.span, item.id);
1625             }
1626             _ => self
1627                 .with_in_trait_impl(false, None, |this| visit::walk_assoc_item(this, item, ctxt)),
1628         }
1629     }
1630 }
1631
1632 /// When encountering an equality constraint in a `where` clause, emit an error. If the code seems
1633 /// like it's setting an associated type, provide an appropriate suggestion.
1634 fn deny_equality_constraints(
1635     this: &mut AstValidator<'_>,
1636     predicate: &WhereEqPredicate,
1637     generics: &Generics,
1638 ) {
1639     let mut err = this.err_handler().struct_span_err(
1640         predicate.span,
1641         "equality constraints are not yet supported in `where` clauses",
1642     );
1643     err.span_label(predicate.span, "not supported");
1644
1645     // Given `<A as Foo>::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
1646     if let TyKind::Path(Some(qself), full_path) = &predicate.lhs_ty.kind {
1647         if let TyKind::Path(None, path) = &qself.ty.kind {
1648             match &path.segments[..] {
1649                 [PathSegment { ident, args: None, .. }] => {
1650                     for param in &generics.params {
1651                         if param.ident == *ident {
1652                             let param = ident;
1653                             match &full_path.segments[qself.position..] {
1654                                 [PathSegment { ident, args, .. }] => {
1655                                     // Make a new `Path` from `foo::Bar` to `Foo<Bar = RhsTy>`.
1656                                     let mut assoc_path = full_path.clone();
1657                                     // Remove `Bar` from `Foo::Bar`.
1658                                     assoc_path.segments.pop();
1659                                     let len = assoc_path.segments.len() - 1;
1660                                     let gen_args = args.as_ref().map(|p| (**p).clone());
1661                                     // Build `<Bar = RhsTy>`.
1662                                     let arg = AngleBracketedArg::Constraint(AssocConstraint {
1663                                         id: rustc_ast::node_id::DUMMY_NODE_ID,
1664                                         ident: *ident,
1665                                         gen_args,
1666                                         kind: AssocConstraintKind::Equality {
1667                                             term: predicate.rhs_ty.clone().into(),
1668                                         },
1669                                         span: ident.span,
1670                                     });
1671                                     // Add `<Bar = RhsTy>` to `Foo`.
1672                                     match &mut assoc_path.segments[len].args {
1673                                         Some(args) => match args.deref_mut() {
1674                                             GenericArgs::Parenthesized(_) => continue,
1675                                             GenericArgs::AngleBracketed(args) => {
1676                                                 args.args.push(arg);
1677                                             }
1678                                         },
1679                                         empty_args => {
1680                                             *empty_args = AngleBracketedArgs {
1681                                                 span: ident.span,
1682                                                 args: vec![arg],
1683                                             }
1684                                             .into();
1685                                         }
1686                                     }
1687                                     err.span_suggestion_verbose(
1688                                         predicate.span,
1689                                         &format!(
1690                                             "if `{}` is an associated type you're trying to set, \
1691                                             use the associated type binding syntax",
1692                                             ident
1693                                         ),
1694                                         format!(
1695                                             "{}: {}",
1696                                             param,
1697                                             pprust::path_to_string(&assoc_path)
1698                                         ),
1699                                         Applicability::MaybeIncorrect,
1700                                     );
1701                                 }
1702                                 _ => {}
1703                             };
1704                         }
1705                     }
1706                 }
1707                 _ => {}
1708             }
1709         }
1710     }
1711     // Given `A: Foo, A::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
1712     if let TyKind::Path(None, full_path) = &predicate.lhs_ty.kind {
1713         if let [potential_param, potential_assoc] = &full_path.segments[..] {
1714             for param in &generics.params {
1715                 if param.ident == potential_param.ident {
1716                     for bound in &param.bounds {
1717                         if let ast::GenericBound::Trait(trait_ref, TraitBoundModifier::None) = bound
1718                         {
1719                             if let [trait_segment] = &trait_ref.trait_ref.path.segments[..] {
1720                                 let assoc = pprust::path_to_string(&ast::Path::from_ident(
1721                                     potential_assoc.ident,
1722                                 ));
1723                                 let ty = pprust::ty_to_string(&predicate.rhs_ty);
1724                                 let (args, span) = match &trait_segment.args {
1725                                     Some(args) => match args.deref() {
1726                                         ast::GenericArgs::AngleBracketed(args) => {
1727                                             let Some(arg) = args.args.last() else {
1728                                                 continue;
1729                                             };
1730                                             (
1731                                                 format!(", {} = {}", assoc, ty),
1732                                                 arg.span().shrink_to_hi(),
1733                                             )
1734                                         }
1735                                         _ => continue,
1736                                     },
1737                                     None => (
1738                                         format!("<{} = {}>", assoc, ty),
1739                                         trait_segment.span().shrink_to_hi(),
1740                                     ),
1741                                 };
1742                                 err.multipart_suggestion(
1743                                     &format!(
1744                                         "if `{}::{}` is an associated type you're trying to set, \
1745                                         use the associated type binding syntax",
1746                                         trait_segment.ident, potential_assoc.ident,
1747                                     ),
1748                                     vec![(span, args), (predicate.span, String::new())],
1749                                     Applicability::MaybeIncorrect,
1750                                 );
1751                             }
1752                         }
1753                     }
1754                 }
1755             }
1756         }
1757     }
1758     err.note(
1759         "see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information",
1760     );
1761     err.emit();
1762 }
1763
1764 pub fn check_crate(session: &Session, krate: &Crate, lints: &mut LintBuffer) -> bool {
1765     let mut validator = AstValidator {
1766         session,
1767         extern_mod: None,
1768         in_trait_impl: false,
1769         in_const_trait_impl: false,
1770         has_proc_macro_decls: false,
1771         outer_impl_trait: None,
1772         is_tilde_const_allowed: false,
1773         is_impl_trait_banned: false,
1774         is_assoc_ty_bound_banned: false,
1775         is_let_allowed: false,
1776         lint_buffer: lints,
1777     };
1778     visit::walk_crate(&mut validator, krate);
1779
1780     validator.has_proc_macro_decls
1781 }