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 generic params on impl block #627

Merged
merged 1 commit into from
Dec 31, 2020
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
6 changes: 5 additions & 1 deletion syntax/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,11 +413,13 @@ impl Hash for Impl {
fn hash<H: Hasher>(&self, state: &mut H) {
let Impl {
impl_token: _,
generics,
negative,
ty,
brace_token: _,
negative_token: _,
} = self;
generics.hash(state);
if *negative {
negative.hash(state);
}
Expand All @@ -431,18 +433,20 @@ impl PartialEq for Impl {
fn eq(&self, other: &Impl) -> bool {
let Impl {
impl_token: _,
generics,
negative,
ty,
brace_token: _,
negative_token: _,
} = self;
let Impl {
impl_token: _,
generics: generics2,
negative: negative2,
ty: ty2,
brace_token: _,
negative_token: _,
} = other;
negative == negative2 && ty == ty2
generics == generics2 && negative == negative2 && ty == ty2
}
}
1 change: 1 addition & 0 deletions syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ pub struct TypeAlias {

pub struct Impl {
pub impl_token: Token![impl],
pub generics: Lifetimes,
pub negative: bool,
pub ty: Type,
pub brace_token: Brace,
Expand Down
34 changes: 29 additions & 5 deletions syntax/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,6 +827,8 @@ fn parse_extern_type_bounded(
}

fn parse_impl(imp: ItemImpl) -> Result<Api> {
let impl_token = imp.impl_token;

if !imp.items.is_empty() {
let mut span = Group::new(Delimiter::Brace, TokenStream::new());
span.set_span(imp.brace_token.span);
Expand All @@ -842,13 +844,35 @@ fn parse_impl(imp: ItemImpl) -> Result<Api> {
));
}

let generics = &imp.generics;
if !generics.params.is_empty() || generics.where_clause.is_some() {
if let Some(where_clause) = imp.generics.where_clause {
return Err(Error::new_spanned(
imp,
"generic parameters on an impl is not supported",
where_clause,
"where-clause on an impl is not supported yet",
));
}
let mut generics = Lifetimes {
lt_token: imp.generics.lt_token,
lifetimes: Punctuated::new(),
gt_token: imp.generics.gt_token,
};
for pair in imp.generics.params.into_pairs() {
let (param, punct) = pair.into_tuple();
match param {
GenericParam::Lifetime(def) if def.bounds.is_empty() => {
generics.lifetimes.push_value(def.lifetime);
if let Some(punct) = punct {
generics.lifetimes.push_punct(punct);
}
}
_ => {
let span = quote!(#impl_token #generics);
return Err(Error::new_spanned(
span,
"generic parameter on an impl is not supported yet",
));
}
}
}

let mut negative_token = None;
let mut self_ty = *imp.self_ty;
Expand All @@ -865,13 +889,13 @@ fn parse_impl(imp: ItemImpl) -> Result<Api> {
}
}

let impl_token = imp.impl_token;
let negative = negative_token.is_some();
let ty = parse_type(&self_ty)?;
let brace_token = imp.brace_token;

Ok(Api::Impl(Impl {
impl_token,
generics,
negative,
ty,
brace_token,
Expand Down
2 changes: 2 additions & 0 deletions syntax/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,12 +192,14 @@ impl ToTokens for Impl {
fn to_tokens(&self, tokens: &mut TokenStream) {
let Impl {
impl_token,
generics,
negative: _,
ty,
brace_token,
negative_token,
} = self;
impl_token.to_tokens(tokens);
generics.to_tokens(tokens);
negative_token.to_tokens(tokens);
ty.to_tokens(tokens);
brace_token.surround(tokens, |_tokens| {});
Expand Down