Skip to content

Commit

Permalink
Merge pull request #85 from auto-impl-rs/fix/const-params
Browse files Browse the repository at this point in the history
Include const params when building generics
  • Loading branch information
KodrAus authored Jun 2, 2022
2 parents c6bd3af + 94df3d0 commit 4a6928d
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ proc-macro-error = "1.0.0"

[dev-dependencies]
trybuild = "1"
rustversion = "1"
2 changes: 1 addition & 1 deletion examples/async_await/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::time::Duration;
use async_std::task;
use std::time::Duration;

use async_trait::async_trait;
use auto_impl::auto_impl;
Expand Down
31 changes: 23 additions & 8 deletions src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
use proc_macro2::{Span as Span2, TokenStream as TokenStream2};
use proc_macro_error::{abort, emit_error};
use quote::{ToTokens, TokenStreamExt};
use syn::{spanned::Spanned, Attribute, FnArg, Ident, ItemTrait, Lifetime, Pat, PatIdent, ReturnType, Signature, TraitBound, TraitBoundModifier, TraitItem, TraitItemConst, TraitItemMethod, TraitItemType, Type, TypeParamBound, WherePredicate, PatType, Token};
use syn::punctuated::Punctuated;
use syn::{
punctuated::Punctuated, spanned::Spanned, Attribute, FnArg, GenericParam, Ident, ItemTrait,
Lifetime, Pat, PatIdent, PatType, ReturnType, Signature, Token, TraitBound, TraitBoundModifier,
TraitItem, TraitItemConst, TraitItemMethod, TraitItemType, Type, TypeParamBound,
WherePredicate,
};

use crate::{
analyze::find_suitable_param_names,
Expand Down Expand Up @@ -602,7 +606,7 @@ fn gen_method_item(
item.sig.ident,
);
Err(())
}
};
}

// Determine the kind of the method, determined by the self type.
Expand Down Expand Up @@ -656,12 +660,21 @@ fn gen_method_item(
// so we can wait a bit still.
let generic_types = sig
.generics
.type_params()
.map(|param| {
let name = &param.ident;
quote! { #name , }
.params
.iter()
.filter_map(|param| match param {
GenericParam::Type(param) => {
let name = &param.ident;
Some(quote! { #name , })
}
GenericParam::Const(param) => {
let name = &param.ident;
Some(quote! { #name , })
}
GenericParam::Lifetime(_) => None,
})
.collect::<TokenStream2>();

let generic_types = if generic_types.is_empty() {
generic_types
} else {
Expand Down Expand Up @@ -801,7 +814,9 @@ fn check_receiver_compatible(
/// Generates a list of comma-separated arguments used to call the function.
/// Currently, only simple names are valid and more complex pattern will lead
/// to an error being emitted. `self` parameters are ignored.
fn get_arg_list<'a>(original_inputs: impl Iterator<Item = &'a FnArg>) -> (Punctuated<FnArg, Token![,]>, TokenStream2) {
fn get_arg_list<'a>(
original_inputs: impl Iterator<Item = &'a FnArg>,
) -> (Punctuated<FnArg, Token![,]>, TokenStream2) {
let mut args = TokenStream2::new();
let mut inputs = Punctuated::new();

Expand Down
10 changes: 10 additions & 0 deletions tests/recent/compile-pass/generic_const_method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use auto_impl::auto_impl;


#[auto_impl(&)]
trait Foo {
fn foo<const I: i32>(&self);
}


fn main() {}
9 changes: 9 additions & 0 deletions tests/recent/compile-pass/generic_mix.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use auto_impl::auto_impl;

#[auto_impl(&, &mut)]
trait MyTrait<'a, T> {
fn execute<'b, U, const N: usize>(&'a self, arg1: &'b T, arg2: &'static str, arg3: U) -> Result<(), String>;
}


fn main() {}
7 changes: 7 additions & 0 deletions tests/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,10 @@ fn ui_compile_fail() {
let t = TestCases::new();
t.compile_fail("tests/compile-fail/*.rs");
}

#[rustversion::since(1.51)]
#[test]
fn ui_recent_compile_pass() {
let t = TestCases::new();
t.pass("tests/recent/compile-pass/*.rs");
}

0 comments on commit 4a6928d

Please sign in to comment.