Skip to main content

rustc_attr_parsing/
target_checking.rs

1use std::borrow::Cow;
2
3use rustc_ast::AttrStyle;
4use rustc_errors::{DiagArgValue, MultiSpan, StashKey};
5use rustc_feature::Features;
6use rustc_hir::attrs::AttributeKind;
7use rustc_hir::{AttrItem, Attribute, MethodKind, Target};
8use rustc_span::{BytePos, FileName, RemapPathScopeComponents, Span, Symbol, sym};
9
10use crate::context::AcceptContext;
11use crate::diagnostics::{
12    InvalidAttrAtCrateLevel, ItemFollowingInnerAttr, UnsupportedAttributesInWhere,
13};
14use crate::session_diagnostics::{InvalidTarget, InvalidTargetHelp};
15use crate::target_checking::Policy::Allow;
16use crate::{AttributeParser, ShouldEmit};
17
18#[derive(#[automatically_derived]
impl ::core::fmt::Debug for AllowedTargets {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            AllowedTargets::AllowList(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "AllowList", &__self_0),
            AllowedTargets::AllowListWarnRest(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "AllowListWarnRest", &__self_0),
            AllowedTargets::ManuallyChecked =>
                ::core::fmt::Formatter::write_str(f, "ManuallyChecked"),
        }
    }
}Debug)]
19pub(crate) enum AllowedTargets {
20    AllowList(&'static [Policy]),
21    AllowListWarnRest(&'static [Policy]),
22    /// This is useful for argument-dependent target checking.
23    /// If debug assertions are enabled,
24    /// this emits a delayed bug if the `cx.check_target(...)` method is not called during attribute parsing.
25    ManuallyChecked,
26}
27
28pub(crate) enum AllowedResult {
29    Allowed,
30    Warn,
31    Error,
32}
33
34impl AllowedTargets {
35    pub(crate) fn is_allowed(&self, target: Target) -> AllowedResult {
36        match self {
37            AllowedTargets::AllowList(list) => {
38                if list.contains(&Policy::Allow(target))
39                    || list.contains(&Policy::AllowSilent(target))
40                {
41                    AllowedResult::Allowed
42                } else if list.contains(&Policy::Warn(target)) {
43                    AllowedResult::Warn
44                } else {
45                    AllowedResult::Error
46                }
47            }
48            AllowedTargets::AllowListWarnRest(list) => {
49                if list.contains(&Policy::Allow(target))
50                    || list.contains(&Policy::AllowSilent(target))
51                {
52                    AllowedResult::Allowed
53                } else if list.contains(&Policy::Error(target)) {
54                    AllowedResult::Error
55                } else {
56                    AllowedResult::Warn
57                }
58            }
59            AllowedTargets::ManuallyChecked => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
60        }
61    }
62
63    pub(crate) fn allowed_targets(&self) -> Vec<Target> {
64        match self {
65            AllowedTargets::AllowList(list) => list,
66            AllowedTargets::AllowListWarnRest(list) => list,
67            AllowedTargets::ManuallyChecked => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
68        }
69        .iter()
70        .filter_map(|target| match target {
71            Policy::Allow(target) => Some(*target),
72            Policy::AllowSilent(_) => None, // Not listed in possible targets
73            Policy::Warn(_) => None,
74            Policy::Error(_) => None,
75        })
76        .collect()
77    }
78}
79
80/// This policy determines what diagnostics should be emitted based on the `Target` of the attribute.
81#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Policy {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            Policy::Allow(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Allow",
                    &__self_0),
            Policy::AllowSilent(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "AllowSilent", &__self_0),
            Policy::Warn(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Warn",
                    &__self_0),
            Policy::Error(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Error",
                    &__self_0),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::Eq for Policy {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Target>;
    }
}Eq, #[automatically_derived]
impl ::core::cmp::PartialEq for Policy {
    #[inline]
    fn eq(&self, other: &Policy) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (Policy::Allow(__self_0), Policy::Allow(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (Policy::AllowSilent(__self_0), Policy::AllowSilent(__arg1_0))
                    => __self_0 == __arg1_0,
                (Policy::Warn(__self_0), Policy::Warn(__arg1_0)) =>
                    __self_0 == __arg1_0,
                (Policy::Error(__self_0), Policy::Error(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => unsafe { ::core::intrinsics::unreachable() }
            }
    }
}PartialEq)]
82pub(crate) enum Policy {
83    /// A target that is allowed.
84    Allow(Target),
85    /// A target that is allowed and not listed in the possible targets.
86    /// This is useful if the target is checked elsewhere.
87    AllowSilent(Target),
88    /// Emits a FCW on this target.
89    /// This is useful if the target was previously allowed but should not be.
90    Warn(Target),
91    /// Emits an error on this target.
92    Error(Target),
93}
94
95impl<'sess> AttributeParser<'sess> {
96    pub(crate) fn check_target(
97        allowed_targets: &AllowedTargets,
98        attribute_args: &'static str,
99        cx: &mut AcceptContext<'_, 'sess>,
100    ) {
101        if #[allow(non_exhaustive_omitted_patterns)] match cx.should_emit {
    ShouldEmit::Nothing => true,
    _ => false,
}matches!(cx.should_emit, ShouldEmit::Nothing) {
102            return;
103        }
104
105        if let AllowedTargets::ManuallyChecked = allowed_targets {
106            #[cfg(debug_assertions)]
107            if !cx.has_target_been_checked {
108                cx.dcx().delayed_bug("Attribute target has not been checked");
109            }
110
111            return;
112        }
113
114        // For crate-level attributes we emit a specific set of lints to warn
115        // people about accidentally not using them on the crate.
116        if let &AllowedTargets::AllowList(&[Allow(Target::Crate)]) = allowed_targets {
117            Self::check_crate_level(cx, false);
118            return;
119        }
120        if let &AllowedTargets::AllowListWarnRest(&[Allow(Target::Crate)]) = allowed_targets {
121            Self::check_crate_level(cx, true);
122            return;
123        }
124
125        let result = allowed_targets.is_allowed(cx.target);
126        if #[allow(non_exhaustive_omitted_patterns)] match result {
    AllowedResult::Allowed => true,
    _ => false,
}matches!(result, AllowedResult::Allowed) {
127            return;
128        }
129
130        let allowed_targets = allowed_targets.allowed_targets();
131        let (applied, only) = allowed_targets_applied(allowed_targets, cx.target, cx.features);
132        let is_diagnostic_attr = cx.attr_path.segments[0] == sym::diagnostic;
133
134        let diag = InvalidTarget {
135            span: cx.attr_span.clone(),
136            name: cx.attr_path.clone(),
137            target: cx.target.plural_name(),
138            only: if only { "only " } else { "" },
139            applied: DiagArgValue::StrListSepByAnd(applied.into_iter().map(Cow::Owned).collect()),
140            attribute_args,
141            help: Self::target_checking_help(attribute_args, cx),
142            previously_accepted: #[allow(non_exhaustive_omitted_patterns)] match result {
    AllowedResult::Warn => true,
    _ => false,
}matches!(result, AllowedResult::Warn) && !is_diagnostic_attr,
143            on_macro_call: #[allow(non_exhaustive_omitted_patterns)] match cx.target {
    Target::MacroCall => true,
    _ => false,
}matches!(cx.target, Target::MacroCall),
144        };
145
146        match result {
147            AllowedResult::Allowed => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("Should have early returned above")));
}unreachable!("Should have early returned above"),
148            AllowedResult::Warn => {
149                let lint = if cx.attr_path.segments[0] == sym::deprecated
150                    && ![
151                        Target::Closure,
152                        Target::Expression,
153                        Target::Statement,
154                        Target::Arm,
155                        Target::MacroCall,
156                    ]
157                    .contains(&cx.target)
158                {
159                    rustc_session::lint::builtin::USELESS_DEPRECATED
160                } else if is_diagnostic_attr {
161                    rustc_session::lint::builtin::MISPLACED_DIAGNOSTIC_ATTRIBUTES
162                } else {
163                    rustc_session::lint::builtin::UNUSED_ATTRIBUTES
164                };
165
166                let attr_span = cx.attr_span.clone();
167                cx.emit_lint(lint, diag, attr_span);
168            }
169            AllowedResult::Error => {
170                cx.dcx().emit_err(diag);
171            }
172        }
173    }
174
175    fn target_checking_help(
176        attribute_args: &'static str,
177        cx: &AcceptContext<'_, '_>,
178    ) -> Option<InvalidTargetHelp> {
179        match &*cx.attr_path.segments {
180            [sym::repr] if attribute_args == "(align(...))" => match cx.target {
181                Target::Fn | Target::Method(..) if cx.features().fn_align() => {
182                    Some(InvalidTargetHelp::UseRustcAlign)
183                }
184                Target::Static if cx.features().static_align() => {
185                    Some(InvalidTargetHelp::UseRustcAlignStatic)
186                }
187                _ => None,
188            },
189            _ => None,
190        }
191    }
192
193    pub(crate) fn check_crate_level(cx: &mut AcceptContext<'_, 'sess>, warn: bool) {
194        if cx.target == Target::Crate {
195            return;
196        }
197
198        let name = cx.attr_path.to_string();
199        let is_used_as_inner = cx.attr_style == AttrStyle::Inner;
200        let target_span = cx.target_span;
201        let attr_span = cx.attr_span;
202
203        let (show_crate_root_help, crate_root_path) = is_used_as_inner
204            .then(|| cx.cx.sess.local_crate_source_file())
205            .flatten()
206            .filter(|src| {
207                !#[allow(non_exhaustive_omitted_patterns)] match cx.cx.sess.source_map().span_to_filename(attr_span)
    {
    FileName::Real(ref name) if name == src => true,
    _ => false,
}matches!(
208                    cx.cx.sess.source_map().span_to_filename(attr_span),
209                    FileName::Real(ref name) if name == src
210                )
211            })
212            .map(|src| {
213                (true, src.path(RemapPathScopeComponents::DIAGNOSTICS).display().to_string())
214            })
215            .unwrap_or_default();
216
217        let diag = crate::diagnostics::InvalidAttrStyle {
218            name,
219            is_used_as_inner,
220            target_span: (!is_used_as_inner).then_some(target_span),
221            target: cx.target.name(),
222            crate_root_path,
223            show_crate_root_help,
224            span: attr_span,
225        };
226        if warn {
227            cx.emit_lint(rustc_session::lint::builtin::UNUSED_ATTRIBUTES, diag, attr_span);
228        } else {
229            cx.emit_err(diag);
230        }
231    }
232
233    // FIXME: Fix "Cannot determine resolution" error and remove built-in macros
234    // from this check.
235    pub(crate) fn check_invalid_crate_level_attr_item(&self, attr: &AttrItem, inner_span: Span) {
236        // Check for builtin attributes at the crate level
237        // which were unsuccessfully resolved due to cannot determine
238        // resolution for the attribute macro error.
239        const ATTRS_TO_CHECK: &[Symbol] =
240            &[sym::derive, sym::test, sym::test_case, sym::global_allocator, sym::bench];
241
242        // FIXME(jdonszelmann): all attrs should be combined here cleaning this up some day.
243        if let Some(name) = ATTRS_TO_CHECK.iter().find(|attr_to_check| #[allow(non_exhaustive_omitted_patterns)] match attr.path.segments.as_ref() {
    [segment] if segment == *attr_to_check => true,
    _ => false,
}matches!(attr.path.segments.as_ref(), [segment] if segment == *attr_to_check)) {
244            let span = attr.span;
245            let name = *name;
246
247            let item = self.first_line_of_next_item(span).map(|span| ItemFollowingInnerAttr { span });
248
249            let err = self.dcx().create_err(InvalidAttrAtCrateLevel {
250                span,
251                pound_to_opening_bracket: span.until(inner_span),
252                name,
253                item,
254            });
255
256            self.dcx().try_steal_replace_and_emit_err(
257                attr.path.span,
258                StashKey::UndeterminedMacroResolution,
259                err,
260            );
261        }
262    }
263
264    fn first_line_of_next_item(&self, span: Span) -> Option<Span> {
265        // We can't exactly call `tcx.hir_free_items()` here because it's too early and querying
266        // this would create a circular dependency. Instead, we resort to getting the original
267        // source code that follows `span` and find the next item from here.
268
269        self.sess()
270            .source_map()
271            .span_to_source(span, |content, _, span_end| {
272                let mut source = &content[span_end..];
273                let initial_source_len = source.len();
274                let span = try {
275                    loop {
276                        let first = source.chars().next()?;
277
278                        if first.is_whitespace() {
279                            let split_idx = source.find(|c: char| !c.is_whitespace())?;
280                            source = &source[split_idx..];
281                        } else if source.starts_with("//") {
282                            let line_idx = source.find('\n')?;
283                            source = &source[line_idx + '\n'.len_utf8()..];
284                        } else if source.starts_with("/*") {
285                            // FIXME: support nested comments.
286                            let close_idx = source.find("*/")?;
287                            source = &source[close_idx + "*/".len()..];
288                        } else if first == '#' {
289                            // FIXME: properly find the end of the attributes in order to accurately
290                            // skip them. This version just consumes the source code until the next
291                            // `]`.
292                            let close_idx = source.find(']')?;
293                            source = &source[close_idx + ']'.len_utf8()..];
294                        } else {
295                            let lo = span_end + initial_source_len - source.len();
296                            let last_line = source.split('\n').next().map(|s| s.trim_end())?;
297
298                            let hi = lo + last_line.len();
299                            let lo = BytePos(lo as u32);
300                            let hi = BytePos(hi as u32);
301                            let next_item_span = Span::new(lo, hi, span.ctxt(), None);
302
303                            break next_item_span;
304                        }
305                    }
306                };
307
308                Ok(span)
309            })
310            .ok()
311            .flatten()
312    }
313
314    pub(crate) fn check_invalid_where_predicate_attrs<'attr>(
315        &self,
316        attrs: impl IntoIterator<Item = &'attr Attribute>,
317    ) {
318        // FIXME(where_clause_attrs): Currently, as the following check shows,
319        // only `#[cfg]` and `#[cfg_attr]` are allowed, but it should be removed
320        // if we allow more attributes (e.g., tool attributes and `allow/deny/warn`)
321        // in where clauses. After that, this function would become useless.
322        let spans = attrs
323            .into_iter()
324            .filter_map(|attr| {
325                match attr {
326                    Attribute::Parsed(AttributeKind::DocComment { span, .. }) => Some(*span),
327                    // FIXME: We shouldn't need to special-case `doc`!
328                    Attribute::Parsed(AttributeKind::Doc(attr)) => Some(attr.first_span),
329                    // Checked during attribute parsing target checking
330                    Attribute::Parsed(_) => None,
331                    Attribute::Unparsed(attr) => Some(attr.span),
332                }
333            })
334            .collect::<Vec<_>>();
335        if !spans.is_empty() {
336            self.dcx()
337                .emit_err(UnsupportedAttributesInWhere { span: MultiSpan::from_spans(spans) });
338        }
339    }
340}
341
342/// Takes a list of `allowed_targets` for an attribute, and the `target` the attribute was applied to.
343/// Does some heuristic-based filtering to remove uninteresting targets, and formats the targets into a string
344pub(crate) fn allowed_targets_applied(
345    mut allowed_targets: Vec<Target>,
346    target: Target,
347    features: Option<&Features>,
348) -> (Vec<String>, bool) {
349    // Remove unstable targets from `allowed_targets` if their features are not enabled
350    if let Some(features) = features {
351        if !features.fn_delegation() {
352            allowed_targets.retain(|t| !#[allow(non_exhaustive_omitted_patterns)] match t {
    Target::Delegation { .. } => true,
    _ => false,
}matches!(t, Target::Delegation { .. }));
353        }
354        if !features.stmt_expr_attributes() {
355            allowed_targets.retain(|t| !#[allow(non_exhaustive_omitted_patterns)] match t {
    Target::Expression | Target::Statement => true,
    _ => false,
}matches!(t, Target::Expression | Target::Statement));
356        }
357        if !features.extern_types() {
358            allowed_targets.retain(|t| !#[allow(non_exhaustive_omitted_patterns)] match t {
    Target::ForeignTy => true,
    _ => false,
}matches!(t, Target::ForeignTy));
359        }
360    }
361
362    // We define groups of "similar" targets.
363    // If at least two of the targets are allowed, and the `target` is not in the group,
364    // we collapse the entire group to a single entry to simplify the target list
365    const FUNCTION_LIKE: &[Target] = &[
366        Target::Fn,
367        Target::Closure,
368        Target::ForeignFn,
369        Target::Method(MethodKind::Inherent),
370        Target::Method(MethodKind::Trait { body: false }),
371        Target::Method(MethodKind::Trait { body: true }),
372        Target::Method(MethodKind::TraitImpl),
373    ];
374    const FUNCTION_WITH_BODY_LIKE: &[Target] = &[
375        Target::Fn,
376        Target::Closure,
377        Target::Method(MethodKind::Inherent),
378        Target::Method(MethodKind::Trait { body: true }),
379        Target::Method(MethodKind::TraitImpl),
380    ];
381    const METHOD_LIKE: &[Target] = &[
382        Target::Method(MethodKind::Inherent),
383        Target::Method(MethodKind::Trait { body: false }),
384        Target::Method(MethodKind::Trait { body: true }),
385        Target::Method(MethodKind::TraitImpl),
386    ];
387    const IMPL_LIKE: &[Target] =
388        &[Target::Impl { of_trait: false }, Target::Impl { of_trait: true }];
389    const ADT_LIKE: &[Target] = &[Target::Struct, Target::Enum, Target::Union];
390
391    let mut added_fake_targets = Vec::new();
392    filter_targets(
393        &mut allowed_targets,
394        FUNCTION_LIKE,
395        "functions",
396        target,
397        &mut added_fake_targets,
398    );
399    filter_targets(
400        &mut allowed_targets,
401        FUNCTION_WITH_BODY_LIKE,
402        "functions with a body",
403        target,
404        &mut added_fake_targets,
405    );
406    filter_targets(&mut allowed_targets, METHOD_LIKE, "methods", target, &mut added_fake_targets);
407    filter_targets(&mut allowed_targets, IMPL_LIKE, "impl blocks", target, &mut added_fake_targets);
408    filter_targets(&mut allowed_targets, ADT_LIKE, "data types", target, &mut added_fake_targets);
409
410    let mut target_strings: Vec<_> = added_fake_targets
411        .iter()
412        .copied()
413        .chain(allowed_targets.iter().map(|t| t.plural_name()))
414        .map(|i| i.to_string())
415        .collect();
416
417    // ensure a consistent order
418    target_strings.sort();
419
420    // If there is now only 1 target left, show that as the only possible target
421    let only_target = target_strings.len() == 1;
422
423    (target_strings, only_target)
424}
425
426fn filter_targets(
427    allowed_targets: &mut Vec<Target>,
428    target_group: &'static [Target],
429    target_group_name: &'static str,
430    target: Target,
431    added_fake_targets: &mut Vec<&'static str>,
432) {
433    if target_group.contains(&target) {
434        return;
435    }
436    if allowed_targets.iter().filter(|at| target_group.contains(at)).count() < 2 {
437        return;
438    }
439    allowed_targets.retain(|t| !target_group.contains(t));
440    added_fake_targets.push(target_group_name);
441}
442
443impl<'f, 'sess> AcceptContext<'f, 'sess> {
444    pub(crate) fn check_target(
445        &mut self,
446        attribute_args: &'static str,
447        allowed_targets: &AllowedTargets,
448    ) {
449        self.ignore_target_checks();
450        AttributeParser::check_target(allowed_targets, attribute_args, self);
451    }
452
453    pub(crate) fn ignore_target_checks(&mut self) {
454        #[cfg(debug_assertions)]
455        {
456            self.has_target_been_checked = true;
457        }
458    }
459}
460
461/// This is the list of all targets to which a attribute can be applied
462/// This is used for:
463/// - `rustc_dummy`, which can be applied to all targets
464/// - Attributes that are not parted to the new target system yet can use this list as a placeholder
465pub(crate) const ALL_TARGETS: &'static [Policy] = {
466    use Policy::Allow;
467    &[
468        Allow(Target::ExternCrate),
469        Allow(Target::Use),
470        Allow(Target::Static),
471        Allow(Target::Const),
472        Allow(Target::Fn),
473        Allow(Target::Closure),
474        Allow(Target::Mod),
475        Allow(Target::ForeignMod),
476        Allow(Target::GlobalAsm),
477        Allow(Target::TyAlias),
478        Allow(Target::Enum),
479        Allow(Target::Variant),
480        Allow(Target::Struct),
481        Allow(Target::Field),
482        Allow(Target::Union),
483        Allow(Target::Trait),
484        Allow(Target::TraitAlias),
485        Allow(Target::Impl { of_trait: false }),
486        Allow(Target::Impl { of_trait: true }),
487        Allow(Target::Expression),
488        Allow(Target::Statement),
489        Allow(Target::Arm),
490        Allow(Target::AssocConst),
491        Allow(Target::Method(MethodKind::Inherent)),
492        Allow(Target::Method(MethodKind::Trait { body: false })),
493        Allow(Target::Method(MethodKind::Trait { body: true })),
494        Allow(Target::Method(MethodKind::TraitImpl)),
495        Allow(Target::AssocTy),
496        Allow(Target::ForeignFn),
497        Allow(Target::ForeignStatic),
498        Allow(Target::ForeignTy),
499        Allow(Target::MacroDef),
500        Allow(Target::Param),
501        Allow(Target::PatField),
502        Allow(Target::ExprField),
503        Allow(Target::WherePredicate),
504        Allow(Target::MacroCall),
505        Allow(Target::Crate),
506        Allow(Target::Delegation { mac: false }),
507        Allow(Target::Delegation { mac: true }),
508        Allow(Target::GenericParam {
509            kind: rustc_hir::target::GenericParamKind::Const,
510            has_default: false,
511        }),
512        Allow(Target::GenericParam {
513            kind: rustc_hir::target::GenericParamKind::Const,
514            has_default: true,
515        }),
516        Allow(Target::GenericParam {
517            kind: rustc_hir::target::GenericParamKind::Lifetime,
518            has_default: false,
519        }),
520        Allow(Target::GenericParam {
521            kind: rustc_hir::target::GenericParamKind::Lifetime,
522            has_default: true,
523        }),
524        Allow(Target::GenericParam {
525            kind: rustc_hir::target::GenericParamKind::Type,
526            has_default: false,
527        }),
528        Allow(Target::GenericParam {
529            kind: rustc_hir::target::GenericParamKind::Type,
530            has_default: true,
531        }),
532        Allow(Target::Loop),
533        Allow(Target::ForLoop),
534        Allow(Target::While),
535        Allow(Target::Break),
536    ]
537};