@@ -8,8 +8,8 @@ use intern::Interned;
88use mbe:: { syntax_node_to_token_tree, DelimiterKind , Punct } ;
99use smallvec:: { smallvec, SmallVec } ;
1010use 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 ;
1313
1414use crate :: {
1515 db:: ExpandDatabase ,
@@ -22,16 +22,15 @@ use crate::{
2222/// Syntactical attributes, without filtering of `cfg_attr`s.
2323#[ derive( Default , Debug , Clone , PartialEq , Eq ) ]
2424pub struct RawAttrs {
25- // FIXME: Make this a ThinArc
26- entries : Option < Arc < [ Attr ] > > ,
25+ entries : Option < ThinArc < ( ) , Attr > > ,
2726}
2827
2928impl ops:: Deref for RawAttrs {
3029 type Target = [ Attr ] ;
3130
3231 fn deref ( & self ) -> & [ Attr ] {
3332 match & self . entries {
34- Some ( it) => it ,
33+ Some ( it) => & it . slice ,
3534 None => & [ ] ,
3635 }
3736 }
@@ -45,20 +44,34 @@ impl RawAttrs {
4544 owner : & dyn ast:: HasAttrs ,
4645 span_map : SpanMapRef < ' _ > ,
4746 ) -> 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 ( ) ;
6067
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 }
6275 }
6376
6477 pub fn from_attrs_owner (
@@ -75,16 +88,20 @@ impl RawAttrs {
7588 ( None , entries @ Some ( _) ) => Self { entries } ,
7689 ( Some ( entries) , None ) => Self { entries : Some ( entries. clone ( ) ) } ,
7790 ( 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| {
8197 let mut it = it. clone ( ) ;
8298 it. id . id = ( it. id . ast_index ( ) as u32 + last_ast_index)
8399 | ( it. id . cfg_attr_index ( ) . unwrap_or ( 0 ) as u32 )
84100 << AttrId :: AST_INDEX_BITS ;
85101 it
86- } ) ) ) ) ,
87- }
102+ } ) )
103+ . collect :: < Vec < _ > > ( ) ;
104+ Self { entries : Some ( ThinArc :: from_header_and_iter ( ( ) , items. into_iter ( ) ) ) }
88105 }
89106 }
90107 }
@@ -100,41 +117,47 @@ impl RawAttrs {
100117 }
101118
102119 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+ }
136128
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 }
138161 }
139162}
140163
@@ -179,16 +202,15 @@ pub struct Attr {
179202#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
180203pub enum AttrInput {
181204 /// `#[attr = "string"]`
182- // FIXME: This is losing span
183- Literal ( SmolStr ) ,
205+ Literal ( tt:: Literal ) ,
184206 /// `#[attr(subtree)]`
185207 TokenTree ( Box < tt:: Subtree > ) ,
186208}
187209
188210impl fmt:: Display for AttrInput {
189211 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
190212 match self {
191- AttrInput :: Literal ( lit) => write ! ( f, " = \" {} \" " , lit . escape_debug ( ) ) ,
213+ AttrInput :: Literal ( lit) => write ! ( f, " = {lit}" ) ,
192214 AttrInput :: TokenTree ( tt) => tt. fmt ( f) ,
193215 }
194216 }
@@ -208,11 +230,10 @@ impl Attr {
208230 } ) ?) ;
209231 let span = span_map. span_for_range ( range) ;
210232 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+ } ) ) )
216237 } else if let Some ( tt) = ast. token_tree ( ) {
217238 let tree = syntax_node_to_token_tree ( tt. syntax ( ) , span_map, span) ;
218239 Some ( Interned :: new ( AttrInput :: TokenTree ( Box :: new ( tree) ) ) )
@@ -245,9 +266,8 @@ impl Attr {
245266 }
246267 Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( tt:: Punct { char : '=' , .. } ) ) ) => {
247268 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 ( ) ) ) )
251271 }
252272 _ => None ,
253273 } ;
@@ -265,9 +285,14 @@ impl Attr {
265285
266286impl Attr {
267287 /// #[path = "string"]
268- pub fn string_value ( & self ) -> Option < & SmolStr > {
288+ pub fn string_value ( & self ) -> Option < & str > {
269289 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 ( '"' ) ,
271296 _ => None ,
272297 }
273298 }
0 commit comments