11//! Builtin macro
22
3+ use std:: borrow:: Cow ;
4+
35use base_db:: AnchoredPath ;
46use cfg:: CfgExpr ;
57use either:: Either ;
@@ -13,7 +15,7 @@ use span::{Edition, FileId, Span};
1315use stdx:: format_to;
1416use syntax:: {
1517 format_smolstr,
16- unescape:: { unescape_byte, unescape_char, unescape_str } ,
18+ unescape:: { unescape_byte, unescape_char} ,
1719} ;
1820use syntax_bridge:: syntax_node_to_token_tree;
1921
@@ -177,12 +179,7 @@ fn line_expand(
177179 // not incremental
178180 ExpandResult :: ok ( tt:: TopSubtree :: invisible_from_leaves (
179181 span,
180- [ tt:: Leaf :: Literal ( tt:: Literal {
181- symbol : sym:: INTEGER_0 ,
182- span,
183- kind : tt:: LitKind :: Integer ,
184- suffix : Some ( sym:: u32) ,
185- } ) ] ,
182+ [ tt:: Leaf :: Literal ( tt:: Literal :: new ( "0" , span, tt:: LitKind :: Integer , "u32" ) ) ] ,
186183 ) )
187184}
188185
@@ -303,7 +300,8 @@ fn format_args_nl_expand(
303300 mut lit @ tt:: Literal { kind : tt:: LitKind :: Str , .. } ,
304301 ) ) ) = lit
305302 {
306- lit. symbol = Symbol :: intern ( & format_smolstr ! ( "{}\\ n" , lit. symbol. as_str( ) ) ) ;
303+ let ( text, suffix) = lit. text_and_suffix ( ) ;
304+ lit. text_and_suffix = Symbol :: intern ( & format_smolstr ! ( "{text}\\ n{suffix}" ) ) ;
307305 tt. set_token ( 1 , lit. into ( ) ) ;
308306 }
309307 ExpandResult :: ok ( quote ! { span =>
@@ -521,14 +519,11 @@ fn compile_error_expand(
521519 let err = match tt. iter ( ) . collect_array ( ) {
522520 Some (
523521 [
524- tt:: TtElement :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
525- symbol : text,
526- span : _,
527- kind : tt:: LitKind :: Str | tt:: LitKind :: StrRaw ( _) ,
528- suffix : _,
529- } ) ) ,
522+ tt:: TtElement :: Leaf ( tt:: Leaf :: Literal (
523+ lit @ tt:: Literal { kind : tt:: LitKind :: Str | tt:: LitKind :: StrRaw ( _) , .. } ,
524+ ) ) ,
530525 ] ,
531- ) => ExpandError :: other ( span, Box :: from ( unescape_symbol ( & text ) . as_str ( ) ) ) ,
526+ ) => ExpandError :: other ( span, Box :: from ( unescape_str ( lit . text ( ) ) ) ) ,
532527 _ => ExpandError :: other ( span, "`compile_error!` argument must be a string" ) ,
533528 } ;
534529
@@ -569,20 +564,20 @@ fn concat_expand(
569564 // as-is.
570565 match it. kind {
571566 tt:: LitKind :: Char => {
572- if let Ok ( c) = unescape_char ( it. symbol . as_str ( ) ) {
567+ if let Ok ( c) = unescape_char ( it. text ( ) ) {
573568 text. push ( c) ;
574569 }
575570 record_span ( it. span ) ;
576571 }
577572 tt:: LitKind :: Integer | tt:: LitKind :: Float => {
578- format_to ! ( text, "{}" , it. symbol . as_str ( ) )
573+ format_to ! ( text, "{}" , it. text ( ) )
579574 }
580575 tt:: LitKind :: Str => {
581- text. push_str ( unescape_symbol ( & it. symbol ) . as_str ( ) ) ;
576+ text. push_str ( & unescape_str ( it. text ( ) ) ) ;
582577 record_span ( it. span ) ;
583578 }
584579 tt:: LitKind :: StrRaw ( _) => {
585- format_to ! ( text, "{}" , it. symbol . as_str ( ) ) ;
580+ format_to ! ( text, "{}" , it. text ( ) ) ;
586581 record_span ( it. span ) ;
587582 }
588583 tt:: LitKind :: Byte
@@ -620,7 +615,7 @@ fn concat_expand(
620615 TtElement :: Leaf ( tt:: Leaf :: Literal ( it) )
621616 if matches ! ( it. kind, tt:: LitKind :: Integer | tt:: LitKind :: Float ) =>
622617 {
623- format_to ! ( text, "-{}" , it. symbol . as_str ( ) ) ;
618+ format_to ! ( text, "-{}" , it. text ( ) ) ;
624619 record_span ( punct. span . cover ( it. span ) ) ;
625620 }
626621 _ => {
@@ -658,26 +653,22 @@ fn concat_bytes_expand(
658653 } ;
659654 for ( i, t) in tt. iter ( ) . enumerate ( ) {
660655 match t {
661- TtElement :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
662- symbol : text,
663- span,
664- kind,
665- suffix : _,
666- } ) ) => {
656+ TtElement :: Leaf ( tt:: Leaf :: Literal ( lit @ tt:: Literal { span, kind, .. } ) ) => {
657+ let text = lit. text ( ) ;
667658 record_span ( span) ;
668659 match kind {
669660 tt:: LitKind :: Byte => {
670- if let Ok ( b) = unescape_byte ( text. as_str ( ) ) {
661+ if let Ok ( b) = unescape_byte ( text) {
671662 bytes. extend (
672663 b. escape_ascii ( ) . filter_map ( |it| char:: from_u32 ( it as u32 ) ) ,
673664 ) ;
674665 }
675666 }
676667 tt:: LitKind :: ByteStr => {
677- bytes. push_str ( text. as_str ( ) ) ;
668+ bytes. push_str ( text) ;
678669 }
679670 tt:: LitKind :: ByteStrRaw ( _) => {
680- bytes. extend ( text. as_str ( ) . escape_debug ( ) ) ;
671+ bytes. extend ( text. escape_debug ( ) ) ;
681672 }
682673 _ => {
683674 err. get_or_insert ( ExpandError :: other ( span, "unexpected token" ) ) ;
@@ -706,12 +697,7 @@ fn concat_bytes_expand(
706697 ExpandResult {
707698 value : tt:: TopSubtree :: invisible_from_leaves (
708699 span,
709- [ tt:: Leaf :: Literal ( tt:: Literal {
710- symbol : Symbol :: intern ( & bytes) ,
711- span,
712- kind : tt:: LitKind :: ByteStr ,
713- suffix : None ,
714- } ) ] ,
700+ [ tt:: Leaf :: Literal ( tt:: Literal :: new_no_suffix ( & bytes, span, tt:: LitKind :: ByteStr ) ) ] ,
715701 ) ,
716702 err,
717703 }
@@ -725,25 +711,19 @@ fn concat_bytes_expand_subtree(
725711) -> Result < ( ) , ExpandError > {
726712 for ( ti, tt) in tree_iter. enumerate ( ) {
727713 match tt {
728- TtElement :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
729- symbol : text,
730- span,
731- kind : tt:: LitKind :: Byte ,
732- suffix : _,
733- } ) ) => {
734- if let Ok ( b) = unescape_byte ( text. as_str ( ) ) {
714+ TtElement :: Leaf ( tt:: Leaf :: Literal (
715+ lit @ tt:: Literal { span, kind : tt:: LitKind :: Byte , .. } ,
716+ ) ) => {
717+ if let Ok ( b) = unescape_byte ( lit. text ( ) ) {
735718 bytes. extend ( b. escape_ascii ( ) . filter_map ( |it| char:: from_u32 ( it as u32 ) ) ) ;
736719 }
737720 record_span ( span) ;
738721 }
739- TtElement :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
740- symbol : text,
741- span,
742- kind : tt:: LitKind :: Integer ,
743- suffix : _,
744- } ) ) => {
722+ TtElement :: Leaf ( tt:: Leaf :: Literal (
723+ lit @ tt:: Literal { span, kind : tt:: LitKind :: Integer , .. } ,
724+ ) ) => {
745725 record_span ( span) ;
746- if let Ok ( b) = text . as_str ( ) . parse :: < u8 > ( ) {
726+ if let Ok ( b) = lit . text ( ) . parse :: < u8 > ( ) {
747727 bytes. extend ( b. escape_ascii ( ) . filter_map ( |it| char:: from_u32 ( it as u32 ) ) ) ;
748728 }
749729 }
@@ -792,18 +772,16 @@ fn parse_string(tt: &tt::TopSubtree) -> Result<(Symbol, Span), ExpandError> {
792772 }
793773
794774 match tt {
795- TtElement :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
796- symbol : text,
775+ TtElement :: Leaf ( tt:: Leaf :: Literal ( lit @ tt:: Literal {
797776 span,
798777 kind : tt:: LitKind :: Str ,
799- suffix : _,
800- } ) ) => Ok ( ( unescape_symbol ( & text) , span) ) ,
801- TtElement :: Leaf ( tt:: Leaf :: Literal ( tt:: Literal {
802- symbol : text,
778+ ..
779+ } ) ) => Ok ( ( Symbol :: intern ( & unescape_str ( lit. text ( ) ) ) , span) ) ,
780+ TtElement :: Leaf ( tt:: Leaf :: Literal ( lit @ tt:: Literal {
803781 span,
804782 kind : tt:: LitKind :: StrRaw ( _) ,
805- suffix : _ ,
806- } ) ) => Ok ( ( text . clone ( ) , span) ) ,
783+ ..
784+ } ) ) => Ok ( ( Symbol :: intern ( lit . text ( ) ) , span) ) ,
807785 TtElement :: Leaf ( l) => Err ( * l. span ( ) ) ,
808786 TtElement :: Subtree ( tt, _) => Err ( tt. delimiter . open . cover ( tt. delimiter . close ) ) ,
809787 }
@@ -855,10 +833,10 @@ fn include_bytes_expand(
855833 let res = tt:: TopSubtree :: invisible_from_leaves (
856834 span,
857835 [ tt:: Leaf :: Literal ( tt:: Literal {
858- symbol : Symbol :: empty ( ) ,
836+ text_and_suffix : Symbol :: empty ( ) ,
859837 span,
860838 kind : tt:: LitKind :: ByteStrRaw ( 1 ) ,
861- suffix : None ,
839+ suffix_len : 0 ,
862840 } ) ] ,
863841 ) ;
864842 ExpandResult :: ok ( res)
@@ -979,17 +957,16 @@ fn quote_expand(
979957 )
980958}
981959
982- fn unescape_symbol ( s : & Symbol ) -> Symbol {
983- if s. as_str ( ) . contains ( '\\' ) {
984- let s = s. as_str ( ) ;
960+ fn unescape_str ( s : & str ) -> Cow < ' _ , str > {
961+ if s. contains ( '\\' ) {
985962 let mut buf = String :: with_capacity ( s. len ( ) ) ;
986- unescape_str ( s, |_, c| {
963+ syntax :: unescape :: unescape_str ( s, |_, c| {
987964 if let Ok ( c) = c {
988965 buf. push ( c)
989966 }
990967 } ) ;
991- Symbol :: intern ( & buf)
968+ Cow :: Owned ( buf)
992969 } else {
993- s . clone ( )
970+ Cow :: Borrowed ( s )
994971 }
995972}
0 commit comments