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