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