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