Skip to content

Commit

Permalink
Merge pull request #1023 from dtolnay/implfor
Browse files Browse the repository at this point in the history
Fix possible panic on invalid ItemImpl
  • Loading branch information
dtolnay committed Apr 27, 2021
2 parents e75710d + 2d2303f commit 7065998
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,8 +1100,8 @@ pub mod parsing {
{
input.parse().map(Item::Trait)
} else if lookahead.peek(Token![impl]) {
let allow_const_impl = true;
if let Some(item) = parse_impl(input, allow_const_impl)? {
let allow_verbatim_impl = true;
if let Some(item) = parse_impl(input, allow_verbatim_impl)? {
Ok(Item::Impl(item))
} else {
Ok(Item::Verbatim(verbatim::between(begin, input)))
Expand Down Expand Up @@ -1138,8 +1138,8 @@ pub mod parsing {
} else if lookahead.peek(Token![impl])
|| lookahead.peek(Token![default]) && !ahead.peek2(Token![!])
{
let allow_const_impl = true;
if let Some(item) = parse_impl(input, allow_const_impl)? {
let allow_verbatim_impl = true;
if let Some(item) = parse_impl(input, allow_verbatim_impl)? {
Ok(Item::Impl(item))
} else {
Ok(Item::Verbatim(verbatim::between(begin, input)))
Expand Down Expand Up @@ -2451,12 +2451,12 @@ pub mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for ItemImpl {
fn parse(input: ParseStream) -> Result<Self> {
let allow_const_impl = false;
parse_impl(input, allow_const_impl).map(Option::unwrap)
let allow_verbatim_impl = false;
parse_impl(input, allow_verbatim_impl).map(Option::unwrap)
}
}

fn parse_impl(input: ParseStream, allow_const_impl: bool) -> Result<Option<ItemImpl>> {
fn parse_impl(input: ParseStream, allow_verbatim_impl: bool) -> Result<Option<ItemImpl>> {
let mut attrs = input.call(Attribute::parse_outer)?;
let defaultness: Option<Token![default]> = input.parse()?;
let unsafety: Option<Token![unsafe]> = input.parse()?;
Expand All @@ -2476,7 +2476,7 @@ pub mod parsing {
Generics::default()
};

let is_const_impl = allow_const_impl
let is_const_impl = allow_verbatim_impl
&& (input.peek(Token![const]) || input.peek(Token![?]) && input.peek2(Token![const]));
if is_const_impl {
input.parse::<Option<Token![?]>>()?;
Expand All @@ -2490,6 +2490,8 @@ pub mod parsing {
None
};

#[cfg(not(feature = "printing"))]
let first_ty_span = input.span();
let mut first_ty: Type = input.parse()?;
let self_ty: Type;
let trait_;
Expand All @@ -2510,6 +2512,11 @@ pub mod parsing {
} else {
unreachable!()
}
} else if !allow_verbatim_impl {
#[cfg(feature = "printing")]
return Err(Error::new_spanned(first_ty_ref, "expected trait path"));
#[cfg(not(feature = "printing"))]
return Err(Error::new(first_ty_span, "expected trait path"));
} else {
trait_ = None;
}
Expand Down

0 comments on commit 7065998

Please sign in to comment.