Skip to content

Commit

Permalink
Merge pull request #1748 from dtolnay/impluse
Browse files Browse the repository at this point in the history
Reject trait bound containing only precise capture
  • Loading branch information
dtolnay authored Oct 18, 2024
2 parents 6afa190 + 81f0899 commit da478ac
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ pub(crate) mod parsing {
TypeReference, TypeSlice, TypeTraitObject, TypeTuple,
};
use crate::verbatim;
use proc_macro2::Span;
use proc_macro2::{Span, TokenTree};

#[cfg_attr(docsrs, doc(cfg(feature = "parsing")))]
impl Parse for Type {
Expand Down Expand Up @@ -859,13 +859,14 @@ pub(crate) mod parsing {
let mut at_least_one_trait = false;
for bound in &bounds {
match bound {
TypeParamBound::Trait(_) | TypeParamBound::Verbatim(_) => {
TypeParamBound::Trait(_) => {
at_least_one_trait = true;
break;
}
TypeParamBound::Lifetime(lifetime) => {
last_lifetime_span = Some(lifetime.ident.span());
}
TypeParamBound::Verbatim(_) => unreachable!(),
}
}
// Just lifetimes like `'a + 'b` is not a TraitObject.
Expand Down Expand Up @@ -902,24 +903,37 @@ pub(crate) mod parsing {
allow_precise_capture,
allow_tilde_const,
)?;
let mut last_lifetime_span = None;
let mut last_nontrait_span = None;
let mut at_least_one_trait = false;
for bound in &bounds {
match bound {
TypeParamBound::Trait(_) | TypeParamBound::Verbatim(_) => {
TypeParamBound::Trait(_) => {
at_least_one_trait = true;
break;
}
TypeParamBound::Lifetime(lifetime) => {
last_lifetime_span = Some(lifetime.ident.span());
last_nontrait_span = Some(lifetime.ident.span());
}
TypeParamBound::Verbatim(verbatim) => {
let mut tokens = verbatim.clone().into_iter();
match tokens.next().unwrap() {
TokenTree::Ident(ident) if ident == "use" => {
last_nontrait_span = Some(tokens.last().unwrap().span());
}
_ => {
// ~const Trait
at_least_one_trait = true;
break;
}
}
}
}
}
if !at_least_one_trait {
let msg = "at least one trait must be specified";
return Err(error::new2(
impl_token.span,
last_lifetime_span.unwrap(),
last_nontrait_span.unwrap(),
msg,
));
}
Expand Down

0 comments on commit da478ac

Please sign in to comment.