11//! Conversions between [`SyntaxNode`] and [`tt::TokenTree`].
22
3+ use std:: fmt;
4+
35use rustc_hash:: { FxHashMap , FxHashSet } ;
46use span:: { SpanAnchor , SpanData , SpanMap } ;
57use stdx:: { never, non_empty_vec:: NonEmptyVec } ;
@@ -9,30 +11,27 @@ use syntax::{
911 SyntaxKind :: * ,
1012 SyntaxNode , SyntaxToken , SyntaxTreeBuilder , TextRange , TextSize , WalkEvent , T ,
1113} ;
12- use tt:: {
13- buffer:: { Cursor , TokenBuffer } ,
14- Span ,
15- } ;
14+ use tt:: buffer:: { Cursor , TokenBuffer } ;
1615
1716use crate :: { to_parser_input:: to_parser_input, tt_iter:: TtIter } ;
1817
1918#[ cfg( test) ]
2019mod tests;
2120
22- pub trait SpanMapper < S : Span > {
21+ pub trait SpanMapper < S > {
2322 fn span_for ( & self , range : TextRange ) -> S ;
2423}
2524
2625impl < S > SpanMapper < SpanData < S > > for SpanMap < S >
2726where
28- SpanData < S > : Span ,
27+ SpanData < S > : Copy ,
2928{
3029 fn span_for ( & self , range : TextRange ) -> SpanData < S > {
3130 self . span_at ( range. start ( ) )
3231 }
3332}
3433
35- impl < S : Span , SM : SpanMapper < S > > SpanMapper < S > for & SM {
34+ impl < S : Copy , SM : SpanMapper < S > > SpanMapper < S > for & SM {
3635 fn span_for ( & self , range : TextRange ) -> S {
3736 SM :: span_for ( self , range)
3837 }
@@ -78,8 +77,7 @@ pub fn syntax_node_to_token_tree<Ctx, SpanMap>(
7877 span : SpanData < Ctx > ,
7978) -> tt:: Subtree < SpanData < Ctx > >
8079where
81- SpanData < Ctx > : Span ,
82- Ctx : Copy ,
80+ SpanData < Ctx > : Copy + fmt:: Debug ,
8381 SpanMap : SpanMapper < SpanData < Ctx > > ,
8482{
8583 let mut c = Converter :: new ( node, map, Default :: default ( ) , Default :: default ( ) , span) ;
@@ -98,8 +96,7 @@ pub fn syntax_node_to_token_tree_modified<Ctx, SpanMap>(
9896) -> tt:: Subtree < SpanData < Ctx > >
9997where
10098 SpanMap : SpanMapper < SpanData < Ctx > > ,
101- SpanData < Ctx > : Span ,
102- Ctx : Copy ,
99+ SpanData < Ctx > : Copy + fmt:: Debug ,
103100{
104101 let mut c = Converter :: new ( node, map, append, remove, call_site) ;
105102 convert_tokens ( & mut c)
@@ -124,8 +121,7 @@ pub fn token_tree_to_syntax_node<Ctx>(
124121 entry_point : parser:: TopEntryPoint ,
125122) -> ( Parse < SyntaxNode > , SpanMap < Ctx > )
126123where
127- SpanData < Ctx > : Span ,
128- Ctx : Copy ,
124+ SpanData < Ctx > : Copy + fmt:: Debug ,
129125{
130126 let buffer = match tt {
131127 tt:: Subtree {
@@ -161,7 +157,7 @@ pub fn parse_to_token_tree<Ctx>(
161157 text : & str ,
162158) -> Option < tt:: Subtree < SpanData < Ctx > > >
163159where
164- SpanData < Ctx > : Span ,
160+ SpanData < Ctx > : Copy + fmt :: Debug ,
165161 Ctx : Copy ,
166162{
167163 let lexed = parser:: LexedStr :: new ( text) ;
@@ -175,7 +171,7 @@ where
175171/// Convert a string to a `TokenTree`. The passed span will be used for all spans of the produced subtree.
176172pub fn parse_to_token_tree_static_span < S > ( span : S , text : & str ) -> Option < tt:: Subtree < S > >
177173where
178- S : Span ,
174+ S : Copy + fmt :: Debug ,
179175{
180176 let lexed = parser:: LexedStr :: new ( text) ;
181177 if lexed. errors ( ) . next ( ) . is_some ( ) {
@@ -186,11 +182,10 @@ where
186182}
187183
188184/// Split token tree with separate expr: $($e:expr)SEP*
189- pub fn parse_exprs_with_sep < S : Span > (
190- tt : & tt:: Subtree < S > ,
191- sep : char ,
192- span : S ,
193- ) -> Vec < tt:: Subtree < S > > {
185+ pub fn parse_exprs_with_sep < S > ( tt : & tt:: Subtree < S > , sep : char , span : S ) -> Vec < tt:: Subtree < S > >
186+ where
187+ S : Copy + fmt:: Debug ,
188+ {
194189 if tt. token_trees . is_empty ( ) {
195190 return Vec :: new ( ) ;
196191 }
@@ -226,7 +221,8 @@ pub fn parse_exprs_with_sep<S: Span>(
226221fn convert_tokens < S , C > ( conv : & mut C ) -> tt:: Subtree < S >
227222where
228223 C : TokenConverter < S > ,
229- S : Span ,
224+ S : Copy + fmt:: Debug ,
225+ C :: Token : fmt:: Debug ,
230226{
231227 let entry = tt:: SubtreeBuilder {
232228 delimiter : tt:: Delimiter :: invisible_spanned ( conv. call_site ( ) ) ,
@@ -485,7 +481,7 @@ struct StaticRawConverter<'a, S> {
485481 span : S ,
486482}
487483
488- trait SrcToken < Ctx , S > : std :: fmt :: Debug {
484+ trait SrcToken < Ctx , S > {
489485 fn kind ( & self , ctx : & Ctx ) -> SyntaxKind ;
490486
491487 fn to_char ( & self , ctx : & Ctx ) -> Option < char > ;
@@ -525,7 +521,7 @@ impl<S, Ctx> SrcToken<RawConverter<'_, Ctx>, S> for usize {
525521 }
526522}
527523
528- impl < S : Span > SrcToken < StaticRawConverter < ' _ , S > , S > for usize {
524+ impl < S : Copy > SrcToken < StaticRawConverter < ' _ , S > , S > for usize {
529525 fn kind ( & self , ctx : & StaticRawConverter < ' _ , S > ) -> SyntaxKind {
530526 ctx. lexed . kind ( * self )
531527 }
@@ -541,7 +537,7 @@ impl<S: Span> SrcToken<StaticRawConverter<'_, S>, S> for usize {
541537
542538impl < Ctx : Copy > TokenConverter < SpanData < Ctx > > for RawConverter < ' _ , Ctx >
543539where
544- SpanData < Ctx > : Span ,
540+ SpanData < Ctx > : Copy ,
545541{
546542 type Token = usize ;
547543
@@ -584,7 +580,7 @@ where
584580
585581impl < S > TokenConverter < S > for StaticRawConverter < ' _ , S >
586582where
587- S : Span ,
583+ S : Copy ,
588584{
589585 type Token = usize ;
590586
@@ -709,7 +705,7 @@ impl<S> SynToken<S> {
709705 }
710706}
711707
712- impl < SpanMap , S : std :: fmt :: Debug > SrcToken < Converter < SpanMap , S > , S > for SynToken < S > {
708+ impl < SpanMap , S > SrcToken < Converter < SpanMap , S > , S > for SynToken < S > {
713709 fn kind ( & self , _ctx : & Converter < SpanMap , S > ) -> SyntaxKind {
714710 match self {
715711 SynToken :: Ordinary ( token) => token. kind ( ) ,
@@ -748,7 +744,7 @@ impl<SpanMap, S: std::fmt::Debug> SrcToken<Converter<SpanMap, S>, S> for SynToke
748744
749745impl < S , SpanMap > TokenConverter < S > for Converter < SpanMap , S >
750746where
751- S : Span ,
747+ S : Copy ,
752748 SpanMap : SpanMapper < S > ,
753749{
754750 type Token = SynToken < S > ;
@@ -828,7 +824,7 @@ where
828824
829825struct TtTreeSink < ' a , Ctx >
830826where
831- SpanData < Ctx > : Span ,
827+ SpanData < Ctx > : Copy ,
832828{
833829 buf : String ,
834830 cursor : Cursor < ' a , SpanData < Ctx > > ,
@@ -839,7 +835,7 @@ where
839835
840836impl < ' a , Ctx > TtTreeSink < ' a , Ctx >
841837where
842- SpanData < Ctx > : Span ,
838+ SpanData < Ctx > : Copy ,
843839{
844840 fn new ( cursor : Cursor < ' a , SpanData < Ctx > > ) -> Self {
845841 TtTreeSink {
@@ -871,7 +867,7 @@ fn delim_to_str(d: tt::DelimiterKind, closing: bool) -> Option<&'static str> {
871867
872868impl < Ctx > TtTreeSink < ' _ , Ctx >
873869where
874- SpanData < Ctx > : Span ,
870+ SpanData < Ctx > : Copy ,
875871{
876872 /// Parses a float literal as if it was a one to two name ref nodes with a dot inbetween.
877873 /// This occurs when a float literal is used as a field access.
0 commit comments