Skip to content

Commit c417977

Browse files
committed
Distinguish Impl's impl generics vs generics on the Self type
1 parent eb472ad commit c417977

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

syntax/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,13 +141,15 @@ pub struct TypeAlias {
141141

142142
pub struct Impl {
143143
pub impl_token: Token![impl],
144-
pub generics: Lifetimes,
144+
pub impl_generics: Lifetimes,
145145
pub negative: bool,
146146
pub ty: Type,
147+
pub ty_generics: Lifetimes,
147148
pub brace_token: Brace,
148149
pub negative_token: Option<Token![!]>,
149150
}
150151

152+
#[derive(Clone, Default)]
151153
pub struct Lifetimes {
152154
pub lt_token: Option<Token![<]>,
153155
pub lifetimes: Punctuated<Lifetime, Token![,]>,

syntax/parse.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -868,7 +868,7 @@ fn parse_impl(imp: ItemImpl) -> Result<Api> {
868868
"where-clause on an impl is not supported yet",
869869
));
870870
}
871-
let mut generics = Lifetimes {
871+
let mut impl_generics = Lifetimes {
872872
lt_token: imp.generics.lt_token,
873873
lifetimes: Punctuated::new(),
874874
gt_token: imp.generics.gt_token,
@@ -877,13 +877,13 @@ fn parse_impl(imp: ItemImpl) -> Result<Api> {
877877
let (param, punct) = pair.into_tuple();
878878
match param {
879879
GenericParam::Lifetime(def) if def.bounds.is_empty() => {
880-
generics.lifetimes.push_value(def.lifetime);
880+
impl_generics.lifetimes.push_value(def.lifetime);
881881
if let Some(punct) = punct {
882-
generics.lifetimes.push_punct(punct);
882+
impl_generics.lifetimes.push_punct(punct);
883883
}
884884
}
885885
_ => {
886-
let span = quote!(#impl_token #generics);
886+
let span = quote!(#impl_token #impl_generics);
887887
return Err(Error::new_spanned(
888888
span,
889889
"generic parameter on an impl is not supported yet",
@@ -907,15 +907,35 @@ fn parse_impl(imp: ItemImpl) -> Result<Api> {
907907
}
908908
}
909909

910-
let negative = negative_token.is_some();
911910
let ty = parse_type(&self_ty)?;
911+
let ty_generics = match &ty {
912+
Type::RustBox(ty)
913+
| Type::RustVec(ty)
914+
| Type::UniquePtr(ty)
915+
| Type::SharedPtr(ty)
916+
| Type::WeakPtr(ty)
917+
| Type::CxxVector(ty) => match &ty.inner {
918+
Type::Ident(ident) => ident.generics.clone(),
919+
_ => Lifetimes::default(),
920+
},
921+
Type::Ident(_)
922+
| Type::Ref(_)
923+
| Type::Str(_)
924+
| Type::Fn(_)
925+
| Type::Void(_)
926+
| Type::SliceRef(_)
927+
| Type::Array(_) => Lifetimes::default(),
928+
};
929+
930+
let negative = negative_token.is_some();
912931
let brace_token = imp.brace_token;
913932

914933
Ok(Api::Impl(Impl {
915934
impl_token,
916-
generics,
935+
impl_generics,
917936
negative,
918937
ty,
938+
ty_generics,
919939
brace_token,
920940
negative_token,
921941
}))

syntax/tokens.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,15 @@ impl ToTokens for Impl {
192192
fn to_tokens(&self, tokens: &mut TokenStream) {
193193
let Impl {
194194
impl_token,
195-
generics,
195+
impl_generics,
196196
negative: _,
197197
ty,
198+
ty_generics: _,
198199
brace_token,
199200
negative_token,
200201
} = self;
201202
impl_token.to_tokens(tokens);
202-
generics.to_tokens(tokens);
203+
impl_generics.to_tokens(tokens);
203204
negative_token.to_tokens(tokens);
204205
ty.to_tokens(tokens);
205206
brace_token.surround(tokens, |_tokens| {});

0 commit comments

Comments
 (0)