-
-
Notifications
You must be signed in to change notification settings - Fork 313
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
Generics
does not tokenize its WhereClause
#782
Comments
Generics
does not tokenize its WhereClause
`Generics
does not tokenize its WhereClause
I definitely think this should be documented. I have just shot my foot, wondering why the constraint is not satisfied, and it was not until I read the code of |
I just came across this issue, because my procedural macro was panicking for some unknown reason. Here's the code I was using: /// Applies the specified bounds to all generic parameters
pub fn apply_bounds(
generics: Generics,
_bound_to_add: TypeParamBound,
) -> Generics {
let (params, where_clause) = (
generics.params,
generics.where_clause,
);
// (shortened -- this isn't what panics)
let edited_params =
params.into_iter() // ...
;
// The invocation below panics due to `#where_clause` being an "unexpected token".
parse_quote! {
<#(#edited_params),*> #where_clause
}
} I was able to fix it by changing that let mut new_generics: Generics = parse_quote! {
< #(#edited_params),* >
};
new_generics.where_clause = where_clause; If I might suggest something, it would be to move the If nothing else, the |
I just encountered a bug in my macro code where I assumed the |
The
ToTokens
impl forGenerics
does not tokenize the containedWhereClause
- instead, any items which contain aGenerics
(e.g.ItemStruct
) explicitly tokenize theWhereClause
in the correct position.This is a major footgun - if a
Generics
is extracted from a parsed item and thenquote!
ed somewhere else, theWhereClause
will silently be lost. This can lead to confusing errors, since the lack of a where clause can cause type-checking to fail.Making
Generics
include itsWhereClause
during tokenization would be a breaking change (and there may be drawbacks that I haven't thought of). However, the documentation should clearly state that tokenizing aGenerics
is lossy, to help prevent confusing errors in downstream crates.The text was updated successfully, but these errors were encountered: