Skip to content

Commit

Permalink
Merge pull request #1083 from dtolnay/tildeconst
Browse files Browse the repository at this point in the history
Parse ~const in where-clause
  • Loading branch information
dtolnay committed Oct 6, 2021
2 parents a7ad301 + f2dbe27 commit 92173e4
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 8 deletions.
45 changes: 44 additions & 1 deletion src/generics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,15 @@ pub mod parsing {
#[cfg_attr(doc_cfg, doc(cfg(feature = "parsing")))]
impl Parse for TraitBound {
fn parse(input: ParseStream) -> Result<Self> {
#[cfg(feature = "full")]
let tilde_const = if input.peek(Token![~]) && input.peek2(Token![const]) {
let tilde_token = input.parse::<Token![~]>()?;
let const_token = input.parse::<Token![const]>()?;
Some((tilde_token, const_token))
} else {
None
};

let modifier: TraitBoundModifier = input.parse()?;
let lifetimes: Option<BoundLifetimes> = input.parse()?;

Expand All @@ -840,6 +849,21 @@ pub mod parsing {
path.segments.last_mut().unwrap().arguments = parenthesized;
}

#[cfg(feature = "full")]
{
if let Some((tilde_token, const_token)) = tilde_const {
path.segments.insert(
0,
PathSegment {
ident: Ident::new("const", const_token.span),
arguments: PathArguments::None,
},
);
let (_const, punct) = path.segments.pairs_mut().next().unwrap().into_tuple();
*punct.unwrap() = Token![::](tilde_token.span);
}
}

Ok(TraitBound {
paren_token: None,
modifier,
Expand Down Expand Up @@ -994,6 +1018,8 @@ mod printing {
use super::*;
use crate::attr::FilterAttrs;
use crate::print::TokensOrDefault;
#[cfg(feature = "full")]
use crate::punctuated::Pair;
use proc_macro2::TokenStream;
#[cfg(feature = "full")]
use proc_macro2::TokenTree;
Expand Down Expand Up @@ -1214,9 +1240,26 @@ mod printing {
impl ToTokens for TraitBound {
fn to_tokens(&self, tokens: &mut TokenStream) {
let to_tokens = |tokens: &mut TokenStream| {
#[cfg(feature = "full")]
let skip = match self.path.segments.pairs().next() {
Some(Pair::Punctuated(t, p)) if t.ident == "const" => {
Token![~](p.spans[0]).to_tokens(tokens);
t.to_tokens(tokens);
1
}
_ => 0,
};
self.modifier.to_tokens(tokens);
self.lifetimes.to_tokens(tokens);
self.path.to_tokens(tokens);
#[cfg(feature = "full")]
{
self.path.leading_colon.to_tokens(tokens);
tokens.append_all(self.path.segments.pairs().skip(skip));
}
#[cfg(not(feature = "full"))]
{
self.path.to_tokens(tokens);
}
};
match &self.paren_token {
Some(paren) => paren.surround(tokens, to_tokens),
Expand Down
7 changes: 0 additions & 7 deletions tests/repo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,6 @@ static EXCLUDE: &[&str] = &[
// https://github.com/dtolnay/syn/issues/1051
"src/test/ui/rfc-2632-const-trait-impl/syntax.rs",

// TODO: ~const in where-clause
// https://github.com/dtolnay/syn/issues/1051
"library/alloc/src/borrow.rs",
"src/test/ui/rfc-2632-const-trait-impl/inherent-impl-const-bounds.rs",
"src/test/ui/rfc-2632-const-trait-impl/trait-where-clause-run.rs",
"src/test/ui/rfc-2632-const-trait-impl/trait-where-clause-self-referential.rs",

// Compile-fail expr parameter in const generic position: f::<1 + 2>()
"src/test/ui/const-generics/early/closing-args-token.rs",
"src/test/ui/const-generics/early/const-expression-parameter.rs",
Expand Down

0 comments on commit 92173e4

Please sign in to comment.