blob: 79e23dfbbf13eea1e2efc785f82831eafa2354a7 [file] [log] [blame]
David Tolnaya9221302018-01-06 18:20:54 -08001//! A trait that can provide the `Span` of the complete contents of a syntax
2//! tree node.
3//!
David Tolnay461d98e2018-01-07 11:07:19 -08004//! *This module is available if Syn is built with both the `"parsing"` and
5//! `"printing"` features.*
6//!
David Tolnaya9221302018-01-06 18:20:54 -08007//! # Example
8//!
9//! Suppose in a procedural macro we have a [`Type`] that we want to assert
10//! implements the [`Sync`] trait. Maybe this is the type of one of the fields
11//! of a struct for which we are deriving a trait implementation, and we need to
12//! be able to pass a reference to one of those fields across threads.
13//!
14//! [`Type`]: ../enum.Type.html
15//! [`Sync`]: https://doc.rust-lang.org/std/marker/trait.Sync.html
16//!
17//! If the field type does *not* implement `Sync` as required, we want the
18//! compiler to report an error pointing out exactly which type it was.
19//!
20//! The following macro code takes a variable `ty` of type `Type` and produces a
21//! static assertion that `Sync` is implemented for that type.
22//!
David Tolnay95989db2019-01-01 15:05:57 -050023//! ```edition2018
David Tolnay9b00f652018-09-01 10:31:02 -070024//! # extern crate proc_macro;
David Tolnay9b00f652018-09-01 10:31:02 -070025//! #
David Tolnay0a6deb22018-01-06 18:26:05 -080026//! use proc_macro::TokenStream;
27//! use proc_macro2::Span;
David Tolnayfd5b1172018-12-31 17:54:36 -050028//! use quote::quote_spanned;
David Tolnay9b00f652018-09-01 10:31:02 -070029//! use syn::Type;
30//! use syn::spanned::Spanned;
David Tolnay0a6deb22018-01-06 18:26:05 -080031//!
32//! # const IGNORE_TOKENS: &str = stringify! {
33//! #[proc_macro_derive(MyMacro)]
34//! # };
35//! pub fn my_macro(input: TokenStream) -> TokenStream {
36//! # let ty = get_a_type();
37//! /* ... */
38//!
Alex Crichton9a4dca22018-03-28 06:32:19 -070039//! let assert_sync = quote_spanned! {ty.span()=>
David Tolnay0a6deb22018-01-06 18:26:05 -080040//! struct _AssertSync where #ty: Sync;
41//! };
42//!
43//! /* ... */
44//! # input
45//! }
David Tolnaya9221302018-01-06 18:20:54 -080046//! #
David Tolnay0a6deb22018-01-06 18:26:05 -080047//! # fn get_a_type() -> Type {
48//! # unimplemented!()
David Tolnaya9221302018-01-06 18:20:54 -080049//! # }
David Tolnaya9221302018-01-06 18:20:54 -080050//! ```
51//!
52//! By inserting this `assert_sync` fragment into the output code generated by
53//! our macro, the user's code will fail to compile if `ty` does not implement
54//! `Sync`. The errors they would see look like the following.
55//!
56//! ```text
57//! error[E0277]: the trait bound `*const i32: std::marker::Sync` is not satisfied
58//! --> src/main.rs:10:21
59//! |
60//! 10 | bad_field: *const i32,
61//! | ^^^^^^^^^^ `*const i32` cannot be shared between threads safely
62//! ```
63//!
64//! In this technique, using the `Type`'s span for the error message makes the
Alex Crichton9a4dca22018-03-28 06:32:19 -070065//! error appear in the correct place underlining the right type.
David Tolnaya9221302018-01-06 18:20:54 -080066
David Tolnayf790b612017-12-31 18:46:57 -050067use proc_macro2::{Span, TokenStream};
Alex Crichtona74a1c82018-05-16 10:20:44 -070068use quote::ToTokens;
David Tolnayf790b612017-12-31 18:46:57 -050069
David Tolnaya9221302018-01-06 18:20:54 -080070/// A trait that can provide the `Span` of the complete contents of a syntax
71/// tree node.
72///
73/// This trait is automatically implemented for all types that implement
David Tolnay6315c652018-08-24 16:05:45 -040074/// [`ToTokens`] from the `quote` crate. It is sealed and cannot be implemented
75/// outside of the Syn crate other than by implementing `ToTokens`.
David Tolnaya9221302018-01-06 18:20:54 -080076///
David Tolnay442cf762018-08-31 11:08:24 -070077/// [`ToTokens`]: https://docs.rs/quote/0.6/quote/trait.ToTokens.html
David Tolnaya9221302018-01-06 18:20:54 -080078///
79/// See the [module documentation] for an example.
80///
81/// [module documentation]: index.html
David Tolnay461d98e2018-01-07 11:07:19 -080082///
83/// *This trait is available if Syn is built with both the `"parsing"` and
84/// `"printing"` features.*
David Tolnay6315c652018-08-24 16:05:45 -040085pub trait Spanned: private::Sealed {
David Tolnaya9221302018-01-06 18:20:54 -080086 /// Returns a `Span` covering the complete contents of this syntax tree
87 /// node, or [`Span::call_site()`] if this node is empty.
88 ///
David Tolnay442cf762018-08-31 11:08:24 -070089 /// [`Span::call_site()`]: https://docs.rs/proc-macro2/0.4/proc_macro2/struct.Span.html#method.call_site
David Tolnayf790b612017-12-31 18:46:57 -050090 fn span(&self) -> Span;
91}
92
David Tolnay6315c652018-08-24 16:05:45 -040093mod private {
94 use quote::ToTokens;
95 pub trait Sealed {}
96 impl<T: ToTokens> Sealed for T {}
97}
98
David Tolnayf790b612017-12-31 18:46:57 -050099impl<T> Spanned for T
100where
101 T: ToTokens,
102{
103 fn span(&self) -> Span {
David Tolnay785a79a2018-10-02 21:28:24 -0700104 join_spans(self.into_token_stream())
105 }
106}
107
108fn join_spans(tokens: TokenStream) -> Span {
David Tolnayf1a10262018-10-13 15:33:50 -0700109 let mut iter = tokens.into_iter().filter_map(|tt| {
110 // FIXME: This shouldn't be required, since optimally spans should
111 // never be invalid. This filter_map can probably be removed when
112 // https://github.com/rust-lang/rust/issues/43081 is resolved.
113 let span = tt.span();
114 let debug = format!("{:?}", span);
115 if debug.ends_with("bytes(0..0)") {
116 None
117 } else {
118 Some(span)
119 }
120 });
David Tolnay785a79a2018-10-02 21:28:24 -0700121
122 let mut joined = match iter.next() {
123 Some(span) => span,
124 None => return Span::call_site(),
125 };
126
127 #[cfg(procmacro2_semver_exempt)]
128 {
129 for next in iter {
130 if let Some(span) = joined.join(next) {
131 joined = span;
132 }
133 }
David Tolnayf790b612017-12-31 18:46:57 -0500134 }
David Tolnay4d942b42018-01-02 22:14:04 -0800135
136 #[cfg(not(procmacro2_semver_exempt))]
David Tolnay785a79a2018-10-02 21:28:24 -0700137 {
David Tolnay4d942b42018-01-02 22:14:04 -0800138 // We can't join spans without procmacro2_semver_exempt so just grab the
139 // first one.
David Tolnay785a79a2018-10-02 21:28:24 -0700140 joined = joined;
David Tolnay4d942b42018-01-02 22:14:04 -0800141 }
David Tolnay785a79a2018-10-02 21:28:24 -0700142
143 joined
David Tolnayf790b612017-12-31 18:46:57 -0500144}