// SPDX-License-Identifier: Apache-2.0 OR MIT12//! A trait that can provide the `Span` of the complete contents of a syntax3//! tree node.4//!5//! <br>6//!7//! # Example8//!9//! Suppose in a procedural macro we have a [`Type`] that we want to assert10//! implements the [`Sync`] trait. Maybe this is the type of one of the fields11//! of a struct for which we are deriving a trait implementation, and we need to12//! be able to pass a reference to one of those fields across threads.13//!14//! [`Type`]: crate::Type15//! [`Sync`]: std::marker::Sync16//!17//! If the field type does *not* implement `Sync` as required, we want the18//! 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 a21//! static assertion that `Sync` is implemented for that type.22//!23//! ```24//! # extern crate proc_macro;25//! #26//! use proc_macro::TokenStream;27//! use proc_macro2::Span;28//! use quote::quote_spanned;29//! use syn::Type;30//! use syn::spanned::Spanned;31//!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//!39//! let assert_sync = quote_spanned! {ty.span()=>40//! struct _AssertSync where #ty: Sync;41//! };42//!43//! /* ... */44//! # input45//! }46//! #47//! # fn get_a_type() -> Type {48//! # unimplemented!()49//! # }50//! ```51//!52//! By inserting this `assert_sync` fragment into the output code generated by53//! our macro, the user's code will fail to compile if `ty` does not implement54//! `Sync`. The errors they would see look like the following.55//!56//! ```text57//! error[E0277]: the trait bound `*const i32: std::marker::Sync` is not satisfied58//! --> src/main.rs:10:2159//! |60//! 10 | bad_field: *const i32,61//! | ^^^^^^^^^^ `*const i32` cannot be shared between threads safely62//! ```63//!64//! In this technique, using the `Type`'s span for the error message makes the65//! error appear in the correct place underlining the right type.66//!67//! <br>68//!69//! # Limitations70//!71//! The underlying [`proc_macro::Span::join`] method is nightly-only. When72//! called from within a procedural macro in a nightly compiler, `Spanned` will73//! use `join` to produce the intended span. When not using a nightly compiler,74//! only the span of the *first token* of the syntax tree node is returned.75//!76//! In the common case of wanting to use the joined span as the span of a77//! `syn::Error`, consider instead using [`syn::Error::new_spanned`] which is78//! able to span the error correctly under the complete syntax tree node without79//! needing the unstable `join`.80//!81//! [`syn::Error::new_spanned`]: crate::Error::new_spanned8283use proc_macro2::Span;84use quote::spanned::Spanned as ToTokens;8586/// A trait that can provide the `Span` of the complete contents of a syntax87/// tree node.88///89/// This trait is automatically implemented for all types that implement90/// [`ToTokens`] from the `quote` crate, as well as for `Span` itself.91///92/// [`ToTokens`]: quote::ToTokens93///94/// See the [module documentation] for an example.95///96/// [module documentation]: self97pub trait Spanned: private::Sealed {98/// Returns a `Span` covering the complete contents of this syntax tree99/// node, or [`Span::call_site()`] if this node is empty.100///101/// [`Span::call_site()`]: proc_macro2::Span::call_site102fn span(&self) -> Span;103}104105impl<T: ?Sized + ToTokens> Spanned for T {106fn span(&self) -> Span {107self.__span()108}109}110111mod private {112use crate::spanned::ToTokens;113114pub trait Sealed {}115impl<T: ?Sized + ToTokens> Sealed for T {}116117#[cfg(any(feature = "full", feature = "derive"))]118impl Sealed for crate::QSelf {}119}120121122