rustc_attr_parsing/attributes/
deprecation.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//! Parsing and validation of builtin attributes

use rustc_ast::attr::AttributeExt;
use rustc_ast::{MetaItem, MetaItemInner};
use rustc_ast_pretty::pprust;
use rustc_attr_data_structures::{DeprecatedSince, Deprecation};
use rustc_feature::Features;
use rustc_session::Session;
use rustc_span::{Span, Symbol, sym};

use super::util::UnsupportedLiteralReason;
use crate::{parse_version, session_diagnostics};

/// Finds the deprecation attribute. `None` if none exists.
pub fn find_deprecation(
    sess: &Session,
    features: &Features,
    attrs: &[impl AttributeExt],
) -> Option<(Deprecation, Span)> {
    let mut depr: Option<(Deprecation, Span)> = None;
    let is_rustc = features.staged_api();

    'outer: for attr in attrs {
        if !attr.has_name(sym::deprecated) {
            continue;
        }

        let mut since = None;
        let mut note = None;
        let mut suggestion = None;

        if attr.is_doc_comment() {
            continue;
        } else if attr.is_word() {
        } else if let Some(value) = attr.value_str() {
            note = Some(value)
        } else if let Some(list) = attr.meta_item_list() {
            let get = |meta: &MetaItem, item: &mut Option<Symbol>| {
                if item.is_some() {
                    sess.dcx().emit_err(session_diagnostics::MultipleItem {
                        span: meta.span,
                        item: pprust::path_to_string(&meta.path),
                    });
                    return false;
                }
                if let Some(v) = meta.value_str() {
                    *item = Some(v);
                    true
                } else {
                    if let Some(lit) = meta.name_value_literal() {
                        sess.dcx().emit_err(session_diagnostics::UnsupportedLiteral {
                            span: lit.span,
                            reason: UnsupportedLiteralReason::DeprecatedString,
                            is_bytestr: lit.kind.is_bytestr(),
                            start_point_span: sess.source_map().start_point(lit.span),
                        });
                    } else {
                        sess.dcx()
                            .emit_err(session_diagnostics::IncorrectMetaItem { span: meta.span });
                    }
                    false
                }
            };

            for meta in &list {
                match meta {
                    MetaItemInner::MetaItem(mi) => match mi.name_or_empty() {
                        sym::since => {
                            if !get(mi, &mut since) {
                                continue 'outer;
                            }
                        }
                        sym::note => {
                            if !get(mi, &mut note) {
                                continue 'outer;
                            }
                        }
                        sym::suggestion => {
                            if !features.deprecated_suggestion() {
                                sess.dcx().emit_err(
                                    session_diagnostics::DeprecatedItemSuggestion {
                                        span: mi.span,
                                        is_nightly: sess.is_nightly_build(),
                                        details: (),
                                    },
                                );
                            }

                            if !get(mi, &mut suggestion) {
                                continue 'outer;
                            }
                        }
                        _ => {
                            sess.dcx().emit_err(session_diagnostics::UnknownMetaItem {
                                span: meta.span(),
                                item: pprust::path_to_string(&mi.path),
                                expected: if features.deprecated_suggestion() {
                                    &["since", "note", "suggestion"]
                                } else {
                                    &["since", "note"]
                                },
                            });
                            continue 'outer;
                        }
                    },
                    MetaItemInner::Lit(lit) => {
                        sess.dcx().emit_err(session_diagnostics::UnsupportedLiteral {
                            span: lit.span,
                            reason: UnsupportedLiteralReason::DeprecatedKvPair,
                            is_bytestr: false,
                            start_point_span: sess.source_map().start_point(lit.span),
                        });
                        continue 'outer;
                    }
                }
            }
        } else {
            continue;
        }

        let since = if let Some(since) = since {
            if since.as_str() == "TBD" {
                DeprecatedSince::Future
            } else if !is_rustc {
                DeprecatedSince::NonStandard(since)
            } else if let Some(version) = parse_version(since) {
                DeprecatedSince::RustcVersion(version)
            } else {
                sess.dcx().emit_err(session_diagnostics::InvalidSince { span: attr.span() });
                DeprecatedSince::Err
            }
        } else if is_rustc {
            sess.dcx().emit_err(session_diagnostics::MissingSince { span: attr.span() });
            DeprecatedSince::Err
        } else {
            DeprecatedSince::Unspecified
        };

        if is_rustc && note.is_none() {
            sess.dcx().emit_err(session_diagnostics::MissingNote { span: attr.span() });
            continue;
        }

        depr = Some((Deprecation { since, note, suggestion }, attr.span()));
    }

    depr
}