Skip to content

Commit 5b820a6

Browse files
committed
Address feedback, remove remaining review comments, add some more docs
1 parent 780616e commit 5b820a6

File tree

2 files changed

+30
-53
lines changed

2 files changed

+30
-53
lines changed

src/libproc_macro/lib.rs

+29-52
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
//! A support library for macro authors when defining new macros.
1212
//!
1313
//! 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]`.
1717
//!
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.
2119
//! This functionality is intended to be expanded over time as more surface
2220
//! area for macro authors is stabilized.
2321
//!
@@ -110,8 +108,8 @@ impl TokenStream {
110108
/// May fail for a number of reasons, for example, if the string contains unbalanced delimiters
111109
/// or characters not existing in the language.
112110
///
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.
115113
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
116114
impl FromStr for TokenStream {
117115
type Err = LexError;
@@ -132,9 +130,9 @@ impl FromStr for TokenStream {
132130
}
133131
}
134132

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
136134
/// 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.
138136
#[stable(feature = "proc_macro_lib", since = "1.15.0")]
139137
impl fmt::Display for TokenStream {
140138
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -152,9 +150,6 @@ impl fmt::Debug for TokenStream {
152150
}
153151

154152
/// 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.
158153
#[unstable(feature = "proc_macro", issue = "38356")]
159154
impl From<TokenTree> for TokenStream {
160155
fn from(tree: TokenTree) -> TokenStream {
@@ -217,9 +212,6 @@ pub mod token_stream {
217212
// need to flattened during iteration over stream's token trees.
218213
// Eventually this needs to be removed in favor of keeping original token trees
219214
// 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.
223215
if tree.span().0 == DUMMY_SP {
224216
if let TokenTree::Group(ref group) = tree {
225217
if group.delimiter() == Delimiter::None {
@@ -246,7 +238,7 @@ pub mod token_stream {
246238

247239
/// `quote!(..)` accepts arbitrary tokens and expands into a `TokenStream` describing the input.
248240
/// 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")]`.
250242
///
251243
/// Unquoting is done with `$`, and works by taking the single next ident as the unquoted term.
252244
/// To quote `$` itself, use `$$`.
@@ -266,9 +258,6 @@ pub fn quote_span(span: Span) -> TokenStream {
266258
}
267259

268260
/// 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.
272261
#[unstable(feature = "proc_macro", issue = "38356")]
273262
#[derive(Copy, Clone)]
274263
pub struct Span(syntax_pos::Span);
@@ -555,10 +544,6 @@ impl fmt::Debug for TokenTree {
555544
}
556545
}
557546

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()`.
562547
#[unstable(feature = "proc_macro", issue = "38356")]
563548
impl From<Group> for TokenTree {
564549
fn from(g: Group) -> TokenTree {
@@ -587,9 +572,9 @@ impl From<Literal> for TokenTree {
587572
}
588573
}
589574

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
591576
/// 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.
593578
#[unstable(feature = "proc_macro", issue = "38356")]
594579
impl fmt::Display for TokenTree {
595580
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -699,14 +684,9 @@ impl fmt::Display for Group {
699684
///
700685
/// Multicharacter operators like `+=` are represented as two instances of `Punct` with different
701686
/// 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`?
705687
#[unstable(feature = "proc_macro", issue = "38356")]
706-
#[derive(Copy, Clone, Debug)]
688+
#[derive(Clone, Debug)]
707689
pub struct Punct {
708-
// REVIEW(INTERNAL) `Punct` can avoid using `char` internally and
709-
// REVIEW(INTERNAL) can keep u8 or an u8-like enum.
710690
ch: char,
711691
spacing: Spacing,
712692
span: Span,
@@ -735,9 +715,6 @@ impl Punct {
735715
///
736716
/// The returned `Punct` will have the default span of `Span::call_site()`
737717
/// 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.
741718
#[unstable(feature = "proc_macro", issue = "38356")]
742719
pub fn new(ch: char, spacing: Spacing) -> Punct {
743720
const LEGAL_CHARS: &[char] = &['=', '<', '>', '!', '~', '+', '-', '*', '/', '%',
@@ -753,10 +730,6 @@ impl Punct {
753730
}
754731

755732
/// 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.
760733
#[unstable(feature = "proc_macro", issue = "38356")]
761734
pub fn as_char(&self) -> char {
762735
self.ch
@@ -794,13 +767,9 @@ impl fmt::Display for Punct {
794767
}
795768

796769
/// 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)]
801771
#[unstable(feature = "proc_macro", issue = "38356")]
802772
pub struct Ident {
803-
// REVIEW(INTERNAL) Symbol + Span is actually `ast::Ident`! We can use it here.
804773
sym: Symbol,
805774
span: Span,
806775
is_raw: bool,
@@ -856,13 +825,6 @@ impl Ident {
856825
ident
857826
}
858827

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-
866828
/// Returns the span of this `Ident`, encompassing the entire string returned
867829
/// by `as_str`.
868830
#[unstable(feature = "proc_macro", issue = "38356")]
@@ -882,6 +844,9 @@ impl Ident {
882844
#[unstable(feature = "proc_macro", issue = "38356")]
883845
impl fmt::Display for Ident {
884846
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
847+
if self.is_raw {
848+
f.write_str("r#")?;
849+
}
885850
self.sym.as_str().fmt(f)
886851
}
887852
}
@@ -910,6 +875,8 @@ macro_rules! suffixed_int_literals {
910875
/// This function will create an integer like `1u32` where the integer
911876
/// value specified is the first part of the token and the integral is
912877
/// 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).
913880
///
914881
/// Literals created through this method have the `Span::call_site()`
915882
/// span by default, which can be configured with the `set_span` method
@@ -934,6 +901,8 @@ macro_rules! unsuffixed_int_literals {
934901
/// specified on this token, meaning that invocations like
935902
/// `Literal::i8_unsuffixed(1)` are equivalent to
936903
/// `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).
937906
///
938907
/// Literals created through this method have the `Span::call_site()`
939908
/// span by default, which can be configured with the `set_span` method
@@ -985,6 +954,8 @@ impl Literal {
985954
/// This constructor is similar to those like `Literal::i8_unsuffixed` where
986955
/// the float's value is emitted directly into the token but no suffix is
987956
/// 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).
988959
///
989960
/// # Panics
990961
///
@@ -1008,6 +979,8 @@ impl Literal {
1008979
/// specified is the preceding part of the token and `f32` is the suffix of
1009980
/// the token. This token will always be inferred to be an `f32` in the
1010981
/// 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).
1011984
///
1012985
/// # Panics
1013986
///
@@ -1030,6 +1003,8 @@ impl Literal {
10301003
/// This constructor is similar to those like `Literal::i8_unsuffixed` where
10311004
/// the float's value is emitted directly into the token but no suffix is
10321005
/// 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).
10331008
///
10341009
/// # Panics
10351010
///
@@ -1053,6 +1028,8 @@ impl Literal {
10531028
/// specified is the preceding part of the token and `f64` is the suffix of
10541029
/// the token. This token will always be inferred to be an `f64` in the
10551030
/// 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).
10561033
///
10571034
/// # Panics
10581035
///
@@ -1347,7 +1324,7 @@ impl TokenTree {
13471324
'#' => Pound,
13481325
'$' => Dollar,
13491326
'?' => Question,
1350-
_ => panic!("unsupported character {}", ch),
1327+
_ => unreachable!(),
13511328
};
13521329

13531330
let tree = TokenTree::Token(span.0, token);

src/libproc_macro/quote.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl Quote for TokenStream {
118118
TokenTree::Punct(ref tt) if tt.as_char() == '$' => {}
119119
_ => panic!("`$` must be followed by an ident or `$` in `quote!`"),
120120
}
121-
} else if let TokenTree::Punct(tt) = tree {
121+
} else if let TokenTree::Punct(ref tt) = tree {
122122
if tt.as_char() == '$' {
123123
after_dollar = true;
124124
return None;

0 commit comments

Comments
 (0)