1use std::fmt;
2
3use rustc_ast::attr::AttributeExt;
4use rustc_ast::token::DocFragmentKind;
5use rustc_ast::{AttrStyle, DelimArgs, MetaItemLit, ast, join_path_idents};
6use rustc_data_structures::stable_hash::{StableHash, StableHashCtxt, StableHasher};
7use rustc_error_messages::{DiagArgValue, IntoDiagArg};
8use rustc_macros::{Decodable, Encodable, StableHash};
9use rustc_span::{AttrId, DUMMY_SP, Ident, Span, Symbol};
10
11use crate::AttributeKind;
12
13#[derive(#[automatically_derived]impl ::core::clone::Clone for AttrArgs {
#[inline]
fn clone(&self) -> Self {
match self {
Self::Empty => AttrArgs::Empty,
Self::Delimited(__self_0) =>
AttrArgs::Delimited(::core::clone::Clone::clone(__self_0)),
Self::Eq { eq_span: __self_0, expr: __self_1 } =>
AttrArgs::Eq {
eq_span: ::core::clone::Clone::clone(__self_0),
expr: ::core::clone::Clone::clone(__self_1),
},
}
}
}Clone, #[automatically_derived]impl ::core::fmt::Debug for AttrArgs {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::Empty => ::core::fmt::Formatter::write_str(f, "Empty"),
Self::Delimited(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Delimited", &__self_0),
Self::Eq { eq_span: __self_0, expr: __self_1 } =>
::core::fmt::Formatter::debug_struct_field2_finish(f,
"Eq eq_span expr", __self_0, &__self_1),
}
}
}Debug, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for AttrArgs {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
AttrArgs::Empty => 0u8.stable_hash(__hcx, __hasher),
AttrArgs::Delimited(ref __binding_0) =>
1u8.stable_hash(__hcx, __hasher),
AttrArgs::Eq {
eq_span: ref __binding_0, expr: ref __binding_1 } =>
2u8.stable_hash(__hcx, __hasher),
}
match *self {
AttrArgs::Empty => {}
AttrArgs::Delimited(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
AttrArgs::Eq {
eq_span: ref __binding_0, expr: ref __binding_1 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for AttrArgs {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
AttrArgs::Empty => { 0usize }
AttrArgs::Delimited(ref __binding_0) => { 1usize }
AttrArgs::Eq {
eq_span: ref __binding_0, expr: ref __binding_1 } => {
2usize
}
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
AttrArgs::Empty => {}
AttrArgs::Delimited(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
AttrArgs::Eq {
eq_span: ref __binding_0, expr: ref __binding_1 } => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for AttrArgs {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => { AttrArgs::Empty }
1usize => {
AttrArgs::Delimited(::rustc_serialize::Decodable::decode(__decoder))
}
2usize => {
AttrArgs::Eq {
eq_span: ::rustc_serialize::Decodable::decode(__decoder),
expr: ::rustc_serialize::Decodable::decode(__decoder),
}
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `AttrArgs`, expected 0..3, actual {0}",
n));
}
}
}
}
};Decodable)]
15pub enum AttrArgs {
16 Empty,
18 Delimited(DelimArgs),
20 Eq {
22 eq_span: Span,
24 expr: MetaItemLit,
26 },
27}
28
29#[derive(#[automatically_derived]impl ::core::clone::Clone for AttrPath {
#[inline]
fn clone(&self) -> Self {
AttrPath {
segments: ::core::clone::Clone::clone(&self.segments),
span: ::core::clone::Clone::clone(&self.span),
}
}
}Clone, #[automatically_derived]impl ::core::fmt::Debug for AttrPath {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f,
"AttrPath segments span", &self.segments, &&self.span)
}
}Debug, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for AttrPath {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
AttrPath { segments: ref __binding_0, span: ref __binding_1
} => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for AttrPath {
fn encode(&self, __encoder: &mut __E) {
let AttrPath {
segments: ref __binding_0, span: ref __binding_1 } = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for AttrPath {
fn decode(__decoder: &mut __D) -> Self {
AttrPath {
segments: ::rustc_serialize::Decodable::decode(__decoder),
span: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable)]
30pub struct AttrPath {
31 pub segments: Box<[Symbol]>,
32 pub span: Span,
33}
34
35impl IntoDiagArg for AttrPath {
36 fn into_diag_arg(self, path: &mut Option<std::path::PathBuf>) -> DiagArgValue {
37 self.to_string().into_diag_arg(path)
38 }
39}
40
41impl AttrPath {
42 pub fn from_ast(path: &ast::Path, lower_span: impl Copy + Fn(Span) -> Span) -> Self {
43 AttrPath {
44 segments: path
45 .segments
46 .iter()
47 .map(|i| i.ident.name)
48 .collect::<Vec<_>>()
49 .into_boxed_slice(),
50 span: lower_span(path.span),
51 }
52 }
53}
54
55impl fmt::Display for AttrPath {
56 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57 f.write_fmt(format_args!("{0}",
join_path_idents(self.segments.iter().map(|i|
Ident { name: *i, span: DUMMY_SP }))))write!(
58 f,
59 "{}",
60 join_path_idents(self.segments.iter().map(|i| Ident { name: *i, span: DUMMY_SP }))
61 )
62 }
63}
64
65#[derive(#[automatically_derived]impl ::core::clone::Clone for AttrItem {
#[inline]
fn clone(&self) -> Self {
AttrItem {
path: ::core::clone::Clone::clone(&self.path),
args: ::core::clone::Clone::clone(&self.args),
style: ::core::clone::Clone::clone(&self.style),
span: ::core::clone::Clone::clone(&self.span),
}
}
}Clone, #[automatically_derived]impl ::core::fmt::Debug for AttrItem {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f,
"AttrItem path args style span", &self.path, &self.args,
&self.style, &&self.span)
}
}Debug, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for AttrItem {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
AttrItem {
path: ref __binding_0,
args: ref __binding_1,
style: ref __binding_2,
span: ref __binding_3 } => {
{ __binding_0.stable_hash(__hcx, __hasher); }
{ __binding_1.stable_hash(__hcx, __hasher); }
{ __binding_2.stable_hash(__hcx, __hasher); }
{ __binding_3.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for AttrItem {
fn encode(&self, __encoder: &mut __E) {
let AttrItem {
path: ref __binding_0,
args: ref __binding_1,
style: ref __binding_2,
span: ref __binding_3 } = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_1,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_2,
__encoder);
::rustc_serialize::Encodable::<__E>::encode(__binding_3,
__encoder);
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for AttrItem {
fn decode(__decoder: &mut __D) -> Self {
AttrItem {
path: ::rustc_serialize::Decodable::decode(__decoder),
args: ::rustc_serialize::Decodable::decode(__decoder),
style: ::rustc_serialize::Decodable::decode(__decoder),
span: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable)]
69pub struct AttrItem {
70 pub path: AttrPath,
72 pub args: AttrArgs,
73 pub style: AttrStyle,
76 pub span: Span,
78}
79
80#[derive(#[automatically_derived]impl ::core::marker::Copy for HashIgnoredAttrId { }Copy, #[automatically_derived]impl ::core::fmt::Debug for HashIgnoredAttrId {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f,
"HashIgnoredAttrId attr_id", &&self.attr_id)
}
}Debug, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for HashIgnoredAttrId {
fn encode(&self, __encoder: &mut __E) {
let HashIgnoredAttrId { attr_id: ref __binding_0 } = *self;
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for HashIgnoredAttrId {
fn decode(__decoder: &mut __D) -> Self {
HashIgnoredAttrId {
attr_id: ::rustc_serialize::Decodable::decode(__decoder),
}
}
}
};Decodable, #[automatically_derived]#[doc(hidden)]unsafe impl ::core::clone::TrivialClone
for HashIgnoredAttrId {
}
#[automatically_derived]impl ::core::clone::Clone for HashIgnoredAttrId {
#[inline]
fn clone(&self) -> Self {
let _: ::core::clone::AssertParamIsClone<AttrId>;
*self
}
}Clone)]
83pub struct HashIgnoredAttrId {
84 pub attr_id: AttrId,
85}
86
87impl StableHash for HashIgnoredAttrId {
88 fn stable_hash<Hcx: StableHashCtxt>(&self, _hcx: &mut Hcx, _hasher: &mut StableHasher) {
89 }
91}
92#[derive(#[automatically_derived]impl ::core::clone::Clone for Attribute {
#[inline]
fn clone(&self) -> Self {
match self {
Self::Parsed(__self_0) =>
Attribute::Parsed(::core::clone::Clone::clone(__self_0)),
Self::Tool(__self_0) =>
Attribute::Tool(::core::clone::Clone::clone(__self_0)),
Self::DeriveHelper(__self_0) =>
Attribute::DeriveHelper(::core::clone::Clone::clone(__self_0)),
}
}
}Clone, #[automatically_derived]impl ::core::fmt::Debug for Attribute {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
Self::Parsed(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"Parsed", &__self_0),
Self::Tool(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f, "Tool",
&__self_0),
Self::DeriveHelper(__self_0) =>
::core::fmt::Formatter::debug_struct_field1_finish(f,
"DeriveHelper", &__self_0),
}
}
}Debug, const _: () =
{
impl<__E: ::rustc_span::SpanEncoder> ::rustc_serialize::Encodable<__E>
for Attribute {
fn encode(&self, __encoder: &mut __E) {
let disc =
match *self {
Attribute::Parsed(ref __binding_0) => { 0usize }
Attribute::Tool(ref __binding_0) => { 1usize }
Attribute::DeriveHelper(ref __binding_0) => { 2usize }
};
::rustc_serialize::Encoder::emit_u8(__encoder, disc as u8);
match *self {
Attribute::Parsed(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
Attribute::Tool(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
Attribute::DeriveHelper(ref __binding_0) => {
::rustc_serialize::Encodable::<__E>::encode(__binding_0,
__encoder);
}
}
}
}
};Encodable, const _: () =
{
impl<__D: ::rustc_span::SpanDecoder> ::rustc_serialize::Decodable<__D>
for Attribute {
fn decode(__decoder: &mut __D) -> Self {
match ::rustc_serialize::Decoder::read_u8(__decoder) as usize
{
0usize => {
Attribute::Parsed(::rustc_serialize::Decodable::decode(__decoder))
}
1usize => {
Attribute::Tool(::rustc_serialize::Decodable::decode(__decoder))
}
2usize => {
Attribute::DeriveHelper(::rustc_serialize::Decodable::decode(__decoder))
}
n => {
::core::panicking::panic_fmt(format_args!("invalid enum variant tag while decoding `Attribute`, expected 0..3, actual {0}",
n));
}
}
}
}
};Decodable, const _: () =
{
impl ::rustc_data_structures::stable_hash::StableHash for Attribute {
#[inline]
fn stable_hash<__Hcx: ::rustc_data_structures::stable_hash::StableHashCtxt>(&self,
__hcx: &mut __Hcx,
__hasher:
&mut ::rustc_data_structures::stable_hash::StableHasher) {
match *self {
Attribute::Parsed(ref __binding_0) =>
0u8.stable_hash(__hcx, __hasher),
Attribute::Tool(ref __binding_0) =>
1u8.stable_hash(__hcx, __hasher),
Attribute::DeriveHelper(ref __binding_0) =>
2u8.stable_hash(__hcx, __hasher),
}
match *self {
Attribute::Parsed(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
Attribute::Tool(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
Attribute::DeriveHelper(ref __binding_0) => {
{ __binding_0.stable_hash(__hcx, __hasher); }
}
}
}
}
};StableHash)]
99pub enum Attribute {
100 Parsed(AttributeKind),
110
111 Tool(Box<AttrItem>),
116
117 DeriveHelper(Box<AttrItem>),
123}
124
125impl Attribute {
126 pub fn value_lit(&self) -> Option<&MetaItemLit> {
127 match &self {
128 Attribute::Tool(n) | Attribute::DeriveHelper(n) => match n.as_ref() {
129 AttrItem { args: AttrArgs::Eq { eq_span: _, expr }, .. } => Some(expr),
130 _ => None,
131 },
132 _ => None,
133 }
134 }
135
136 pub fn is_prefix_attr_for_suggestions(&self) -> bool {
137 match self {
138 Attribute::Tool(attr) | Attribute::DeriveHelper(attr) => {
139 attr.span.desugaring_kind().is_none()
140 }
141 Attribute::Parsed(_) => true,
144 }
145 }
146}
147
148impl AttributeExt for Attribute {
149 #[inline]
150 fn value_str(&self) -> Option<Symbol> {
151 self.value_lit().and_then(|x| x.value_as_str())
152 }
153
154 #[inline]
156 fn name(&self) -> Option<Symbol> {
157 match &self {
158 Attribute::DeriveHelper(n) => {
159 if let [ident] = n.path.segments.as_ref() {
160 Some(*ident)
161 } else {
162 None
163 }
164 }
165 Attribute::Parsed(..) => None,
166 Attribute::Tool(..) => None,
168 }
169 }
170
171 #[inline]
172 fn path_matches(&self, name: &[Symbol]) -> bool {
173 match &self {
174 Attribute::Tool(n) | Attribute::DeriveHelper(n) => n.path.segments.iter().eq(name),
175 _ => false,
176 }
177 }
178
179 #[inline]
180 fn doc_str_and_fragment_kind(&self) -> Option<(Symbol, DocFragmentKind, Span)> {
181 match &self {
182 Attribute::Parsed(AttributeKind::DocComment { kind, comment, span, .. }) => {
183 Some((*comment, *kind, *span))
184 }
185 _ => None,
186 }
187 }
188
189 fn doc_resolution_scope(&self) -> Option<AttrStyle> {
190 match self {
191 Attribute::Parsed(AttributeKind::DocComment { style, .. }) => Some(*style),
192 _ => None,
193 }
194 }
195}