Skip to content
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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ members = [
"crates/standalone/error/cgp-error-std",

"crates/tests/cgp-tests",
"crates/tests/cgp-macro-tests",
]

[workspace.package]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::types::attributes::{
DefaultImplAttribute, DefaultImplAttributes, UseProviderAttribute, UseProviderAttributes,
UseTypeAttribute, UseTypeAttributes, UsesAttributes,
};
use crate::types::ident::IdentWithTypeArgs;
use crate::types::ident::PathWithTypeArgs;

#[derive(Default)]
pub struct CgpImplAttributes {
Expand All @@ -27,7 +27,7 @@ impl CgpImplAttributes {
match ident.to_string().as_ref() {
"uses" => {
let uses = attribute.parse_args_with(
Punctuated::<IdentWithTypeArgs, Comma>::parse_terminated,
Punctuated::<PathWithTypeArgs, Comma>::parse_terminated,
)?;

parsed_attributes.uses.imports.extend(uses);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ use syn::token::In;
use syn::{Generics, ItemImpl, Type};

use crate::parse_internal;
use crate::types::ident::IdentWithTypeArgs;
use crate::types::ident::PathWithTypeArgs;
use crate::types::path::UniPathOrType;

pub struct DefaultImplAttribute {
pub key_type: UniPathOrType,
pub in_token: In,
pub namespace: IdentWithTypeArgs,
pub namespace: PathWithTypeArgs,
}

impl DefaultImplAttribute {
Expand All @@ -23,7 +23,7 @@ impl DefaultImplAttribute {

namespace_trait_path
.type_args
.make_args()
.args
.push(parse_internal!(__Components__));

let mut generics = provider_generics.clone();
Expand Down
9 changes: 4 additions & 5 deletions crates/macros/cgp-macro-core/src/types/attributes/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use syn::{Attribute, GenericParam, TypeParamBound, WherePredicate};
use crate::types::attributes::{
UseProviderAttribute, UseProviderAttributes, UseTypeAttribute, UseTypeAttributes,
};
use crate::types::ident::IdentWithTypeArgs;
use crate::types::ident::PathWithTypeArgs;

#[derive(Default)]
pub struct FunctionAttributes {
pub extend: Vec<TypeParamBound>,
pub extend_where: Vec<WherePredicate>,
pub uses: Vec<IdentWithTypeArgs>,
pub uses: Vec<PathWithTypeArgs>,
pub use_type: UseTypeAttributes,
pub use_provider: UseProviderAttributes,
pub impl_generics: Vec<GenericParam>,
Expand All @@ -35,9 +35,8 @@ impl FunctionAttributes {

parsed_attributes.extend_where.extend(where_predicates);
} else if ident == "uses" {
let uses = attribute.parse_args_with(
Punctuated::<IdentWithTypeArgs, Comma>::parse_terminated,
)?;
let uses = attribute
.parse_args_with(Punctuated::<PathWithTypeArgs, Comma>::parse_terminated)?;

parsed_attributes.uses.extend(uses);
} else if ident == "use_type" {
Expand Down
6 changes: 3 additions & 3 deletions crates/macros/cgp-macro-core/src/types/attributes/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use syn::token::In;

use crate::exports::RedirectLookup;
use crate::functions::parse_internal;
use crate::types::ident::{IdentWithTypeArgs, IdentWithTypeGenerics};
use crate::types::ident::{IdentWithTypeGenerics, PathWithTypeArgs};
use crate::types::path::UniPath;

#[derive(Clone)]
pub struct PrefixAttribute {
pub path: UniPath,
pub _in_token: In,
pub namespace: IdentWithTypeArgs,
pub namespace: PathWithTypeArgs,
}

impl PrefixAttribute {
Expand All @@ -22,7 +22,7 @@ impl PrefixAttribute {
let mut namespace = self.namespace.clone();
namespace
.type_args
.make_args()
.args
.push(parse_internal!(__Components__));

let mut path = self.path.clone();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use syn::token::{Colon, Plus};
use syn::{Type, TypeParamBound, WherePredicate};

use crate::parse_internal;
use crate::types::ident::IdentWithTypeArgs;
use crate::types::ident::PathWithTypeArgs;

pub struct UseProviderAttribute {
pub context_type: Type,
pub provider_type: Type,
pub colon: Colon,
pub provider_trait_bounds: Punctuated<IdentWithTypeArgs, Plus>,
pub provider_trait_bounds: Punctuated<PathWithTypeArgs, Plus>,
}

impl UseProviderAttribute {
Expand All @@ -24,7 +24,7 @@ impl UseProviderAttribute {
let mut bound = bound.clone();
bound
.type_args
.make_args()
.args
.insert(0, parse_internal!(#context_type));

bounds.push(parse_internal!(#bound));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use syn::{Ident, Type, braced};

use crate::parse_internal;
use crate::types::attributes::UseTypeIdent;
use crate::types::ident::IdentWithTypeArgs;
use crate::types::ident::{IdentWithTypeArgs, PathWithTypeArgs};

#[derive(Clone)]
pub struct UseTypeAttribute {
pub context_type: Type,
pub trait_path: IdentWithTypeArgs,
pub trait_path: PathWithTypeArgs,
pub type_idents: Vec<UseTypeIdent>,
}

Expand All @@ -34,6 +34,12 @@ impl Parse for UseTypeAttribute {
let (context_type, body) = if input.peek(At) {
let _: At = input.parse()?;

// The context type is followed by a `::`-separated trait path, so it
// must parse only a single identifier head. This is the one place
// that deliberately keeps `IdentWithTypeArgs` rather than the
// otherwise-dominant `PathWithTypeArgs`: a path parser is greedy
// across `::` and would silently consume the trailing `::Trait::Type`
// here, with no parse error. Do NOT swap this for `PathWithTypeArgs`.
let context_type: Type = input.parse::<IdentWithTypeArgs>()?.into();

let _: Colon = input.parse()?;
Expand All @@ -51,7 +57,7 @@ impl Parse for UseTypeAttribute {

let trait_path = if body.peek(Lt) {
let _: Lt = body.parse()?;
let trait_path: IdentWithTypeArgs = body.parse()?;
let trait_path: PathWithTypeArgs = body.parse()?;
let _: Gt = body.parse()?;
trait_path
} else {
Expand Down
4 changes: 2 additions & 2 deletions crates/macros/cgp-macro-core/src/types/attributes/uses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ use syn::token::Plus;

use crate::parse_internal;
use crate::traits::ToTypeParamBounds;
use crate::types::ident::IdentWithTypeArgs;
use crate::types::ident::PathWithTypeArgs;

#[derive(Default)]
pub struct UsesAttributes {
pub imports: Vec<IdentWithTypeArgs>,
pub imports: Vec<PathWithTypeArgs>,
}

impl ToTypeParamBounds for UsesAttributes {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl PreprocessedCgpComponent {
let component_name = &self.args.component_name;
EmptyStruct {
ident: component_name.ident.clone(),
generics: component_name.type_generics.generics.clone(),
generics: component_name.type_generics.to_generics(),
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/macros/cgp-macro-core/src/types/cgp_impl/lowered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use syn::{Error, Ident, ImplItem, ItemImpl, Type};
use crate::functions::{parse_internal, to_snake_case_ident};
use crate::types::cgp_impl::{CgpProviderOrBareImpl, ImplArgs};
use crate::types::cgp_provider::{ItemCgpProvider, ProviderArgs};
use crate::types::ident::IdentWithTypeArgs;
use crate::types::ident::PathWithTypeArgs;
use crate::visitors::{
ReplaceSelfReceiverVisitor, ReplaceSelfTypeVisitor, ReplaceSelfValueVisitor,
};
Expand All @@ -17,7 +17,7 @@ pub struct LoweredCgpImpl {
pub args: ImplArgs,
pub item_impl: ItemImpl,
pub context_type: Type,
pub provider_trait_path: IdentWithTypeArgs,
pub provider_trait_path: PathWithTypeArgs,
pub default_impls: Vec<ItemImpl>,
}

Expand Down Expand Up @@ -82,7 +82,7 @@ impl LoweredCgpImpl {

provider_trait_path
.type_args
.make_args()
.args
.insert(0, parse_internal!(#context_type));

out_impl.trait_ = Some((
Expand Down
8 changes: 4 additions & 4 deletions crates/macros/cgp-macro-core/src/types/cgp_provider/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use syn::{Error, Ident, ItemImpl, Type};
use crate::functions::parse_internal;
use crate::types::cgp_provider::{LoweredCgpProvider, ProviderArgs};
use crate::types::empty_struct::EmptyStruct;
use crate::types::ident::{IdentWithTypeArgs, IdentWithTypeGenerics};
use crate::types::ident::{IdentWithTypeGenerics, PathWithTypeArgs};
use crate::types::provider_impl::ItemProviderImpl;

pub struct ItemCgpProvider {
Expand Down Expand Up @@ -37,11 +37,11 @@ impl ItemCgpProvider {
Error::new(item_impl.span(), "expect provider trait name to be present")
})?;

let provider_trait: IdentWithTypeArgs =
let provider_trait: PathWithTypeArgs =
parse_internal(provider_trait_path.to_token_stream())?;

let component_ident = Ident::new(
&format!("{}Component", provider_trait.ident),
&format!("{}Component", provider_trait.ident()),
provider_trait.span(),
);

Expand All @@ -61,7 +61,7 @@ impl ItemCgpProvider {

let provider_struct = EmptyStruct {
ident: provider_type.ident.clone(),
generics: provider_type.type_generics.generics.clone(),
generics: provider_type.type_generics.to_generics(),
};

Ok(Some(provider_struct))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ use quote::{ToTokens, quote};
use syn::punctuated::Punctuated;
use syn::spanned::Spanned;
use syn::token::Comma;
use syn::{Error, GenericArgument, Lifetime, Type};
use syn::{Error, Lifetime, Type};

use crate::types::generics::GenericArguments;
use crate::types::ident::{TypeArg, TypeArgs};

pub struct ProviderImplArgs {
pub context_type: Type,
Expand All @@ -18,30 +18,28 @@ pub enum ProviderImplArg {
}

impl ProviderImplArgs {
pub fn from_generic_args(generic_args: &GenericArguments) -> syn::Result<Self> {
pub fn from_generic_args(generic_args: &TypeArgs) -> syn::Result<Self> {
let mut impl_args: Punctuated<ProviderImplArg, Comma> = Punctuated::new();
let mut context_type: Option<Type> = None;

if let Some(args) = &generic_args.args {
for arg in &args.args {
match arg {
GenericArgument::Lifetime(life) => {
impl_args.push(ProviderImplArg::Life(life.clone()));
}
GenericArgument::Type(ty) => {
if context_type.is_none() {
context_type = Some(ty.clone());
} else {
impl_args.push(ProviderImplArg::Type(ty.clone()));
}
}
_ => {
return Err(Error::new(
arg.span(),
format!("unsupported type argument: {:?}", arg),
));
for arg in &generic_args.args {
match arg {
TypeArg::Lifetime(life) => {
impl_args.push(ProviderImplArg::Life(life.clone()));
}
TypeArg::Type(ty) => {
if context_type.is_none() {
context_type = Some(ty.clone());
} else {
impl_args.push(ProviderImplArg::Type(ty.clone()));
}
}
TypeArg::Const(expr) => {
return Err(Error::new(
expr.span(),
"const arguments are not supported in provider impl trait arguments",
));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use syn::{Generics, Ident, Type};

use crate::parse_internal;
use crate::types::delegate_component::{EvalDelegateEntry, EvaluatedDelegateEntry};
use crate::types::ident::IdentWithTypeArgs;
use crate::types::ident::PathWithTypeArgs;

pub trait EvalForEntries {
fn eval_for_entries(&self, table_type: &Type) -> syn::Result<Vec<EvaluatedForEntry>>;
Expand All @@ -17,7 +17,7 @@ pub struct EvaluatedForEntry {
pub table_type: Type,
pub for_key: Ident,
pub for_value: Ident,
pub namespace: IdentWithTypeArgs,
pub namespace: PathWithTypeArgs,
pub mapping_key: Type,
pub mapping_value: Type,
}
Expand Down Expand Up @@ -47,17 +47,18 @@ impl EvalDelegateEntry for EvaluatedForEntry {
let table_type = &self.table_type;

let namespace_trait: Type = {
let namespace_ident = &self.namespace.ident;
let mut namespace_generics = self.namespace.type_args.clone();
let namespace_generic_args = &mut namespace_generics.make_args();

namespace_generic_args.push(parse_internal!(#table_type));

namespace_generic_args.push(parse_internal! {
Delegate = #mapping_value
});

parse_internal!( #namespace_ident #namespace_generics )
// The namespace argument list is extended with the table type and a
// `Delegate = ..` associated binding. The binding cannot live inside
// a `TypeArgs` (which faithfully rejects associated bindings), so the
// trait bound is reconstructed directly from the parsed path and its
// existing arguments.
let namespace_path = &self.namespace.path;

let existing_args = self.namespace.type_args.args.iter();

parse_internal! {
#namespace_path < #( #existing_args, )* #table_type, Delegate = #mapping_value >
}
};

let mut generics = self.generics.clone();
Expand Down
Loading
Loading