rustc_ast_passes/
ast_validation.rs

1//! Validate AST before lowering it to HIR.
2//!
3//! This pass intends to check that the constructed AST is *syntactically valid* to allow the rest
4//! of the compiler to assume that the AST is valid. These checks cannot be performed during parsing
5//! because attribute macros are allowed to accept certain pieces of invalid syntax such as a
6//! function without body outside of a trait definition:
7//!
8//! ```ignore (illustrative)
9//! #[my_attribute]
10//! mod foo {
11//!     fn missing_body();
12//! }
13//! ```
14//!
15//! These checks are run post-expansion, after AST is frozen, to be able to check for erroneous
16//! constructions produced by proc macros. This pass is only intended for simple checks that do not
17//! require name resolution or type checking, or other kinds of complex analysis.
18
19use std::mem;
20use std::ops::{Deref, DerefMut};
21
22use itertools::{Either, Itertools};
23use rustc_abi::ExternAbi;
24use rustc_ast::ptr::P;
25use rustc_ast::visit::{AssocCtxt, BoundKind, FnCtxt, FnKind, Visitor, walk_list};
26use rustc_ast::*;
27use rustc_ast_pretty::pprust::{self, State};
28use rustc_data_structures::fx::FxIndexMap;
29use rustc_errors::DiagCtxtHandle;
30use rustc_feature::Features;
31use rustc_parse::validate_attr;
32use rustc_session::Session;
33use rustc_session::lint::builtin::{
34    DEPRECATED_WHERE_CLAUSE_LOCATION, MISSING_ABI, MISSING_UNSAFE_ON_EXTERN,
35    PATTERNS_IN_FNS_WITHOUT_BODY,
36};
37use rustc_session::lint::{BuiltinLintDiag, LintBuffer};
38use rustc_span::{Ident, Span, kw, sym};
39use thin_vec::thin_vec;
40
41use crate::errors::{self, TildeConstReason};
42
43/// Is `self` allowed semantically as the first parameter in an `FnDecl`?
44enum SelfSemantic {
45    Yes,
46    No,
47}
48
49enum TraitOrTraitImpl {
50    Trait { span: Span, constness_span: Option<Span> },
51    TraitImpl { constness: Const, polarity: ImplPolarity, trait_ref_span: Span },
52}
53
54impl TraitOrTraitImpl {
55    fn constness(&self) -> Option<Span> {
56        match self {
57            Self::Trait { constness_span: Some(span), .. }
58            | Self::TraitImpl { constness: Const::Yes(span), .. } => Some(*span),
59            _ => None,
60        }
61    }
62}
63
64struct AstValidator<'a> {
65    sess: &'a Session,
66    features: &'a Features,
67
68    /// The span of the `extern` in an `extern { ... }` block, if any.
69    extern_mod_span: Option<Span>,
70
71    outer_trait_or_trait_impl: Option<TraitOrTraitImpl>,
72
73    has_proc_macro_decls: bool,
74
75    /// Used to ban nested `impl Trait`, e.g., `impl Into<impl Debug>`.
76    /// Nested `impl Trait` _is_ allowed in associated type position,
77    /// e.g., `impl Iterator<Item = impl Debug>`.
78    outer_impl_trait_span: Option<Span>,
79
80    disallow_tilde_const: Option<TildeConstReason>,
81
82    /// Used to ban explicit safety on foreign items when the extern block is not marked as unsafe.
83    extern_mod_safety: Option<Safety>,
84
85    lint_node_id: NodeId,
86
87    is_sdylib_interface: bool,
88
89    lint_buffer: &'a mut LintBuffer,
90}
91
92impl<'a> AstValidator<'a> {
93    fn with_in_trait_impl(
94        &mut self,
95        trait_: Option<(Const, ImplPolarity, &'a TraitRef)>,
96        f: impl FnOnce(&mut Self),
97    ) {
98        let old = mem::replace(
99            &mut self.outer_trait_or_trait_impl,
100            trait_.map(|(constness, polarity, trait_ref)| TraitOrTraitImpl::TraitImpl {
101                constness,
102                polarity,
103                trait_ref_span: trait_ref.path.span,
104            }),
105        );
106        f(self);
107        self.outer_trait_or_trait_impl = old;
108    }
109
110    fn with_in_trait(
111        &mut self,
112        span: Span,
113        constness_span: Option<Span>,
114        f: impl FnOnce(&mut Self),
115    ) {
116        let old = mem::replace(
117            &mut self.outer_trait_or_trait_impl,
118            Some(TraitOrTraitImpl::Trait { span, constness_span }),
119        );
120        f(self);
121        self.outer_trait_or_trait_impl = old;
122    }
123
124    fn with_in_extern_mod(&mut self, extern_mod_safety: Safety, f: impl FnOnce(&mut Self)) {
125        let old = mem::replace(&mut self.extern_mod_safety, Some(extern_mod_safety));
126        f(self);
127        self.extern_mod_safety = old;
128    }
129
130    fn with_tilde_const(
131        &mut self,
132        disallowed: Option<TildeConstReason>,
133        f: impl FnOnce(&mut Self),
134    ) {
135        let old = mem::replace(&mut self.disallow_tilde_const, disallowed);
136        f(self);
137        self.disallow_tilde_const = old;
138    }
139
140    fn check_type_alias_where_clause_location(
141        &mut self,
142        ty_alias: &TyAlias,
143    ) -> Result<(), errors::WhereClauseBeforeTypeAlias> {
144        if ty_alias.ty.is_none() || !ty_alias.where_clauses.before.has_where_token {
145            return Ok(());
146        }
147
148        let (before_predicates, after_predicates) =
149            ty_alias.generics.where_clause.predicates.split_at(ty_alias.where_clauses.split);
150        let span = ty_alias.where_clauses.before.span;
151
152        let sugg = if !before_predicates.is_empty() || !ty_alias.where_clauses.after.has_where_token
153        {
154            let mut state = State::new();
155
156            if !ty_alias.where_clauses.after.has_where_token {
157                state.space();
158                state.word_space("where");
159            }
160
161            let mut first = after_predicates.is_empty();
162            for p in before_predicates {
163                if !first {
164                    state.word_space(",");
165                }
166                first = false;
167                state.print_where_predicate(p);
168            }
169
170            errors::WhereClauseBeforeTypeAliasSugg::Move {
171                left: span,
172                snippet: state.s.eof(),
173                right: ty_alias.where_clauses.after.span.shrink_to_hi(),
174            }
175        } else {
176            errors::WhereClauseBeforeTypeAliasSugg::Remove { span }
177        };
178
179        Err(errors::WhereClauseBeforeTypeAlias { span, sugg })
180    }
181
182    fn with_impl_trait(&mut self, outer_span: Option<Span>, f: impl FnOnce(&mut Self)) {
183        let old = mem::replace(&mut self.outer_impl_trait_span, outer_span);
184        f(self);
185        self.outer_impl_trait_span = old;
186    }
187
188    // Mirrors `visit::walk_ty`, but tracks relevant state.
189    fn walk_ty(&mut self, t: &'a Ty) {
190        match &t.kind {
191            TyKind::ImplTrait(_, bounds) => {
192                self.with_impl_trait(Some(t.span), |this| visit::walk_ty(this, t));
193
194                // FIXME(precise_capturing): If we were to allow `use` in other positions
195                // (e.g. GATs), then we must validate those as well. However, we don't have
196                // a good way of doing this with the current `Visitor` structure.
197                let mut use_bounds = bounds
198                    .iter()
199                    .filter_map(|bound| match bound {
200                        GenericBound::Use(_, span) => Some(span),
201                        _ => None,
202                    })
203                    .copied();
204                if let Some(bound1) = use_bounds.next()
205                    && let Some(bound2) = use_bounds.next()
206                {
207                    self.dcx().emit_err(errors::DuplicatePreciseCapturing { bound1, bound2 });
208                }
209            }
210            TyKind::TraitObject(..) => self
211                .with_tilde_const(Some(TildeConstReason::TraitObject), |this| {
212                    visit::walk_ty(this, t)
213                }),
214            _ => visit::walk_ty(self, t),
215        }
216    }
217
218    fn visit_struct_field_def(&mut self, field: &'a FieldDef) {
219        if let Some(ref ident) = field.ident
220            && ident.name == kw::Underscore
221        {
222            self.visit_vis(&field.vis);
223            self.visit_ident(ident);
224            self.visit_ty_common(&field.ty);
225            self.walk_ty(&field.ty);
226            walk_list!(self, visit_attribute, &field.attrs);
227        } else {
228            self.visit_field_def(field);
229        }
230    }
231
232    fn dcx(&self) -> DiagCtxtHandle<'a> {
233        self.sess.dcx()
234    }
235
236    fn visibility_not_permitted(&self, vis: &Visibility, note: errors::VisibilityNotPermittedNote) {
237        if let VisibilityKind::Inherited = vis.kind {
238            return;
239        }
240
241        self.dcx().emit_err(errors::VisibilityNotPermitted {
242            span: vis.span,
243            note,
244            remove_qualifier_sugg: vis.span,
245        });
246    }
247
248    fn check_decl_no_pat(decl: &FnDecl, mut report_err: impl FnMut(Span, Option<Ident>, bool)) {
249        for Param { pat, .. } in &decl.inputs {
250            match pat.kind {
251                PatKind::Missing | PatKind::Ident(BindingMode::NONE, _, None) | PatKind::Wild => {}
252                PatKind::Ident(BindingMode::MUT, ident, None) => {
253                    report_err(pat.span, Some(ident), true)
254                }
255                _ => report_err(pat.span, None, false),
256            }
257        }
258    }
259
260    fn check_trait_fn_not_const(&self, constness: Const, parent: &TraitOrTraitImpl) {
261        let Const::Yes(span) = constness else {
262            return;
263        };
264
265        let const_trait_impl = self.features.const_trait_impl();
266        let make_impl_const_sugg = if const_trait_impl
267            && let TraitOrTraitImpl::TraitImpl {
268                constness: Const::No,
269                polarity: ImplPolarity::Positive,
270                trait_ref_span,
271                ..
272            } = parent
273        {
274            Some(trait_ref_span.shrink_to_lo())
275        } else {
276            None
277        };
278
279        let make_trait_const_sugg = if const_trait_impl
280            && let TraitOrTraitImpl::Trait { span, constness_span: None } = parent
281        {
282            Some(span.shrink_to_lo())
283        } else {
284            None
285        };
286
287        let parent_constness = parent.constness();
288        self.dcx().emit_err(errors::TraitFnConst {
289            span,
290            in_impl: matches!(parent, TraitOrTraitImpl::TraitImpl { .. }),
291            const_context_label: parent_constness,
292            remove_const_sugg: (
293                self.sess.source_map().span_extend_while_whitespace(span),
294                match parent_constness {
295                    Some(_) => rustc_errors::Applicability::MachineApplicable,
296                    None => rustc_errors::Applicability::MaybeIncorrect,
297                },
298            ),
299            requires_multiple_changes: make_impl_const_sugg.is_some()
300                || make_trait_const_sugg.is_some(),
301            make_impl_const_sugg,
302            make_trait_const_sugg,
303        });
304    }
305
306    fn check_fn_decl(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
307        self.check_decl_num_args(fn_decl);
308        self.check_decl_cvariadic_pos(fn_decl);
309        self.check_decl_attrs(fn_decl);
310        self.check_decl_self_param(fn_decl, self_semantic);
311    }
312
313    /// Emits fatal error if function declaration has more than `u16::MAX` arguments
314    /// Error is fatal to prevent errors during typechecking
315    fn check_decl_num_args(&self, fn_decl: &FnDecl) {
316        let max_num_args: usize = u16::MAX.into();
317        if fn_decl.inputs.len() > max_num_args {
318            let Param { span, .. } = fn_decl.inputs[0];
319            self.dcx().emit_fatal(errors::FnParamTooMany { span, max_num_args });
320        }
321    }
322
323    /// Emits an error if a function declaration has a variadic parameter in the
324    /// beginning or middle of parameter list.
325    /// Example: `fn foo(..., x: i32)` will emit an error.
326    fn check_decl_cvariadic_pos(&self, fn_decl: &FnDecl) {
327        match &*fn_decl.inputs {
328            [ps @ .., _] => {
329                for Param { ty, span, .. } in ps {
330                    if let TyKind::CVarArgs = ty.kind {
331                        self.dcx().emit_err(errors::FnParamCVarArgsNotLast { span: *span });
332                    }
333                }
334            }
335            _ => {}
336        }
337    }
338
339    fn check_decl_attrs(&self, fn_decl: &FnDecl) {
340        fn_decl
341            .inputs
342            .iter()
343            .flat_map(|i| i.attrs.as_ref())
344            .filter(|attr| {
345                let arr = [
346                    sym::allow,
347                    sym::cfg_trace,
348                    sym::cfg_attr_trace,
349                    sym::deny,
350                    sym::expect,
351                    sym::forbid,
352                    sym::warn,
353                ];
354                !attr.has_any_name(&arr) && rustc_attr_parsing::is_builtin_attr(*attr)
355            })
356            .for_each(|attr| {
357                if attr.is_doc_comment() {
358                    self.dcx().emit_err(errors::FnParamDocComment { span: attr.span });
359                } else {
360                    self.dcx().emit_err(errors::FnParamForbiddenAttr { span: attr.span });
361                }
362            });
363    }
364
365    fn check_decl_self_param(&self, fn_decl: &FnDecl, self_semantic: SelfSemantic) {
366        if let (SelfSemantic::No, [param, ..]) = (self_semantic, &*fn_decl.inputs) {
367            if param.is_self() {
368                self.dcx().emit_err(errors::FnParamForbiddenSelf { span: param.span });
369            }
370        }
371    }
372
373    /// This ensures that items can only be `unsafe` (or unmarked) outside of extern
374    /// blocks.
375    ///
376    /// This additionally ensures that within extern blocks, items can only be
377    /// `safe`/`unsafe` inside of a `unsafe`-adorned extern block.
378    fn check_item_safety(&self, span: Span, safety: Safety) {
379        match self.extern_mod_safety {
380            Some(extern_safety) => {
381                if matches!(safety, Safety::Unsafe(_) | Safety::Safe(_))
382                    && extern_safety == Safety::Default
383                {
384                    self.dcx().emit_err(errors::InvalidSafetyOnExtern {
385                        item_span: span,
386                        block: Some(self.current_extern_span().shrink_to_lo()),
387                    });
388                }
389            }
390            None => {
391                if matches!(safety, Safety::Safe(_)) {
392                    self.dcx().emit_err(errors::InvalidSafetyOnItem { span });
393                }
394            }
395        }
396    }
397
398    fn check_bare_fn_safety(&self, span: Span, safety: Safety) {
399        if matches!(safety, Safety::Safe(_)) {
400            self.dcx().emit_err(errors::InvalidSafetyOnBareFn { span });
401        }
402    }
403
404    fn check_defaultness(&self, span: Span, defaultness: Defaultness) {
405        if let Defaultness::Default(def_span) = defaultness {
406            let span = self.sess.source_map().guess_head_span(span);
407            self.dcx().emit_err(errors::ForbiddenDefault { span, def_span });
408        }
409    }
410
411    /// If `sp` ends with a semicolon, returns it as a `Span`
412    /// Otherwise, returns `sp.shrink_to_hi()`
413    fn ending_semi_or_hi(&self, sp: Span) -> Span {
414        let source_map = self.sess.source_map();
415        let end = source_map.end_point(sp);
416
417        if source_map.span_to_snippet(end).is_ok_and(|s| s == ";") {
418            end
419        } else {
420            sp.shrink_to_hi()
421        }
422    }
423
424    fn check_type_no_bounds(&self, bounds: &[GenericBound], ctx: &str) {
425        let span = match bounds {
426            [] => return,
427            [b0] => b0.span(),
428            [b0, .., bl] => b0.span().to(bl.span()),
429        };
430        self.dcx().emit_err(errors::BoundInContext { span, ctx });
431    }
432
433    fn check_foreign_ty_genericless(
434        &self,
435        generics: &Generics,
436        where_clauses: &TyAliasWhereClauses,
437    ) {
438        let cannot_have = |span, descr, remove_descr| {
439            self.dcx().emit_err(errors::ExternTypesCannotHave {
440                span,
441                descr,
442                remove_descr,
443                block_span: self.current_extern_span(),
444            });
445        };
446
447        if !generics.params.is_empty() {
448            cannot_have(generics.span, "generic parameters", "generic parameters");
449        }
450
451        let check_where_clause = |where_clause: TyAliasWhereClause| {
452            if where_clause.has_where_token {
453                cannot_have(where_clause.span, "`where` clauses", "`where` clause");
454            }
455        };
456
457        check_where_clause(where_clauses.before);
458        check_where_clause(where_clauses.after);
459    }
460
461    fn check_foreign_kind_bodyless(&self, ident: Ident, kind: &str, body_span: Option<Span>) {
462        let Some(body_span) = body_span else {
463            return;
464        };
465        self.dcx().emit_err(errors::BodyInExtern {
466            span: ident.span,
467            body: body_span,
468            block: self.current_extern_span(),
469            kind,
470        });
471    }
472
473    /// An `fn` in `extern { ... }` cannot have a body `{ ... }`.
474    fn check_foreign_fn_bodyless(&self, ident: Ident, body: Option<&Block>) {
475        let Some(body) = body else {
476            return;
477        };
478        self.dcx().emit_err(errors::FnBodyInExtern {
479            span: ident.span,
480            body: body.span,
481            block: self.current_extern_span(),
482        });
483    }
484
485    fn current_extern_span(&self) -> Span {
486        self.sess.source_map().guess_head_span(self.extern_mod_span.unwrap())
487    }
488
489    /// An `fn` in `extern { ... }` cannot have qualifiers, e.g. `async fn`.
490    fn check_foreign_fn_headerless(
491        &self,
492        // Deconstruct to ensure exhaustiveness
493        FnHeader { safety: _, coroutine_kind, constness, ext }: FnHeader,
494    ) {
495        let report_err = |span, kw| {
496            self.dcx().emit_err(errors::FnQualifierInExtern {
497                span,
498                kw,
499                block: self.current_extern_span(),
500            });
501        };
502        match coroutine_kind {
503            Some(kind) => report_err(kind.span(), kind.as_str()),
504            None => (),
505        }
506        match constness {
507            Const::Yes(span) => report_err(span, "const"),
508            Const::No => (),
509        }
510        match ext {
511            Extern::None => (),
512            Extern::Implicit(span) | Extern::Explicit(_, span) => report_err(span, "extern"),
513        }
514    }
515
516    /// An item in `extern { ... }` cannot use non-ascii identifier.
517    fn check_foreign_item_ascii_only(&self, ident: Ident) {
518        if !ident.as_str().is_ascii() {
519            self.dcx().emit_err(errors::ExternItemAscii {
520                span: ident.span,
521                block: self.current_extern_span(),
522            });
523        }
524    }
525
526    /// Reject invalid C-variadic types.
527    ///
528    /// C-variadics must be:
529    /// - Non-const
530    /// - Either foreign, or free and `unsafe extern "C"` semantically
531    fn check_c_variadic_type(&self, fk: FnKind<'a>) {
532        let variadic_spans: Vec<_> = fk
533            .decl()
534            .inputs
535            .iter()
536            .filter(|arg| matches!(arg.ty.kind, TyKind::CVarArgs))
537            .map(|arg| arg.span)
538            .collect();
539
540        if variadic_spans.is_empty() {
541            return;
542        }
543
544        if let Some(header) = fk.header() {
545            if let Const::Yes(const_span) = header.constness {
546                let mut spans = variadic_spans.clone();
547                spans.push(const_span);
548                self.dcx().emit_err(errors::ConstAndCVariadic {
549                    spans,
550                    const_span,
551                    variadic_spans: variadic_spans.clone(),
552                });
553            }
554        }
555
556        match (fk.ctxt(), fk.header()) {
557            (Some(FnCtxt::Foreign), _) => return,
558            (Some(FnCtxt::Free), Some(header)) => match header.ext {
559                Extern::Explicit(StrLit { symbol_unescaped: sym::C, .. }, _)
560                | Extern::Explicit(StrLit { symbol_unescaped: sym::C_dash_unwind, .. }, _)
561                | Extern::Implicit(_)
562                    if matches!(header.safety, Safety::Unsafe(_)) =>
563                {
564                    return;
565                }
566                _ => {}
567            },
568            _ => {}
569        };
570
571        self.dcx().emit_err(errors::BadCVariadic { span: variadic_spans });
572    }
573
574    fn check_item_named(&self, ident: Ident, kind: &str) {
575        if ident.name != kw::Underscore {
576            return;
577        }
578        self.dcx().emit_err(errors::ItemUnderscore { span: ident.span, kind });
579    }
580
581    fn check_nomangle_item_asciionly(&self, ident: Ident, item_span: Span) {
582        if ident.name.as_str().is_ascii() {
583            return;
584        }
585        let span = self.sess.source_map().guess_head_span(item_span);
586        self.dcx().emit_err(errors::NoMangleAscii { span });
587    }
588
589    fn check_mod_file_item_asciionly(&self, ident: Ident) {
590        if ident.name.as_str().is_ascii() {
591            return;
592        }
593        self.dcx().emit_err(errors::ModuleNonAscii { span: ident.span, name: ident.name });
594    }
595
596    fn deny_generic_params(&self, generics: &Generics, ident_span: Span) {
597        if !generics.params.is_empty() {
598            self.dcx()
599                .emit_err(errors::AutoTraitGeneric { span: generics.span, ident: ident_span });
600        }
601    }
602
603    fn deny_super_traits(&self, bounds: &GenericBounds, ident_span: Span) {
604        if let [.., last] = &bounds[..] {
605            let span = ident_span.shrink_to_hi().to(last.span());
606            self.dcx().emit_err(errors::AutoTraitBounds { span, ident: ident_span });
607        }
608    }
609
610    fn deny_where_clause(&self, where_clause: &WhereClause, ident_span: Span) {
611        if !where_clause.predicates.is_empty() {
612            // FIXME: The current diagnostic is misleading since it only talks about
613            // super trait and lifetime bounds while we should just say “bounds”.
614            self.dcx()
615                .emit_err(errors::AutoTraitBounds { span: where_clause.span, ident: ident_span });
616        }
617    }
618
619    fn deny_items(&self, trait_items: &[P<AssocItem>], ident_span: Span) {
620        if !trait_items.is_empty() {
621            let spans: Vec<_> = trait_items.iter().map(|i| i.kind.ident().unwrap().span).collect();
622            let total = trait_items.first().unwrap().span.to(trait_items.last().unwrap().span);
623            self.dcx().emit_err(errors::AutoTraitItems { spans, total, ident: ident_span });
624        }
625    }
626
627    fn correct_generic_order_suggestion(&self, data: &AngleBracketedArgs) -> String {
628        // Lifetimes always come first.
629        let lt_sugg = data.args.iter().filter_map(|arg| match arg {
630            AngleBracketedArg::Arg(lt @ GenericArg::Lifetime(_)) => {
631                Some(pprust::to_string(|s| s.print_generic_arg(lt)))
632            }
633            _ => None,
634        });
635        let args_sugg = data.args.iter().filter_map(|a| match a {
636            AngleBracketedArg::Arg(GenericArg::Lifetime(_)) | AngleBracketedArg::Constraint(_) => {
637                None
638            }
639            AngleBracketedArg::Arg(arg) => Some(pprust::to_string(|s| s.print_generic_arg(arg))),
640        });
641        // Constraints always come last.
642        let constraint_sugg = data.args.iter().filter_map(|a| match a {
643            AngleBracketedArg::Arg(_) => None,
644            AngleBracketedArg::Constraint(c) => {
645                Some(pprust::to_string(|s| s.print_assoc_item_constraint(c)))
646            }
647        });
648        format!(
649            "<{}>",
650            lt_sugg.chain(args_sugg).chain(constraint_sugg).collect::<Vec<String>>().join(", ")
651        )
652    }
653
654    /// Enforce generic args coming before constraints in `<...>` of a path segment.
655    fn check_generic_args_before_constraints(&self, data: &AngleBracketedArgs) {
656        // Early exit in case it's partitioned as it should be.
657        if data.args.iter().is_partitioned(|arg| matches!(arg, AngleBracketedArg::Arg(_))) {
658            return;
659        }
660        // Find all generic argument coming after the first constraint...
661        let (constraint_spans, arg_spans): (Vec<Span>, Vec<Span>) =
662            data.args.iter().partition_map(|arg| match arg {
663                AngleBracketedArg::Constraint(c) => Either::Left(c.span),
664                AngleBracketedArg::Arg(a) => Either::Right(a.span()),
665            });
666        let args_len = arg_spans.len();
667        let constraint_len = constraint_spans.len();
668        // ...and then error:
669        self.dcx().emit_err(errors::ArgsBeforeConstraint {
670            arg_spans: arg_spans.clone(),
671            constraints: constraint_spans[0],
672            args: *arg_spans.iter().last().unwrap(),
673            data: data.span,
674            constraint_spans: errors::EmptyLabelManySpans(constraint_spans),
675            arg_spans2: errors::EmptyLabelManySpans(arg_spans),
676            suggestion: self.correct_generic_order_suggestion(data),
677            constraint_len,
678            args_len,
679        });
680    }
681
682    fn visit_ty_common(&mut self, ty: &'a Ty) {
683        match &ty.kind {
684            TyKind::BareFn(bfty) => {
685                self.check_bare_fn_safety(bfty.decl_span, bfty.safety);
686                self.check_fn_decl(&bfty.decl, SelfSemantic::No);
687                Self::check_decl_no_pat(&bfty.decl, |span, _, _| {
688                    self.dcx().emit_err(errors::PatternFnPointer { span });
689                });
690                if let Extern::Implicit(extern_span) = bfty.ext {
691                    self.handle_missing_abi(extern_span, ty.id);
692                }
693            }
694            TyKind::TraitObject(bounds, ..) => {
695                let mut any_lifetime_bounds = false;
696                for bound in bounds {
697                    if let GenericBound::Outlives(lifetime) = bound {
698                        if any_lifetime_bounds {
699                            self.dcx()
700                                .emit_err(errors::TraitObjectBound { span: lifetime.ident.span });
701                            break;
702                        }
703                        any_lifetime_bounds = true;
704                    }
705                }
706            }
707            TyKind::ImplTrait(_, bounds) => {
708                if let Some(outer_impl_trait_sp) = self.outer_impl_trait_span {
709                    self.dcx().emit_err(errors::NestedImplTrait {
710                        span: ty.span,
711                        outer: outer_impl_trait_sp,
712                        inner: ty.span,
713                    });
714                }
715
716                if !bounds.iter().any(|b| matches!(b, GenericBound::Trait(..))) {
717                    self.dcx().emit_err(errors::AtLeastOneTrait { span: ty.span });
718                }
719            }
720            _ => {}
721        }
722    }
723
724    fn handle_missing_abi(&mut self, span: Span, id: NodeId) {
725        // FIXME(davidtwco): This is a hack to detect macros which produce spans of the
726        // call site which do not have a macro backtrace. See #61963.
727        if span.edition().at_least_edition_future() && self.features.explicit_extern_abis() {
728            self.dcx().emit_err(errors::MissingAbi { span });
729        } else if self
730            .sess
731            .source_map()
732            .span_to_snippet(span)
733            .is_ok_and(|snippet| !snippet.starts_with("#["))
734        {
735            self.lint_buffer.buffer_lint(
736                MISSING_ABI,
737                id,
738                span,
739                BuiltinLintDiag::MissingAbi(span, ExternAbi::FALLBACK),
740            )
741        }
742    }
743
744    // Used within `visit_item` for item kinds where we don't call `visit::walk_item`.
745    fn visit_attrs_vis(&mut self, attrs: &'a AttrVec, vis: &'a Visibility) {
746        walk_list!(self, visit_attribute, attrs);
747        self.visit_vis(vis);
748    }
749
750    // Used within `visit_item` for item kinds where we don't call `visit::walk_item`.
751    fn visit_attrs_vis_ident(&mut self, attrs: &'a AttrVec, vis: &'a Visibility, ident: &'a Ident) {
752        walk_list!(self, visit_attribute, attrs);
753        self.visit_vis(vis);
754        self.visit_ident(ident);
755    }
756}
757
758/// Checks that generic parameters are in the correct order,
759/// which is lifetimes, then types and then consts. (`<'a, T, const N: usize>`)
760fn validate_generic_param_order(dcx: DiagCtxtHandle<'_>, generics: &[GenericParam], span: Span) {
761    let mut max_param: Option<ParamKindOrd> = None;
762    let mut out_of_order = FxIndexMap::default();
763    let mut param_idents = Vec::with_capacity(generics.len());
764
765    for (idx, param) in generics.iter().enumerate() {
766        let ident = param.ident;
767        let (kind, bounds, span) = (&param.kind, &param.bounds, ident.span);
768        let (ord_kind, ident) = match &param.kind {
769            GenericParamKind::Lifetime => (ParamKindOrd::Lifetime, ident.to_string()),
770            GenericParamKind::Type { .. } => (ParamKindOrd::TypeOrConst, ident.to_string()),
771            GenericParamKind::Const { ty, .. } => {
772                let ty = pprust::ty_to_string(ty);
773                (ParamKindOrd::TypeOrConst, format!("const {ident}: {ty}"))
774            }
775        };
776        param_idents.push((kind, ord_kind, bounds, idx, ident));
777        match max_param {
778            Some(max_param) if max_param > ord_kind => {
779                let entry = out_of_order.entry(ord_kind).or_insert((max_param, vec![]));
780                entry.1.push(span);
781            }
782            Some(_) | None => max_param = Some(ord_kind),
783        };
784    }
785
786    if !out_of_order.is_empty() {
787        let mut ordered_params = "<".to_string();
788        param_idents.sort_by_key(|&(_, po, _, i, _)| (po, i));
789        let mut first = true;
790        for (kind, _, bounds, _, ident) in param_idents {
791            if !first {
792                ordered_params += ", ";
793            }
794            ordered_params += &ident;
795
796            if !bounds.is_empty() {
797                ordered_params += ": ";
798                ordered_params += &pprust::bounds_to_string(bounds);
799            }
800
801            match kind {
802                GenericParamKind::Type { default: Some(default) } => {
803                    ordered_params += " = ";
804                    ordered_params += &pprust::ty_to_string(default);
805                }
806                GenericParamKind::Type { default: None } => (),
807                GenericParamKind::Lifetime => (),
808                GenericParamKind::Const { ty: _, kw_span: _, default: Some(default) } => {
809                    ordered_params += " = ";
810                    ordered_params += &pprust::expr_to_string(&default.value);
811                }
812                GenericParamKind::Const { ty: _, kw_span: _, default: None } => (),
813            }
814            first = false;
815        }
816
817        ordered_params += ">";
818
819        for (param_ord, (max_param, spans)) in &out_of_order {
820            dcx.emit_err(errors::OutOfOrderParams {
821                spans: spans.clone(),
822                sugg_span: span,
823                param_ord,
824                max_param,
825                ordered_params: &ordered_params,
826            });
827        }
828    }
829}
830
831impl<'a> Visitor<'a> for AstValidator<'a> {
832    fn visit_attribute(&mut self, attr: &Attribute) {
833        validate_attr::check_attr(&self.sess.psess, attr, self.lint_node_id);
834    }
835
836    fn visit_ty(&mut self, ty: &'a Ty) {
837        self.visit_ty_common(ty);
838        self.walk_ty(ty)
839    }
840
841    fn visit_item(&mut self, item: &'a Item) {
842        if item.attrs.iter().any(|attr| attr.is_proc_macro_attr()) {
843            self.has_proc_macro_decls = true;
844        }
845
846        let previous_lint_node_id = mem::replace(&mut self.lint_node_id, item.id);
847
848        if let Some(ident) = item.kind.ident()
849            && attr::contains_name(&item.attrs, sym::no_mangle)
850        {
851            self.check_nomangle_item_asciionly(ident, item.span);
852        }
853
854        match &item.kind {
855            ItemKind::Impl(box Impl {
856                safety,
857                polarity,
858                defaultness: _,
859                constness,
860                generics,
861                of_trait: Some(t),
862                self_ty,
863                items,
864            }) => {
865                self.visit_attrs_vis(&item.attrs, &item.vis);
866                self.visibility_not_permitted(
867                    &item.vis,
868                    errors::VisibilityNotPermittedNote::TraitImpl,
869                );
870                if let TyKind::Dummy = self_ty.kind {
871                    // Abort immediately otherwise the `TyKind::Dummy` will reach HIR lowering,
872                    // which isn't allowed. Not a problem for this obscure, obsolete syntax.
873                    self.dcx().emit_fatal(errors::ObsoleteAuto { span: item.span });
874                }
875                if let (&Safety::Unsafe(span), &ImplPolarity::Negative(sp)) = (safety, polarity) {
876                    self.dcx().emit_err(errors::UnsafeNegativeImpl {
877                        span: sp.to(t.path.span),
878                        negative: sp,
879                        r#unsafe: span,
880                    });
881                }
882
883                let disallowed = matches!(constness, Const::No)
884                    .then(|| TildeConstReason::TraitImpl { span: item.span });
885                self.with_tilde_const(disallowed, |this| this.visit_generics(generics));
886                self.visit_trait_ref(t);
887                self.visit_ty(self_ty);
888
889                self.with_in_trait_impl(Some((*constness, *polarity, t)), |this| {
890                    walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl { of_trait: true });
891                });
892            }
893            ItemKind::Impl(box Impl {
894                safety,
895                polarity,
896                defaultness,
897                constness,
898                generics,
899                of_trait: None,
900                self_ty,
901                items,
902            }) => {
903                let error = |annotation_span, annotation, only_trait| errors::InherentImplCannot {
904                    span: self_ty.span,
905                    annotation_span,
906                    annotation,
907                    self_ty: self_ty.span,
908                    only_trait,
909                };
910
911                self.visit_attrs_vis(&item.attrs, &item.vis);
912                self.visibility_not_permitted(
913                    &item.vis,
914                    errors::VisibilityNotPermittedNote::IndividualImplItems,
915                );
916                if let &Safety::Unsafe(span) = safety {
917                    self.dcx().emit_err(errors::InherentImplCannotUnsafe {
918                        span: self_ty.span,
919                        annotation_span: span,
920                        annotation: "unsafe",
921                        self_ty: self_ty.span,
922                    });
923                }
924                if let &ImplPolarity::Negative(span) = polarity {
925                    self.dcx().emit_err(error(span, "negative", false));
926                }
927                if let &Defaultness::Default(def_span) = defaultness {
928                    self.dcx().emit_err(error(def_span, "`default`", true));
929                }
930                if let &Const::Yes(span) = constness {
931                    self.dcx().emit_err(error(span, "`const`", true));
932                }
933
934                self.with_tilde_const(Some(TildeConstReason::Impl { span: item.span }), |this| {
935                    this.visit_generics(generics)
936                });
937                self.visit_ty(self_ty);
938                self.with_in_trait_impl(None, |this| {
939                    walk_list!(this, visit_assoc_item, items, AssocCtxt::Impl { of_trait: false });
940                });
941            }
942            ItemKind::Fn(
943                func @ box Fn {
944                    defaultness,
945                    ident,
946                    generics: _,
947                    sig,
948                    contract: _,
949                    body,
950                    define_opaque: _,
951                },
952            ) => {
953                self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
954                self.check_defaultness(item.span, *defaultness);
955
956                let is_intrinsic = item.attrs.iter().any(|a| a.has_name(sym::rustc_intrinsic));
957                if body.is_none() && !is_intrinsic && !self.is_sdylib_interface {
958                    self.dcx().emit_err(errors::FnWithoutBody {
959                        span: item.span,
960                        replace_span: self.ending_semi_or_hi(item.span),
961                        extern_block_suggestion: match sig.header.ext {
962                            Extern::None => None,
963                            Extern::Implicit(start_span) => {
964                                Some(errors::ExternBlockSuggestion::Implicit {
965                                    start_span,
966                                    end_span: item.span.shrink_to_hi(),
967                                })
968                            }
969                            Extern::Explicit(abi, start_span) => {
970                                Some(errors::ExternBlockSuggestion::Explicit {
971                                    start_span,
972                                    end_span: item.span.shrink_to_hi(),
973                                    abi: abi.symbol_unescaped,
974                                })
975                            }
976                        },
977                    });
978                }
979
980                let kind = FnKind::Fn(FnCtxt::Free, &item.vis, &*func);
981                self.visit_fn(kind, item.span, item.id);
982            }
983            ItemKind::ForeignMod(ForeignMod { extern_span, abi, safety, .. }) => {
984                let old_item = mem::replace(&mut self.extern_mod_span, Some(item.span));
985                self.visibility_not_permitted(
986                    &item.vis,
987                    errors::VisibilityNotPermittedNote::IndividualForeignItems,
988                );
989
990                if &Safety::Default == safety {
991                    if item.span.at_least_rust_2024() {
992                        self.dcx().emit_err(errors::MissingUnsafeOnExtern { span: item.span });
993                    } else {
994                        self.lint_buffer.buffer_lint(
995                            MISSING_UNSAFE_ON_EXTERN,
996                            item.id,
997                            item.span,
998                            BuiltinLintDiag::MissingUnsafeOnExtern {
999                                suggestion: item.span.shrink_to_lo(),
1000                            },
1001                        );
1002                    }
1003                }
1004
1005                if abi.is_none() {
1006                    self.handle_missing_abi(*extern_span, item.id);
1007                }
1008                self.with_in_extern_mod(*safety, |this| {
1009                    visit::walk_item(this, item);
1010                });
1011                self.extern_mod_span = old_item;
1012            }
1013            ItemKind::Enum(_, _, def) => {
1014                for variant in &def.variants {
1015                    self.visibility_not_permitted(
1016                        &variant.vis,
1017                        errors::VisibilityNotPermittedNote::EnumVariant,
1018                    );
1019                    for field in variant.data.fields() {
1020                        self.visibility_not_permitted(
1021                            &field.vis,
1022                            errors::VisibilityNotPermittedNote::EnumVariant,
1023                        );
1024                    }
1025                }
1026                visit::walk_item(self, item)
1027            }
1028            ItemKind::Trait(box Trait { is_auto, generics, ident, bounds, items, .. }) => {
1029                self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
1030                let is_const_trait =
1031                    attr::find_by_name(&item.attrs, sym::const_trait).map(|attr| attr.span);
1032                if *is_auto == IsAuto::Yes {
1033                    // Auto traits cannot have generics, super traits nor contain items.
1034                    self.deny_generic_params(generics, ident.span);
1035                    self.deny_super_traits(bounds, ident.span);
1036                    self.deny_where_clause(&generics.where_clause, ident.span);
1037                    self.deny_items(items, ident.span);
1038                }
1039
1040                // Equivalent of `visit::walk_item` for `ItemKind::Trait` that inserts a bound
1041                // context for the supertraits.
1042                let disallowed =
1043                    is_const_trait.is_none().then(|| TildeConstReason::Trait { span: item.span });
1044                self.with_tilde_const(disallowed, |this| {
1045                    this.visit_generics(generics);
1046                    walk_list!(this, visit_param_bound, bounds, BoundKind::SuperTraits)
1047                });
1048                self.with_in_trait(item.span, is_const_trait, |this| {
1049                    walk_list!(this, visit_assoc_item, items, AssocCtxt::Trait);
1050                });
1051            }
1052            ItemKind::Mod(safety, ident, mod_kind) => {
1053                if let &Safety::Unsafe(span) = safety {
1054                    self.dcx().emit_err(errors::UnsafeItem { span, kind: "module" });
1055                }
1056                // Ensure that `path` attributes on modules are recorded as used (cf. issue #35584).
1057                if !matches!(mod_kind, ModKind::Loaded(_, Inline::Yes, _, _))
1058                    && !attr::contains_name(&item.attrs, sym::path)
1059                {
1060                    self.check_mod_file_item_asciionly(*ident);
1061                }
1062                visit::walk_item(self, item)
1063            }
1064            ItemKind::Struct(ident, generics, vdata) => match vdata {
1065                VariantData::Struct { fields, .. } => {
1066                    self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
1067                    self.visit_generics(generics);
1068                    // Permit `Anon{Struct,Union}` as field type.
1069                    walk_list!(self, visit_struct_field_def, fields);
1070                }
1071                _ => visit::walk_item(self, item),
1072            },
1073            ItemKind::Union(ident, generics, vdata) => {
1074                if vdata.fields().is_empty() {
1075                    self.dcx().emit_err(errors::FieldlessUnion { span: item.span });
1076                }
1077                match vdata {
1078                    VariantData::Struct { fields, .. } => {
1079                        self.visit_attrs_vis_ident(&item.attrs, &item.vis, ident);
1080                        self.visit_generics(generics);
1081                        // Permit `Anon{Struct,Union}` as field type.
1082                        walk_list!(self, visit_struct_field_def, fields);
1083                    }
1084                    _ => visit::walk_item(self, item),
1085                }
1086            }
1087            ItemKind::Const(box ConstItem { defaultness, expr, .. }) => {
1088                self.check_defaultness(item.span, *defaultness);
1089                if expr.is_none() {
1090                    self.dcx().emit_err(errors::ConstWithoutBody {
1091                        span: item.span,
1092                        replace_span: self.ending_semi_or_hi(item.span),
1093                    });
1094                }
1095                visit::walk_item(self, item);
1096            }
1097            ItemKind::Static(box StaticItem { expr, safety, .. }) => {
1098                self.check_item_safety(item.span, *safety);
1099                if matches!(safety, Safety::Unsafe(_)) {
1100                    self.dcx().emit_err(errors::UnsafeStatic { span: item.span });
1101                }
1102
1103                if expr.is_none() {
1104                    self.dcx().emit_err(errors::StaticWithoutBody {
1105                        span: item.span,
1106                        replace_span: self.ending_semi_or_hi(item.span),
1107                    });
1108                }
1109                visit::walk_item(self, item);
1110            }
1111            ItemKind::TyAlias(
1112                ty_alias @ box TyAlias { defaultness, bounds, where_clauses, ty, .. },
1113            ) => {
1114                self.check_defaultness(item.span, *defaultness);
1115                if ty.is_none() {
1116                    self.dcx().emit_err(errors::TyAliasWithoutBody {
1117                        span: item.span,
1118                        replace_span: self.ending_semi_or_hi(item.span),
1119                    });
1120                }
1121                self.check_type_no_bounds(bounds, "this context");
1122
1123                if self.features.lazy_type_alias() {
1124                    if let Err(err) = self.check_type_alias_where_clause_location(ty_alias) {
1125                        self.dcx().emit_err(err);
1126                    }
1127                } else if where_clauses.after.has_where_token {
1128                    self.dcx().emit_err(errors::WhereClauseAfterTypeAlias {
1129                        span: where_clauses.after.span,
1130                        help: self.sess.is_nightly_build(),
1131                    });
1132                }
1133                visit::walk_item(self, item);
1134            }
1135            _ => visit::walk_item(self, item),
1136        }
1137
1138        self.lint_node_id = previous_lint_node_id;
1139    }
1140
1141    fn visit_foreign_item(&mut self, fi: &'a ForeignItem) {
1142        match &fi.kind {
1143            ForeignItemKind::Fn(box Fn { defaultness, ident, sig, body, .. }) => {
1144                self.check_defaultness(fi.span, *defaultness);
1145                self.check_foreign_fn_bodyless(*ident, body.as_deref());
1146                self.check_foreign_fn_headerless(sig.header);
1147                self.check_foreign_item_ascii_only(*ident);
1148            }
1149            ForeignItemKind::TyAlias(box TyAlias {
1150                defaultness,
1151                ident,
1152                generics,
1153                where_clauses,
1154                bounds,
1155                ty,
1156                ..
1157            }) => {
1158                self.check_defaultness(fi.span, *defaultness);
1159                self.check_foreign_kind_bodyless(*ident, "type", ty.as_ref().map(|b| b.span));
1160                self.check_type_no_bounds(bounds, "`extern` blocks");
1161                self.check_foreign_ty_genericless(generics, where_clauses);
1162                self.check_foreign_item_ascii_only(*ident);
1163            }
1164            ForeignItemKind::Static(box StaticItem { ident, safety, expr, .. }) => {
1165                self.check_item_safety(fi.span, *safety);
1166                self.check_foreign_kind_bodyless(*ident, "static", expr.as_ref().map(|b| b.span));
1167                self.check_foreign_item_ascii_only(*ident);
1168            }
1169            ForeignItemKind::MacCall(..) => {}
1170        }
1171
1172        visit::walk_item(self, fi)
1173    }
1174
1175    // Mirrors `visit::walk_generic_args`, but tracks relevant state.
1176    fn visit_generic_args(&mut self, generic_args: &'a GenericArgs) {
1177        match generic_args {
1178            GenericArgs::AngleBracketed(data) => {
1179                self.check_generic_args_before_constraints(data);
1180
1181                for arg in &data.args {
1182                    match arg {
1183                        AngleBracketedArg::Arg(arg) => self.visit_generic_arg(arg),
1184                        // Associated type bindings such as `Item = impl Debug` in
1185                        // `Iterator<Item = Debug>` are allowed to contain nested `impl Trait`.
1186                        AngleBracketedArg::Constraint(constraint) => {
1187                            self.with_impl_trait(None, |this| {
1188                                this.visit_assoc_item_constraint(constraint);
1189                            });
1190                        }
1191                    }
1192                }
1193            }
1194            GenericArgs::Parenthesized(data) => {
1195                walk_list!(self, visit_ty, &data.inputs);
1196                if let FnRetTy::Ty(ty) = &data.output {
1197                    // `-> Foo` syntax is essentially an associated type binding,
1198                    // so it is also allowed to contain nested `impl Trait`.
1199                    self.with_impl_trait(None, |this| this.visit_ty(ty));
1200                }
1201            }
1202            GenericArgs::ParenthesizedElided(_span) => {}
1203        }
1204    }
1205
1206    fn visit_generics(&mut self, generics: &'a Generics) {
1207        let mut prev_param_default = None;
1208        for param in &generics.params {
1209            match param.kind {
1210                GenericParamKind::Lifetime => (),
1211                GenericParamKind::Type { default: Some(_), .. }
1212                | GenericParamKind::Const { default: Some(_), .. } => {
1213                    prev_param_default = Some(param.ident.span);
1214                }
1215                GenericParamKind::Type { .. } | GenericParamKind::Const { .. } => {
1216                    if let Some(span) = prev_param_default {
1217                        self.dcx().emit_err(errors::GenericDefaultTrailing { span });
1218                        break;
1219                    }
1220                }
1221            }
1222        }
1223
1224        validate_generic_param_order(self.dcx(), &generics.params, generics.span);
1225
1226        for predicate in &generics.where_clause.predicates {
1227            let span = predicate.span;
1228            if let WherePredicateKind::EqPredicate(predicate) = &predicate.kind {
1229                deny_equality_constraints(self, predicate, span, generics);
1230            }
1231        }
1232        walk_list!(self, visit_generic_param, &generics.params);
1233        for predicate in &generics.where_clause.predicates {
1234            match &predicate.kind {
1235                WherePredicateKind::BoundPredicate(bound_pred) => {
1236                    // This is slightly complicated. Our representation for poly-trait-refs contains a single
1237                    // binder and thus we only allow a single level of quantification. However,
1238                    // the syntax of Rust permits quantification in two places in where clauses,
1239                    // e.g., `T: for <'a> Foo<'a>` and `for <'a, 'b> &'b T: Foo<'a>`. If both are
1240                    // defined, then error.
1241                    if !bound_pred.bound_generic_params.is_empty() {
1242                        for bound in &bound_pred.bounds {
1243                            match bound {
1244                                GenericBound::Trait(t) => {
1245                                    if !t.bound_generic_params.is_empty() {
1246                                        self.dcx()
1247                                            .emit_err(errors::NestedLifetimes { span: t.span });
1248                                    }
1249                                }
1250                                GenericBound::Outlives(_) => {}
1251                                GenericBound::Use(..) => {}
1252                            }
1253                        }
1254                    }
1255                }
1256                _ => {}
1257            }
1258            self.visit_where_predicate(predicate);
1259        }
1260    }
1261
1262    fn visit_param_bound(&mut self, bound: &'a GenericBound, ctxt: BoundKind) {
1263        match bound {
1264            GenericBound::Trait(trait_ref) => {
1265                match (ctxt, trait_ref.modifiers.constness, trait_ref.modifiers.polarity) {
1266                    (BoundKind::SuperTraits, BoundConstness::Never, BoundPolarity::Maybe(_))
1267                        if !self.features.more_maybe_bounds() =>
1268                    {
1269                        self.sess
1270                            .create_feature_err(
1271                                errors::OptionalTraitSupertrait {
1272                                    span: trait_ref.span,
1273                                    path_str: pprust::path_to_string(&trait_ref.trait_ref.path),
1274                                },
1275                                sym::more_maybe_bounds,
1276                            )
1277                            .emit();
1278                    }
1279                    (BoundKind::TraitObject, BoundConstness::Never, BoundPolarity::Maybe(_))
1280                        if !self.features.more_maybe_bounds() =>
1281                    {
1282                        self.sess
1283                            .create_feature_err(
1284                                errors::OptionalTraitObject { span: trait_ref.span },
1285                                sym::more_maybe_bounds,
1286                            )
1287                            .emit();
1288                    }
1289                    (
1290                        BoundKind::TraitObject,
1291                        BoundConstness::Always(_),
1292                        BoundPolarity::Positive,
1293                    ) => {
1294                        self.dcx().emit_err(errors::ConstBoundTraitObject { span: trait_ref.span });
1295                    }
1296                    (_, BoundConstness::Maybe(span), BoundPolarity::Positive)
1297                        if let Some(reason) = self.disallow_tilde_const =>
1298                    {
1299                        self.dcx().emit_err(errors::TildeConstDisallowed { span, reason });
1300                    }
1301                    _ => {}
1302                }
1303
1304                // Negative trait bounds are not allowed to have associated constraints
1305                if let BoundPolarity::Negative(_) = trait_ref.modifiers.polarity
1306                    && let Some(segment) = trait_ref.trait_ref.path.segments.last()
1307                {
1308                    match segment.args.as_deref() {
1309                        Some(ast::GenericArgs::AngleBracketed(args)) => {
1310                            for arg in &args.args {
1311                                if let ast::AngleBracketedArg::Constraint(constraint) = arg {
1312                                    self.dcx().emit_err(errors::ConstraintOnNegativeBound {
1313                                        span: constraint.span,
1314                                    });
1315                                }
1316                            }
1317                        }
1318                        // The lowered form of parenthesized generic args contains an associated type binding.
1319                        Some(ast::GenericArgs::Parenthesized(args)) => {
1320                            self.dcx().emit_err(errors::NegativeBoundWithParentheticalNotation {
1321                                span: args.span,
1322                            });
1323                        }
1324                        Some(ast::GenericArgs::ParenthesizedElided(_)) | None => {}
1325                    }
1326                }
1327            }
1328            GenericBound::Outlives(_) => {}
1329            GenericBound::Use(_, span) => match ctxt {
1330                BoundKind::Impl => {}
1331                BoundKind::Bound | BoundKind::TraitObject | BoundKind::SuperTraits => {
1332                    self.dcx().emit_err(errors::PreciseCapturingNotAllowedHere {
1333                        loc: ctxt.descr(),
1334                        span: *span,
1335                    });
1336                }
1337            },
1338        }
1339
1340        visit::walk_param_bound(self, bound)
1341    }
1342
1343    fn visit_fn(&mut self, fk: FnKind<'a>, span: Span, id: NodeId) {
1344        // Only associated `fn`s can have `self` parameters.
1345        let self_semantic = match fk.ctxt() {
1346            Some(FnCtxt::Assoc(_)) => SelfSemantic::Yes,
1347            _ => SelfSemantic::No,
1348        };
1349        self.check_fn_decl(fk.decl(), self_semantic);
1350
1351        if let Some(&FnHeader { safety, .. }) = fk.header() {
1352            self.check_item_safety(span, safety);
1353        }
1354
1355        self.check_c_variadic_type(fk);
1356
1357        // Functions cannot both be `const async` or `const gen`
1358        if let Some(&FnHeader {
1359            constness: Const::Yes(const_span),
1360            coroutine_kind: Some(coroutine_kind),
1361            ..
1362        }) = fk.header()
1363        {
1364            self.dcx().emit_err(errors::ConstAndCoroutine {
1365                spans: vec![coroutine_kind.span(), const_span],
1366                const_span,
1367                coroutine_span: coroutine_kind.span(),
1368                coroutine_kind: coroutine_kind.as_str(),
1369                span,
1370            });
1371        }
1372
1373        if let FnKind::Fn(
1374            _,
1375            _,
1376            Fn {
1377                sig: FnSig { header: FnHeader { ext: Extern::Implicit(extern_span), .. }, .. },
1378                ..
1379            },
1380        ) = fk
1381        {
1382            self.handle_missing_abi(*extern_span, id);
1383        }
1384
1385        // Functions without bodies cannot have patterns.
1386        if let FnKind::Fn(ctxt, _, Fn { body: None, sig, .. }) = fk {
1387            Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
1388                if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
1389                    if let Some(ident) = ident {
1390                        self.lint_buffer.buffer_lint(
1391                            PATTERNS_IN_FNS_WITHOUT_BODY,
1392                            id,
1393                            span,
1394                            BuiltinLintDiag::PatternsInFnsWithoutBody {
1395                                span,
1396                                ident,
1397                                is_foreign: matches!(ctxt, FnCtxt::Foreign),
1398                            },
1399                        )
1400                    }
1401                } else {
1402                    match ctxt {
1403                        FnCtxt::Foreign => self.dcx().emit_err(errors::PatternInForeign { span }),
1404                        _ => self.dcx().emit_err(errors::PatternInBodiless { span }),
1405                    };
1406                }
1407            });
1408        }
1409
1410        let tilde_const_allowed =
1411            matches!(fk.header(), Some(FnHeader { constness: ast::Const::Yes(_), .. }))
1412                || matches!(fk.ctxt(), Some(FnCtxt::Assoc(_)))
1413                    && self
1414                        .outer_trait_or_trait_impl
1415                        .as_ref()
1416                        .and_then(TraitOrTraitImpl::constness)
1417                        .is_some();
1418
1419        let disallowed = (!tilde_const_allowed).then(|| match fk {
1420            FnKind::Fn(_, _, f) => TildeConstReason::Function { ident: f.ident.span },
1421            FnKind::Closure(..) => TildeConstReason::Closure,
1422        });
1423        self.with_tilde_const(disallowed, |this| visit::walk_fn(this, fk));
1424    }
1425
1426    fn visit_assoc_item(&mut self, item: &'a AssocItem, ctxt: AssocCtxt) {
1427        if let Some(ident) = item.kind.ident()
1428            && attr::contains_name(&item.attrs, sym::no_mangle)
1429        {
1430            self.check_nomangle_item_asciionly(ident, item.span);
1431        }
1432
1433        if ctxt == AssocCtxt::Trait || self.outer_trait_or_trait_impl.is_none() {
1434            self.check_defaultness(item.span, item.kind.defaultness());
1435        }
1436
1437        if let AssocCtxt::Impl { .. } = ctxt {
1438            match &item.kind {
1439                AssocItemKind::Const(box ConstItem { expr: None, .. }) => {
1440                    self.dcx().emit_err(errors::AssocConstWithoutBody {
1441                        span: item.span,
1442                        replace_span: self.ending_semi_or_hi(item.span),
1443                    });
1444                }
1445                AssocItemKind::Fn(box Fn { body, .. }) => {
1446                    if body.is_none() && !self.is_sdylib_interface {
1447                        self.dcx().emit_err(errors::AssocFnWithoutBody {
1448                            span: item.span,
1449                            replace_span: self.ending_semi_or_hi(item.span),
1450                        });
1451                    }
1452                }
1453                AssocItemKind::Type(box TyAlias { bounds, ty, .. }) => {
1454                    if ty.is_none() {
1455                        self.dcx().emit_err(errors::AssocTypeWithoutBody {
1456                            span: item.span,
1457                            replace_span: self.ending_semi_or_hi(item.span),
1458                        });
1459                    }
1460                    self.check_type_no_bounds(bounds, "`impl`s");
1461                }
1462                _ => {}
1463            }
1464        }
1465
1466        if let AssocItemKind::Type(ty_alias) = &item.kind
1467            && let Err(err) = self.check_type_alias_where_clause_location(ty_alias)
1468        {
1469            let sugg = match err.sugg {
1470                errors::WhereClauseBeforeTypeAliasSugg::Remove { .. } => None,
1471                errors::WhereClauseBeforeTypeAliasSugg::Move { snippet, right, .. } => {
1472                    Some((right, snippet))
1473                }
1474            };
1475            self.lint_buffer.buffer_lint(
1476                DEPRECATED_WHERE_CLAUSE_LOCATION,
1477                item.id,
1478                err.span,
1479                BuiltinLintDiag::DeprecatedWhereclauseLocation(err.span, sugg),
1480            );
1481        }
1482
1483        if let Some(parent) = &self.outer_trait_or_trait_impl {
1484            self.visibility_not_permitted(&item.vis, errors::VisibilityNotPermittedNote::TraitImpl);
1485            if let AssocItemKind::Fn(box Fn { sig, .. }) = &item.kind {
1486                self.check_trait_fn_not_const(sig.header.constness, parent);
1487            }
1488        }
1489
1490        if let AssocItemKind::Const(ci) = &item.kind {
1491            self.check_item_named(ci.ident, "const");
1492        }
1493
1494        let parent_is_const =
1495            self.outer_trait_or_trait_impl.as_ref().and_then(TraitOrTraitImpl::constness).is_some();
1496
1497        match &item.kind {
1498            AssocItemKind::Fn(func)
1499                if parent_is_const
1500                    || ctxt == AssocCtxt::Trait
1501                    || matches!(func.sig.header.constness, Const::Yes(_)) =>
1502            {
1503                self.visit_attrs_vis_ident(&item.attrs, &item.vis, &func.ident);
1504                let kind = FnKind::Fn(FnCtxt::Assoc(ctxt), &item.vis, &*func);
1505                self.visit_fn(kind, item.span, item.id);
1506            }
1507            AssocItemKind::Type(_) => {
1508                let disallowed = (!parent_is_const).then(|| match self.outer_trait_or_trait_impl {
1509                    Some(TraitOrTraitImpl::Trait { .. }) => {
1510                        TildeConstReason::TraitAssocTy { span: item.span }
1511                    }
1512                    Some(TraitOrTraitImpl::TraitImpl { .. }) => {
1513                        TildeConstReason::TraitImplAssocTy { span: item.span }
1514                    }
1515                    None => TildeConstReason::InherentAssocTy { span: item.span },
1516                });
1517                self.with_tilde_const(disallowed, |this| {
1518                    this.with_in_trait_impl(None, |this| visit::walk_assoc_item(this, item, ctxt))
1519                })
1520            }
1521            _ => self.with_in_trait_impl(None, |this| visit::walk_assoc_item(this, item, ctxt)),
1522        }
1523    }
1524}
1525
1526/// When encountering an equality constraint in a `where` clause, emit an error. If the code seems
1527/// like it's setting an associated type, provide an appropriate suggestion.
1528fn deny_equality_constraints(
1529    this: &AstValidator<'_>,
1530    predicate: &WhereEqPredicate,
1531    predicate_span: Span,
1532    generics: &Generics,
1533) {
1534    let mut err = errors::EqualityInWhere { span: predicate_span, assoc: None, assoc2: None };
1535
1536    // Given `<A as Foo>::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
1537    if let TyKind::Path(Some(qself), full_path) = &predicate.lhs_ty.kind
1538        && let TyKind::Path(None, path) = &qself.ty.kind
1539        && let [PathSegment { ident, args: None, .. }] = &path.segments[..]
1540    {
1541        for param in &generics.params {
1542            if param.ident == *ident
1543                && let [PathSegment { ident, args, .. }] = &full_path.segments[qself.position..]
1544            {
1545                // Make a new `Path` from `foo::Bar` to `Foo<Bar = RhsTy>`.
1546                let mut assoc_path = full_path.clone();
1547                // Remove `Bar` from `Foo::Bar`.
1548                assoc_path.segments.pop();
1549                let len = assoc_path.segments.len() - 1;
1550                let gen_args = args.as_deref().cloned();
1551                // Build `<Bar = RhsTy>`.
1552                let arg = AngleBracketedArg::Constraint(AssocItemConstraint {
1553                    id: rustc_ast::node_id::DUMMY_NODE_ID,
1554                    ident: *ident,
1555                    gen_args,
1556                    kind: AssocItemConstraintKind::Equality {
1557                        term: predicate.rhs_ty.clone().into(),
1558                    },
1559                    span: ident.span,
1560                });
1561                // Add `<Bar = RhsTy>` to `Foo`.
1562                match &mut assoc_path.segments[len].args {
1563                    Some(args) => match args.deref_mut() {
1564                        GenericArgs::Parenthesized(_) | GenericArgs::ParenthesizedElided(..) => {
1565                            continue;
1566                        }
1567                        GenericArgs::AngleBracketed(args) => {
1568                            args.args.push(arg);
1569                        }
1570                    },
1571                    empty_args => {
1572                        *empty_args = Some(
1573                            AngleBracketedArgs { span: ident.span, args: thin_vec![arg] }.into(),
1574                        );
1575                    }
1576                }
1577                err.assoc = Some(errors::AssociatedSuggestion {
1578                    span: predicate_span,
1579                    ident: *ident,
1580                    param: param.ident,
1581                    path: pprust::path_to_string(&assoc_path),
1582                })
1583            }
1584        }
1585    }
1586
1587    let mut suggest =
1588        |poly: &PolyTraitRef, potential_assoc: &PathSegment, predicate: &WhereEqPredicate| {
1589            if let [trait_segment] = &poly.trait_ref.path.segments[..] {
1590                let assoc = pprust::path_to_string(&ast::Path::from_ident(potential_assoc.ident));
1591                let ty = pprust::ty_to_string(&predicate.rhs_ty);
1592                let (args, span) = match &trait_segment.args {
1593                    Some(args) => match args.deref() {
1594                        ast::GenericArgs::AngleBracketed(args) => {
1595                            let Some(arg) = args.args.last() else {
1596                                return;
1597                            };
1598                            (format!(", {assoc} = {ty}"), arg.span().shrink_to_hi())
1599                        }
1600                        _ => return,
1601                    },
1602                    None => (format!("<{assoc} = {ty}>"), trait_segment.span().shrink_to_hi()),
1603                };
1604                let removal_span = if generics.where_clause.predicates.len() == 1 {
1605                    // We're removing th eonly where bound left, remove the whole thing.
1606                    generics.where_clause.span
1607                } else {
1608                    let mut span = predicate_span;
1609                    let mut prev_span: Option<Span> = None;
1610                    let mut preds = generics.where_clause.predicates.iter().peekable();
1611                    // Find the predicate that shouldn't have been in the where bound list.
1612                    while let Some(pred) = preds.next() {
1613                        if let WherePredicateKind::EqPredicate(_) = pred.kind
1614                            && pred.span == predicate_span
1615                        {
1616                            if let Some(next) = preds.peek() {
1617                                // This is the first predicate, remove the trailing comma as well.
1618                                span = span.with_hi(next.span.lo());
1619                            } else if let Some(prev_span) = prev_span {
1620                                // Remove the previous comma as well.
1621                                span = span.with_lo(prev_span.hi());
1622                            }
1623                        }
1624                        prev_span = Some(pred.span);
1625                    }
1626                    span
1627                };
1628                err.assoc2 = Some(errors::AssociatedSuggestion2 {
1629                    span,
1630                    args,
1631                    predicate: removal_span,
1632                    trait_segment: trait_segment.ident,
1633                    potential_assoc: potential_assoc.ident,
1634                });
1635            }
1636        };
1637
1638    if let TyKind::Path(None, full_path) = &predicate.lhs_ty.kind {
1639        // Given `A: Foo, Foo::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
1640        for bounds in generics.params.iter().map(|p| &p.bounds).chain(
1641            generics.where_clause.predicates.iter().filter_map(|pred| match &pred.kind {
1642                WherePredicateKind::BoundPredicate(p) => Some(&p.bounds),
1643                _ => None,
1644            }),
1645        ) {
1646            for bound in bounds {
1647                if let GenericBound::Trait(poly) = bound
1648                    && poly.modifiers == TraitBoundModifiers::NONE
1649                {
1650                    if full_path.segments[..full_path.segments.len() - 1]
1651                        .iter()
1652                        .map(|segment| segment.ident.name)
1653                        .zip(poly.trait_ref.path.segments.iter().map(|segment| segment.ident.name))
1654                        .all(|(a, b)| a == b)
1655                        && let Some(potential_assoc) = full_path.segments.iter().last()
1656                    {
1657                        suggest(poly, potential_assoc, predicate);
1658                    }
1659                }
1660            }
1661        }
1662        // Given `A: Foo, A::Bar = RhsTy`, suggest `A: Foo<Bar = RhsTy>`.
1663        if let [potential_param, potential_assoc] = &full_path.segments[..] {
1664            for (ident, bounds) in generics.params.iter().map(|p| (p.ident, &p.bounds)).chain(
1665                generics.where_clause.predicates.iter().filter_map(|pred| match &pred.kind {
1666                    WherePredicateKind::BoundPredicate(p)
1667                        if let ast::TyKind::Path(None, path) = &p.bounded_ty.kind
1668                            && let [segment] = &path.segments[..] =>
1669                    {
1670                        Some((segment.ident, &p.bounds))
1671                    }
1672                    _ => None,
1673                }),
1674            ) {
1675                if ident == potential_param.ident {
1676                    for bound in bounds {
1677                        if let ast::GenericBound::Trait(poly) = bound
1678                            && poly.modifiers == TraitBoundModifiers::NONE
1679                        {
1680                            suggest(poly, potential_assoc, predicate);
1681                        }
1682                    }
1683                }
1684            }
1685        }
1686    }
1687    this.dcx().emit_err(err);
1688}
1689
1690pub fn check_crate(
1691    sess: &Session,
1692    features: &Features,
1693    krate: &Crate,
1694    is_sdylib_interface: bool,
1695    lints: &mut LintBuffer,
1696) -> bool {
1697    let mut validator = AstValidator {
1698        sess,
1699        features,
1700        extern_mod_span: None,
1701        outer_trait_or_trait_impl: None,
1702        has_proc_macro_decls: false,
1703        outer_impl_trait_span: None,
1704        disallow_tilde_const: Some(TildeConstReason::Item),
1705        extern_mod_safety: None,
1706        lint_node_id: CRATE_NODE_ID,
1707        is_sdylib_interface,
1708        lint_buffer: lints,
1709    };
1710    visit::walk_crate(&mut validator, krate);
1711
1712    validator.has_proc_macro_decls
1713}