rustc_passes/
check_attr.rs

1// FIXME(jdonszelmann): should become rustc_attr_validation
2//! This module implements some validity checks for attributes.
3//! In particular it verifies that `#[inline]` and `#[repr]` attributes are
4//! attached to items that actually support them and if there are
5//! conflicts between multiple such attributes attached to the same
6//! item.
7
8use std::cell::Cell;
9use std::collections::hash_map::Entry;
10use std::slice;
11
12use rustc_abi::{Align, ExternAbi, Size};
13use rustc_ast::{AttrStyle, LitKind, MetaItemInner, MetaItemKind, ast};
14use rustc_attr_parsing::{AttributeParser, Late};
15use rustc_data_structures::fx::FxHashMap;
16use rustc_errors::{Applicability, DiagCtxtHandle, IntoDiagArg, MultiSpan, StashKey};
17use rustc_feature::{
18    ACCEPTED_LANG_FEATURES, AttributeDuplicates, AttributeType, BUILTIN_ATTRIBUTE_MAP,
19    BuiltinAttribute,
20};
21use rustc_hir::attrs::{AttributeKind, InlineAttr, MirDialect, MirPhase, ReprAttr};
22use rustc_hir::def::DefKind;
23use rustc_hir::def_id::LocalModDefId;
24use rustc_hir::intravisit::{self, Visitor};
25use rustc_hir::{
26    self as hir, Attribute, CRATE_HIR_ID, CRATE_OWNER_ID, FnSig, ForeignItem, HirId, Item,
27    ItemKind, MethodKind, PartialConstStability, Safety, Stability, StabilityLevel, Target,
28    TraitItem, find_attr,
29};
30use rustc_macros::LintDiagnostic;
31use rustc_middle::hir::nested_filter;
32use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
33use rustc_middle::query::Providers;
34use rustc_middle::traits::ObligationCause;
35use rustc_middle::ty::error::{ExpectedFound, TypeError};
36use rustc_middle::ty::{self, TyCtxt, TypingMode};
37use rustc_middle::{bug, span_bug};
38use rustc_session::config::CrateType;
39use rustc_session::lint;
40use rustc_session::lint::builtin::{
41    CONFLICTING_REPR_HINTS, INVALID_DOC_ATTRIBUTES, INVALID_MACRO_EXPORT_ARGUMENTS,
42    MALFORMED_DIAGNOSTIC_ATTRIBUTES, MISPLACED_DIAGNOSTIC_ATTRIBUTES, UNUSED_ATTRIBUTES,
43};
44use rustc_session::parse::feature_err;
45use rustc_span::edition::Edition;
46use rustc_span::{BytePos, DUMMY_SP, Span, Symbol, edition, sym};
47use rustc_trait_selection::error_reporting::InferCtxtErrorExt;
48use rustc_trait_selection::infer::{TyCtxtInferExt, ValuePairs};
49use rustc_trait_selection::traits::ObligationCtxt;
50use tracing::debug;
51
52use crate::{errors, fluent_generated as fluent};
53
54#[derive(LintDiagnostic)]
55#[diag(passes_diagnostic_diagnostic_on_unimplemented_only_for_traits)]
56struct DiagnosticOnUnimplementedOnlyForTraits;
57
58fn target_from_impl_item<'tcx>(tcx: TyCtxt<'tcx>, impl_item: &hir::ImplItem<'_>) -> Target {
59    match impl_item.kind {
60        hir::ImplItemKind::Const(..) => Target::AssocConst,
61        hir::ImplItemKind::Fn(..) => {
62            let parent_def_id = tcx.hir_get_parent_item(impl_item.hir_id()).def_id;
63            let containing_item = tcx.hir_expect_item(parent_def_id);
64            let containing_impl_is_for_trait = match &containing_item.kind {
65                hir::ItemKind::Impl(impl_) => impl_.of_trait.is_some(),
66                _ => bug!("parent of an ImplItem must be an Impl"),
67            };
68            if containing_impl_is_for_trait {
69                Target::Method(MethodKind::Trait { body: true })
70            } else {
71                Target::Method(MethodKind::Inherent)
72            }
73        }
74        hir::ImplItemKind::Type(..) => Target::AssocTy,
75    }
76}
77
78#[derive(Clone, Copy)]
79enum ItemLike<'tcx> {
80    Item(&'tcx Item<'tcx>),
81    ForeignItem,
82}
83
84#[derive(Copy, Clone)]
85pub(crate) enum ProcMacroKind {
86    FunctionLike,
87    Derive,
88    Attribute,
89}
90
91impl IntoDiagArg for ProcMacroKind {
92    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
93        match self {
94            ProcMacroKind::Attribute => "attribute proc macro",
95            ProcMacroKind::Derive => "derive proc macro",
96            ProcMacroKind::FunctionLike => "function-like proc macro",
97        }
98        .into_diag_arg(&mut None)
99    }
100}
101
102struct CheckAttrVisitor<'tcx> {
103    tcx: TyCtxt<'tcx>,
104
105    // Whether or not this visitor should abort after finding errors
106    abort: Cell<bool>,
107}
108
109impl<'tcx> CheckAttrVisitor<'tcx> {
110    fn dcx(&self) -> DiagCtxtHandle<'tcx> {
111        self.tcx.dcx()
112    }
113
114    /// Checks any attribute.
115    fn check_attributes(
116        &self,
117        hir_id: HirId,
118        span: Span,
119        target: Target,
120        item: Option<ItemLike<'_>>,
121    ) {
122        let mut doc_aliases = FxHashMap::default();
123        let mut specified_inline = None;
124        let mut seen = FxHashMap::default();
125        let attrs = self.tcx.hir_attrs(hir_id);
126        for attr in attrs {
127            let mut style = None;
128            match attr {
129                Attribute::Parsed(AttributeKind::ProcMacro(_)) => {
130                    self.check_proc_macro(hir_id, target, ProcMacroKind::FunctionLike)
131                }
132                Attribute::Parsed(AttributeKind::ProcMacroAttribute(_)) => {
133                    self.check_proc_macro(hir_id, target, ProcMacroKind::Attribute);
134                }
135                Attribute::Parsed(AttributeKind::ProcMacroDerive { .. }) => {
136                    self.check_proc_macro(hir_id, target, ProcMacroKind::Derive)
137                }
138                &Attribute::Parsed(AttributeKind::TypeConst(attr_span)) => {
139                    self.check_type_const(hir_id, attr_span, target)
140                }
141                Attribute::Parsed(
142                    AttributeKind::Stability {
143                        span: attr_span,
144                        stability: Stability { level, feature },
145                    }
146                    | AttributeKind::ConstStability {
147                        span: attr_span,
148                        stability: PartialConstStability { level, feature, .. },
149                    },
150                ) => self.check_stability(*attr_span, span, level, *feature),
151                Attribute::Parsed(AttributeKind::Inline(InlineAttr::Force { .. }, ..)) => {} // handled separately below
152                Attribute::Parsed(AttributeKind::Inline(kind, attr_span)) => {
153                    self.check_inline(hir_id, *attr_span, kind, target)
154                }
155                Attribute::Parsed(AttributeKind::LoopMatch(attr_span)) => {
156                    self.check_loop_match(hir_id, *attr_span, target)
157                }
158                Attribute::Parsed(AttributeKind::ConstContinue(attr_span)) => {
159                    self.check_const_continue(hir_id, *attr_span, target)
160                }
161                Attribute::Parsed(AttributeKind::AllowInternalUnsafe(attr_span) | AttributeKind::AllowInternalUnstable(.., attr_span)) => {
162                    self.check_macro_only_attr(*attr_span, span, target, attrs)
163                }
164                Attribute::Parsed(AttributeKind::AllowConstFnUnstable(_, first_span)) => {
165                    self.check_rustc_allow_const_fn_unstable(hir_id, *first_span, span, target)
166                }
167                Attribute::Parsed(AttributeKind::Deprecation { .. }) => {
168                    self.check_deprecated(hir_id, attr, span, target)
169                }
170                Attribute::Parsed(AttributeKind::TargetFeature(_, attr_span)) => {
171                    self.check_target_feature(hir_id, *attr_span, target, attrs)
172                }
173                Attribute::Parsed(AttributeKind::RustcObjectLifetimeDefault) => {
174                    self.check_object_lifetime_default(hir_id);
175                }
176                &Attribute::Parsed(AttributeKind::PubTransparent(attr_span)) => {
177                    self.check_rustc_pub_transparent(attr_span, span, attrs)
178                }
179                Attribute::Parsed(AttributeKind::Align { align, span: attr_span }) => {
180                    self.check_align(*align, *attr_span)
181                }
182                Attribute::Parsed(AttributeKind::Naked(..)) => {
183                    self.check_naked(hir_id, target)
184                }
185                Attribute::Parsed(AttributeKind::TrackCaller(attr_span)) => {
186                    self.check_track_caller(hir_id, *attr_span, attrs, target)
187                }
188                Attribute::Parsed(AttributeKind::NonExhaustive(attr_span)) => {
189                    self.check_non_exhaustive(*attr_span, span, target, item)
190                }
191                &Attribute::Parsed(AttributeKind::FfiPure(attr_span)) => {
192                    self.check_ffi_pure(attr_span, attrs)
193                }
194                Attribute::Parsed(AttributeKind::MayDangle(attr_span)) => {
195                    self.check_may_dangle(hir_id, *attr_span)
196                }
197                Attribute::Parsed(AttributeKind::MustUse { span, .. }) => {
198                    self.check_must_use(hir_id, *span, target)
199                }
200                &Attribute::Parsed(AttributeKind::CustomMir(dialect, phase, attr_span)) => {
201                    self.check_custom_mir(dialect, phase, attr_span)
202                }
203                Attribute::Parsed(
204                    AttributeKind::BodyStability { .. }
205                    | AttributeKind::ConstStabilityIndirect
206                    | AttributeKind::MacroTransparency(_)
207                    | AttributeKind::Pointee(..)
208                    | AttributeKind::Dummy
209                    | AttributeKind::RustcBuiltinMacro { .. }
210                    | AttributeKind::Ignore { .. }
211                    | AttributeKind::Path(..)
212                    | AttributeKind::NoImplicitPrelude(..)
213                    | AttributeKind::AutomaticallyDerived(..)
214                    | AttributeKind::Marker(..)
215                    | AttributeKind::SkipDuringMethodDispatch { .. }
216                    | AttributeKind::Coinductive(..)
217                    | AttributeKind::ConstTrait(..)
218                    | AttributeKind::DenyExplicitImpl(..)
219                    | AttributeKind::DoNotImplementViaObject(..)
220                    | AttributeKind::SpecializationTrait(..)
221                    | AttributeKind::UnsafeSpecializationMarker(..)
222                    | AttributeKind::ParenSugar(..)
223                    | AttributeKind::AllowIncoherentImpl(..)
224                    | AttributeKind::Confusables { .. }
225                    // `#[doc]` is actually a lot more than just doc comments, so is checked below
226                    | AttributeKind::DocComment {..}
227                    // handled below this loop and elsewhere
228                    | AttributeKind::Repr { .. }
229                    | AttributeKind::Cold(..)
230                    | AttributeKind::ExportName { .. }
231                    | AttributeKind::CoherenceIsCore
232                    | AttributeKind::Fundamental
233                    | AttributeKind::Optimize(..)
234                    | AttributeKind::LinkSection { .. }
235                    | AttributeKind::MacroUse { .. }
236                    | AttributeKind::MacroEscape( .. )
237                    | AttributeKind::RustcLayoutScalarValidRangeStart(..)
238                    | AttributeKind::RustcLayoutScalarValidRangeEnd(..)
239                    | AttributeKind::ExportStable
240                    | AttributeKind::FfiConst(..)
241                    | AttributeKind::UnstableFeatureBound(..)
242                    | AttributeKind::AsPtr(..)
243                    | AttributeKind::LinkName { .. }
244                    | AttributeKind::LinkOrdinal { .. }
245                    | AttributeKind::NoMangle(..)
246                    | AttributeKind::Used { .. }
247                    | AttributeKind::PassByValue (..)
248                    | AttributeKind::StdInternalSymbol (..)
249                    | AttributeKind::Coverage (..)
250                    | AttributeKind::ShouldPanic { .. }
251                    | AttributeKind::Coroutine(..)
252                    | AttributeKind::Linkage(..),
253                ) => { /* do nothing  */ }
254                Attribute::Unparsed(attr_item) => {
255                    style = Some(attr_item.style);
256                    match attr.path().as_slice() {
257                        [sym::diagnostic, sym::do_not_recommend, ..] => {
258                            self.check_do_not_recommend(attr.span(), hir_id, target, attr, item)
259                        }
260                        [sym::diagnostic, sym::on_unimplemented, ..] => {
261                            self.check_diagnostic_on_unimplemented(attr.span(), hir_id, target)
262                        }
263                        [sym::no_sanitize, ..] => {
264                            self.check_no_sanitize(attr, span, target)
265                        }
266                        [sym::thread_local, ..] => self.check_thread_local(attr, span, target),
267                        [sym::doc, ..] => self.check_doc_attrs(
268                            attr,
269                            attr_item.style,
270                            hir_id,
271                            target,
272                            &mut specified_inline,
273                            &mut doc_aliases,
274                        ),
275                        [sym::no_link, ..] => self.check_no_link(hir_id, attr, span, target),
276                        [sym::debugger_visualizer, ..] => self.check_debugger_visualizer(attr, target),
277                        [sym::rustc_no_implicit_autorefs, ..] => {
278                            self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
279                        }
280                        [sym::rustc_never_returns_null_ptr, ..] => {
281                            self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
282                        }
283                        [sym::rustc_legacy_const_generics, ..] => {
284                            self.check_rustc_legacy_const_generics(hir_id, attr, span, target, item)
285                        }
286                        [sym::rustc_lint_query_instability, ..] => {
287                            self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
288                        }
289                        [sym::rustc_lint_untracked_query_information, ..] => {
290                            self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
291                        }
292                        [sym::rustc_lint_diagnostics, ..] => {
293                            self.check_applied_to_fn_or_method(hir_id, attr.span(), span, target)
294                        }
295                        [sym::rustc_lint_opt_ty, ..] => self.check_rustc_lint_opt_ty(attr, span, target),
296                        [sym::rustc_lint_opt_deny_field_access, ..] => {
297                            self.check_rustc_lint_opt_deny_field_access(attr, span, target)
298                        }
299                        [sym::rustc_clean, ..]
300                        | [sym::rustc_dirty, ..]
301                        | [sym::rustc_if_this_changed, ..]
302                        | [sym::rustc_then_this_would_need, ..] => self.check_rustc_dirty_clean(attr),
303                        [sym::rustc_must_implement_one_of, ..] => self.check_must_be_applied_to_trait(attr.span(), span, target),
304                        [sym::collapse_debuginfo, ..] => self.check_collapse_debuginfo(attr, span, target),
305                        [sym::must_not_suspend, ..] => self.check_must_not_suspend(attr, span, target),
306                        [sym::rustc_has_incoherent_inherent_impls, ..] => {
307                            self.check_has_incoherent_inherent_impls(attr, span, target)
308                        }
309                        [sym::link, ..] => self.check_link(hir_id, attr, span, target),
310                        [sym::macro_export, ..] => self.check_macro_export(hir_id, attr, target),
311                        [sym::autodiff_forward, ..] | [sym::autodiff_reverse, ..] => {
312                            self.check_autodiff(hir_id, attr, span, target)
313                        }
314                        [
315                            // ok
316                            sym::allow
317                            | sym::expect
318                            | sym::warn
319                            | sym::deny
320                            | sym::forbid
321                            | sym::cfg
322                            | sym::cfg_attr
323                            | sym::cfg_trace
324                            | sym::cfg_attr_trace
325                            // need to be fixed
326                            | sym::cfi_encoding // FIXME(cfi_encoding)
327                            | sym::instruction_set // broken on stable!!!
328                            | sym::windows_subsystem // broken on stable!!!
329                            | sym::patchable_function_entry // FIXME(patchable_function_entry)
330                            | sym::deprecated_safe // FIXME(deprecated_safe)
331                            // internal
332                            | sym::prelude_import
333                            | sym::panic_handler
334                            | sym::lang
335                            | sym::needs_allocator
336                            | sym::default_lib_allocator,
337                            ..
338                        ] => {}
339                        [name, rest@..] => {
340                            match BUILTIN_ATTRIBUTE_MAP.get(name) {
341                                // checked below
342                                Some(BuiltinAttribute { type_: AttributeType::CrateLevel, .. }) => {}
343                                Some(_) => {
344                                    if rest.len() > 0 && AttributeParser::<Late>::is_parsed_attribute(slice::from_ref(name)) {
345                                        // Check if we tried to use a builtin attribute as an attribute namespace, like `#[must_use::skip]`.
346                                        // This check is here to solve https://github.com/rust-lang/rust/issues/137590
347                                        // An error is already produced for this case elsewhere
348                                        continue
349                                    }
350
351                                    // FIXME: differentiate between unstable and internal attributes just
352                                    // like we do with features instead of just accepting `rustc_`
353                                    // attributes by name. That should allow trimming the above list, too.
354                                    if !name.as_str().starts_with("rustc_") {
355                                        span_bug!(
356                                            attr.span(),
357                                            "builtin attribute {name:?} not handled by `CheckAttrVisitor`"
358                                        )
359                                    }
360                                }
361                                None => (),
362                            }
363                        }
364                        [] => unreachable!(),
365                    }
366                }
367            }
368
369            let builtin = attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name));
370
371            if hir_id != CRATE_HIR_ID {
372                if let Some(BuiltinAttribute { type_: AttributeType::CrateLevel, .. }) =
373                    attr.ident().and_then(|ident| BUILTIN_ATTRIBUTE_MAP.get(&ident.name))
374                {
375                    match style {
376                        Some(ast::AttrStyle::Outer) => self.tcx.emit_node_span_lint(
377                            UNUSED_ATTRIBUTES,
378                            hir_id,
379                            attr.span(),
380                            errors::OuterCrateLevelAttr,
381                        ),
382                        Some(ast::AttrStyle::Inner) | None => self.tcx.emit_node_span_lint(
383                            UNUSED_ATTRIBUTES,
384                            hir_id,
385                            attr.span(),
386                            errors::InnerCrateLevelAttr,
387                        ),
388                    }
389                }
390            }
391
392            if let Some(BuiltinAttribute { duplicates, .. }) = builtin {
393                check_duplicates(self.tcx, attr, hir_id, *duplicates, &mut seen);
394            }
395
396            self.check_unused_attribute(hir_id, attr, style)
397        }
398
399        self.check_repr(attrs, span, target, item, hir_id);
400        self.check_rustc_force_inline(hir_id, attrs, target);
401        self.check_mix_no_mangle_export(hir_id, attrs);
402    }
403
404    fn inline_attr_str_error_with_macro_def(&self, hir_id: HirId, attr_span: Span, sym: &str) {
405        self.tcx.emit_node_span_lint(
406            UNUSED_ATTRIBUTES,
407            hir_id,
408            attr_span,
409            errors::IgnoredAttrWithMacro { sym },
410        );
411    }
412
413    /// Checks if `#[diagnostic::do_not_recommend]` is applied on a trait impl and that it has no
414    /// arguments.
415    fn check_do_not_recommend(
416        &self,
417        attr_span: Span,
418        hir_id: HirId,
419        target: Target,
420        attr: &Attribute,
421        item: Option<ItemLike<'_>>,
422    ) {
423        if !matches!(target, Target::Impl { .. })
424            || matches!(
425                item,
426                Some(ItemLike::Item(hir::Item {  kind: hir::ItemKind::Impl(_impl),.. }))
427                    if _impl.of_trait.is_none()
428            )
429        {
430            self.tcx.emit_node_span_lint(
431                MISPLACED_DIAGNOSTIC_ATTRIBUTES,
432                hir_id,
433                attr_span,
434                errors::IncorrectDoNotRecommendLocation,
435            );
436        }
437        if !attr.is_word() {
438            self.tcx.emit_node_span_lint(
439                MALFORMED_DIAGNOSTIC_ATTRIBUTES,
440                hir_id,
441                attr_span,
442                errors::DoNotRecommendDoesNotExpectArgs,
443            );
444        }
445    }
446
447    /// Checks if `#[diagnostic::on_unimplemented]` is applied to a trait definition
448    fn check_diagnostic_on_unimplemented(&self, attr_span: Span, hir_id: HirId, target: Target) {
449        if !matches!(target, Target::Trait) {
450            self.tcx.emit_node_span_lint(
451                MISPLACED_DIAGNOSTIC_ATTRIBUTES,
452                hir_id,
453                attr_span,
454                DiagnosticOnUnimplementedOnlyForTraits,
455            );
456        }
457    }
458
459    /// Checks if an `#[inline]` is applied to a function or a closure.
460    fn check_inline(&self, hir_id: HirId, attr_span: Span, kind: &InlineAttr, target: Target) {
461        match target {
462            Target::Fn
463            | Target::Closure
464            | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {
465                // `#[inline]` is ignored if the symbol must be codegened upstream because it's exported.
466                if let Some(did) = hir_id.as_owner()
467                    && self.tcx.def_kind(did).has_codegen_attrs()
468                    && kind != &InlineAttr::Never
469                {
470                    let attrs = self.tcx.codegen_fn_attrs(did);
471                    // Not checking naked as `#[inline]` is forbidden for naked functions anyways.
472                    if attrs.contains_extern_indicator(self.tcx, did.into()) {
473                        self.tcx.emit_node_span_lint(
474                            UNUSED_ATTRIBUTES,
475                            hir_id,
476                            attr_span,
477                            errors::InlineIgnoredForExported {},
478                        );
479                    }
480                }
481            }
482            _ => {}
483        }
484    }
485
486    fn check_no_sanitize(&self, attr: &Attribute, span: Span, target: Target) {
487        if let Some(list) = attr.meta_item_list() {
488            for item in list.iter() {
489                let sym = item.name();
490                match sym {
491                    Some(s @ sym::address | s @ sym::hwaddress) => {
492                        let is_valid =
493                            matches!(target, Target::Fn | Target::Method(..) | Target::Static);
494                        if !is_valid {
495                            self.dcx().emit_err(errors::NoSanitize {
496                                attr_span: item.span(),
497                                defn_span: span,
498                                accepted_kind: "a function or static",
499                                attr_str: s.as_str(),
500                            });
501                        }
502                    }
503                    _ => {
504                        let is_valid = matches!(target, Target::Fn | Target::Method(..));
505                        if !is_valid {
506                            self.dcx().emit_err(errors::NoSanitize {
507                                attr_span: item.span(),
508                                defn_span: span,
509                                accepted_kind: "a function",
510                                attr_str: &match sym {
511                                    Some(name) => name.to_string(),
512                                    None => "...".to_string(),
513                                },
514                            });
515                        }
516                    }
517                }
518            }
519        }
520    }
521
522    /// Checks if `#[naked]` is applied to a function definition.
523    fn check_naked(&self, hir_id: HirId, target: Target) {
524        match target {
525            Target::Fn
526            | Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent) => {
527                let fn_sig = self.tcx.hir_node(hir_id).fn_sig().unwrap();
528                let abi = fn_sig.header.abi;
529                if abi.is_rustic_abi() && !self.tcx.features().naked_functions_rustic_abi() {
530                    feature_err(
531                        &self.tcx.sess,
532                        sym::naked_functions_rustic_abi,
533                        fn_sig.span,
534                        format!(
535                            "`#[naked]` is currently unstable on `extern \"{}\"` functions",
536                            abi.as_str()
537                        ),
538                    )
539                    .emit();
540                }
541            }
542            _ => {}
543        }
544    }
545
546    /// Debugging aid for `object_lifetime_default` query.
547    fn check_object_lifetime_default(&self, hir_id: HirId) {
548        let tcx = self.tcx;
549        if let Some(owner_id) = hir_id.as_owner()
550            && let Some(generics) = tcx.hir_get_generics(owner_id.def_id)
551        {
552            for p in generics.params {
553                let hir::GenericParamKind::Type { .. } = p.kind else { continue };
554                let default = tcx.object_lifetime_default(p.def_id);
555                let repr = match default {
556                    ObjectLifetimeDefault::Empty => "BaseDefault".to_owned(),
557                    ObjectLifetimeDefault::Static => "'static".to_owned(),
558                    ObjectLifetimeDefault::Param(def_id) => tcx.item_name(def_id).to_string(),
559                    ObjectLifetimeDefault::Ambiguous => "Ambiguous".to_owned(),
560                };
561                tcx.dcx().emit_err(errors::ObjectLifetimeErr { span: p.span, repr });
562            }
563        }
564    }
565
566    /// Checks if `#[collapse_debuginfo]` is applied to a macro.
567    fn check_collapse_debuginfo(&self, attr: &Attribute, span: Span, target: Target) {
568        match target {
569            Target::MacroDef => {}
570            _ => {
571                self.tcx.dcx().emit_err(errors::CollapseDebuginfo {
572                    attr_span: attr.span(),
573                    defn_span: span,
574                });
575            }
576        }
577    }
578
579    /// Checks if a `#[track_caller]` is applied to a function.
580    fn check_track_caller(
581        &self,
582        hir_id: HirId,
583        attr_span: Span,
584        attrs: &[Attribute],
585        target: Target,
586    ) {
587        match target {
588            Target::Fn => {
589                // `#[track_caller]` is not valid on weak lang items because they are called via
590                // `extern` declarations and `#[track_caller]` would alter their ABI.
591                if let Some((lang_item, _)) = hir::lang_items::extract(attrs)
592                    && let Some(item) = hir::LangItem::from_name(lang_item)
593                    && item.is_weak()
594                {
595                    let sig = self.tcx.hir_node(hir_id).fn_sig().unwrap();
596
597                    self.dcx().emit_err(errors::LangItemWithTrackCaller {
598                        attr_span,
599                        name: lang_item,
600                        sig_span: sig.span,
601                    });
602                }
603            }
604            _ => {}
605        }
606    }
607
608    /// Checks if the `#[non_exhaustive]` attribute on an `item` is valid.
609    fn check_non_exhaustive(
610        &self,
611        attr_span: Span,
612        span: Span,
613        target: Target,
614        item: Option<ItemLike<'_>>,
615    ) {
616        match target {
617            Target::Struct => {
618                if let Some(ItemLike::Item(hir::Item {
619                    kind: hir::ItemKind::Struct(_, _, hir::VariantData::Struct { fields, .. }),
620                    ..
621                })) = item
622                    && !fields.is_empty()
623                    && fields.iter().any(|f| f.default.is_some())
624                {
625                    self.dcx().emit_err(errors::NonExhaustiveWithDefaultFieldValues {
626                        attr_span,
627                        defn_span: span,
628                    });
629                }
630            }
631            _ => {}
632        }
633    }
634
635    /// Checks if the `#[target_feature]` attribute on `item` is valid.
636    fn check_target_feature(
637        &self,
638        hir_id: HirId,
639        attr_span: Span,
640        target: Target,
641        attrs: &[Attribute],
642    ) {
643        match target {
644            Target::Method(MethodKind::Trait { body: true } | MethodKind::Inherent)
645            | Target::Fn => {
646                // `#[target_feature]` is not allowed in lang items.
647                if let Some((lang_item, _)) = hir::lang_items::extract(attrs)
648                    // Calling functions with `#[target_feature]` is
649                    // not unsafe on WASM, see #84988
650                    && !self.tcx.sess.target.is_like_wasm
651                    && !self.tcx.sess.opts.actually_rustdoc
652                {
653                    let sig = self.tcx.hir_node(hir_id).fn_sig().unwrap();
654
655                    self.dcx().emit_err(errors::LangItemWithTargetFeature {
656                        attr_span,
657                        name: lang_item,
658                        sig_span: sig.span,
659                    });
660                }
661            }
662            _ => {}
663        }
664    }
665
666    /// Checks if the `#[thread_local]` attribute on `item` is valid.
667    fn check_thread_local(&self, attr: &Attribute, span: Span, target: Target) {
668        match target {
669            Target::ForeignStatic | Target::Static => {}
670            _ => {
671                self.dcx().emit_err(errors::AttrShouldBeAppliedToStatic {
672                    attr_span: attr.span(),
673                    defn_span: span,
674                });
675            }
676        }
677    }
678
679    fn doc_attr_str_error(&self, meta: &MetaItemInner, attr_name: &str) {
680        self.dcx().emit_err(errors::DocExpectStr { attr_span: meta.span(), attr_name });
681    }
682
683    fn check_doc_alias_value(
684        &self,
685        meta: &MetaItemInner,
686        doc_alias: Symbol,
687        hir_id: HirId,
688        target: Target,
689        is_list: bool,
690        aliases: &mut FxHashMap<String, Span>,
691    ) {
692        let tcx = self.tcx;
693        let span = meta.name_value_literal_span().unwrap_or_else(|| meta.span());
694        let attr_str =
695            &format!("`#[doc(alias{})]`", if is_list { "(\"...\")" } else { " = \"...\"" });
696        if doc_alias == sym::empty {
697            tcx.dcx().emit_err(errors::DocAliasEmpty { span, attr_str });
698            return;
699        }
700
701        let doc_alias_str = doc_alias.as_str();
702        if let Some(c) = doc_alias_str
703            .chars()
704            .find(|&c| c == '"' || c == '\'' || (c.is_whitespace() && c != ' '))
705        {
706            tcx.dcx().emit_err(errors::DocAliasBadChar { span, attr_str, char_: c });
707            return;
708        }
709        if doc_alias_str.starts_with(' ') || doc_alias_str.ends_with(' ') {
710            tcx.dcx().emit_err(errors::DocAliasStartEnd { span, attr_str });
711            return;
712        }
713
714        let span = meta.span();
715        if let Some(location) = match target {
716            Target::AssocTy => {
717                if let DefKind::Impl { .. } =
718                    self.tcx.def_kind(self.tcx.local_parent(hir_id.owner.def_id))
719                {
720                    Some("type alias in implementation block")
721                } else {
722                    None
723                }
724            }
725            Target::AssocConst => {
726                let parent_def_id = self.tcx.hir_get_parent_item(hir_id).def_id;
727                let containing_item = self.tcx.hir_expect_item(parent_def_id);
728                // We can't link to trait impl's consts.
729                let err = "associated constant in trait implementation block";
730                match containing_item.kind {
731                    ItemKind::Impl(hir::Impl { of_trait: Some(_), .. }) => Some(err),
732                    _ => None,
733                }
734            }
735            // we check the validity of params elsewhere
736            Target::Param => return,
737            Target::Expression
738            | Target::Statement
739            | Target::Arm
740            | Target::ForeignMod
741            | Target::Closure
742            | Target::Impl { .. }
743            | Target::WherePredicate => Some(target.name()),
744            Target::ExternCrate
745            | Target::Use
746            | Target::Static
747            | Target::Const
748            | Target::Fn
749            | Target::Mod
750            | Target::GlobalAsm
751            | Target::TyAlias
752            | Target::Enum
753            | Target::Variant
754            | Target::Struct
755            | Target::Field
756            | Target::Union
757            | Target::Trait
758            | Target::TraitAlias
759            | Target::Method(..)
760            | Target::ForeignFn
761            | Target::ForeignStatic
762            | Target::ForeignTy
763            | Target::GenericParam { .. }
764            | Target::MacroDef
765            | Target::PatField
766            | Target::ExprField
767            | Target::Crate
768            | Target::MacroCall
769            | Target::Delegation { .. } => None,
770        } {
771            tcx.dcx().emit_err(errors::DocAliasBadLocation { span, attr_str, location });
772            return;
773        }
774        if self.tcx.hir_opt_name(hir_id) == Some(doc_alias) {
775            tcx.dcx().emit_err(errors::DocAliasNotAnAlias { span, attr_str });
776            return;
777        }
778        if let Err(entry) = aliases.try_insert(doc_alias_str.to_owned(), span) {
779            self.tcx.emit_node_span_lint(
780                UNUSED_ATTRIBUTES,
781                hir_id,
782                span,
783                errors::DocAliasDuplicated { first_defn: *entry.entry.get() },
784            );
785        }
786    }
787
788    fn check_doc_alias(
789        &self,
790        meta: &MetaItemInner,
791        hir_id: HirId,
792        target: Target,
793        aliases: &mut FxHashMap<String, Span>,
794    ) {
795        if let Some(values) = meta.meta_item_list() {
796            for v in values {
797                match v.lit() {
798                    Some(l) => match l.kind {
799                        LitKind::Str(s, _) => {
800                            self.check_doc_alias_value(v, s, hir_id, target, true, aliases);
801                        }
802                        _ => {
803                            self.tcx
804                                .dcx()
805                                .emit_err(errors::DocAliasNotStringLiteral { span: v.span() });
806                        }
807                    },
808                    None => {
809                        self.tcx
810                            .dcx()
811                            .emit_err(errors::DocAliasNotStringLiteral { span: v.span() });
812                    }
813                }
814            }
815        } else if let Some(doc_alias) = meta.value_str() {
816            self.check_doc_alias_value(meta, doc_alias, hir_id, target, false, aliases)
817        } else {
818            self.dcx().emit_err(errors::DocAliasMalformed { span: meta.span() });
819        }
820    }
821
822    fn check_doc_keyword(&self, meta: &MetaItemInner, hir_id: HirId) {
823        fn is_doc_keyword(s: Symbol) -> bool {
824            // FIXME: Once rustdoc can handle URL conflicts on case insensitive file systems, we
825            // can remove the `SelfTy` case here, remove `sym::SelfTy`, and update the
826            // `#[doc(keyword = "SelfTy")` attribute in `library/std/src/keyword_docs.rs`.
827            s.is_reserved(|| edition::LATEST_STABLE_EDITION) || s.is_weak() || s == sym::SelfTy
828        }
829
830        let doc_keyword = match meta.value_str() {
831            Some(value) if value != sym::empty => value,
832            _ => return self.doc_attr_str_error(meta, "keyword"),
833        };
834
835        let item_kind = match self.tcx.hir_node(hir_id) {
836            hir::Node::Item(item) => Some(&item.kind),
837            _ => None,
838        };
839        match item_kind {
840            Some(ItemKind::Mod(_, module)) => {
841                if !module.item_ids.is_empty() {
842                    self.dcx().emit_err(errors::DocKeywordEmptyMod { span: meta.span() });
843                    return;
844                }
845            }
846            _ => {
847                self.dcx().emit_err(errors::DocKeywordNotMod { span: meta.span() });
848                return;
849            }
850        }
851        if !is_doc_keyword(doc_keyword) {
852            self.dcx().emit_err(errors::DocKeywordNotKeyword {
853                span: meta.name_value_literal_span().unwrap_or_else(|| meta.span()),
854                keyword: doc_keyword,
855            });
856        }
857    }
858
859    fn check_doc_fake_variadic(&self, meta: &MetaItemInner, hir_id: HirId) {
860        let item_kind = match self.tcx.hir_node(hir_id) {
861            hir::Node::Item(item) => Some(&item.kind),
862            _ => None,
863        };
864        match item_kind {
865            Some(ItemKind::Impl(i)) => {
866                let is_valid = doc_fake_variadic_is_allowed_self_ty(i.self_ty)
867                    || if let Some(&[hir::GenericArg::Type(ty)]) = i
868                        .of_trait
869                        .and_then(|of_trait| of_trait.trait_ref.path.segments.last())
870                        .map(|last_segment| last_segment.args().args)
871                    {
872                        matches!(&ty.kind, hir::TyKind::Tup([_]))
873                    } else {
874                        false
875                    };
876                if !is_valid {
877                    self.dcx().emit_err(errors::DocFakeVariadicNotValid { span: meta.span() });
878                }
879            }
880            _ => {
881                self.dcx().emit_err(errors::DocKeywordOnlyImpl { span: meta.span() });
882            }
883        }
884    }
885
886    fn check_doc_search_unbox(&self, meta: &MetaItemInner, hir_id: HirId) {
887        let hir::Node::Item(item) = self.tcx.hir_node(hir_id) else {
888            self.dcx().emit_err(errors::DocSearchUnboxInvalid { span: meta.span() });
889            return;
890        };
891        match item.kind {
892            ItemKind::Enum(_, generics, _) | ItemKind::Struct(_, generics, _)
893                if generics.params.len() != 0 => {}
894            ItemKind::Trait(_, _, _, _, generics, _, items)
895                if generics.params.len() != 0
896                    || items.iter().any(|item| {
897                        matches!(self.tcx.def_kind(item.owner_id), DefKind::AssocTy)
898                    }) => {}
899            ItemKind::TyAlias(_, generics, _) if generics.params.len() != 0 => {}
900            _ => {
901                self.dcx().emit_err(errors::DocSearchUnboxInvalid { span: meta.span() });
902            }
903        }
904    }
905
906    /// Checks `#[doc(inline)]`/`#[doc(no_inline)]` attributes.
907    ///
908    /// A doc inlining attribute is invalid if it is applied to a non-`use` item, or
909    /// if there are conflicting attributes for one item.
910    ///
911    /// `specified_inline` is used to keep track of whether we have
912    /// already seen an inlining attribute for this item.
913    /// If so, `specified_inline` holds the value and the span of
914    /// the first `inline`/`no_inline` attribute.
915    fn check_doc_inline(
916        &self,
917        style: AttrStyle,
918        meta: &MetaItemInner,
919        hir_id: HirId,
920        target: Target,
921        specified_inline: &mut Option<(bool, Span)>,
922    ) {
923        match target {
924            Target::Use | Target::ExternCrate => {
925                let do_inline = meta.has_name(sym::inline);
926                if let Some((prev_inline, prev_span)) = *specified_inline {
927                    if do_inline != prev_inline {
928                        let mut spans = MultiSpan::from_spans(vec![prev_span, meta.span()]);
929                        spans.push_span_label(prev_span, fluent::passes_doc_inline_conflict_first);
930                        spans.push_span_label(
931                            meta.span(),
932                            fluent::passes_doc_inline_conflict_second,
933                        );
934                        self.dcx().emit_err(errors::DocKeywordConflict { spans });
935                    }
936                } else {
937                    *specified_inline = Some((do_inline, meta.span()));
938                }
939            }
940            _ => {
941                self.tcx.emit_node_span_lint(
942                    INVALID_DOC_ATTRIBUTES,
943                    hir_id,
944                    meta.span(),
945                    errors::DocInlineOnlyUse {
946                        attr_span: meta.span(),
947                        item_span: (style == AttrStyle::Outer).then(|| self.tcx.hir_span(hir_id)),
948                    },
949                );
950            }
951        }
952    }
953
954    fn check_doc_masked(
955        &self,
956        style: AttrStyle,
957        meta: &MetaItemInner,
958        hir_id: HirId,
959        target: Target,
960    ) {
961        if target != Target::ExternCrate {
962            self.tcx.emit_node_span_lint(
963                INVALID_DOC_ATTRIBUTES,
964                hir_id,
965                meta.span(),
966                errors::DocMaskedOnlyExternCrate {
967                    attr_span: meta.span(),
968                    item_span: (style == AttrStyle::Outer).then(|| self.tcx.hir_span(hir_id)),
969                },
970            );
971            return;
972        }
973
974        if self.tcx.extern_mod_stmt_cnum(hir_id.owner.def_id).is_none() {
975            self.tcx.emit_node_span_lint(
976                INVALID_DOC_ATTRIBUTES,
977                hir_id,
978                meta.span(),
979                errors::DocMaskedNotExternCrateSelf {
980                    attr_span: meta.span(),
981                    item_span: (style == AttrStyle::Outer).then(|| self.tcx.hir_span(hir_id)),
982                },
983            );
984        }
985    }
986
987    /// Checks that an attribute is *not* used at the crate level. Returns `true` if valid.
988    fn check_attr_not_crate_level(
989        &self,
990        meta: &MetaItemInner,
991        hir_id: HirId,
992        attr_name: &str,
993    ) -> bool {
994        if CRATE_HIR_ID == hir_id {
995            self.dcx().emit_err(errors::DocAttrNotCrateLevel { span: meta.span(), attr_name });
996            return false;
997        }
998        true
999    }
1000
1001    /// Checks that an attribute is used at the crate level. Returns `true` if valid.
1002    fn check_attr_crate_level(
1003        &self,
1004        attr: &Attribute,
1005        style: AttrStyle,
1006        meta: &MetaItemInner,
1007        hir_id: HirId,
1008    ) -> bool {
1009        if hir_id != CRATE_HIR_ID {
1010            // insert a bang between `#` and `[...`
1011            let bang_span = attr.span().lo() + BytePos(1);
1012            let sugg = (style == AttrStyle::Outer
1013                && self.tcx.hir_get_parent_item(hir_id) == CRATE_OWNER_ID)
1014                .then_some(errors::AttrCrateLevelOnlySugg {
1015                    attr: attr.span().with_lo(bang_span).with_hi(bang_span),
1016                });
1017            self.tcx.emit_node_span_lint(
1018                INVALID_DOC_ATTRIBUTES,
1019                hir_id,
1020                meta.span(),
1021                errors::AttrCrateLevelOnly { sugg },
1022            );
1023            return false;
1024        }
1025        true
1026    }
1027
1028    /// Checks that `doc(test(...))` attribute contains only valid attributes and are at the right place.
1029    fn check_test_attr(
1030        &self,
1031        attr: &Attribute,
1032        style: AttrStyle,
1033        meta: &MetaItemInner,
1034        hir_id: HirId,
1035    ) {
1036        if let Some(metas) = meta.meta_item_list() {
1037            for i_meta in metas {
1038                match (i_meta.name(), i_meta.meta_item()) {
1039                    (Some(sym::attr), _) => {
1040                        // Allowed everywhere like `#[doc]`
1041                    }
1042                    (Some(sym::no_crate_inject), _) => {
1043                        self.check_attr_crate_level(attr, style, meta, hir_id);
1044                    }
1045                    (_, Some(m)) => {
1046                        self.tcx.emit_node_span_lint(
1047                            INVALID_DOC_ATTRIBUTES,
1048                            hir_id,
1049                            i_meta.span(),
1050                            errors::DocTestUnknown {
1051                                path: rustc_ast_pretty::pprust::path_to_string(&m.path),
1052                            },
1053                        );
1054                    }
1055                    (_, None) => {
1056                        self.tcx.emit_node_span_lint(
1057                            INVALID_DOC_ATTRIBUTES,
1058                            hir_id,
1059                            i_meta.span(),
1060                            errors::DocTestLiteral,
1061                        );
1062                    }
1063                }
1064            }
1065        } else {
1066            self.tcx.emit_node_span_lint(
1067                INVALID_DOC_ATTRIBUTES,
1068                hir_id,
1069                meta.span(),
1070                errors::DocTestTakesList,
1071            );
1072        }
1073    }
1074
1075    /// Check that the `#![doc(cfg_hide(...))]` attribute only contains a list of attributes.
1076    ///
1077    fn check_doc_cfg_hide(&self, meta: &MetaItemInner, hir_id: HirId) {
1078        if meta.meta_item_list().is_none() {
1079            self.tcx.emit_node_span_lint(
1080                INVALID_DOC_ATTRIBUTES,
1081                hir_id,
1082                meta.span(),
1083                errors::DocCfgHideTakesList,
1084            );
1085        }
1086    }
1087
1088    /// Runs various checks on `#[doc]` attributes.
1089    ///
1090    /// `specified_inline` should be initialized to `None` and kept for the scope
1091    /// of one item. Read the documentation of [`check_doc_inline`] for more information.
1092    ///
1093    /// [`check_doc_inline`]: Self::check_doc_inline
1094    fn check_doc_attrs(
1095        &self,
1096        attr: &Attribute,
1097        style: AttrStyle,
1098        hir_id: HirId,
1099        target: Target,
1100        specified_inline: &mut Option<(bool, Span)>,
1101        aliases: &mut FxHashMap<String, Span>,
1102    ) {
1103        if let Some(list) = attr.meta_item_list() {
1104            for meta in &list {
1105                if let Some(i_meta) = meta.meta_item() {
1106                    match i_meta.name() {
1107                        Some(sym::alias) => {
1108                            if self.check_attr_not_crate_level(meta, hir_id, "alias") {
1109                                self.check_doc_alias(meta, hir_id, target, aliases);
1110                            }
1111                        }
1112
1113                        Some(sym::keyword) => {
1114                            if self.check_attr_not_crate_level(meta, hir_id, "keyword") {
1115                                self.check_doc_keyword(meta, hir_id);
1116                            }
1117                        }
1118
1119                        Some(sym::fake_variadic) => {
1120                            if self.check_attr_not_crate_level(meta, hir_id, "fake_variadic") {
1121                                self.check_doc_fake_variadic(meta, hir_id);
1122                            }
1123                        }
1124
1125                        Some(sym::search_unbox) => {
1126                            if self.check_attr_not_crate_level(meta, hir_id, "fake_variadic") {
1127                                self.check_doc_search_unbox(meta, hir_id);
1128                            }
1129                        }
1130
1131                        Some(sym::test) => {
1132                            self.check_test_attr(attr, style, meta, hir_id);
1133                        }
1134
1135                        Some(
1136                            sym::html_favicon_url
1137                            | sym::html_logo_url
1138                            | sym::html_playground_url
1139                            | sym::issue_tracker_base_url
1140                            | sym::html_root_url
1141                            | sym::html_no_source,
1142                        ) => {
1143                            self.check_attr_crate_level(attr, style, meta, hir_id);
1144                        }
1145
1146                        Some(sym::cfg_hide) => {
1147                            if self.check_attr_crate_level(attr, style, meta, hir_id) {
1148                                self.check_doc_cfg_hide(meta, hir_id);
1149                            }
1150                        }
1151
1152                        Some(sym::inline | sym::no_inline) => {
1153                            self.check_doc_inline(style, meta, hir_id, target, specified_inline)
1154                        }
1155
1156                        Some(sym::masked) => self.check_doc_masked(style, meta, hir_id, target),
1157
1158                        Some(sym::cfg | sym::hidden | sym::notable_trait) => {}
1159
1160                        Some(sym::rust_logo) => {
1161                            if self.check_attr_crate_level(attr, style, meta, hir_id)
1162                                && !self.tcx.features().rustdoc_internals()
1163                            {
1164                                feature_err(
1165                                    &self.tcx.sess,
1166                                    sym::rustdoc_internals,
1167                                    meta.span(),
1168                                    fluent::passes_doc_rust_logo,
1169                                )
1170                                .emit();
1171                            }
1172                        }
1173
1174                        _ => {
1175                            let path = rustc_ast_pretty::pprust::path_to_string(&i_meta.path);
1176                            if i_meta.has_name(sym::spotlight) {
1177                                self.tcx.emit_node_span_lint(
1178                                    INVALID_DOC_ATTRIBUTES,
1179                                    hir_id,
1180                                    i_meta.span,
1181                                    errors::DocTestUnknownSpotlight { path, span: i_meta.span },
1182                                );
1183                            } else if i_meta.has_name(sym::include)
1184                                && let Some(value) = i_meta.value_str()
1185                            {
1186                                let applicability = if list.len() == 1 {
1187                                    Applicability::MachineApplicable
1188                                } else {
1189                                    Applicability::MaybeIncorrect
1190                                };
1191                                // If there are multiple attributes, the suggestion would suggest
1192                                // deleting all of them, which is incorrect.
1193                                self.tcx.emit_node_span_lint(
1194                                    INVALID_DOC_ATTRIBUTES,
1195                                    hir_id,
1196                                    i_meta.span,
1197                                    errors::DocTestUnknownInclude {
1198                                        path,
1199                                        value: value.to_string(),
1200                                        inner: match style {
1201                                            AttrStyle::Inner => "!",
1202                                            AttrStyle::Outer => "",
1203                                        },
1204                                        sugg: (attr.span(), applicability),
1205                                    },
1206                                );
1207                            } else if i_meta.has_name(sym::passes)
1208                                || i_meta.has_name(sym::no_default_passes)
1209                            {
1210                                self.tcx.emit_node_span_lint(
1211                                    INVALID_DOC_ATTRIBUTES,
1212                                    hir_id,
1213                                    i_meta.span,
1214                                    errors::DocTestUnknownPasses { path, span: i_meta.span },
1215                                );
1216                            } else if i_meta.has_name(sym::plugins) {
1217                                self.tcx.emit_node_span_lint(
1218                                    INVALID_DOC_ATTRIBUTES,
1219                                    hir_id,
1220                                    i_meta.span,
1221                                    errors::DocTestUnknownPlugins { path, span: i_meta.span },
1222                                );
1223                            } else {
1224                                self.tcx.emit_node_span_lint(
1225                                    INVALID_DOC_ATTRIBUTES,
1226                                    hir_id,
1227                                    i_meta.span,
1228                                    errors::DocTestUnknownAny { path },
1229                                );
1230                            }
1231                        }
1232                    }
1233                } else {
1234                    self.tcx.emit_node_span_lint(
1235                        INVALID_DOC_ATTRIBUTES,
1236                        hir_id,
1237                        meta.span(),
1238                        errors::DocInvalid,
1239                    );
1240                }
1241            }
1242        }
1243    }
1244
1245    fn check_has_incoherent_inherent_impls(&self, attr: &Attribute, span: Span, target: Target) {
1246        match target {
1247            Target::Trait | Target::Struct | Target::Enum | Target::Union | Target::ForeignTy => {}
1248            _ => {
1249                self.tcx
1250                    .dcx()
1251                    .emit_err(errors::HasIncoherentInherentImpl { attr_span: attr.span(), span });
1252            }
1253        }
1254    }
1255
1256    fn check_ffi_pure(&self, attr_span: Span, attrs: &[Attribute]) {
1257        if find_attr!(attrs, AttributeKind::FfiConst(_)) {
1258            // `#[ffi_const]` functions cannot be `#[ffi_pure]`
1259            self.dcx().emit_err(errors::BothFfiConstAndPure { attr_span });
1260        }
1261    }
1262
1263    /// Warns against some misuses of `#[must_use]`
1264    fn check_must_use(&self, hir_id: HirId, attr_span: Span, target: Target) {
1265        if matches!(
1266            target,
1267            Target::Fn
1268                | Target::Enum
1269                | Target::Struct
1270                | Target::Union
1271                | Target::Method(MethodKind::Trait { body: false } | MethodKind::Inherent)
1272                | Target::ForeignFn
1273                // `impl Trait` in return position can trip
1274                // `unused_must_use` if `Trait` is marked as
1275                // `#[must_use]`
1276                | Target::Trait
1277        ) {
1278            return;
1279        }
1280
1281        // `#[must_use]` can be applied to a trait method definition with a default body
1282        if let Target::Method(MethodKind::Trait { body: true }) = target
1283            && let parent_def_id = self.tcx.hir_get_parent_item(hir_id).def_id
1284            && let containing_item = self.tcx.hir_expect_item(parent_def_id)
1285            && let hir::ItemKind::Trait(..) = containing_item.kind
1286        {
1287            return;
1288        }
1289
1290        self.tcx.emit_node_span_lint(
1291            UNUSED_ATTRIBUTES,
1292            hir_id,
1293            attr_span,
1294            errors::MustUseNoEffect { target: target.plural_name(), attr_span },
1295        );
1296    }
1297
1298    /// Checks if `#[must_not_suspend]` is applied to a struct, enum, union, or trait.
1299    fn check_must_not_suspend(&self, attr: &Attribute, span: Span, target: Target) {
1300        match target {
1301            Target::Struct | Target::Enum | Target::Union | Target::Trait => {}
1302            _ => {
1303                self.dcx().emit_err(errors::MustNotSuspend { attr_span: attr.span(), span });
1304            }
1305        }
1306    }
1307
1308    /// Checks if `#[may_dangle]` is applied to a lifetime or type generic parameter in `Drop` impl.
1309    fn check_may_dangle(&self, hir_id: HirId, attr_span: Span) {
1310        if let hir::Node::GenericParam(param) = self.tcx.hir_node(hir_id)
1311            && matches!(
1312                param.kind,
1313                hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. }
1314            )
1315            && matches!(param.source, hir::GenericParamSource::Generics)
1316            && let parent_hir_id = self.tcx.parent_hir_id(hir_id)
1317            && let hir::Node::Item(item) = self.tcx.hir_node(parent_hir_id)
1318            && let hir::ItemKind::Impl(impl_) = item.kind
1319            && let Some(of_trait) = impl_.of_trait
1320            && let Some(def_id) = of_trait.trait_ref.trait_def_id()
1321            && self.tcx.is_lang_item(def_id, hir::LangItem::Drop)
1322        {
1323            return;
1324        }
1325
1326        self.dcx().emit_err(errors::InvalidMayDangle { attr_span });
1327    }
1328
1329    /// Checks if `#[link]` is applied to an item other than a foreign module.
1330    fn check_link(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
1331        if target == Target::ForeignMod
1332            && let hir::Node::Item(item) = self.tcx.hir_node(hir_id)
1333            && let Item { kind: ItemKind::ForeignMod { abi, .. }, .. } = item
1334            && !matches!(abi, ExternAbi::Rust)
1335        {
1336            return;
1337        }
1338
1339        self.tcx.emit_node_span_lint(
1340            UNUSED_ATTRIBUTES,
1341            hir_id,
1342            attr.span(),
1343            errors::Link { span: (target != Target::ForeignMod).then_some(span) },
1344        );
1345    }
1346
1347    /// Checks if `#[no_link]` is applied to an `extern crate`.
1348    fn check_no_link(&self, hir_id: HirId, attr: &Attribute, span: Span, target: Target) {
1349        match target {
1350            Target::ExternCrate => {}
1351            // FIXME(#80564): We permit struct fields, match arms and macro defs to have an
1352            // `#[no_link]` attribute with just a lint, because we previously
1353            // erroneously allowed it and some crates used it accidentally, to be compatible
1354            // with crates depending on them, we can't throw an error here.
1355            Target::Field | Target::Arm | Target::MacroDef => {
1356                self.inline_attr_str_error_with_macro_def(hir_id, attr.span(), "no_link");
1357            }
1358            _ => {
1359                self.dcx().emit_err(errors::NoLink { attr_span: attr.span(), span });
1360            }
1361        }
1362    }
1363
1364    /// Checks if `#[rustc_legacy_const_generics]` is applied to a function and has a valid argument.
1365    fn check_rustc_legacy_const_generics(
1366        &self,
1367        hir_id: HirId,
1368        attr: &Attribute,
1369        span: Span,
1370        target: Target,
1371        item: Option<ItemLike<'_>>,
1372    ) {
1373        let is_function = matches!(target, Target::Fn);
1374        if !is_function {
1375            self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
1376                attr_span: attr.span(),
1377                defn_span: span,
1378                on_crate: hir_id == CRATE_HIR_ID,
1379            });
1380            return;
1381        }
1382
1383        let Some(list) = attr.meta_item_list() else {
1384            // The attribute form is validated on AST.
1385            return;
1386        };
1387
1388        let Some(ItemLike::Item(Item {
1389            kind: ItemKind::Fn { sig: FnSig { decl, .. }, generics, .. },
1390            ..
1391        })) = item
1392        else {
1393            bug!("should be a function item");
1394        };
1395
1396        for param in generics.params {
1397            match param.kind {
1398                hir::GenericParamKind::Const { .. } => {}
1399                _ => {
1400                    self.dcx().emit_err(errors::RustcLegacyConstGenericsOnly {
1401                        attr_span: attr.span(),
1402                        param_span: param.span,
1403                    });
1404                    return;
1405                }
1406            }
1407        }
1408
1409        if list.len() != generics.params.len() {
1410            self.dcx().emit_err(errors::RustcLegacyConstGenericsIndex {
1411                attr_span: attr.span(),
1412                generics_span: generics.span,
1413            });
1414            return;
1415        }
1416
1417        let arg_count = decl.inputs.len() as u128 + generics.params.len() as u128;
1418        let mut invalid_args = vec![];
1419        for meta in list {
1420            if let Some(LitKind::Int(val, _)) = meta.lit().map(|lit| &lit.kind) {
1421                if *val >= arg_count {
1422                    let span = meta.span();
1423                    self.dcx().emit_err(errors::RustcLegacyConstGenericsIndexExceed {
1424                        span,
1425                        arg_count: arg_count as usize,
1426                    });
1427                    return;
1428                }
1429            } else {
1430                invalid_args.push(meta.span());
1431            }
1432        }
1433
1434        if !invalid_args.is_empty() {
1435            self.dcx().emit_err(errors::RustcLegacyConstGenericsIndexNegative { invalid_args });
1436        }
1437    }
1438
1439    /// Helper function for checking that the provided attribute is only applied to a function or
1440    /// method.
1441    fn check_applied_to_fn_or_method(
1442        &self,
1443        hir_id: HirId,
1444        attr_span: Span,
1445        defn_span: Span,
1446        target: Target,
1447    ) {
1448        let is_function = matches!(target, Target::Fn | Target::Method(..));
1449        if !is_function {
1450            self.dcx().emit_err(errors::AttrShouldBeAppliedToFn {
1451                attr_span,
1452                defn_span,
1453                on_crate: hir_id == CRATE_HIR_ID,
1454            });
1455        }
1456    }
1457
1458    /// Checks that the `#[rustc_lint_opt_ty]` attribute is only applied to a struct.
1459    fn check_rustc_lint_opt_ty(&self, attr: &Attribute, span: Span, target: Target) {
1460        match target {
1461            Target::Struct => {}
1462            _ => {
1463                self.dcx().emit_err(errors::RustcLintOptTy { attr_span: attr.span(), span });
1464            }
1465        }
1466    }
1467
1468    /// Checks that the `#[rustc_lint_opt_deny_field_access]` attribute is only applied to a field.
1469    fn check_rustc_lint_opt_deny_field_access(&self, attr: &Attribute, span: Span, target: Target) {
1470        match target {
1471            Target::Field => {}
1472            _ => {
1473                self.tcx
1474                    .dcx()
1475                    .emit_err(errors::RustcLintOptDenyFieldAccess { attr_span: attr.span(), span });
1476            }
1477        }
1478    }
1479
1480    /// Checks that the dep-graph debugging attributes are only present when the query-dep-graph
1481    /// option is passed to the compiler.
1482    fn check_rustc_dirty_clean(&self, attr: &Attribute) {
1483        if !self.tcx.sess.opts.unstable_opts.query_dep_graph {
1484            self.dcx().emit_err(errors::RustcDirtyClean { span: attr.span() });
1485        }
1486    }
1487
1488    /// Checks if the attribute is applied to a trait.
1489    fn check_must_be_applied_to_trait(&self, attr_span: Span, defn_span: Span, target: Target) {
1490        match target {
1491            Target::Trait => {}
1492            _ => {
1493                self.dcx().emit_err(errors::AttrShouldBeAppliedToTrait { attr_span, defn_span });
1494            }
1495        }
1496    }
1497
1498    /// Checks if the `#[repr]` attributes on `item` are valid.
1499    fn check_repr(
1500        &self,
1501        attrs: &[Attribute],
1502        span: Span,
1503        target: Target,
1504        item: Option<ItemLike<'_>>,
1505        hir_id: HirId,
1506    ) {
1507        // Extract the names of all repr hints, e.g., [foo, bar, align] for:
1508        // ```
1509        // #[repr(foo)]
1510        // #[repr(bar, align(8))]
1511        // ```
1512        let (reprs, first_attr_span) = find_attr!(attrs, AttributeKind::Repr { reprs, first_span } => (reprs.as_slice(), Some(*first_span))).unwrap_or((&[], None));
1513
1514        let mut int_reprs = 0;
1515        let mut is_explicit_rust = false;
1516        let mut is_c = false;
1517        let mut is_simd = false;
1518        let mut is_transparent = false;
1519
1520        for (repr, repr_span) in reprs {
1521            match repr {
1522                ReprAttr::ReprRust => {
1523                    is_explicit_rust = true;
1524                    match target {
1525                        Target::Struct | Target::Union | Target::Enum => continue,
1526                        _ => {
1527                            self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1528                                hint_span: *repr_span,
1529                                span,
1530                            });
1531                        }
1532                    }
1533                }
1534                ReprAttr::ReprC => {
1535                    is_c = true;
1536                    match target {
1537                        Target::Struct | Target::Union | Target::Enum => continue,
1538                        _ => {
1539                            self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1540                                hint_span: *repr_span,
1541                                span,
1542                            });
1543                        }
1544                    }
1545                }
1546                ReprAttr::ReprAlign(align) => {
1547                    match target {
1548                        Target::Struct | Target::Union | Target::Enum => {}
1549                        Target::Fn | Target::Method(_) => {
1550                            self.dcx().emit_err(errors::ReprAlignShouldBeAlign {
1551                                span: *repr_span,
1552                                item: target.plural_name(),
1553                            });
1554                        }
1555                        _ => {
1556                            self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1557                                hint_span: *repr_span,
1558                                span,
1559                            });
1560                        }
1561                    }
1562
1563                    self.check_align(*align, *repr_span);
1564                }
1565                ReprAttr::ReprPacked(_) => {
1566                    if target != Target::Struct && target != Target::Union {
1567                        self.dcx().emit_err(errors::AttrApplication::StructUnion {
1568                            hint_span: *repr_span,
1569                            span,
1570                        });
1571                    } else {
1572                        continue;
1573                    }
1574                }
1575                ReprAttr::ReprSimd => {
1576                    is_simd = true;
1577                    if target != Target::Struct {
1578                        self.dcx().emit_err(errors::AttrApplication::Struct {
1579                            hint_span: *repr_span,
1580                            span,
1581                        });
1582                    } else {
1583                        continue;
1584                    }
1585                }
1586                ReprAttr::ReprTransparent => {
1587                    is_transparent = true;
1588                    match target {
1589                        Target::Struct | Target::Union | Target::Enum => continue,
1590                        _ => {
1591                            self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1592                                hint_span: *repr_span,
1593                                span,
1594                            });
1595                        }
1596                    }
1597                }
1598                ReprAttr::ReprInt(_) => {
1599                    int_reprs += 1;
1600                    if target != Target::Enum {
1601                        self.dcx().emit_err(errors::AttrApplication::Enum {
1602                            hint_span: *repr_span,
1603                            span,
1604                        });
1605                    } else {
1606                        continue;
1607                    }
1608                }
1609            };
1610        }
1611
1612        // catch `repr()` with no arguments, applied to an item (i.e. not `#![repr()]`)
1613        if let Some(first_attr_span) = first_attr_span
1614            && reprs.is_empty()
1615            && item.is_some()
1616        {
1617            match target {
1618                Target::Struct | Target::Union | Target::Enum => {}
1619                Target::Fn | Target::Method(_) => {
1620                    self.dcx().emit_err(errors::ReprAlignShouldBeAlign {
1621                        span: first_attr_span,
1622                        item: target.plural_name(),
1623                    });
1624                }
1625                _ => {
1626                    self.dcx().emit_err(errors::AttrApplication::StructEnumUnion {
1627                        hint_span: first_attr_span,
1628                        span,
1629                    });
1630                }
1631            }
1632            return;
1633        }
1634
1635        // Just point at all repr hints if there are any incompatibilities.
1636        // This is not ideal, but tracking precisely which ones are at fault is a huge hassle.
1637        let hint_spans = reprs.iter().map(|(_, span)| *span);
1638
1639        // Error on repr(transparent, <anything else>).
1640        if is_transparent && reprs.len() > 1 {
1641            let hint_spans = hint_spans.clone().collect();
1642            self.dcx().emit_err(errors::TransparentIncompatible {
1643                hint_spans,
1644                target: target.to_string(),
1645            });
1646        }
1647        if is_explicit_rust && (int_reprs > 0 || is_c || is_simd) {
1648            let hint_spans = hint_spans.clone().collect();
1649            self.dcx().emit_err(errors::ReprConflicting { hint_spans });
1650        }
1651        // Warn on repr(u8, u16), repr(C, simd), and c-like-enum-repr(C, u8)
1652        if (int_reprs > 1)
1653            || (is_simd && is_c)
1654            || (int_reprs == 1
1655                && is_c
1656                && item.is_some_and(|item| {
1657                    if let ItemLike::Item(item) = item { is_c_like_enum(item) } else { false }
1658                }))
1659        {
1660            self.tcx.emit_node_span_lint(
1661                CONFLICTING_REPR_HINTS,
1662                hir_id,
1663                hint_spans.collect::<Vec<Span>>(),
1664                errors::ReprConflictingLint,
1665            );
1666        }
1667    }
1668
1669    fn check_align(&self, align: Align, span: Span) {
1670        if align.bytes() > 2_u64.pow(29) {
1671            // for values greater than 2^29, a different error will be emitted, make sure that happens
1672            self.dcx().span_delayed_bug(
1673                span,
1674                "alignment greater than 2^29 should be errored on elsewhere",
1675            );
1676        } else {
1677            // only do this check when <= 2^29 to prevent duplicate errors:
1678            // alignment greater than 2^29 not supported
1679            // alignment is too large for the current target
1680
1681            let max = Size::from_bits(self.tcx.sess.target.pointer_width).signed_int_max() as u64;
1682            if align.bytes() > max {
1683                self.dcx().emit_err(errors::InvalidReprAlignForTarget { span, size: max });
1684            }
1685        }
1686    }
1687
1688    /// Outputs an error for attributes that can only be applied to macros, such as
1689    /// `#[allow_internal_unsafe]` and `#[allow_internal_unstable]`.
1690    /// (Allows proc_macro functions)
1691    // FIXME(jdonszelmann): if possible, move to attr parsing
1692    fn check_macro_only_attr(
1693        &self,
1694        attr_span: Span,
1695        span: Span,
1696        target: Target,
1697        attrs: &[Attribute],
1698    ) {
1699        match target {
1700            Target::Fn => {
1701                for attr in attrs {
1702                    if attr.is_proc_macro_attr() {
1703                        // return on proc macros
1704                        return;
1705                    }
1706                }
1707                self.tcx.dcx().emit_err(errors::MacroOnlyAttribute { attr_span, span });
1708            }
1709            _ => {}
1710        }
1711    }
1712
1713    /// Checks if the items on the `#[debugger_visualizer]` attribute are valid.
1714    fn check_debugger_visualizer(&self, attr: &Attribute, target: Target) {
1715        // Here we only check that the #[debugger_visualizer] attribute is attached
1716        // to nothing other than a module. All other checks are done in the
1717        // `debugger_visualizer` query where they need to be done for decoding
1718        // anyway.
1719        match target {
1720            Target::Mod => {}
1721            _ => {
1722                self.dcx().emit_err(errors::DebugVisualizerPlacement { span: attr.span() });
1723            }
1724        }
1725    }
1726
1727    /// Outputs an error for `#[allow_internal_unstable]` which can only be applied to macros.
1728    /// (Allows proc_macro functions)
1729    fn check_rustc_allow_const_fn_unstable(
1730        &self,
1731        hir_id: HirId,
1732        attr_span: Span,
1733        span: Span,
1734        target: Target,
1735    ) {
1736        match target {
1737            Target::Fn | Target::Method(_) => {
1738                if !self.tcx.is_const_fn(hir_id.expect_owner().to_def_id()) {
1739                    self.tcx.dcx().emit_err(errors::RustcAllowConstFnUnstable { attr_span, span });
1740                }
1741            }
1742            _ => {}
1743        }
1744    }
1745
1746    fn check_stability(
1747        &self,
1748        attr_span: Span,
1749        item_span: Span,
1750        level: &StabilityLevel,
1751        feature: Symbol,
1752    ) {
1753        // Stable *language* features shouldn't be used as unstable library features.
1754        // (Not doing this for stable library features is checked by tidy.)
1755        if level.is_unstable()
1756            && ACCEPTED_LANG_FEATURES.iter().find(|f| f.name == feature).is_some()
1757        {
1758            self.tcx
1759                .dcx()
1760                .emit_err(errors::UnstableAttrForAlreadyStableFeature { attr_span, item_span });
1761        }
1762    }
1763
1764    fn check_deprecated(&self, hir_id: HirId, attr: &Attribute, _span: Span, target: Target) {
1765        match target {
1766            Target::AssocConst | Target::Method(..) | Target::AssocTy
1767                if matches!(
1768                    self.tcx.def_kind(self.tcx.local_parent(hir_id.owner.def_id)),
1769                    DefKind::Impl { of_trait: true }
1770                ) =>
1771            {
1772                self.tcx.emit_node_span_lint(
1773                    UNUSED_ATTRIBUTES,
1774                    hir_id,
1775                    attr.span(),
1776                    errors::DeprecatedAnnotationHasNoEffect { span: attr.span() },
1777                );
1778            }
1779            _ => {}
1780        }
1781    }
1782
1783    fn check_macro_export(&self, hir_id: HirId, attr: &Attribute, target: Target) {
1784        if target != Target::MacroDef {
1785            self.tcx.emit_node_span_lint(
1786                UNUSED_ATTRIBUTES,
1787                hir_id,
1788                attr.span(),
1789                errors::MacroExport::Normal,
1790            );
1791        } else if let Some(meta_item_list) = attr.meta_item_list()
1792            && !meta_item_list.is_empty()
1793        {
1794            if meta_item_list.len() > 1 {
1795                self.tcx.emit_node_span_lint(
1796                    INVALID_MACRO_EXPORT_ARGUMENTS,
1797                    hir_id,
1798                    attr.span(),
1799                    errors::MacroExport::TooManyItems,
1800                );
1801            } else if !meta_item_list[0].has_name(sym::local_inner_macros) {
1802                self.tcx.emit_node_span_lint(
1803                    INVALID_MACRO_EXPORT_ARGUMENTS,
1804                    hir_id,
1805                    meta_item_list[0].span(),
1806                    errors::MacroExport::InvalidArgument,
1807                );
1808            }
1809        } else {
1810            // special case when `#[macro_export]` is applied to a macro 2.0
1811            let (_, macro_definition, _) = self.tcx.hir_node(hir_id).expect_item().expect_macro();
1812            let is_decl_macro = !macro_definition.macro_rules;
1813
1814            if is_decl_macro {
1815                self.tcx.emit_node_span_lint(
1816                    UNUSED_ATTRIBUTES,
1817                    hir_id,
1818                    attr.span(),
1819                    errors::MacroExport::OnDeclMacro,
1820                );
1821            }
1822        }
1823    }
1824
1825    fn check_unused_attribute(&self, hir_id: HirId, attr: &Attribute, style: Option<AttrStyle>) {
1826        // Warn on useless empty attributes.
1827        // FIXME(jdonszelmann): this lint should be moved to attribute parsing, see `AcceptContext::warn_empty_attribute`
1828        let note = if attr.has_any_name(&[
1829            sym::allow,
1830            sym::expect,
1831            sym::warn,
1832            sym::deny,
1833            sym::forbid,
1834            sym::feature,
1835        ]) && attr.meta_item_list().is_some_and(|list| list.is_empty())
1836        {
1837            errors::UnusedNote::EmptyList { name: attr.name().unwrap() }
1838        } else if attr.has_any_name(&[sym::allow, sym::warn, sym::deny, sym::forbid, sym::expect])
1839            && let Some(meta) = attr.meta_item_list()
1840            && let [meta] = meta.as_slice()
1841            && let Some(item) = meta.meta_item()
1842            && let MetaItemKind::NameValue(_) = &item.kind
1843            && item.path == sym::reason
1844        {
1845            errors::UnusedNote::NoLints { name: attr.name().unwrap() }
1846        } else if attr.has_any_name(&[sym::allow, sym::warn, sym::deny, sym::forbid, sym::expect])
1847            && let Some(meta) = attr.meta_item_list()
1848            && meta.iter().any(|meta| {
1849                meta.meta_item().map_or(false, |item| item.path == sym::linker_messages)
1850            })
1851        {
1852            if hir_id != CRATE_HIR_ID {
1853                match style {
1854                    Some(ast::AttrStyle::Outer) => self.tcx.emit_node_span_lint(
1855                        UNUSED_ATTRIBUTES,
1856                        hir_id,
1857                        attr.span(),
1858                        errors::OuterCrateLevelAttr,
1859                    ),
1860                    Some(ast::AttrStyle::Inner) | None => self.tcx.emit_node_span_lint(
1861                        UNUSED_ATTRIBUTES,
1862                        hir_id,
1863                        attr.span(),
1864                        errors::InnerCrateLevelAttr,
1865                    ),
1866                };
1867                return;
1868            } else {
1869                let never_needs_link = self
1870                    .tcx
1871                    .crate_types()
1872                    .iter()
1873                    .all(|kind| matches!(kind, CrateType::Rlib | CrateType::Staticlib));
1874                if never_needs_link {
1875                    errors::UnusedNote::LinkerMessagesBinaryCrateOnly
1876                } else {
1877                    return;
1878                }
1879            }
1880        } else if attr.has_name(sym::default_method_body_is_const) {
1881            errors::UnusedNote::DefaultMethodBodyConst
1882        } else {
1883            return;
1884        };
1885
1886        self.tcx.emit_node_span_lint(
1887            UNUSED_ATTRIBUTES,
1888            hir_id,
1889            attr.span(),
1890            errors::Unused { attr_span: attr.span(), note },
1891        );
1892    }
1893
1894    /// A best effort attempt to create an error for a mismatching proc macro signature.
1895    ///
1896    /// If this best effort goes wrong, it will just emit a worse error later (see #102923)
1897    fn check_proc_macro(&self, hir_id: HirId, target: Target, kind: ProcMacroKind) {
1898        if target != Target::Fn {
1899            return;
1900        }
1901
1902        let tcx = self.tcx;
1903        let Some(token_stream_def_id) = tcx.get_diagnostic_item(sym::TokenStream) else {
1904            return;
1905        };
1906        let Some(token_stream) = tcx.type_of(token_stream_def_id).no_bound_vars() else {
1907            return;
1908        };
1909
1910        let def_id = hir_id.expect_owner().def_id;
1911        let param_env = ty::ParamEnv::empty();
1912
1913        let infcx = tcx.infer_ctxt().build(TypingMode::non_body_analysis());
1914        let ocx = ObligationCtxt::new_with_diagnostics(&infcx);
1915
1916        let span = tcx.def_span(def_id);
1917        let fresh_args = infcx.fresh_args_for_item(span, def_id.to_def_id());
1918        let sig = tcx.liberate_late_bound_regions(
1919            def_id.to_def_id(),
1920            tcx.fn_sig(def_id).instantiate(tcx, fresh_args),
1921        );
1922
1923        let mut cause = ObligationCause::misc(span, def_id);
1924        let sig = ocx.normalize(&cause, param_env, sig);
1925
1926        // proc macro is not WF.
1927        let errors = ocx.select_where_possible();
1928        if !errors.is_empty() {
1929            return;
1930        }
1931
1932        let expected_sig = tcx.mk_fn_sig(
1933            std::iter::repeat(token_stream).take(match kind {
1934                ProcMacroKind::Attribute => 2,
1935                ProcMacroKind::Derive | ProcMacroKind::FunctionLike => 1,
1936            }),
1937            token_stream,
1938            false,
1939            Safety::Safe,
1940            ExternAbi::Rust,
1941        );
1942
1943        if let Err(terr) = ocx.eq(&cause, param_env, expected_sig, sig) {
1944            let mut diag = tcx.dcx().create_err(errors::ProcMacroBadSig { span, kind });
1945
1946            let hir_sig = tcx.hir_fn_sig_by_hir_id(hir_id);
1947            if let Some(hir_sig) = hir_sig {
1948                #[allow(rustc::diagnostic_outside_of_impl)] // FIXME
1949                match terr {
1950                    TypeError::ArgumentMutability(idx) | TypeError::ArgumentSorts(_, idx) => {
1951                        if let Some(ty) = hir_sig.decl.inputs.get(idx) {
1952                            diag.span(ty.span);
1953                            cause.span = ty.span;
1954                        } else if idx == hir_sig.decl.inputs.len() {
1955                            let span = hir_sig.decl.output.span();
1956                            diag.span(span);
1957                            cause.span = span;
1958                        }
1959                    }
1960                    TypeError::ArgCount => {
1961                        if let Some(ty) = hir_sig.decl.inputs.get(expected_sig.inputs().len()) {
1962                            diag.span(ty.span);
1963                            cause.span = ty.span;
1964                        }
1965                    }
1966                    TypeError::SafetyMismatch(_) => {
1967                        // FIXME: Would be nice if we had a span here..
1968                    }
1969                    TypeError::AbiMismatch(_) => {
1970                        // FIXME: Would be nice if we had a span here..
1971                    }
1972                    TypeError::VariadicMismatch(_) => {
1973                        // FIXME: Would be nice if we had a span here..
1974                    }
1975                    _ => {}
1976                }
1977            }
1978
1979            infcx.err_ctxt().note_type_err(
1980                &mut diag,
1981                &cause,
1982                None,
1983                Some(param_env.and(ValuePairs::PolySigs(ExpectedFound {
1984                    expected: ty::Binder::dummy(expected_sig),
1985                    found: ty::Binder::dummy(sig),
1986                }))),
1987                terr,
1988                false,
1989                None,
1990            );
1991            diag.emit();
1992            self.abort.set(true);
1993        }
1994
1995        let errors = ocx.select_all_or_error();
1996        if !errors.is_empty() {
1997            infcx.err_ctxt().report_fulfillment_errors(errors);
1998            self.abort.set(true);
1999        }
2000    }
2001
2002    fn check_type_const(&self, hir_id: HirId, attr_span: Span, target: Target) {
2003        let tcx = self.tcx;
2004        if target == Target::AssocConst
2005            && let parent = tcx.parent(hir_id.expect_owner().to_def_id())
2006            && self.tcx.def_kind(parent) == DefKind::Trait
2007        {
2008            return;
2009        } else {
2010            self.dcx()
2011                .struct_span_err(
2012                    attr_span,
2013                    "`#[type_const]` must only be applied to trait associated constants",
2014                )
2015                .emit();
2016        }
2017    }
2018
2019    fn check_rustc_pub_transparent(&self, attr_span: Span, span: Span, attrs: &[Attribute]) {
2020        if !find_attr!(attrs, AttributeKind::Repr { reprs, .. } => reprs.iter().any(|(r, _)| r == &ReprAttr::ReprTransparent))
2021            .unwrap_or(false)
2022        {
2023            self.dcx().emit_err(errors::RustcPubTransparent { span, attr_span });
2024        }
2025    }
2026
2027    fn check_rustc_force_inline(&self, hir_id: HirId, attrs: &[Attribute], target: Target) {
2028        if let (Target::Closure, None) = (
2029            target,
2030            find_attr!(attrs, AttributeKind::Inline(InlineAttr::Force { attr_span, .. }, _) => *attr_span),
2031        ) {
2032            let is_coro = matches!(
2033                self.tcx.hir_expect_expr(hir_id).kind,
2034                hir::ExprKind::Closure(hir::Closure {
2035                    kind: hir::ClosureKind::Coroutine(..) | hir::ClosureKind::CoroutineClosure(..),
2036                    ..
2037                })
2038            );
2039            let parent_did = self.tcx.hir_get_parent_item(hir_id).to_def_id();
2040            let parent_span = self.tcx.def_span(parent_did);
2041
2042            if let Some(attr_span) = find_attr!(
2043                self.tcx.get_all_attrs(parent_did),
2044                AttributeKind::Inline(InlineAttr::Force { attr_span, .. }, _) => *attr_span
2045            ) && is_coro
2046            {
2047                self.dcx().emit_err(errors::RustcForceInlineCoro { attr_span, span: parent_span });
2048            }
2049        }
2050    }
2051
2052    fn check_mix_no_mangle_export(&self, hir_id: HirId, attrs: &[Attribute]) {
2053        if let Some(export_name_span) = find_attr!(attrs, AttributeKind::ExportName { span: export_name_span, .. } => *export_name_span)
2054            && let Some(no_mangle_span) =
2055                find_attr!(attrs, AttributeKind::NoMangle(no_mangle_span) => *no_mangle_span)
2056        {
2057            let no_mangle_attr = if no_mangle_span.edition() >= Edition::Edition2024 {
2058                "#[unsafe(no_mangle)]"
2059            } else {
2060                "#[no_mangle]"
2061            };
2062            let export_name_attr = if export_name_span.edition() >= Edition::Edition2024 {
2063                "#[unsafe(export_name)]"
2064            } else {
2065                "#[export_name]"
2066            };
2067
2068            self.tcx.emit_node_span_lint(
2069                lint::builtin::UNUSED_ATTRIBUTES,
2070                hir_id,
2071                no_mangle_span,
2072                errors::MixedExportNameAndNoMangle {
2073                    no_mangle_span,
2074                    export_name_span,
2075                    no_mangle_attr,
2076                    export_name_attr,
2077                },
2078            );
2079        }
2080    }
2081
2082    /// Checks if `#[autodiff]` is applied to an item other than a function item.
2083    fn check_autodiff(&self, _hir_id: HirId, _attr: &Attribute, span: Span, target: Target) {
2084        debug!("check_autodiff");
2085        match target {
2086            Target::Fn => {}
2087            _ => {
2088                self.dcx().emit_err(errors::AutoDiffAttr { attr_span: span });
2089                self.abort.set(true);
2090            }
2091        }
2092    }
2093
2094    fn check_loop_match(&self, hir_id: HirId, attr_span: Span, target: Target) {
2095        let node_span = self.tcx.hir_span(hir_id);
2096
2097        if !matches!(target, Target::Expression) {
2098            return; // Handled in target checking during attr parse
2099        }
2100
2101        if !matches!(self.tcx.hir_expect_expr(hir_id).kind, hir::ExprKind::Loop(..)) {
2102            self.dcx().emit_err(errors::LoopMatchAttr { attr_span, node_span });
2103        };
2104    }
2105
2106    fn check_const_continue(&self, hir_id: HirId, attr_span: Span, target: Target) {
2107        let node_span = self.tcx.hir_span(hir_id);
2108
2109        if !matches!(target, Target::Expression) {
2110            return; // Handled in target checking during attr parse
2111        }
2112
2113        if !matches!(self.tcx.hir_expect_expr(hir_id).kind, hir::ExprKind::Break(..)) {
2114            self.dcx().emit_err(errors::ConstContinueAttr { attr_span, node_span });
2115        };
2116    }
2117
2118    fn check_custom_mir(
2119        &self,
2120        dialect: Option<(MirDialect, Span)>,
2121        phase: Option<(MirPhase, Span)>,
2122        attr_span: Span,
2123    ) {
2124        let Some((dialect, dialect_span)) = dialect else {
2125            if let Some((_, phase_span)) = phase {
2126                self.dcx()
2127                    .emit_err(errors::CustomMirPhaseRequiresDialect { attr_span, phase_span });
2128            }
2129            return;
2130        };
2131
2132        match dialect {
2133            MirDialect::Analysis => {
2134                if let Some((MirPhase::Optimized, phase_span)) = phase {
2135                    self.dcx().emit_err(errors::CustomMirIncompatibleDialectAndPhase {
2136                        dialect,
2137                        phase: MirPhase::Optimized,
2138                        attr_span,
2139                        dialect_span,
2140                        phase_span,
2141                    });
2142                }
2143            }
2144
2145            MirDialect::Built => {
2146                if let Some((phase, phase_span)) = phase {
2147                    self.dcx().emit_err(errors::CustomMirIncompatibleDialectAndPhase {
2148                        dialect,
2149                        phase,
2150                        attr_span,
2151                        dialect_span,
2152                        phase_span,
2153                    });
2154                }
2155            }
2156            MirDialect::Runtime => {}
2157        }
2158    }
2159}
2160
2161impl<'tcx> Visitor<'tcx> for CheckAttrVisitor<'tcx> {
2162    type NestedFilter = nested_filter::OnlyBodies;
2163
2164    fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
2165        self.tcx
2166    }
2167
2168    fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
2169        // Historically we've run more checks on non-exported than exported macros,
2170        // so this lets us continue to run them while maintaining backwards compatibility.
2171        // In the long run, the checks should be harmonized.
2172        if let ItemKind::Macro(_, macro_def, _) = item.kind {
2173            let def_id = item.owner_id.to_def_id();
2174            if macro_def.macro_rules && !self.tcx.has_attr(def_id, sym::macro_export) {
2175                check_non_exported_macro_for_invalid_attrs(self.tcx, item);
2176            }
2177        }
2178
2179        let target = Target::from_item(item);
2180        self.check_attributes(item.hir_id(), item.span, target, Some(ItemLike::Item(item)));
2181        intravisit::walk_item(self, item)
2182    }
2183
2184    fn visit_where_predicate(&mut self, where_predicate: &'tcx hir::WherePredicate<'tcx>) {
2185        // FIXME(where_clause_attrs): Currently, as the following check shows,
2186        // only `#[cfg]` and `#[cfg_attr]` are allowed, but it should be removed
2187        // if we allow more attributes (e.g., tool attributes and `allow/deny/warn`)
2188        // in where clauses. After that, only `self.check_attributes` should be enough.
2189        const ATTRS_ALLOWED: &[Symbol] = &[sym::cfg_trace, sym::cfg_attr_trace];
2190        let spans = self
2191            .tcx
2192            .hir_attrs(where_predicate.hir_id)
2193            .iter()
2194            .filter(|attr| !ATTRS_ALLOWED.iter().any(|&sym| attr.has_name(sym)))
2195            .filter(|attr| !attr.is_parsed_attr())
2196            .map(|attr| attr.span())
2197            .collect::<Vec<_>>();
2198        if !spans.is_empty() {
2199            self.tcx.dcx().emit_err(errors::UnsupportedAttributesInWhere { span: spans.into() });
2200        }
2201        self.check_attributes(
2202            where_predicate.hir_id,
2203            where_predicate.span,
2204            Target::WherePredicate,
2205            None,
2206        );
2207        intravisit::walk_where_predicate(self, where_predicate)
2208    }
2209
2210    fn visit_generic_param(&mut self, generic_param: &'tcx hir::GenericParam<'tcx>) {
2211        let target = Target::from_generic_param(generic_param);
2212        self.check_attributes(generic_param.hir_id, generic_param.span, target, None);
2213        intravisit::walk_generic_param(self, generic_param)
2214    }
2215
2216    fn visit_trait_item(&mut self, trait_item: &'tcx TraitItem<'tcx>) {
2217        let target = Target::from_trait_item(trait_item);
2218        self.check_attributes(trait_item.hir_id(), trait_item.span, target, None);
2219        intravisit::walk_trait_item(self, trait_item)
2220    }
2221
2222    fn visit_field_def(&mut self, struct_field: &'tcx hir::FieldDef<'tcx>) {
2223        self.check_attributes(struct_field.hir_id, struct_field.span, Target::Field, None);
2224        intravisit::walk_field_def(self, struct_field);
2225    }
2226
2227    fn visit_arm(&mut self, arm: &'tcx hir::Arm<'tcx>) {
2228        self.check_attributes(arm.hir_id, arm.span, Target::Arm, None);
2229        intravisit::walk_arm(self, arm);
2230    }
2231
2232    fn visit_foreign_item(&mut self, f_item: &'tcx ForeignItem<'tcx>) {
2233        let target = Target::from_foreign_item(f_item);
2234        self.check_attributes(f_item.hir_id(), f_item.span, target, Some(ItemLike::ForeignItem));
2235        intravisit::walk_foreign_item(self, f_item)
2236    }
2237
2238    fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
2239        let target = target_from_impl_item(self.tcx, impl_item);
2240        self.check_attributes(impl_item.hir_id(), impl_item.span, target, None);
2241        intravisit::walk_impl_item(self, impl_item)
2242    }
2243
2244    fn visit_stmt(&mut self, stmt: &'tcx hir::Stmt<'tcx>) {
2245        // When checking statements ignore expressions, they will be checked later.
2246        if let hir::StmtKind::Let(l) = stmt.kind {
2247            self.check_attributes(l.hir_id, stmt.span, Target::Statement, None);
2248        }
2249        intravisit::walk_stmt(self, stmt)
2250    }
2251
2252    fn visit_expr(&mut self, expr: &'tcx hir::Expr<'tcx>) {
2253        let target = match expr.kind {
2254            hir::ExprKind::Closure { .. } => Target::Closure,
2255            _ => Target::Expression,
2256        };
2257
2258        self.check_attributes(expr.hir_id, expr.span, target, None);
2259        intravisit::walk_expr(self, expr)
2260    }
2261
2262    fn visit_expr_field(&mut self, field: &'tcx hir::ExprField<'tcx>) {
2263        self.check_attributes(field.hir_id, field.span, Target::ExprField, None);
2264        intravisit::walk_expr_field(self, field)
2265    }
2266
2267    fn visit_variant(&mut self, variant: &'tcx hir::Variant<'tcx>) {
2268        self.check_attributes(variant.hir_id, variant.span, Target::Variant, None);
2269        intravisit::walk_variant(self, variant)
2270    }
2271
2272    fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
2273        self.check_attributes(param.hir_id, param.span, Target::Param, None);
2274
2275        intravisit::walk_param(self, param);
2276    }
2277
2278    fn visit_pat_field(&mut self, field: &'tcx hir::PatField<'tcx>) {
2279        self.check_attributes(field.hir_id, field.span, Target::PatField, None);
2280        intravisit::walk_pat_field(self, field);
2281    }
2282}
2283
2284fn is_c_like_enum(item: &Item<'_>) -> bool {
2285    if let ItemKind::Enum(_, _, ref def) = item.kind {
2286        for variant in def.variants {
2287            match variant.data {
2288                hir::VariantData::Unit(..) => { /* continue */ }
2289                _ => return false,
2290            }
2291        }
2292        true
2293    } else {
2294        false
2295    }
2296}
2297
2298// FIXME: Fix "Cannot determine resolution" error and remove built-in macros
2299// from this check.
2300fn check_invalid_crate_level_attr(tcx: TyCtxt<'_>, attrs: &[Attribute]) {
2301    // Check for builtin attributes at the crate level
2302    // which were unsuccessfully resolved due to cannot determine
2303    // resolution for the attribute macro error.
2304    const ATTRS_TO_CHECK: &[Symbol] = &[
2305        sym::macro_export,
2306        sym::rustc_main,
2307        sym::derive,
2308        sym::test,
2309        sym::test_case,
2310        sym::global_allocator,
2311        sym::bench,
2312    ];
2313
2314    for attr in attrs {
2315        // FIXME(jdonszelmann): all attrs should be combined here cleaning this up some day.
2316        let (span, name) = if let Some(a) =
2317            ATTRS_TO_CHECK.iter().find(|attr_to_check| attr.has_name(**attr_to_check))
2318        {
2319            (attr.span(), *a)
2320        } else if let Attribute::Parsed(AttributeKind::Repr {
2321            reprs: _,
2322            first_span: first_attr_span,
2323        }) = attr
2324        {
2325            (*first_attr_span, sym::repr)
2326        } else {
2327            continue;
2328        };
2329
2330        let item = tcx
2331            .hir_free_items()
2332            .map(|id| tcx.hir_item(id))
2333            .find(|item| !item.span.is_dummy()) // Skip prelude `use`s
2334            .map(|item| errors::ItemFollowingInnerAttr {
2335                span: if let Some(ident) = item.kind.ident() { ident.span } else { item.span },
2336                kind: tcx.def_descr(item.owner_id.to_def_id()),
2337            });
2338        let err = tcx.dcx().create_err(errors::InvalidAttrAtCrateLevel {
2339            span,
2340            sugg_span: tcx
2341                .sess
2342                .source_map()
2343                .span_to_snippet(span)
2344                .ok()
2345                .filter(|src| src.starts_with("#!["))
2346                .map(|_| span.with_lo(span.lo() + BytePos(1)).with_hi(span.lo() + BytePos(2))),
2347            name,
2348            item,
2349        });
2350
2351        if let Attribute::Unparsed(p) = attr {
2352            tcx.dcx().try_steal_replace_and_emit_err(
2353                p.path.span,
2354                StashKey::UndeterminedMacroResolution,
2355                err,
2356            );
2357        } else {
2358            err.emit();
2359        }
2360    }
2361}
2362
2363fn check_non_exported_macro_for_invalid_attrs(tcx: TyCtxt<'_>, item: &Item<'_>) {
2364    let attrs = tcx.hir_attrs(item.hir_id());
2365
2366    if let Some(attr_span) = find_attr!(attrs, AttributeKind::Inline(i, span) if !matches!(i, InlineAttr::Force{..}) => *span)
2367    {
2368        tcx.dcx().emit_err(errors::NonExportedMacroInvalidAttrs { attr_span });
2369    }
2370}
2371
2372fn check_mod_attrs(tcx: TyCtxt<'_>, module_def_id: LocalModDefId) {
2373    let check_attr_visitor = &mut CheckAttrVisitor { tcx, abort: Cell::new(false) };
2374    tcx.hir_visit_item_likes_in_module(module_def_id, check_attr_visitor);
2375    if module_def_id.to_local_def_id().is_top_level_module() {
2376        check_attr_visitor.check_attributes(CRATE_HIR_ID, DUMMY_SP, Target::Mod, None);
2377        check_invalid_crate_level_attr(tcx, tcx.hir_krate_attrs());
2378    }
2379    if check_attr_visitor.abort.get() {
2380        tcx.dcx().abort_if_errors()
2381    }
2382}
2383
2384pub(crate) fn provide(providers: &mut Providers) {
2385    *providers = Providers { check_mod_attrs, ..*providers };
2386}
2387
2388// FIXME(jdonszelmann): remove, check during parsing
2389fn check_duplicates(
2390    tcx: TyCtxt<'_>,
2391    attr: &Attribute,
2392    hir_id: HirId,
2393    duplicates: AttributeDuplicates,
2394    seen: &mut FxHashMap<Symbol, Span>,
2395) {
2396    use AttributeDuplicates::*;
2397    if matches!(duplicates, WarnFollowingWordOnly) && !attr.is_word() {
2398        return;
2399    }
2400    let attr_name = attr.name().unwrap();
2401    match duplicates {
2402        DuplicatesOk => {}
2403        WarnFollowing | FutureWarnFollowing | WarnFollowingWordOnly | FutureWarnPreceding => {
2404            match seen.entry(attr_name) {
2405                Entry::Occupied(mut entry) => {
2406                    let (this, other) = if matches!(duplicates, FutureWarnPreceding) {
2407                        let to_remove = entry.insert(attr.span());
2408                        (to_remove, attr.span())
2409                    } else {
2410                        (attr.span(), *entry.get())
2411                    };
2412                    tcx.emit_node_span_lint(
2413                        UNUSED_ATTRIBUTES,
2414                        hir_id,
2415                        this,
2416                        errors::UnusedDuplicate {
2417                            this,
2418                            other,
2419                            warning: matches!(
2420                                duplicates,
2421                                FutureWarnFollowing | FutureWarnPreceding
2422                            ),
2423                        },
2424                    );
2425                }
2426                Entry::Vacant(entry) => {
2427                    entry.insert(attr.span());
2428                }
2429            }
2430        }
2431        ErrorFollowing | ErrorPreceding => match seen.entry(attr_name) {
2432            Entry::Occupied(mut entry) => {
2433                let (this, other) = if matches!(duplicates, ErrorPreceding) {
2434                    let to_remove = entry.insert(attr.span());
2435                    (to_remove, attr.span())
2436                } else {
2437                    (attr.span(), *entry.get())
2438                };
2439                tcx.dcx().emit_err(errors::UnusedMultiple { this, other, name: attr_name });
2440            }
2441            Entry::Vacant(entry) => {
2442                entry.insert(attr.span());
2443            }
2444        },
2445    }
2446}
2447
2448fn doc_fake_variadic_is_allowed_self_ty(self_ty: &hir::Ty<'_>) -> bool {
2449    matches!(&self_ty.kind, hir::TyKind::Tup([_]))
2450        || if let hir::TyKind::FnPtr(fn_ptr_ty) = &self_ty.kind {
2451            fn_ptr_ty.decl.inputs.len() == 1
2452        } else {
2453            false
2454        }
2455        || (if let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = &self_ty.kind
2456            && let Some(&[hir::GenericArg::Type(ty)]) =
2457                path.segments.last().map(|last| last.args().args)
2458        {
2459            doc_fake_variadic_is_allowed_self_ty(ty.as_unambig_ty())
2460        } else {
2461            false
2462        })
2463}