Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: dtolnay/quote
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: 1.0.17
Choose a base ref
...
head repository: dtolnay/quote
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: 1.0.18
Choose a head ref
  • 9 commits
  • 4 files changed
  • 2 contributors

Commits on Mar 28, 2022

  1. Copy the full SHA
    678d83d View commit details
  2. Merge pull request #216 from dtolnay/character0

    Use a character other than \0 for testing unicode escapes
    dtolnay authored Mar 28, 2022
    Copy the full SHA
    a53dd09 View commit details

Commits on Apr 6, 2022

  1. Copy the full SHA
    31c3be4 View commit details

Commits on Apr 11, 2022

  1. Special case quote!/quote_spanned! for 1 and 2 tts.

    These make crates that use `quote` compile faster.
    
    I also tried specializing for 3 tts, but the rules are more complex and
    the additional perf wins are much smaller than they are for 1 and 2 tts.
    nnethercote committed Apr 11, 2022
    Copy the full SHA
    4d75f2e View commit details
  2. Merge pull request #217 from nnethercote/special-case-1-2

    Special case `quote!`/`quote_spanned!` for 1 and 2 tts.
    dtolnay authored Apr 11, 2022
    Copy the full SHA
    2d23d9d View commit details
  3. Touch up PR 217

    dtolnay committed Apr 11, 2022
    Copy the full SHA
    b5160b1 View commit details
  4. 1
    Copy the full SHA
    241b8d6 View commit details
  5. Merge pull request #219 from dtolnay/doc

    Show simpler rules in macro documentation
    dtolnay authored Apr 11, 2022
    Copy the full SHA
    0447cf0 View commit details
  6. Release 1.0.18

    dtolnay committed Apr 11, 2022
    Copy the full SHA
    8c954ca View commit details
Showing with 84 additions and 9 deletions.
  1. +1 −1 Cargo.toml
  2. +66 −1 src/lib.rs
  3. +6 −6 tests/test.rs
  4. +11 −1 tests/ui/not-quotable.stderr
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "quote"
version = "1.0.17" # don't forget to update html_root_url, version in readme for breaking changes
version = "1.0.18" # don't forget to update html_root_url, version in readme for breaking changes
authors = ["David Tolnay <dtolnay@gmail.com>"]
license = "MIT OR Apache-2.0"
description = "Quasi-quoting macro quote!(...)"
67 changes: 66 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -81,7 +81,7 @@
//! ```
// Quote types in rustdoc of other crates get linked to here.
#![doc(html_root_url = "https://docs.rs/quote/1.0.17")]
#![doc(html_root_url = "https://docs.rs/quote/1.0.18")]
#![allow(
clippy::doc_markdown,
clippy::missing_errors_doc,
@@ -468,11 +468,42 @@ pub mod spanned;
/// # }
/// # }
/// ```
#[cfg(doc)]
#[macro_export]
macro_rules! quote {
($($tt:tt)*) => {
...
};
}

#[cfg(not(doc))]
#[macro_export]
macro_rules! quote {
() => {
$crate::__private::TokenStream::new()
};

// Special case rule for a single tt, for performance.
($tt:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::quote_token!($tt _s);
_s
}};

// Special case rules for two tts, for performance.
(# $var:ident) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::ToTokens::to_tokens(&$var, &mut _s);
_s
}};
($tt1:tt $tt2:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::quote_token!($tt1 _s);
$crate::quote_token!($tt2 _s);
_s
}};

// Rule for any other number of tokens.
($($tt:tt)*) => {{
let mut _s = $crate::__private::TokenStream::new();
$crate::quote_each_token!(_s $($tt)*);
@@ -576,12 +607,46 @@ macro_rules! quote {
/// In this example it is important for the where-clause to be spanned with the
/// line/column information of the user's input type so that error messages are
/// placed appropriately by the compiler.
#[cfg(doc)]
#[macro_export]
macro_rules! quote_spanned {
($span:expr=> $($tt:tt)*) => {
...
};
}

#[cfg(not(doc))]
#[macro_export]
macro_rules! quote_spanned {
($span:expr=>) => {{
let _: $crate::__private::Span = $span;
$crate::__private::TokenStream::new()
}};

// Special case rule for a single tt, for performance.
($span:expr=> $tt:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
$crate::quote_token_spanned!($tt _s _span);
_s
}};

// Special case rules for two tts, for performance.
($span:expr=> # $var:ident) => {{
let mut _s = $crate::__private::TokenStream::new();
let _: $crate::__private::Span = $span;
$crate::ToTokens::to_tokens(&$var, &mut _s);
_s
}};
($span:expr=> $tt1:tt $tt2:tt) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
$crate::quote_token_spanned!($tt1 _s _span);
$crate::quote_token_spanned!($tt2 _s _span);
_s
}};

// Rule for any other number of tokens.
($span:expr=> $($tt:tt)*) => {{
let mut _s = $crate::__private::TokenStream::new();
let _span: $crate::__private::Span = $span;
12 changes: 6 additions & 6 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -200,7 +200,7 @@ fn test_floating() {

#[test]
fn test_char() {
let zero = '\0';
let zero = '\u{1}';
let pound = '#';
let quote = '"';
let apost = '\'';
@@ -210,23 +210,23 @@ fn test_char() {
let tokens = quote! {
#zero #pound #quote #apost #newline #heart
};
let expected = "'\\u{0}' '#' '\"' '\\'' '\\n' '\u{2764}'";
let expected = "'\\u{1}' '#' '\"' '\\'' '\\n' '\u{2764}'";
assert_eq!(expected, tokens.to_string());
}

#[test]
fn test_str() {
let s = "\0 a 'b \" c";
let s = "\u{1} a 'b \" c";
let tokens = quote!(#s);
let expected = "\"\\u{0} a 'b \\\" c\"";
let expected = "\"\\u{1} a 'b \\\" c\"";
assert_eq!(expected, tokens.to_string());
}

#[test]
fn test_string() {
let s = "\0 a 'b \" c".to_string();
let s = "\u{1} a 'b \" c".to_string();
let tokens = quote!(#s);
let expected = "\"\\u{0} a 'b \\\" c\"";
let expected = "\"\\u{1} a 'b \\\" c\"";
assert_eq!(expected, tokens.to_string());
}

12 changes: 11 additions & 1 deletion tests/ui/not-quotable.stderr
Original file line number Diff line number Diff line change
@@ -4,4 +4,14 @@ error[E0277]: the trait bound `Ipv4Addr: ToTokens` is not satisfied
6 | let _ = quote! { #ip };
| ^^^^^^^^^^^^^^ the trait `ToTokens` is not implemented for `Ipv4Addr`
|
= note: this error originates in the macro `$crate::quote_token_with_context` (in Nightly builds, run with -Z macro-backtrace for more info)
= help: the following other types implement trait `ToTokens`:
&'a T
&'a mut T
Box<T>
Cow<'a, T>
Option<T>
Rc<T>
RepInterp<T>
String
and 23 others
= note: this error originates in the macro `quote` (in Nightly builds, run with -Z macro-backtrace for more info)