11
11
//! A support library for macro authors when defining new macros.
12
12
//!
13
13
//! This library, provided by the standard distribution, provides the types
14
- //! consumed in the interfaces of procedurally defined macro definitions.
15
- //! Currently the primary use of this crate is to provide the ability to define
16
- //! new custom derive modes through `#[proc_macro_derive]`.
14
+ //! consumed in the interfaces of procedurally defined macro definitions such as
15
+ //! function-like macros `#[proc_macro]`, macro attribures `#[proc_macro_attribute]` and
16
+ //! custom derive attributes `#[proc_macro_derive]`.
17
17
//!
18
- //! Note that this crate is intentionally very bare-bones currently. The main
19
- //! type, `TokenStream`, only supports `fmt::Display` and `FromStr`
20
- //! implementations, indicating that it can only go to and come from a string.
18
+ //! Note that this crate is intentionally bare-bones currently.
21
19
//! This functionality is intended to be expanded over time as more surface
22
20
//! area for macro authors is stabilized.
23
21
//!
@@ -110,8 +108,8 @@ impl TokenStream {
110
108
/// May fail for a number of reasons, for example, if the string contains unbalanced delimiters
111
109
/// or characters not existing in the language.
112
110
///
113
- /// REVIEW The function actually panics on any error and never returns `LexError`.
114
- /// REVIEW Should the panics be documented?
111
+ /// NOTE: Some errors may cause panics instead of returning `LexError`. We reserve the right to
112
+ /// change these errors into `LexError`s later.
115
113
#[ stable( feature = "proc_macro_lib" , since = "1.15.0" ) ]
116
114
impl FromStr for TokenStream {
117
115
type Err = LexError ;
@@ -132,9 +130,9 @@ impl FromStr for TokenStream {
132
130
}
133
131
}
134
132
135
- /// Prints the token stream as a string that should be losslessly convertible back
133
+ /// Prints the token stream as a string that is supposed to be losslessly convertible back
136
134
/// into the same token stream (modulo spans), except for possibly `TokenTree::Group`s
137
- /// with `Delimiter::None` delimiters.
135
+ /// with `Delimiter::None` delimiters and negative numeric literals .
138
136
#[ stable( feature = "proc_macro_lib" , since = "1.15.0" ) ]
139
137
impl fmt:: Display for TokenStream {
140
138
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -152,9 +150,6 @@ impl fmt::Debug for TokenStream {
152
150
}
153
151
154
152
/// Creates a token stream containing a single token tree.
155
- ///
156
- /// REVIEW We don't generally have impls `From<T> for Collection<T>`, but I see why this exists
157
- /// REVIEW from practical point of view.
158
153
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
159
154
impl From < TokenTree > for TokenStream {
160
155
fn from ( tree : TokenTree ) -> TokenStream {
@@ -217,9 +212,6 @@ pub mod token_stream {
217
212
// need to flattened during iteration over stream's token trees.
218
213
// Eventually this needs to be removed in favor of keeping original token trees
219
214
// and not doing the roundtrip through AST.
220
- //
221
- // REVIEW This may actually be observable if we can create a dummy span via
222
- // proc macro API, but it looks like we can't do it with 1.2 yet.
223
215
if tree. span ( ) . 0 == DUMMY_SP {
224
216
if let TokenTree :: Group ( ref group) = tree {
225
217
if group. delimiter ( ) == Delimiter :: None {
@@ -246,7 +238,7 @@ pub mod token_stream {
246
238
247
239
/// `quote!(..)` accepts arbitrary tokens and expands into a `TokenStream` describing the input.
248
240
/// For example, `quote!(a + b)` will produce a expression, that, when evaluated, constructs
249
- /// the `TokenStream` `[Word ("a"), Punct('+', Alone), Word ("b")]`.
241
+ /// the `TokenStream` `[Ident ("a"), Punct('+', Alone), Ident ("b")]`.
250
242
///
251
243
/// Unquoting is done with `$`, and works by taking the single next ident as the unquoted term.
252
244
/// To quote `$` itself, use `$$`.
@@ -266,9 +258,6 @@ pub fn quote_span(span: Span) -> TokenStream {
266
258
}
267
259
268
260
/// A region of source code, along with macro expansion information.
269
- ///
270
- /// REVIEW ATTENTION: `Copy` impl on a struct with private fields.
271
- /// REVIEW Do we want to guarantee `Span` to be `Copy`? Yes.
272
261
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
273
262
#[ derive( Copy , Clone ) ]
274
263
pub struct Span ( syntax_pos:: Span ) ;
@@ -555,10 +544,6 @@ impl fmt::Debug for TokenTree {
555
544
}
556
545
}
557
546
558
- /// REVIEW the impls below are kind of `From<T> for Option<T>`, not strictly necessary,
559
- /// REVIEW but convenient. No harm, I guess. I'd actually like to see impls
560
- /// REVIEW `From<Group/Ident/Punct/Literal> for TokenStream` to avoid stuttering like
561
- /// REVIEW `TokenTree::Literal(Literal::string("lalala")).into()`.
562
547
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
563
548
impl From < Group > for TokenTree {
564
549
fn from ( g : Group ) -> TokenTree {
@@ -587,9 +572,9 @@ impl From<Literal> for TokenTree {
587
572
}
588
573
}
589
574
590
- /// Prints the token tree as a string that should be losslessly convertible back
575
+ /// Prints the token tree as a string that is supposed to be losslessly convertible back
591
576
/// into the same token tree (modulo spans), except for possibly `TokenTree::Group`s
592
- /// with `Delimiter::None` delimiters.
577
+ /// with `Delimiter::None` delimiters and negative numeric literals .
593
578
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
594
579
impl fmt:: Display for TokenTree {
595
580
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
@@ -699,14 +684,9 @@ impl fmt::Display for Group {
699
684
///
700
685
/// Multicharacter operators like `+=` are represented as two instances of `Punct` with different
701
686
/// forms of `Spacing` returned.
702
- ///
703
- /// REVIEW ATTENTION: `Copy` impl on a struct with private fields.
704
- /// REVIEW Do we want to guarantee `Punct` to be `Copy`?
705
687
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
706
- #[ derive( Copy , Clone , Debug ) ]
688
+ #[ derive( Clone , Debug ) ]
707
689
pub struct Punct {
708
- // REVIEW(INTERNAL) `Punct` can avoid using `char` internally and
709
- // REVIEW(INTERNAL) can keep u8 or an u8-like enum.
710
690
ch : char ,
711
691
spacing : Spacing ,
712
692
span : Span ,
@@ -735,9 +715,6 @@ impl Punct {
735
715
///
736
716
/// The returned `Punct` will have the default span of `Span::call_site()`
737
717
/// which can be further configured with the `set_span` method below.
738
- ///
739
- /// REVIEW Why we even use `char` here? There's no reason to use unicode here.
740
- /// REVIEW I guess because it's more convenient to write `new('+')` than `new(b'+')`, that's ok.
741
718
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
742
719
pub fn new ( ch : char , spacing : Spacing ) -> Punct {
743
720
const LEGAL_CHARS : & [ char ] = & [ '=' , '<' , '>' , '!' , '~' , '+' , '-' , '*' , '/' , '%' ,
@@ -753,10 +730,6 @@ impl Punct {
753
730
}
754
731
755
732
/// Returns the value of this punctuation character as `char`.
756
- ///
757
- /// REVIEW Again, there's no need for unicode here,
758
- /// REVIEW except for maybe future compatibility in case Rust turns into APL,
759
- /// REVIEW but if it's more convenient to use `char` then that's okay.
760
733
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
761
734
pub fn as_char ( & self ) -> char {
762
735
self . ch
@@ -794,13 +767,9 @@ impl fmt::Display for Punct {
794
767
}
795
768
796
769
/// An identifier (`ident`) or lifetime identifier (`'ident`).
797
- ///
798
- /// REVIEW ATTENTION: `Copy` impl on a struct with private fields.
799
- /// REVIEW Do we want to guarantee `Ident` to be `Copy`?
800
- #[ derive( Copy , Clone , Debug ) ]
770
+ #[ derive( Clone , Debug ) ]
801
771
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
802
772
pub struct Ident {
803
- // REVIEW(INTERNAL) Symbol + Span is actually `ast::Ident`! We can use it here.
804
773
sym : Symbol ,
805
774
span : Span ,
806
775
is_raw : bool ,
@@ -856,13 +825,6 @@ impl Ident {
856
825
ident
857
826
}
858
827
859
- // FIXME: Remove this, do not stabilize
860
- /// Get a reference to the interned string.
861
- #[ unstable( feature = "proc_macro" , issue = "38356" ) ]
862
- pub fn as_str ( & self ) -> & str {
863
- unsafe { & * ( & * self . sym . as_str ( ) as * const str ) }
864
- }
865
-
866
828
/// Returns the span of this `Ident`, encompassing the entire string returned
867
829
/// by `as_str`.
868
830
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
@@ -882,6 +844,9 @@ impl Ident {
882
844
#[ unstable( feature = "proc_macro" , issue = "38356" ) ]
883
845
impl fmt:: Display for Ident {
884
846
fn fmt ( & self , f : & mut fmt:: Formatter ) -> fmt:: Result {
847
+ if self . is_raw {
848
+ f. write_str ( "r#" ) ?;
849
+ }
885
850
self . sym . as_str ( ) . fmt ( f)
886
851
}
887
852
}
@@ -910,6 +875,8 @@ macro_rules! suffixed_int_literals {
910
875
/// This function will create an integer like `1u32` where the integer
911
876
/// value specified is the first part of the token and the integral is
912
877
/// also suffixed at the end.
878
+ /// Literals created from negative numbers may not survive rountrips through
879
+ /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
913
880
///
914
881
/// Literals created through this method have the `Span::call_site()`
915
882
/// span by default, which can be configured with the `set_span` method
@@ -934,6 +901,8 @@ macro_rules! unsuffixed_int_literals {
934
901
/// specified on this token, meaning that invocations like
935
902
/// `Literal::i8_unsuffixed(1)` are equivalent to
936
903
/// `Literal::u32_unsuffixed(1)`.
904
+ /// Literals created from negative numbers may not survive rountrips through
905
+ /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
937
906
///
938
907
/// Literals created through this method have the `Span::call_site()`
939
908
/// span by default, which can be configured with the `set_span` method
@@ -985,6 +954,8 @@ impl Literal {
985
954
/// This constructor is similar to those like `Literal::i8_unsuffixed` where
986
955
/// the float's value is emitted directly into the token but no suffix is
987
956
/// used, so it may be inferred to be a `f64` later in the compiler.
957
+ /// Literals created from negative numbers may not survive rountrips through
958
+ /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
988
959
///
989
960
/// # Panics
990
961
///
@@ -1008,6 +979,8 @@ impl Literal {
1008
979
/// specified is the preceding part of the token and `f32` is the suffix of
1009
980
/// the token. This token will always be inferred to be an `f32` in the
1010
981
/// compiler.
982
+ /// Literals created from negative numbers may not survive rountrips through
983
+ /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
1011
984
///
1012
985
/// # Panics
1013
986
///
@@ -1030,6 +1003,8 @@ impl Literal {
1030
1003
/// This constructor is similar to those like `Literal::i8_unsuffixed` where
1031
1004
/// the float's value is emitted directly into the token but no suffix is
1032
1005
/// used, so it may be inferred to be a `f64` later in the compiler.
1006
+ /// Literals created from negative numbers may not survive rountrips through
1007
+ /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
1033
1008
///
1034
1009
/// # Panics
1035
1010
///
@@ -1053,6 +1028,8 @@ impl Literal {
1053
1028
/// specified is the preceding part of the token and `f64` is the suffix of
1054
1029
/// the token. This token will always be inferred to be an `f64` in the
1055
1030
/// compiler.
1031
+ /// Literals created from negative numbers may not survive rountrips through
1032
+ /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
1056
1033
///
1057
1034
/// # Panics
1058
1035
///
@@ -1347,7 +1324,7 @@ impl TokenTree {
1347
1324
'#' => Pound ,
1348
1325
'$' => Dollar ,
1349
1326
'?' => Question ,
1350
- _ => panic ! ( "unsupported character {}" , ch ) ,
1327
+ _ => unreachable ! ( ) ,
1351
1328
} ;
1352
1329
1353
1330
let tree = TokenTree :: Token ( span. 0 , token) ;
0 commit comments