Skip to content

Commit

Permalink
Merge pull request #48 from steffahn/fix_type_annotations_needed
Browse files Browse the repository at this point in the history
Add explicit type arguments to `BorrowedFields` constructor in `generate_checker_summoner`-generated code.
  • Loading branch information
someguynamedjosh authored Oct 19, 2021
2 parents 20d8de8 + 884c1e4 commit 8e26b9b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 6 deletions.
8 changes: 8 additions & 0 deletions examples/src/ok_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ where
data4: &'this mut C,
}

/// Regression test for #46
#[self_referencing]
struct PreviouslyBrokeAutoGeneratedChecker<T: 'static> {
x: T,
#[borrows(mut x)]
y: &'this (),
}

#[test]
fn box_and_ref() {
let bar = BoxAndRefBuilder {
Expand Down
3 changes: 2 additions & 1 deletion ouroboros_macro/src/generate/summon_checker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,15 @@ pub fn generate_checker_summoner(info: &StructInfo) -> Result<TokenStream, Error
}
let generic_params = info.generic_params();
let where_clause = &info.generics.where_clause;
let borrowed_generic_params_inferred = info.borrowed_generic_params_inferred();
Ok(quote! {
fn check_if_okay_according_to_checkers<#generic_params>(
#(#params,)*
)
#where_clause
{
#(#code;)*
BorrowedFields {
BorrowedFields::#borrowed_generic_params_inferred {
#(#value_consumers,)*
#(#template_consumers,)*
};
Expand Down
16 changes: 14 additions & 2 deletions ouroboros_macro/src/info_structures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::utils::{make_generic_arguments, make_generic_consumers, replace_this_
use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, ToTokens};
use syn::{
punctuated::Punctuated, token::Comma, Attribute, Error, GenericParam, Generics, Type,
Visibility,
punctuated::Punctuated, token::Comma, Attribute, ConstParam, Error, GenericParam, Generics,
LifetimeDef, Type, TypeParam, Visibility,
};

#[derive(Clone, Copy)]
Expand Down Expand Up @@ -75,6 +75,18 @@ impl StructInfo {
}
}

/// Same as generic_params but without bounds and with '_ prepended twice.
pub fn borrowed_generic_params_inferred(&self) -> TokenStream {
use GenericParam::*;
let params = self.generic_params().iter().map(|p| match p {
Type(TypeParam { ident, .. }) | Const(ConstParam { ident, .. }) => {
ident.to_token_stream()
}
Lifetime(LifetimeDef { lifetime, .. }) => lifetime.to_token_stream(),
});
quote! { <'_, '_, #(#params,)*> }
}

pub fn generic_arguments(&self) -> Vec<TokenStream> {
make_generic_arguments(&self.generics)
}
Expand Down
4 changes: 1 addition & 3 deletions ouroboros_macro/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use quote::{format_ident, quote};
use syn::{GenericParam, Generics, Visibility};

/// Makes phantom data definitions so that we don't get unused template parameter errors.
pub fn make_generic_consumers(
generics: &Generics,
) -> impl Iterator<Item = (TokenStream, Ident)> {
pub fn make_generic_consumers(generics: &Generics) -> impl Iterator<Item = (TokenStream, Ident)> {
generics
.params
.clone()
Expand Down

0 comments on commit 8e26b9b

Please sign in to comment.