@@ -8,8 +8,8 @@ use intern::Interned;
8
8
use mbe:: { syntax_node_to_token_tree, DelimiterKind , Punct } ;
9
9
use smallvec:: { smallvec, SmallVec } ;
10
10
use span:: { Span , SyntaxContextId } ;
11
- use syntax:: { ast, match_ast, AstNode , AstToken , SmolStr , SyntaxNode } ;
12
- use triomphe:: Arc ;
11
+ use syntax:: { ast, format_smolstr , match_ast, AstNode , AstToken , SmolStr , SyntaxNode } ;
12
+ use triomphe:: ThinArc ;
13
13
14
14
use crate :: {
15
15
db:: ExpandDatabase ,
@@ -22,16 +22,15 @@ use crate::{
22
22
/// Syntactical attributes, without filtering of `cfg_attr`s.
23
23
#[ derive( Default , Debug , Clone , PartialEq , Eq ) ]
24
24
pub struct RawAttrs {
25
- // FIXME: Make this a ThinArc
26
- entries : Option < Arc < [ Attr ] > > ,
25
+ entries : Option < ThinArc < ( ) , Attr > > ,
27
26
}
28
27
29
28
impl ops:: Deref for RawAttrs {
30
29
type Target = [ Attr ] ;
31
30
32
31
fn deref ( & self ) -> & [ Attr ] {
33
32
match & self . entries {
34
- Some ( it) => it ,
33
+ Some ( it) => & it . slice ,
35
34
None => & [ ] ,
36
35
}
37
36
}
@@ -45,20 +44,34 @@ impl RawAttrs {
45
44
owner : & dyn ast:: HasAttrs ,
46
45
span_map : SpanMapRef < ' _ > ,
47
46
) -> Self {
48
- let entries = collect_attrs ( owner) . filter_map ( |( id, attr) | match attr {
49
- Either :: Left ( attr) => {
50
- attr. meta ( ) . and_then ( |meta| Attr :: from_src ( db, meta, span_map, id) )
51
- }
52
- Either :: Right ( comment) => comment. doc_comment ( ) . map ( |doc| Attr {
53
- id,
54
- input : Some ( Interned :: new ( AttrInput :: Literal ( SmolStr :: new ( doc) ) ) ) ,
55
- path : Interned :: new ( ModPath :: from ( crate :: name!( doc) ) ) ,
56
- ctxt : span_map. span_for_range ( comment. syntax ( ) . text_range ( ) ) . ctx ,
57
- } ) ,
58
- } ) ;
59
- let entries: Arc < [ Attr ] > = Arc :: from_iter ( entries) ;
47
+ let entries: Vec < _ > = collect_attrs ( owner)
48
+ . filter_map ( |( id, attr) | match attr {
49
+ Either :: Left ( attr) => {
50
+ attr. meta ( ) . and_then ( |meta| Attr :: from_src ( db, meta, span_map, id) )
51
+ }
52
+ Either :: Right ( comment) => comment. doc_comment ( ) . map ( |doc| {
53
+ let span = span_map. span_for_range ( comment. syntax ( ) . text_range ( ) ) ;
54
+ Attr {
55
+ id,
56
+ input : Some ( Interned :: new ( AttrInput :: Literal ( tt:: Literal {
57
+ // FIXME: Escape quotes from comment content
58
+ text : SmolStr :: new ( format_smolstr ! ( "\" {doc}\" " , ) ) ,
59
+ span,
60
+ } ) ) ) ,
61
+ path : Interned :: new ( ModPath :: from ( crate :: name!( doc) ) ) ,
62
+ ctxt : span. ctx ,
63
+ }
64
+ } ) ,
65
+ } )
66
+ . collect ( ) ;
60
67
61
- Self { entries : if entries. is_empty ( ) { None } else { Some ( entries) } }
68
+ let entries = if entries. is_empty ( ) {
69
+ None
70
+ } else {
71
+ Some ( ThinArc :: from_header_and_iter ( ( ) , entries. into_iter ( ) ) )
72
+ } ;
73
+
74
+ RawAttrs { entries }
62
75
}
63
76
64
77
pub fn from_attrs_owner (
@@ -75,16 +88,20 @@ impl RawAttrs {
75
88
( None , entries @ Some ( _) ) => Self { entries } ,
76
89
( Some ( entries) , None ) => Self { entries : Some ( entries. clone ( ) ) } ,
77
90
( Some ( a) , Some ( b) ) => {
78
- let last_ast_index = a. last ( ) . map_or ( 0 , |it| it. id . ast_index ( ) + 1 ) as u32 ;
79
- Self {
80
- entries : Some ( Arc :: from_iter ( a. iter ( ) . cloned ( ) . chain ( b. iter ( ) . map ( |it| {
91
+ let last_ast_index = a. slice . last ( ) . map_or ( 0 , |it| it. id . ast_index ( ) + 1 ) as u32 ;
92
+ let items = a
93
+ . slice
94
+ . iter ( )
95
+ . cloned ( )
96
+ . chain ( b. slice . iter ( ) . map ( |it| {
81
97
let mut it = it. clone ( ) ;
82
98
it. id . id = ( it. id . ast_index ( ) as u32 + last_ast_index)
83
99
| ( it. id . cfg_attr_index ( ) . unwrap_or ( 0 ) as u32 )
84
100
<< AttrId :: AST_INDEX_BITS ;
85
101
it
86
- } ) ) ) ) ,
87
- }
102
+ } ) )
103
+ . collect :: < Vec < _ > > ( ) ;
104
+ Self { entries : Some ( ThinArc :: from_header_and_iter ( ( ) , items. into_iter ( ) ) ) }
88
105
}
89
106
}
90
107
}
@@ -100,41 +117,47 @@ impl RawAttrs {
100
117
}
101
118
102
119
let crate_graph = db. crate_graph ( ) ;
103
- let new_attrs = Arc :: from_iter ( self . iter ( ) . flat_map ( |attr| -> SmallVec < [ _ ; 1 ] > {
104
- let is_cfg_attr =
105
- attr. path . as_ident ( ) . map_or ( false , |name| * name == crate :: name![ cfg_attr] ) ;
106
- if !is_cfg_attr {
107
- return smallvec ! [ attr. clone( ) ] ;
108
- }
109
-
110
- let subtree = match attr. token_tree_value ( ) {
111
- Some ( it) => it,
112
- _ => return smallvec ! [ attr. clone( ) ] ,
113
- } ;
114
-
115
- let ( cfg, parts) = match parse_cfg_attr_input ( subtree) {
116
- Some ( it) => it,
117
- None => return smallvec ! [ attr. clone( ) ] ,
118
- } ;
119
- let index = attr. id ;
120
- let attrs = parts
121
- . enumerate ( )
122
- . take ( 1 << AttrId :: CFG_ATTR_BITS )
123
- . filter_map ( |( idx, attr) | Attr :: from_tt ( db, attr, index. with_cfg_attr ( idx) ) ) ;
124
-
125
- let cfg_options = & crate_graph[ krate] . cfg_options ;
126
- let cfg = Subtree { delimiter : subtree. delimiter , token_trees : Box :: from ( cfg) } ;
127
- let cfg = CfgExpr :: parse ( & cfg) ;
128
- if cfg_options. check ( & cfg) == Some ( false ) {
129
- smallvec ! [ ]
130
- } else {
131
- cov_mark:: hit!( cfg_attr_active) ;
132
-
133
- attrs. collect ( )
134
- }
135
- } ) ) ;
120
+ let new_attrs =
121
+ self . iter ( )
122
+ . flat_map ( |attr| -> SmallVec < [ _ ; 1 ] > {
123
+ let is_cfg_attr =
124
+ attr. path . as_ident ( ) . map_or ( false , |name| * name == crate :: name![ cfg_attr] ) ;
125
+ if !is_cfg_attr {
126
+ return smallvec ! [ attr. clone( ) ] ;
127
+ }
136
128
137
- RawAttrs { entries : Some ( new_attrs) }
129
+ let subtree = match attr. token_tree_value ( ) {
130
+ Some ( it) => it,
131
+ _ => return smallvec ! [ attr. clone( ) ] ,
132
+ } ;
133
+
134
+ let ( cfg, parts) = match parse_cfg_attr_input ( subtree) {
135
+ Some ( it) => it,
136
+ None => return smallvec ! [ attr. clone( ) ] ,
137
+ } ;
138
+ let index = attr. id ;
139
+ let attrs = parts. enumerate ( ) . take ( 1 << AttrId :: CFG_ATTR_BITS ) . filter_map (
140
+ |( idx, attr) | Attr :: from_tt ( db, attr, index. with_cfg_attr ( idx) ) ,
141
+ ) ;
142
+
143
+ let cfg_options = & crate_graph[ krate] . cfg_options ;
144
+ let cfg = Subtree { delimiter : subtree. delimiter , token_trees : Box :: from ( cfg) } ;
145
+ let cfg = CfgExpr :: parse ( & cfg) ;
146
+ if cfg_options. check ( & cfg) == Some ( false ) {
147
+ smallvec ! [ ]
148
+ } else {
149
+ cov_mark:: hit!( cfg_attr_active) ;
150
+
151
+ attrs. collect ( )
152
+ }
153
+ } )
154
+ . collect :: < Vec < _ > > ( ) ;
155
+ let entries = if new_attrs. is_empty ( ) {
156
+ None
157
+ } else {
158
+ Some ( ThinArc :: from_header_and_iter ( ( ) , new_attrs. into_iter ( ) ) )
159
+ } ;
160
+ RawAttrs { entries }
138
161
}
139
162
}
140
163
@@ -179,16 +202,15 @@ pub struct Attr {
179
202
#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
180
203
pub enum AttrInput {
181
204
/// `#[attr = "string"]`
182
- // FIXME: This is losing span
183
- Literal ( SmolStr ) ,
205
+ Literal ( tt:: Literal ) ,
184
206
/// `#[attr(subtree)]`
185
207
TokenTree ( Box < tt:: Subtree > ) ,
186
208
}
187
209
188
210
impl fmt:: Display for AttrInput {
189
211
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
190
212
match self {
191
- AttrInput :: Literal ( lit) => write ! ( f, " = \" {} \" " , lit . escape_debug ( ) ) ,
213
+ AttrInput :: Literal ( lit) => write ! ( f, " = {lit}" ) ,
192
214
AttrInput :: TokenTree ( tt) => tt. fmt ( f) ,
193
215
}
194
216
}
@@ -208,11 +230,10 @@ impl Attr {
208
230
} ) ?) ;
209
231
let span = span_map. span_for_range ( range) ;
210
232
let input = if let Some ( ast:: Expr :: Literal ( lit) ) = ast. expr ( ) {
211
- let value = match lit. kind ( ) {
212
- ast:: LiteralKind :: String ( string) => string. value ( ) ?. into ( ) ,
213
- _ => lit. syntax ( ) . first_token ( ) ?. text ( ) . trim_matches ( '"' ) . into ( ) ,
214
- } ;
215
- Some ( Interned :: new ( AttrInput :: Literal ( value) ) )
233
+ Some ( Interned :: new ( AttrInput :: Literal ( tt:: Literal {
234
+ text : lit. token ( ) . text ( ) . into ( ) ,
235
+ span,
236
+ } ) ) )
216
237
} else if let Some ( tt) = ast. token_tree ( ) {
217
238
let tree = syntax_node_to_token_tree ( tt. syntax ( ) , span_map, span) ;
218
239
Some ( Interned :: new ( AttrInput :: TokenTree ( Box :: new ( tree) ) ) )
@@ -245,9 +266,8 @@ impl Attr {
245
266
}
246
267
Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( tt:: Punct { char : '=' , .. } ) ) ) => {
247
268
let input = match input. get ( 1 ) {
248
- Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal { text, .. } ) ) ) => {
249
- //FIXME the trimming here isn't quite right, raw strings are not handled
250
- Some ( Interned :: new ( AttrInput :: Literal ( text. trim_matches ( '"' ) . into ( ) ) ) )
269
+ Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Literal ( lit) ) ) => {
270
+ Some ( Interned :: new ( AttrInput :: Literal ( lit. clone ( ) ) ) )
251
271
}
252
272
_ => None ,
253
273
} ;
@@ -265,9 +285,14 @@ impl Attr {
265
285
266
286
impl Attr {
267
287
/// #[path = "string"]
268
- pub fn string_value ( & self ) -> Option < & SmolStr > {
288
+ pub fn string_value ( & self ) -> Option < & str > {
269
289
match self . input . as_deref ( ) ? {
270
- AttrInput :: Literal ( it) => Some ( it) ,
290
+ AttrInput :: Literal ( it) => match it. text . strip_prefix ( 'r' ) {
291
+ Some ( it) => it. trim_matches ( '#' ) ,
292
+ None => it. text . as_str ( ) ,
293
+ }
294
+ . strip_prefix ( '"' ) ?
295
+ . strip_suffix ( '"' ) ,
271
296
_ => None ,
272
297
}
273
298
}
0 commit comments