Skip to content

Commit

Permalink
Merge pull request #1722 from dtolnay/exprpath
Browse files Browse the repository at this point in the history
Insert turbofish into paths in expression position
  • Loading branch information
dtolnay committed Aug 17, 2024
2 parents 5dbfeae + e459ee7 commit b693682
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 68 deletions.
39 changes: 35 additions & 4 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ ast_enum! {
}
}

ast_enum_of_structs! {
ast_enum! {
/// Content of a compile-time structured attribute.
///
/// ## Path
Expand Down Expand Up @@ -625,6 +625,24 @@ impl<'a> FilterAttrs<'a> for &'a [Attribute] {
}
}

impl From<Path> for Meta {
fn from(meta: Path) -> Meta {
Meta::Path(meta)
}
}

impl From<MetaList> for Meta {
fn from(meta: MetaList) -> Meta {
Meta::List(meta)
}
}

impl From<MetaNameValue> for Meta {
fn from(meta: MetaNameValue) -> Meta {
Meta::NameValue(meta)
}
}

#[cfg(feature = "parsing")]
pub(crate) mod parsing {
use crate::attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue};
Expand Down Expand Up @@ -757,7 +775,9 @@ pub(crate) mod parsing {

#[cfg(feature = "printing")]
mod printing {
use crate::attr::{AttrStyle, Attribute, MetaList, MetaNameValue};
use crate::attr::{AttrStyle, Attribute, Meta, MetaList, MetaNameValue};
use crate::path;
use crate::path::printing::PathStyle;
use proc_macro2::TokenStream;
use quote::ToTokens;

Expand All @@ -774,18 +794,29 @@ mod printing {
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for Meta {
fn to_tokens(&self, tokens: &mut TokenStream) {
match self {
Meta::Path(path) => path::printing::print_path(tokens, path, PathStyle::Mod),
Meta::List(meta_list) => meta_list.to_tokens(tokens),
Meta::NameValue(meta_name_value) => meta_name_value.to_tokens(tokens),
}
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for MetaList {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.path.to_tokens(tokens);
path::printing::print_path(tokens, &self.path, PathStyle::Mod);
self.delimiter.surround(tokens, self.tokens.clone());
}
}

#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for MetaNameValue {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.path.to_tokens(tokens);
path::printing::print_path(tokens, &self.path, PathStyle::Mod);
self.eq_token.to_tokens(tokens);
self.value.to_tokens(tokens);
}
Expand Down
13 changes: 10 additions & 3 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3013,6 +3013,7 @@ pub(crate) mod printing {
use crate::fixup::FixupContext;
use crate::op::BinOp;
use crate::path;
use crate::path::printing::PathStyle;
use crate::precedence::Precedence;
use crate::token;
#[cfg(feature = "full")]
Expand Down Expand Up @@ -3626,7 +3627,13 @@ pub(crate) mod printing {
);
e.dot_token.to_tokens(tokens);
e.method.to_tokens(tokens);
e.turbofish.to_tokens(tokens);
if let Some(turbofish) = &e.turbofish {
path::printing::print_angle_bracketed_generic_arguments(
tokens,
turbofish,
PathStyle::Expr,
);
}
e.paren_token.surround(tokens, |tokens| {
e.args.to_tokens(tokens);
});
Expand All @@ -3646,7 +3653,7 @@ pub(crate) mod printing {
impl ToTokens for ExprPath {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
path::printing::print_path(tokens, &self.qself, &self.path);
path::printing::print_qpath(tokens, &self.qself, &self.path, PathStyle::Expr);
}
}

Expand Down Expand Up @@ -3733,7 +3740,7 @@ pub(crate) mod printing {
impl ToTokens for ExprStruct {
fn to_tokens(&self, tokens: &mut TokenStream) {
outer_attrs_to_tokens(&self.attrs, tokens);
path::printing::print_path(tokens, &self.qself, &self.path);
path::printing::print_qpath(tokens, &self.qself, &self.path, PathStyle::Expr);
self.brace_token.surround(tokens, |tokens| {
self.fields.to_tokens(tokens);
if let Some(dot2_token) = &self.dot2_token {
Expand Down
4 changes: 3 additions & 1 deletion src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2894,6 +2894,8 @@ mod printing {
UsePath, UseRename, Variadic,
};
use crate::mac::MacroDelimiter;
use crate::path;
use crate::path::printing::PathStyle;
use crate::print::TokensOrDefault;
use crate::ty::Type;
use proc_macro2::TokenStream;
Expand Down Expand Up @@ -3135,7 +3137,7 @@ mod printing {
impl ToTokens for ItemMacro {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(self.attrs.outer());
self.mac.path.to_tokens(tokens);
path::printing::print_path(tokens, &self.mac.path, PathStyle::Mod);
self.mac.bang_token.to_tokens(tokens);
self.ident.to_tokens(tokens);
match &self.mac.delimiter {
Expand Down
4 changes: 3 additions & 1 deletion src/mac.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,8 @@ pub(crate) mod parsing {
#[cfg(feature = "printing")]
mod printing {
use crate::mac::{Macro, MacroDelimiter};
use crate::path;
use crate::path::printing::PathStyle;
use crate::token;
use proc_macro2::{Delimiter, TokenStream};
use quote::ToTokens;
Expand All @@ -215,7 +217,7 @@ mod printing {
#[cfg_attr(docsrs, doc(cfg(feature = "printing")))]
impl ToTokens for Macro {
fn to_tokens(&self, tokens: &mut TokenStream) {
self.path.to_tokens(tokens);
path::printing::print_path(tokens, &self.path, PathStyle::Mod);
self.bang_token.to_tokens(tokens);
self.delimiter.surround(tokens, self.tokens.clone());
}
Expand Down
5 changes: 3 additions & 2 deletions src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ mod printing {
PatTuple, PatTupleStruct, PatType, PatWild,
};
use crate::path;
use crate::path::printing::PathStyle;
use proc_macro2::TokenStream;
use quote::{ToTokens, TokenStreamExt};

Expand Down Expand Up @@ -880,7 +881,7 @@ mod printing {
impl ToTokens for PatStruct {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(self.attrs.outer());
path::printing::print_path(tokens, &self.qself, &self.path);
path::printing::print_qpath(tokens, &self.qself, &self.path, PathStyle::Expr);
self.brace_token.surround(tokens, |tokens| {
self.fields.to_tokens(tokens);
// NOTE: We need a comma before the dot2 token if it is present.
Expand Down Expand Up @@ -915,7 +916,7 @@ mod printing {
impl ToTokens for PatTupleStruct {
fn to_tokens(&self, tokens: &mut TokenStream) {
tokens.append_all(self.attrs.outer());
path::printing::print_path(tokens, &self.qself, &self.path);
path::printing::print_qpath(tokens, &self.qself, &self.path, PathStyle::Expr);
self.paren_token.surround(tokens, |tokens| {
self.elems.to_tokens(tokens);
});
Expand Down
Loading

0 comments on commit b693682

Please sign in to comment.