diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2021-08-13 00:35:41 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2021-08-13 00:35:41 -0300 |
commit | 5cf5f84335c759338f993175fbbdc8bfba7fb5d3 (patch) | |
tree | 7887fe972ce4baa5c58947b2cf27a55c1079fe27 | |
parent | bffdb1e7ad54b0b857d69d45b8791c68741a6ea0 (diff) |
Fix `impl trait where` without `impl where`
-rw-r--r-- | Cargo.toml | 2 | ||||
-rw-r--r-- | src/lib.rs | 14 | ||||
-rw-r--r-- | tests/14-where-on-trait.rs | 25 | ||||
-rw-r--r-- | tests/progress.rs | 1 |
4 files changed, 35 insertions, 7 deletions
diff --git a/Cargo.toml b/Cargo.toml index 6f7645d..d606dd9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "impl_trait" -version = "0.1.5" +version = "0.1.6" authors = ["SoniEx2 <endermoneymod@gmail.com>"] edition = "2018" description = "Allows impl trait inside inherent impl." diff --git a/src/lib.rs b/src/lib.rs index 192d56b..348e4e1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -210,14 +210,16 @@ pub fn impl_trait(item: TokenStream) -> TokenStream { // *then* add the "where" (from the impl-trait) found.last_mut().unwrap().push(tt.clone()); // and the parent bounds (except the "where") - found.last_mut().unwrap().extend((&where_bounds).into_iter().skip(1).cloned()); - // also make sure that there's an ',' at the correct place - if let Some(&TokenTree::Punct(ref x)) = where_bounds.last() { - if x.as_char() == ',' { - continue 'main_loop; + if !where_bounds.is_empty() { + found.last_mut().unwrap().extend((&where_bounds).into_iter().skip(1).cloned()); + // also make sure that there's an ',' at the correct place + if let Some(&TokenTree::Punct(ref x)) = where_bounds.last() { + if x.as_char() == ',' { + continue 'main_loop; + } } + found.last_mut().unwrap().push(proc_macro::Punct::new(',', proc_macro::Spacing::Alone).into()); } - found.last_mut().unwrap().push(proc_macro::Punct::new(',', proc_macro::Spacing::Alone).into()); continue 'main_loop; } } diff --git a/tests/14-where-on-trait.rs b/tests/14-where-on-trait.rs new file mode 100644 index 0000000..56c7a4a --- /dev/null +++ b/tests/14-where-on-trait.rs @@ -0,0 +1,25 @@ +// Checks that the impl trait can have where. + +use impl_trait::impl_trait; + +struct Foo<T>(T); +trait Bar<U> { +} + +impl_trait! { + impl<T> Foo<T> { + impl trait<U> Bar<U> where Self: From<U> { + } + } +} +impl From<()> for Foo<()> { + fn from(x: ()) -> Self { + Self { 0: x } + } +} + +fn static_assert_1<T: Bar<U>, U>(_t: T, _u: U) {} + +fn main() { + static_assert_1(Foo(()), ()); +} diff --git a/tests/progress.rs b/tests/progress.rs index ef302b0..1cece81 100644 --- a/tests/progress.rs +++ b/tests/progress.rs @@ -14,6 +14,7 @@ fn tests() { t.pass("tests/11-traits-generics-docs.rs"); t.pass("tests/12-combining-generics.rs"); t.pass("tests/13-trait-generics.rs"); + t.pass("tests/14-where-on-trait.rs"); t.pass("tests/98-readme.rs"); t.pass("tests/99-goal.rs"); } |