@@ -8,7 +8,7 @@ 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 } ;
11+ use syntax:: { ast, format_smolstr , match_ast, AstNode , AstToken , SmolStr , SyntaxNode } ;
1212use triomphe:: Arc ;
1313
1414use crate :: {
@@ -49,11 +49,18 @@ impl RawAttrs {
4949 Either :: Left ( attr) => {
5050 attr. meta ( ) . and_then ( |meta| Attr :: from_src ( db, meta, span_map, id) )
5151 }
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 ,
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+ }
5764 } ) ,
5865 } ) ;
5966 let entries: Arc < [ Attr ] > = Arc :: from_iter ( entries) ;
@@ -179,16 +186,15 @@ pub struct Attr {
179186#[ derive( Debug , Clone , PartialEq , Eq , Hash ) ]
180187pub enum AttrInput {
181188 /// `#[attr = "string"]`
182- // FIXME: This is losing span
183- Literal ( SmolStr ) ,
189+ Literal ( tt:: Literal ) ,
184190 /// `#[attr(subtree)]`
185191 TokenTree ( Box < tt:: Subtree > ) ,
186192}
187193
188194impl fmt:: Display for AttrInput {
189195 fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
190196 match self {
191- AttrInput :: Literal ( lit) => write ! ( f, " = \" {} \" " , lit . escape_debug ( ) ) ,
197+ AttrInput :: Literal ( lit) => write ! ( f, " = {lit}" ) ,
192198 AttrInput :: TokenTree ( tt) => tt. fmt ( f) ,
193199 }
194200 }
@@ -208,11 +214,10 @@ impl Attr {
208214 } ) ?) ;
209215 let span = span_map. span_for_range ( range) ;
210216 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) ) )
217+ Some ( Interned :: new ( AttrInput :: Literal ( tt:: Literal {
218+ text : lit. token ( ) . text ( ) . into ( ) ,
219+ span,
220+ } ) ) )
216221 } else if let Some ( tt) = ast. token_tree ( ) {
217222 let tree = syntax_node_to_token_tree ( tt. syntax ( ) , span_map, span) ;
218223 Some ( Interned :: new ( AttrInput :: TokenTree ( Box :: new ( tree) ) ) )
@@ -245,9 +250,8 @@ impl Attr {
245250 }
246251 Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Punct ( tt:: Punct { char : '=' , .. } ) ) ) => {
247252 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 ( ) ) ) )
253+ Some ( tt:: TokenTree :: Leaf ( tt:: Leaf :: Literal ( lit) ) ) => {
254+ Some ( Interned :: new ( AttrInput :: Literal ( lit. clone ( ) ) ) )
251255 }
252256 _ => None ,
253257 } ;
@@ -265,9 +269,14 @@ impl Attr {
265269
266270impl Attr {
267271 /// #[path = "string"]
268- pub fn string_value ( & self ) -> Option < & SmolStr > {
272+ pub fn string_value ( & self ) -> Option < & str > {
269273 match self . input . as_deref ( ) ? {
270- AttrInput :: Literal ( it) => Some ( it) ,
274+ AttrInput :: Literal ( it) => match it. text . strip_prefix ( 'r' ) {
275+ Some ( it) => it. trim_matches ( '#' ) ,
276+ None => it. text . as_str ( ) ,
277+ }
278+ . strip_prefix ( '"' ) ?
279+ . strip_suffix ( '"' ) ,
271280 _ => None ,
272281 }
273282 }
0 commit comments