Skip to main content

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