]> git.lizzy.rs Git - rust.git/blob - library/proc_macro/src/lib.rs
Rollup merge of #90782 - ricobbe:binutils-dlltool, r=michaelwoerister
[rust.git] / library / proc_macro / src / lib.rs
1 //! A support library for macro authors when defining new macros.
2 //!
3 //! This library, provided by the standard distribution, provides the types
4 //! consumed in the interfaces of procedurally defined macro definitions such as
5 //! function-like macros `#[proc_macro]`, macro attributes `#[proc_macro_attribute]` and
6 //! custom derive attributes`#[proc_macro_derive]`.
7 //!
8 //! See [the book] for more.
9 //!
10 //! [the book]: ../book/ch19-06-macros.html#procedural-macros-for-generating-code-from-attributes
11
12 #![stable(feature = "proc_macro_lib", since = "1.15.0")]
13 #![deny(missing_docs)]
14 #![doc(
15     html_playground_url = "https://play.rust-lang.org/",
16     issue_tracker_base_url = "https://github.com/rust-lang/rust/issues/",
17     test(no_crate_inject, attr(deny(warnings))),
18     test(attr(allow(dead_code, deprecated, unused_variables, unused_mut)))
19 )]
20 #![feature(rustc_allow_const_fn_unstable)]
21 #![feature(nll)]
22 #![feature(staged_api)]
23 #![feature(const_fn_trait_bound)]
24 #![feature(const_fn_fn_ptr_basics)]
25 #![feature(allow_internal_unstable)]
26 #![feature(decl_macro)]
27 #![feature(extern_types)]
28 #![feature(negative_impls)]
29 #![feature(auto_traits)]
30 #![feature(restricted_std)]
31 #![feature(rustc_attrs)]
32 #![feature(min_specialization)]
33 #![feature(panic_update_hook)]
34 #![recursion_limit = "256"]
35
36 #[unstable(feature = "proc_macro_internals", issue = "27812")]
37 #[doc(hidden)]
38 pub mod bridge;
39
40 mod diagnostic;
41
42 #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
43 pub use diagnostic::{Diagnostic, Level, MultiSpan};
44
45 use std::cmp::Ordering;
46 use std::ops::RangeBounds;
47 use std::path::PathBuf;
48 use std::str::FromStr;
49 use std::{error, fmt, iter, mem};
50
51 /// Determines whether proc_macro has been made accessible to the currently
52 /// running program.
53 ///
54 /// The proc_macro crate is only intended for use inside the implementation of
55 /// procedural macros. All the functions in this crate panic if invoked from
56 /// outside of a procedural macro, such as from a build script or unit test or
57 /// ordinary Rust binary.
58 ///
59 /// With consideration for Rust libraries that are designed to support both
60 /// macro and non-macro use cases, `proc_macro::is_available()` provides a
61 /// non-panicking way to detect whether the infrastructure required to use the
62 /// API of proc_macro is presently available. Returns true if invoked from
63 /// inside of a procedural macro, false if invoked from any other binary.
64 #[stable(feature = "proc_macro_is_available", since = "1.57.0")]
65 pub fn is_available() -> bool {
66     bridge::Bridge::is_available()
67 }
68
69 /// The main type provided by this crate, representing an abstract stream of
70 /// tokens, or, more specifically, a sequence of token trees.
71 /// The type provide interfaces for iterating over those token trees and, conversely,
72 /// collecting a number of token trees into one stream.
73 ///
74 /// This is both the input and output of `#[proc_macro]`, `#[proc_macro_attribute]`
75 /// and `#[proc_macro_derive]` definitions.
76 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
77 #[derive(Clone)]
78 pub struct TokenStream(bridge::client::TokenStream);
79
80 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
81 impl !Send for TokenStream {}
82 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
83 impl !Sync for TokenStream {}
84
85 /// Error returned from `TokenStream::from_str`.
86 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
87 #[non_exhaustive]
88 #[derive(Debug)]
89 pub struct LexError;
90
91 #[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
92 impl fmt::Display for LexError {
93     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
94         f.write_str("cannot parse string into token stream")
95     }
96 }
97
98 #[stable(feature = "proc_macro_lexerror_impls", since = "1.44.0")]
99 impl error::Error for LexError {}
100
101 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
102 impl !Send for LexError {}
103 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
104 impl !Sync for LexError {}
105
106 /// Error returned from `TokenStream::expand_expr`.
107 #[unstable(feature = "proc_macro_expand", issue = "90765")]
108 #[non_exhaustive]
109 #[derive(Debug)]
110 pub struct ExpandError;
111
112 #[unstable(feature = "proc_macro_expand", issue = "90765")]
113 impl fmt::Display for ExpandError {
114     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
115         f.write_str("macro expansion failed")
116     }
117 }
118
119 #[unstable(feature = "proc_macro_expand", issue = "90765")]
120 impl error::Error for ExpandError {}
121
122 #[unstable(feature = "proc_macro_expand", issue = "90765")]
123 impl !Send for ExpandError {}
124
125 #[unstable(feature = "proc_macro_expand", issue = "90765")]
126 impl !Sync for ExpandError {}
127
128 impl TokenStream {
129     /// Returns an empty `TokenStream` containing no token trees.
130     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
131     pub fn new() -> TokenStream {
132         TokenStream(bridge::client::TokenStream::new())
133     }
134
135     /// Checks if this `TokenStream` is empty.
136     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
137     pub fn is_empty(&self) -> bool {
138         self.0.is_empty()
139     }
140
141     /// Parses this `TokenStream` as an expression and attempts to expand any
142     /// macros within it. Returns the expanded `TokenStream`.
143     ///
144     /// Currently only expressions expanding to literals will succeed, although
145     /// this may be relaxed in the future.
146     ///
147     /// NOTE: In error conditions, `expand_expr` may leave macros unexpanded,
148     /// report an error, failing compilation, and/or return an `Err(..)`. The
149     /// specific behavior for any error condition, and what conditions are
150     /// considered errors, is unspecified and may change in the future.
151     #[unstable(feature = "proc_macro_expand", issue = "90765")]
152     pub fn expand_expr(&self) -> Result<TokenStream, ExpandError> {
153         match bridge::client::TokenStream::expand_expr(&self.0) {
154             Ok(stream) => Ok(TokenStream(stream)),
155             Err(_) => Err(ExpandError),
156         }
157     }
158 }
159
160 /// Attempts to break the string into tokens and parse those tokens into a token stream.
161 /// May fail for a number of reasons, for example, if the string contains unbalanced delimiters
162 /// or characters not existing in the language.
163 /// All tokens in the parsed stream get `Span::call_site()` spans.
164 ///
165 /// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to
166 /// change these errors into `LexError`s later.
167 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
168 impl FromStr for TokenStream {
169     type Err = LexError;
170
171     fn from_str(src: &str) -> Result<TokenStream, LexError> {
172         Ok(TokenStream(bridge::client::TokenStream::from_str(src)))
173     }
174 }
175
176 // N.B., the bridge only provides `to_string`, implement `fmt::Display`
177 // based on it (the reverse of the usual relationship between the two).
178 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
179 impl ToString for TokenStream {
180     fn to_string(&self) -> String {
181         self.0.to_string()
182     }
183 }
184
185 /// Prints the token stream as a string that is supposed to be losslessly convertible back
186 /// into the same token stream (modulo spans), except for possibly `TokenTree::Group`s
187 /// with `Delimiter::None` delimiters and negative numeric literals.
188 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
189 impl fmt::Display for TokenStream {
190     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
191         f.write_str(&self.to_string())
192     }
193 }
194
195 /// Prints token in a form convenient for debugging.
196 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
197 impl fmt::Debug for TokenStream {
198     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
199         f.write_str("TokenStream ")?;
200         f.debug_list().entries(self.clone()).finish()
201     }
202 }
203
204 #[stable(feature = "proc_macro_token_stream_default", since = "1.45.0")]
205 impl Default for TokenStream {
206     fn default() -> Self {
207         TokenStream::new()
208     }
209 }
210
211 #[unstable(feature = "proc_macro_quote", issue = "54722")]
212 pub use quote::{quote, quote_span};
213
214 /// Creates a token stream containing a single token tree.
215 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
216 impl From<TokenTree> for TokenStream {
217     fn from(tree: TokenTree) -> TokenStream {
218         TokenStream(bridge::client::TokenStream::from_token_tree(match tree {
219             TokenTree::Group(tt) => bridge::TokenTree::Group(tt.0),
220             TokenTree::Punct(tt) => bridge::TokenTree::Punct(tt.0),
221             TokenTree::Ident(tt) => bridge::TokenTree::Ident(tt.0),
222             TokenTree::Literal(tt) => bridge::TokenTree::Literal(tt.0),
223         }))
224     }
225 }
226
227 /// Collects a number of token trees into a single stream.
228 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
229 impl iter::FromIterator<TokenTree> for TokenStream {
230     fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
231         trees.into_iter().map(TokenStream::from).collect()
232     }
233 }
234
235 /// A "flattening" operation on token streams, collects token trees
236 /// from multiple token streams into a single stream.
237 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
238 impl iter::FromIterator<TokenStream> for TokenStream {
239     fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
240         let mut builder = bridge::client::TokenStreamBuilder::new();
241         streams.into_iter().for_each(|stream| builder.push(stream.0));
242         TokenStream(builder.build())
243     }
244 }
245
246 #[stable(feature = "token_stream_extend", since = "1.30.0")]
247 impl Extend<TokenTree> for TokenStream {
248     fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I) {
249         self.extend(trees.into_iter().map(TokenStream::from));
250     }
251 }
252
253 #[stable(feature = "token_stream_extend", since = "1.30.0")]
254 impl Extend<TokenStream> for TokenStream {
255     fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
256         // FIXME(eddyb) Use an optimized implementation if/when possible.
257         *self = iter::once(mem::replace(self, Self::new())).chain(streams).collect();
258     }
259 }
260
261 /// Public implementation details for the `TokenStream` type, such as iterators.
262 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
263 pub mod token_stream {
264     use crate::{bridge, Group, Ident, Literal, Punct, TokenStream, TokenTree};
265
266     /// An iterator over `TokenStream`'s `TokenTree`s.
267     /// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups,
268     /// and returns whole groups as token trees.
269     #[derive(Clone)]
270     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
271     pub struct IntoIter(bridge::client::TokenStreamIter);
272
273     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
274     impl Iterator for IntoIter {
275         type Item = TokenTree;
276
277         fn next(&mut self) -> Option<TokenTree> {
278             self.0.next().map(|tree| match tree {
279                 bridge::TokenTree::Group(tt) => TokenTree::Group(Group(tt)),
280                 bridge::TokenTree::Punct(tt) => TokenTree::Punct(Punct(tt)),
281                 bridge::TokenTree::Ident(tt) => TokenTree::Ident(Ident(tt)),
282                 bridge::TokenTree::Literal(tt) => TokenTree::Literal(Literal(tt)),
283             })
284         }
285     }
286
287     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
288     impl IntoIterator for TokenStream {
289         type Item = TokenTree;
290         type IntoIter = IntoIter;
291
292         fn into_iter(self) -> IntoIter {
293             IntoIter(self.0.into_iter())
294         }
295     }
296 }
297
298 /// `quote!(..)` accepts arbitrary tokens and expands into a `TokenStream` describing the input.
299 /// For example, `quote!(a + b)` will produce an expression, that, when evaluated, constructs
300 /// the `TokenStream` `[Ident("a"), Punct('+', Alone), Ident("b")]`.
301 ///
302 /// Unquoting is done with `$`, and works by taking the single next ident as the unquoted term.
303 /// To quote `$` itself, use `$$`.
304 #[unstable(feature = "proc_macro_quote", issue = "54722")]
305 #[allow_internal_unstable(proc_macro_def_site, proc_macro_internals)]
306 #[rustc_builtin_macro]
307 pub macro quote($($t:tt)*) {
308     /* compiler built-in */
309 }
310
311 #[unstable(feature = "proc_macro_internals", issue = "27812")]
312 #[doc(hidden)]
313 mod quote;
314
315 /// A region of source code, along with macro expansion information.
316 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
317 #[derive(Copy, Clone)]
318 pub struct Span(bridge::client::Span);
319
320 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
321 impl !Send for Span {}
322 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
323 impl !Sync for Span {}
324
325 macro_rules! diagnostic_method {
326     ($name:ident, $level:expr) => {
327         /// Creates a new `Diagnostic` with the given `message` at the span
328         /// `self`.
329         #[unstable(feature = "proc_macro_diagnostic", issue = "54140")]
330         pub fn $name<T: Into<String>>(self, message: T) -> Diagnostic {
331             Diagnostic::spanned(self, $level, message)
332         }
333     };
334 }
335
336 impl Span {
337     /// A span that resolves at the macro definition site.
338     #[unstable(feature = "proc_macro_def_site", issue = "54724")]
339     pub fn def_site() -> Span {
340         Span(bridge::client::Span::def_site())
341     }
342
343     /// The span of the invocation of the current procedural macro.
344     /// Identifiers created with this span will be resolved as if they were written
345     /// directly at the macro call location (call-site hygiene) and other code
346     /// at the macro call site will be able to refer to them as well.
347     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
348     pub fn call_site() -> Span {
349         Span(bridge::client::Span::call_site())
350     }
351
352     /// A span that represents `macro_rules` hygiene, and sometimes resolves at the macro
353     /// definition site (local variables, labels, `$crate`) and sometimes at the macro
354     /// call site (everything else).
355     /// The span location is taken from the call-site.
356     #[stable(feature = "proc_macro_mixed_site", since = "1.45.0")]
357     pub fn mixed_site() -> Span {
358         Span(bridge::client::Span::mixed_site())
359     }
360
361     /// The original source file into which this span points.
362     #[unstable(feature = "proc_macro_span", issue = "54725")]
363     pub fn source_file(&self) -> SourceFile {
364         SourceFile(self.0.source_file())
365     }
366
367     /// The `Span` for the tokens in the previous macro expansion from which
368     /// `self` was generated from, if any.
369     #[unstable(feature = "proc_macro_span", issue = "54725")]
370     pub fn parent(&self) -> Option<Span> {
371         self.0.parent().map(Span)
372     }
373
374     /// The span for the origin source code that `self` was generated from. If
375     /// this `Span` wasn't generated from other macro expansions then the return
376     /// value is the same as `*self`.
377     #[unstable(feature = "proc_macro_span", issue = "54725")]
378     pub fn source(&self) -> Span {
379         Span(self.0.source())
380     }
381
382     /// Gets the starting line/column in the source file for this span.
383     #[unstable(feature = "proc_macro_span", issue = "54725")]
384     pub fn start(&self) -> LineColumn {
385         self.0.start().add_1_to_column()
386     }
387
388     /// Gets the ending line/column in the source file for this span.
389     #[unstable(feature = "proc_macro_span", issue = "54725")]
390     pub fn end(&self) -> LineColumn {
391         self.0.end().add_1_to_column()
392     }
393
394     /// Creates an empty span pointing to directly before this span.
395     #[unstable(feature = "proc_macro_span_shrink", issue = "87552")]
396     pub fn before(&self) -> Span {
397         Span(self.0.before())
398     }
399
400     /// Creates an empty span pointing to directly after this span.
401     #[unstable(feature = "proc_macro_span_shrink", issue = "87552")]
402     pub fn after(&self) -> Span {
403         Span(self.0.after())
404     }
405
406     /// Creates a new span encompassing `self` and `other`.
407     ///
408     /// Returns `None` if `self` and `other` are from different files.
409     #[unstable(feature = "proc_macro_span", issue = "54725")]
410     pub fn join(&self, other: Span) -> Option<Span> {
411         self.0.join(other.0).map(Span)
412     }
413
414     /// Creates a new span with the same line/column information as `self` but
415     /// that resolves symbols as though it were at `other`.
416     #[stable(feature = "proc_macro_span_resolved_at", since = "1.45.0")]
417     pub fn resolved_at(&self, other: Span) -> Span {
418         Span(self.0.resolved_at(other.0))
419     }
420
421     /// Creates a new span with the same name resolution behavior as `self` but
422     /// with the line/column information of `other`.
423     #[stable(feature = "proc_macro_span_located_at", since = "1.45.0")]
424     pub fn located_at(&self, other: Span) -> Span {
425         other.resolved_at(*self)
426     }
427
428     /// Compares to spans to see if they're equal.
429     #[unstable(feature = "proc_macro_span", issue = "54725")]
430     pub fn eq(&self, other: &Span) -> bool {
431         self.0 == other.0
432     }
433
434     /// Returns the source text behind a span. This preserves the original source
435     /// code, including spaces and comments. It only returns a result if the span
436     /// corresponds to real source code.
437     ///
438     /// Note: The observable result of a macro should only rely on the tokens and
439     /// not on this source text. The result of this function is a best effort to
440     /// be used for diagnostics only.
441     #[unstable(feature = "proc_macro_span", issue = "54725")]
442     pub fn source_text(&self) -> Option<String> {
443         self.0.source_text()
444     }
445
446     // Used by the implementation of `Span::quote`
447     #[doc(hidden)]
448     #[unstable(feature = "proc_macro_internals", issue = "27812")]
449     pub fn save_span(&self) -> usize {
450         self.0.save_span()
451     }
452
453     // Used by the implementation of `Span::quote`
454     #[doc(hidden)]
455     #[unstable(feature = "proc_macro_internals", issue = "27812")]
456     pub fn recover_proc_macro_span(id: usize) -> Span {
457         Span(bridge::client::Span::recover_proc_macro_span(id))
458     }
459
460     diagnostic_method!(error, Level::Error);
461     diagnostic_method!(warning, Level::Warning);
462     diagnostic_method!(note, Level::Note);
463     diagnostic_method!(help, Level::Help);
464 }
465
466 /// Prints a span in a form convenient for debugging.
467 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
468 impl fmt::Debug for Span {
469     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
470         self.0.fmt(f)
471     }
472 }
473
474 /// A line-column pair representing the start or end of a `Span`.
475 #[unstable(feature = "proc_macro_span", issue = "54725")]
476 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
477 pub struct LineColumn {
478     /// The 1-indexed line in the source file on which the span starts or ends (inclusive).
479     #[unstable(feature = "proc_macro_span", issue = "54725")]
480     pub line: usize,
481     /// The 1-indexed column (number of bytes in UTF-8 encoding) in the source
482     /// file on which the span starts or ends (inclusive).
483     #[unstable(feature = "proc_macro_span", issue = "54725")]
484     pub column: usize,
485 }
486
487 impl LineColumn {
488     fn add_1_to_column(self) -> Self {
489         LineColumn { line: self.line, column: self.column + 1 }
490     }
491 }
492
493 #[unstable(feature = "proc_macro_span", issue = "54725")]
494 impl !Send for LineColumn {}
495 #[unstable(feature = "proc_macro_span", issue = "54725")]
496 impl !Sync for LineColumn {}
497
498 #[unstable(feature = "proc_macro_span", issue = "54725")]
499 impl Ord for LineColumn {
500     fn cmp(&self, other: &Self) -> Ordering {
501         self.line.cmp(&other.line).then(self.column.cmp(&other.column))
502     }
503 }
504
505 #[unstable(feature = "proc_macro_span", issue = "54725")]
506 impl PartialOrd for LineColumn {
507     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
508         Some(self.cmp(other))
509     }
510 }
511
512 /// The source file of a given `Span`.
513 #[unstable(feature = "proc_macro_span", issue = "54725")]
514 #[derive(Clone)]
515 pub struct SourceFile(bridge::client::SourceFile);
516
517 impl SourceFile {
518     /// Gets the path to this source file.
519     ///
520     /// ### Note
521     /// If the code span associated with this `SourceFile` was generated by an external macro, this
522     /// macro, this might not be an actual path on the filesystem. Use [`is_real`] to check.
523     ///
524     /// Also note that even if `is_real` returns `true`, if `--remap-path-prefix` was passed on
525     /// the command line, the path as given might not actually be valid.
526     ///
527     /// [`is_real`]: Self::is_real
528     #[unstable(feature = "proc_macro_span", issue = "54725")]
529     pub fn path(&self) -> PathBuf {
530         PathBuf::from(self.0.path())
531     }
532
533     /// Returns `true` if this source file is a real source file, and not generated by an external
534     /// macro's expansion.
535     #[unstable(feature = "proc_macro_span", issue = "54725")]
536     pub fn is_real(&self) -> bool {
537         // This is a hack until intercrate spans are implemented and we can have real source files
538         // for spans generated in external macros.
539         // https://github.com/rust-lang/rust/pull/43604#issuecomment-333334368
540         self.0.is_real()
541     }
542 }
543
544 #[unstable(feature = "proc_macro_span", issue = "54725")]
545 impl fmt::Debug for SourceFile {
546     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
547         f.debug_struct("SourceFile")
548             .field("path", &self.path())
549             .field("is_real", &self.is_real())
550             .finish()
551     }
552 }
553
554 #[unstable(feature = "proc_macro_span", issue = "54725")]
555 impl PartialEq for SourceFile {
556     fn eq(&self, other: &Self) -> bool {
557         self.0.eq(&other.0)
558     }
559 }
560
561 #[unstable(feature = "proc_macro_span", issue = "54725")]
562 impl Eq for SourceFile {}
563
564 /// A single token or a delimited sequence of token trees (e.g., `[1, (), ..]`).
565 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
566 #[derive(Clone)]
567 pub enum TokenTree {
568     /// A token stream surrounded by bracket delimiters.
569     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
570     Group(#[stable(feature = "proc_macro_lib2", since = "1.29.0")] Group),
571     /// An identifier.
572     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
573     Ident(#[stable(feature = "proc_macro_lib2", since = "1.29.0")] Ident),
574     /// A single punctuation character (`+`, `,`, `$`, etc.).
575     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
576     Punct(#[stable(feature = "proc_macro_lib2", since = "1.29.0")] Punct),
577     /// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc.
578     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
579     Literal(#[stable(feature = "proc_macro_lib2", since = "1.29.0")] Literal),
580 }
581
582 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
583 impl !Send for TokenTree {}
584 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
585 impl !Sync for TokenTree {}
586
587 impl TokenTree {
588     /// Returns the span of this tree, delegating to the `span` method of
589     /// the contained token or a delimited stream.
590     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
591     pub fn span(&self) -> Span {
592         match *self {
593             TokenTree::Group(ref t) => t.span(),
594             TokenTree::Ident(ref t) => t.span(),
595             TokenTree::Punct(ref t) => t.span(),
596             TokenTree::Literal(ref t) => t.span(),
597         }
598     }
599
600     /// Configures the span for *only this token*.
601     ///
602     /// Note that if this token is a `Group` then this method will not configure
603     /// the span of each of the internal tokens, this will simply delegate to
604     /// the `set_span` method of each variant.
605     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
606     pub fn set_span(&mut self, span: Span) {
607         match *self {
608             TokenTree::Group(ref mut t) => t.set_span(span),
609             TokenTree::Ident(ref mut t) => t.set_span(span),
610             TokenTree::Punct(ref mut t) => t.set_span(span),
611             TokenTree::Literal(ref mut t) => t.set_span(span),
612         }
613     }
614 }
615
616 /// Prints token tree in a form convenient for debugging.
617 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
618 impl fmt::Debug for TokenTree {
619     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
620         // Each of these has the name in the struct type in the derived debug,
621         // so don't bother with an extra layer of indirection
622         match *self {
623             TokenTree::Group(ref tt) => tt.fmt(f),
624             TokenTree::Ident(ref tt) => tt.fmt(f),
625             TokenTree::Punct(ref tt) => tt.fmt(f),
626             TokenTree::Literal(ref tt) => tt.fmt(f),
627         }
628     }
629 }
630
631 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
632 impl From<Group> for TokenTree {
633     fn from(g: Group) -> TokenTree {
634         TokenTree::Group(g)
635     }
636 }
637
638 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
639 impl From<Ident> for TokenTree {
640     fn from(g: Ident) -> TokenTree {
641         TokenTree::Ident(g)
642     }
643 }
644
645 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
646 impl From<Punct> for TokenTree {
647     fn from(g: Punct) -> TokenTree {
648         TokenTree::Punct(g)
649     }
650 }
651
652 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
653 impl From<Literal> for TokenTree {
654     fn from(g: Literal) -> TokenTree {
655         TokenTree::Literal(g)
656     }
657 }
658
659 // N.B., the bridge only provides `to_string`, implement `fmt::Display`
660 // based on it (the reverse of the usual relationship between the two).
661 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
662 impl ToString for TokenTree {
663     fn to_string(&self) -> String {
664         match *self {
665             TokenTree::Group(ref t) => t.to_string(),
666             TokenTree::Ident(ref t) => t.to_string(),
667             TokenTree::Punct(ref t) => t.to_string(),
668             TokenTree::Literal(ref t) => t.to_string(),
669         }
670     }
671 }
672
673 /// Prints the token tree as a string that is supposed to be losslessly convertible back
674 /// into the same token tree (modulo spans), except for possibly `TokenTree::Group`s
675 /// with `Delimiter::None` delimiters and negative numeric literals.
676 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
677 impl fmt::Display for TokenTree {
678     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
679         f.write_str(&self.to_string())
680     }
681 }
682
683 /// A delimited token stream.
684 ///
685 /// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s.
686 #[derive(Clone)]
687 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
688 pub struct Group(bridge::client::Group);
689
690 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
691 impl !Send for Group {}
692 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
693 impl !Sync for Group {}
694
695 /// Describes how a sequence of token trees is delimited.
696 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
697 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
698 pub enum Delimiter {
699     /// `( ... )`
700     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
701     Parenthesis,
702     /// `{ ... }`
703     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
704     Brace,
705     /// `[ ... ]`
706     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
707     Bracket,
708     /// `Ø ... Ã˜`
709     /// An implicit delimiter, that may, for example, appear around tokens coming from a
710     /// "macro variable" `$var`. It is important to preserve operator priorities in cases like
711     /// `$var * 3` where `$var` is `1 + 2`.
712     /// Implicit delimiters might not survive roundtrip of a token stream through a string.
713     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
714     None,
715 }
716
717 impl Group {
718     /// Creates a new `Group` with the given delimiter and token stream.
719     ///
720     /// This constructor will set the span for this group to
721     /// `Span::call_site()`. To change the span you can use the `set_span`
722     /// method below.
723     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
724     pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
725         Group(bridge::client::Group::new(delimiter, stream.0))
726     }
727
728     /// Returns the delimiter of this `Group`
729     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
730     pub fn delimiter(&self) -> Delimiter {
731         self.0.delimiter()
732     }
733
734     /// Returns the `TokenStream` of tokens that are delimited in this `Group`.
735     ///
736     /// Note that the returned token stream does not include the delimiter
737     /// returned above.
738     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
739     pub fn stream(&self) -> TokenStream {
740         TokenStream(self.0.stream())
741     }
742
743     /// Returns the span for the delimiters of this token stream, spanning the
744     /// entire `Group`.
745     ///
746     /// ```text
747     /// pub fn span(&self) -> Span {
748     ///            ^^^^^^^
749     /// ```
750     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
751     pub fn span(&self) -> Span {
752         Span(self.0.span())
753     }
754
755     /// Returns the span pointing to the opening delimiter of this group.
756     ///
757     /// ```text
758     /// pub fn span_open(&self) -> Span {
759     ///                 ^
760     /// ```
761     #[stable(feature = "proc_macro_group_span", since = "1.55.0")]
762     pub fn span_open(&self) -> Span {
763         Span(self.0.span_open())
764     }
765
766     /// Returns the span pointing to the closing delimiter of this group.
767     ///
768     /// ```text
769     /// pub fn span_close(&self) -> Span {
770     ///                        ^
771     /// ```
772     #[stable(feature = "proc_macro_group_span", since = "1.55.0")]
773     pub fn span_close(&self) -> Span {
774         Span(self.0.span_close())
775     }
776
777     /// Configures the span for this `Group`'s delimiters, but not its internal
778     /// tokens.
779     ///
780     /// This method will **not** set the span of all the internal tokens spanned
781     /// by this group, but rather it will only set the span of the delimiter
782     /// tokens at the level of the `Group`.
783     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
784     pub fn set_span(&mut self, span: Span) {
785         self.0.set_span(span.0);
786     }
787 }
788
789 // N.B., the bridge only provides `to_string`, implement `fmt::Display`
790 // based on it (the reverse of the usual relationship between the two).
791 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
792 impl ToString for Group {
793     fn to_string(&self) -> String {
794         TokenStream::from(TokenTree::from(self.clone())).to_string()
795     }
796 }
797
798 /// Prints the group as a string that should be losslessly convertible back
799 /// into the same group (modulo spans), except for possibly `TokenTree::Group`s
800 /// with `Delimiter::None` delimiters.
801 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
802 impl fmt::Display for Group {
803     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
804         f.write_str(&self.to_string())
805     }
806 }
807
808 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
809 impl fmt::Debug for Group {
810     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
811         f.debug_struct("Group")
812             .field("delimiter", &self.delimiter())
813             .field("stream", &self.stream())
814             .field("span", &self.span())
815             .finish()
816     }
817 }
818
819 /// A `Punct` is a single punctuation character such as `+`, `-` or `#`.
820 ///
821 /// Multi-character operators like `+=` are represented as two instances of `Punct` with different
822 /// forms of `Spacing` returned.
823 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
824 #[derive(Clone)]
825 pub struct Punct(bridge::client::Punct);
826
827 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
828 impl !Send for Punct {}
829 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
830 impl !Sync for Punct {}
831
832 /// Describes whether a `Punct` is followed immediately by another `Punct` ([`Spacing::Joint`]) or
833 /// by a different token or whitespace ([`Spacing::Alone`]).
834 #[derive(Copy, Clone, Debug, PartialEq, Eq)]
835 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
836 pub enum Spacing {
837     /// A `Punct` is not immediately followed by another `Punct`.
838     /// E.g. `+` is `Alone` in `+ =`, `+ident` and `+()`.
839     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
840     Alone,
841     /// A `Punct` is immediately followed by another `Punct`.
842     /// E.g. `+` is `Joint` in `+=` and `++`.
843     ///
844     /// Additionally, single quote `'` can join with identifiers to form lifetimes: `'ident`.
845     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
846     Joint,
847 }
848
849 impl Punct {
850     /// Creates a new `Punct` from the given character and spacing.
851     /// The `ch` argument must be a valid punctuation character permitted by the language,
852     /// otherwise the function will panic.
853     ///
854     /// The returned `Punct` will have the default span of `Span::call_site()`
855     /// which can be further configured with the `set_span` method below.
856     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
857     pub fn new(ch: char, spacing: Spacing) -> Punct {
858         Punct(bridge::client::Punct::new(ch, spacing))
859     }
860
861     /// Returns the value of this punctuation character as `char`.
862     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
863     pub fn as_char(&self) -> char {
864         self.0.as_char()
865     }
866
867     /// Returns the spacing of this punctuation character, indicating whether it's immediately
868     /// followed by another `Punct` in the token stream, so they can potentially be combined into
869     /// a multi-character operator (`Joint`), or it's followed by some other token or whitespace
870     /// (`Alone`) so the operator has certainly ended.
871     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
872     pub fn spacing(&self) -> Spacing {
873         self.0.spacing()
874     }
875
876     /// Returns the span for this punctuation character.
877     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
878     pub fn span(&self) -> Span {
879         Span(self.0.span())
880     }
881
882     /// Configure the span for this punctuation character.
883     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
884     pub fn set_span(&mut self, span: Span) {
885         self.0 = self.0.with_span(span.0);
886     }
887 }
888
889 // N.B., the bridge only provides `to_string`, implement `fmt::Display`
890 // based on it (the reverse of the usual relationship between the two).
891 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
892 impl ToString for Punct {
893     fn to_string(&self) -> String {
894         TokenStream::from(TokenTree::from(self.clone())).to_string()
895     }
896 }
897
898 /// Prints the punctuation character as a string that should be losslessly convertible
899 /// back into the same character.
900 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
901 impl fmt::Display for Punct {
902     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
903         f.write_str(&self.to_string())
904     }
905 }
906
907 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
908 impl fmt::Debug for Punct {
909     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
910         f.debug_struct("Punct")
911             .field("ch", &self.as_char())
912             .field("spacing", &self.spacing())
913             .field("span", &self.span())
914             .finish()
915     }
916 }
917
918 #[stable(feature = "proc_macro_punct_eq", since = "1.50.0")]
919 impl PartialEq<char> for Punct {
920     fn eq(&self, rhs: &char) -> bool {
921         self.as_char() == *rhs
922     }
923 }
924
925 #[stable(feature = "proc_macro_punct_eq_flipped", since = "1.52.0")]
926 impl PartialEq<Punct> for char {
927     fn eq(&self, rhs: &Punct) -> bool {
928         *self == rhs.as_char()
929     }
930 }
931
932 /// An identifier (`ident`).
933 #[derive(Clone)]
934 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
935 pub struct Ident(bridge::client::Ident);
936
937 impl Ident {
938     /// Creates a new `Ident` with the given `string` as well as the specified
939     /// `span`.
940     /// The `string` argument must be a valid identifier permitted by the
941     /// language (including keywords, e.g. `self` or `fn`). Otherwise, the function will panic.
942     ///
943     /// Note that `span`, currently in rustc, configures the hygiene information
944     /// for this identifier.
945     ///
946     /// As of this time `Span::call_site()` explicitly opts-in to "call-site" hygiene
947     /// meaning that identifiers created with this span will be resolved as if they were written
948     /// directly at the location of the macro call, and other code at the macro call site will be
949     /// able to refer to them as well.
950     ///
951     /// Later spans like `Span::def_site()` will allow to opt-in to "definition-site" hygiene
952     /// meaning that identifiers created with this span will be resolved at the location of the
953     /// macro definition and other code at the macro call site will not be able to refer to them.
954     ///
955     /// Due to the current importance of hygiene this constructor, unlike other
956     /// tokens, requires a `Span` to be specified at construction.
957     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
958     pub fn new(string: &str, span: Span) -> Ident {
959         Ident(bridge::client::Ident::new(string, span.0, false))
960     }
961
962     /// Same as `Ident::new`, but creates a raw identifier (`r#ident`).
963     /// The `string` argument be a valid identifier permitted by the language
964     /// (including keywords, e.g. `fn`). Keywords which are usable in path segments
965     /// (e.g. `self`, `super`) are not supported, and will cause a panic.
966     #[stable(feature = "proc_macro_raw_ident", since = "1.47.0")]
967     pub fn new_raw(string: &str, span: Span) -> Ident {
968         Ident(bridge::client::Ident::new(string, span.0, true))
969     }
970
971     /// Returns the span of this `Ident`, encompassing the entire string returned
972     /// by [`to_string`](Self::to_string).
973     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
974     pub fn span(&self) -> Span {
975         Span(self.0.span())
976     }
977
978     /// Configures the span of this `Ident`, possibly changing its hygiene context.
979     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
980     pub fn set_span(&mut self, span: Span) {
981         self.0 = self.0.with_span(span.0);
982     }
983 }
984
985 // N.B., the bridge only provides `to_string`, implement `fmt::Display`
986 // based on it (the reverse of the usual relationship between the two).
987 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
988 impl ToString for Ident {
989     fn to_string(&self) -> String {
990         TokenStream::from(TokenTree::from(self.clone())).to_string()
991     }
992 }
993
994 /// Prints the identifier as a string that should be losslessly convertible
995 /// back into the same identifier.
996 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
997 impl fmt::Display for Ident {
998     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
999         f.write_str(&self.to_string())
1000     }
1001 }
1002
1003 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1004 impl fmt::Debug for Ident {
1005     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1006         f.debug_struct("Ident")
1007             .field("ident", &self.to_string())
1008             .field("span", &self.span())
1009             .finish()
1010     }
1011 }
1012
1013 /// A literal string (`"hello"`), byte string (`b"hello"`),
1014 /// character (`'a'`), byte character (`b'a'`), an integer or floating point number
1015 /// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`).
1016 /// Boolean literals like `true` and `false` do not belong here, they are `Ident`s.
1017 #[derive(Clone)]
1018 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1019 pub struct Literal(bridge::client::Literal);
1020
1021 macro_rules! suffixed_int_literals {
1022     ($($name:ident => $kind:ident,)*) => ($(
1023         /// Creates a new suffixed integer literal with the specified value.
1024         ///
1025         /// This function will create an integer like `1u32` where the integer
1026         /// value specified is the first part of the token and the integral is
1027         /// also suffixed at the end.
1028         /// Literals created from negative numbers might not survive round-trips through
1029         /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
1030         ///
1031         /// Literals created through this method have the `Span::call_site()`
1032         /// span by default, which can be configured with the `set_span` method
1033         /// below.
1034         #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1035         pub fn $name(n: $kind) -> Literal {
1036             Literal(bridge::client::Literal::typed_integer(&n.to_string(), stringify!($kind)))
1037         }
1038     )*)
1039 }
1040
1041 macro_rules! unsuffixed_int_literals {
1042     ($($name:ident => $kind:ident,)*) => ($(
1043         /// Creates a new unsuffixed integer literal with the specified value.
1044         ///
1045         /// This function will create an integer like `1` where the integer
1046         /// value specified is the first part of the token. No suffix is
1047         /// specified on this token, meaning that invocations like
1048         /// `Literal::i8_unsuffixed(1)` are equivalent to
1049         /// `Literal::u32_unsuffixed(1)`.
1050         /// Literals created from negative numbers might not survive rountrips through
1051         /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
1052         ///
1053         /// Literals created through this method have the `Span::call_site()`
1054         /// span by default, which can be configured with the `set_span` method
1055         /// below.
1056         #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1057         pub fn $name(n: $kind) -> Literal {
1058             Literal(bridge::client::Literal::integer(&n.to_string()))
1059         }
1060     )*)
1061 }
1062
1063 impl Literal {
1064     suffixed_int_literals! {
1065         u8_suffixed => u8,
1066         u16_suffixed => u16,
1067         u32_suffixed => u32,
1068         u64_suffixed => u64,
1069         u128_suffixed => u128,
1070         usize_suffixed => usize,
1071         i8_suffixed => i8,
1072         i16_suffixed => i16,
1073         i32_suffixed => i32,
1074         i64_suffixed => i64,
1075         i128_suffixed => i128,
1076         isize_suffixed => isize,
1077     }
1078
1079     unsuffixed_int_literals! {
1080         u8_unsuffixed => u8,
1081         u16_unsuffixed => u16,
1082         u32_unsuffixed => u32,
1083         u64_unsuffixed => u64,
1084         u128_unsuffixed => u128,
1085         usize_unsuffixed => usize,
1086         i8_unsuffixed => i8,
1087         i16_unsuffixed => i16,
1088         i32_unsuffixed => i32,
1089         i64_unsuffixed => i64,
1090         i128_unsuffixed => i128,
1091         isize_unsuffixed => isize,
1092     }
1093
1094     /// Creates a new unsuffixed floating-point literal.
1095     ///
1096     /// This constructor is similar to those like `Literal::i8_unsuffixed` where
1097     /// the float's value is emitted directly into the token but no suffix is
1098     /// used, so it may be inferred to be a `f64` later in the compiler.
1099     /// Literals created from negative numbers might not survive rountrips through
1100     /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
1101     ///
1102     /// # Panics
1103     ///
1104     /// This function requires that the specified float is finite, for
1105     /// example if it is infinity or NaN this function will panic.
1106     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1107     pub fn f32_unsuffixed(n: f32) -> Literal {
1108         if !n.is_finite() {
1109             panic!("Invalid float literal {}", n);
1110         }
1111         let mut repr = n.to_string();
1112         if !repr.contains('.') {
1113             repr.push_str(".0");
1114         }
1115         Literal(bridge::client::Literal::float(&repr))
1116     }
1117
1118     /// Creates a new suffixed floating-point literal.
1119     ///
1120     /// This constructor will create a literal like `1.0f32` where the value
1121     /// specified is the preceding part of the token and `f32` is the suffix of
1122     /// the token. This token will always be inferred to be an `f32` in the
1123     /// compiler.
1124     /// Literals created from negative numbers might not survive rountrips through
1125     /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
1126     ///
1127     /// # Panics
1128     ///
1129     /// This function requires that the specified float is finite, for
1130     /// example if it is infinity or NaN this function will panic.
1131     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1132     pub fn f32_suffixed(n: f32) -> Literal {
1133         if !n.is_finite() {
1134             panic!("Invalid float literal {}", n);
1135         }
1136         Literal(bridge::client::Literal::f32(&n.to_string()))
1137     }
1138
1139     /// Creates a new unsuffixed floating-point literal.
1140     ///
1141     /// This constructor is similar to those like `Literal::i8_unsuffixed` where
1142     /// the float's value is emitted directly into the token but no suffix is
1143     /// used, so it may be inferred to be a `f64` later in the compiler.
1144     /// Literals created from negative numbers might not survive rountrips through
1145     /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
1146     ///
1147     /// # Panics
1148     ///
1149     /// This function requires that the specified float is finite, for
1150     /// example if it is infinity or NaN this function will panic.
1151     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1152     pub fn f64_unsuffixed(n: f64) -> Literal {
1153         if !n.is_finite() {
1154             panic!("Invalid float literal {}", n);
1155         }
1156         let mut repr = n.to_string();
1157         if !repr.contains('.') {
1158             repr.push_str(".0");
1159         }
1160         Literal(bridge::client::Literal::float(&repr))
1161     }
1162
1163     /// Creates a new suffixed floating-point literal.
1164     ///
1165     /// This constructor will create a literal like `1.0f64` where the value
1166     /// specified is the preceding part of the token and `f64` is the suffix of
1167     /// the token. This token will always be inferred to be an `f64` in the
1168     /// compiler.
1169     /// Literals created from negative numbers might not survive rountrips through
1170     /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal).
1171     ///
1172     /// # Panics
1173     ///
1174     /// This function requires that the specified float is finite, for
1175     /// example if it is infinity or NaN this function will panic.
1176     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1177     pub fn f64_suffixed(n: f64) -> Literal {
1178         if !n.is_finite() {
1179             panic!("Invalid float literal {}", n);
1180         }
1181         Literal(bridge::client::Literal::f64(&n.to_string()))
1182     }
1183
1184     /// String literal.
1185     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1186     pub fn string(string: &str) -> Literal {
1187         Literal(bridge::client::Literal::string(string))
1188     }
1189
1190     /// Character literal.
1191     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1192     pub fn character(ch: char) -> Literal {
1193         Literal(bridge::client::Literal::character(ch))
1194     }
1195
1196     /// Byte string literal.
1197     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1198     pub fn byte_string(bytes: &[u8]) -> Literal {
1199         Literal(bridge::client::Literal::byte_string(bytes))
1200     }
1201
1202     /// Returns the span encompassing this literal.
1203     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1204     pub fn span(&self) -> Span {
1205         Span(self.0.span())
1206     }
1207
1208     /// Configures the span associated for this literal.
1209     #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1210     pub fn set_span(&mut self, span: Span) {
1211         self.0.set_span(span.0);
1212     }
1213
1214     /// Returns a `Span` that is a subset of `self.span()` containing only the
1215     /// source bytes in range `range`. Returns `None` if the would-be trimmed
1216     /// span is outside the bounds of `self`.
1217     // FIXME(SergioBenitez): check that the byte range starts and ends at a
1218     // UTF-8 boundary of the source. otherwise, it's likely that a panic will
1219     // occur elsewhere when the source text is printed.
1220     // FIXME(SergioBenitez): there is no way for the user to know what
1221     // `self.span()` actually maps to, so this method can currently only be
1222     // called blindly. For example, `to_string()` for the character 'c' returns
1223     // "'\u{63}'"; there is no way for the user to know whether the source text
1224     // was 'c' or whether it was '\u{63}'.
1225     #[unstable(feature = "proc_macro_span", issue = "54725")]
1226     pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
1227         self.0.subspan(range.start_bound().cloned(), range.end_bound().cloned()).map(Span)
1228     }
1229 }
1230
1231 /// Parse a single literal from its stringified representation.
1232 ///
1233 /// In order to parse successfully, the input string must not contain anything
1234 /// but the literal token. Specifically, it must not contain whitespace or
1235 /// comments in addition to the literal.
1236 ///
1237 /// The resulting literal token will have a `Span::call_site()` span.
1238 ///
1239 /// NOTE: some errors may cause panics instead of returning `LexError`. We
1240 /// reserve the right to change these errors into `LexError`s later.
1241 #[stable(feature = "proc_macro_literal_parse", since = "1.54.0")]
1242 impl FromStr for Literal {
1243     type Err = LexError;
1244
1245     fn from_str(src: &str) -> Result<Self, LexError> {
1246         match bridge::client::Literal::from_str(src) {
1247             Ok(literal) => Ok(Literal(literal)),
1248             Err(()) => Err(LexError),
1249         }
1250     }
1251 }
1252
1253 // N.B., the bridge only provides `to_string`, implement `fmt::Display`
1254 // based on it (the reverse of the usual relationship between the two).
1255 #[stable(feature = "proc_macro_lib", since = "1.15.0")]
1256 impl ToString for Literal {
1257     fn to_string(&self) -> String {
1258         self.0.to_string()
1259     }
1260 }
1261
1262 /// Prints the literal as a string that should be losslessly convertible
1263 /// back into the same literal (except for possible rounding for floating point literals).
1264 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1265 impl fmt::Display for Literal {
1266     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1267         f.write_str(&self.to_string())
1268     }
1269 }
1270
1271 #[stable(feature = "proc_macro_lib2", since = "1.29.0")]
1272 impl fmt::Debug for Literal {
1273     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1274         self.0.fmt(f)
1275     }
1276 }
1277
1278 /// Tracked access to environment variables.
1279 #[unstable(feature = "proc_macro_tracked_env", issue = "74690")]
1280 pub mod tracked_env {
1281     use std::env::{self, VarError};
1282     use std::ffi::OsStr;
1283
1284     /// Retrieve an environment variable and add it to build dependency info.
1285     /// Build system executing the compiler will know that the variable was accessed during
1286     /// compilation, and will be able to rerun the build when the value of that variable changes.
1287     /// Besides the dependency tracking this function should be equivalent to `env::var` from the
1288     /// standard library, except that the argument must be UTF-8.
1289     #[unstable(feature = "proc_macro_tracked_env", issue = "74690")]
1290     pub fn var<K: AsRef<OsStr> + AsRef<str>>(key: K) -> Result<String, VarError> {
1291         let key: &str = key.as_ref();
1292         let value = env::var(key);
1293         crate::bridge::client::FreeFunctions::track_env_var(key, value.as_deref().ok());
1294         value
1295     }
1296 }
1297
1298 /// Tracked access to additional files.
1299 #[unstable(feature = "track_path", issue = "73921")]
1300 pub mod tracked_path {
1301
1302     /// Track a file explicitly.
1303     ///
1304     /// Commonly used for tracking asset preprocessing.
1305     #[unstable(feature = "track_path", issue = "73921")]
1306     pub fn path<P: AsRef<str>>(path: P) {
1307         let path: &str = path.as_ref();
1308         crate::bridge::client::FreeFunctions::track_path(path);
1309     }
1310 }