Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse use<'a, T> precise capturing bounds as verbatim #1707

Merged
merged 1 commit into from
Jul 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,27 @@ pub(crate) mod parsing {

let begin = input.fork();

if cfg!(feature = "full") && input.peek(Token![use]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've gated this behind cfg(feature = "full") since that's what ~const does below.

input.parse::<Token![use]>()?;
input.parse::<Token![<]>()?;
while !input.peek(Token![>]) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opted for a manual loop here since I'm not certain Punctuated gives me the right API for this.

if input.peek(Lifetime) {
input.parse::<Lifetime>()?;
} else if input.peek(Ident) {
input.parse::<Ident>()?;
} else {
break;
}
if input.peek(Token![,]) {
input.parse::<Token![,]>()?;
} else {
break;
}
}
input.parse::<Token![>]>()?;
return Ok(TypeParamBound::Verbatim(verbatim::between(&begin, input)));
}

let content;
let (paren_token, content) = if input.peek(token::Paren) {
(Some(parenthesized!(content in input)), &content)
Expand Down
47 changes: 47 additions & 0 deletions tests/test_ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,50 @@ fn test_tuple_comma() {
}
"###);
}

#[test]
fn test_impl_trait_use() {
let tokens = quote! {
impl Sized + use<'_, 'a, A, Test>
};

snapshot!(tokens as Type, @r###"
Type::ImplTrait {
bounds: [
TypeParamBound::Trait(TraitBound {
path: Path {
segments: [
PathSegment {
ident: "Sized",
},
],
},
}),
Token![+],
TypeParamBound::Verbatim(`use < '_ , 'a , A , Test >`),
],
}
"###);

let trailing = quote! {
impl Sized + use<'_,>
};

snapshot!(trailing as Type, @r###"
Type::ImplTrait {
bounds: [
TypeParamBound::Trait(TraitBound {
path: Path {
segments: [
PathSegment {
ident: "Sized",
},
],
},
}),
Token![+],
TypeParamBound::Verbatim(`use < '_ , >`),
],
}
"###);
}