Skip to content

Commit ee104e6

Browse files
committed
Remove lifetime from impl blocks; add to fns.
Previously we parameterized some impl blocks by <'a>. Instead, parameterize each function individually.
1 parent 356c442 commit ee104e6

File tree

5 files changed

+22
-76
lines changed

5 files changed

+22
-76
lines changed

engine/src/conversion/api.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -719,10 +719,3 @@ impl<T: AnalysisPhase> Api<T> {
719719
Ok(Box::new(std::iter::once(Api::Enum { name, item })))
720720
}
721721
}
722-
723-
/// Whether a type is a pointer of some kind.
724-
pub(crate) enum Pointerness {
725-
Not,
726-
ConstPtr,
727-
MutPtr,
728-
}

engine/src/conversion/codegen_cpp/function_wrapper_cpp.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ use syn::{Type, TypePtr};
1010

1111
use crate::conversion::{
1212
analysis::fun::function_wrapper::{CppConversionType, TypeConversionPolicy},
13-
api::Pointerness,
1413
ConvertErrorFromCpp,
1514
};
1615

@@ -63,17 +62,6 @@ impl TypeConversionPolicy {
6362
cpp_name_map.type_to_cpp(self.cxxbridge_type())
6463
}
6564

66-
pub(crate) fn is_a_pointer(&self) -> Pointerness {
67-
match self.cxxbridge_type() {
68-
Type::Ptr(TypePtr {
69-
mutability: Some(_),
70-
..
71-
}) => Pointerness::MutPtr,
72-
Type::Ptr(_) => Pointerness::ConstPtr,
73-
_ => Pointerness::Not,
74-
}
75-
}
76-
7765
fn unique_ptr_wrapped_type(
7866
&self,
7967
original_name_map: &CppNameMap,

engine/src/conversion/codegen_rs/fun_codegen.rs

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
// option. This file may not be copied, modified, or distributed
77
// except according to those terms.
88

9-
use autocxx_parser::IncludeCppConfig;
109
use indexmap::set::IndexSet as HashSet;
1110
use std::borrow::Cow;
1211

@@ -24,15 +23,15 @@ use super::{
2423
function_wrapper_rs::RustParamConversion,
2524
maybe_unsafes_to_tokens,
2625
unqualify::{unqualify_params, unqualify_ret_type},
27-
ImplBlockDetails, ImplBlockKey, MaybeUnsafeStmt, RsCodegenResult, TraitImplBlockDetails, Use,
26+
ImplBlockDetails, MaybeUnsafeStmt, RsCodegenResult, TraitImplBlockDetails, Use,
2827
};
2928
use crate::{
3029
conversion::{
3130
analysis::fun::{
3231
function_wrapper::TypeConversionPolicy, ArgumentAnalysis, FnAnalysis, FnKind,
3332
MethodKind, RustRenameStrategy, TraitMethodDetails,
3433
},
35-
api::{Pointerness, UnsafetyNeeded},
34+
api::UnsafetyNeeded,
3635
},
3736
minisyn::minisynize_vec,
3837
types::{Namespace, QualifiedName},
@@ -91,13 +90,13 @@ pub(super) fn gen_function(
9190
analysis: FnAnalysis,
9291
cpp_call_name: String,
9392
non_pod_types: &HashSet<QualifiedName>,
94-
config: &IncludeCppConfig,
9593
) -> RsCodegenResult {
9694
if analysis.ignore_reason.is_err() || !analysis.externally_callable {
9795
return RsCodegenResult::default();
9896
}
9997
let cxxbridge_name = analysis.cxxbridge_name;
10098
let rust_name = &analysis.rust_name;
99+
eprintln!("GENERATING {rust_name}");
101100
let ret_type = analysis.ret_type;
102101
let ret_conversion = analysis.ret_conversion;
103102
let param_details = analysis.param_details;
@@ -125,7 +124,6 @@ pub(super) fn gen_function(
125124
non_pod_types,
126125
ret_type: &ret_type,
127126
ret_conversion: &ret_conversion,
128-
reference_wrappers: config.unsafe_policy.requires_cpprefs(),
129127
};
130128
// In rare occasions, we might need to give an explicit lifetime.
131129
let (lifetime_tokens, params, ret_type) = add_explicit_lifetime_if_necessary(
@@ -239,7 +237,6 @@ struct FnGenerator<'a> {
239237
always_unsafe_due_to_trait_definition: bool,
240238
doc_attrs: &'a Vec<Attribute>,
241239
non_pod_types: &'a HashSet<QualifiedName>,
242-
reference_wrappers: bool,
243240
}
244241

245242
impl<'a> FnGenerator<'a> {
@@ -396,39 +393,15 @@ impl<'a> FnGenerator<'a> {
396393
let rust_name = make_ident(self.rust_name);
397394
let unsafety = self.unsafety.wrapper_token();
398395
let doc_attrs = self.doc_attrs;
399-
let receiver_pointerness = self
400-
.param_details
401-
.iter()
402-
.next()
403-
.map(|pd| pd.conversion.is_a_pointer())
404-
.unwrap_or(Pointerness::Not);
405396
let ty = impl_block_type_name.get_final_ident();
406-
let ty = match receiver_pointerness {
407-
Pointerness::MutPtr if self.reference_wrappers => ImplBlockKey {
408-
ty: parse_quote! {
409-
#ty
410-
},
411-
lifetime: Some(parse_quote! { 'a }),
412-
},
413-
Pointerness::ConstPtr if self.reference_wrappers => ImplBlockKey {
414-
ty: parse_quote! {
415-
#ty
416-
},
417-
lifetime: Some(parse_quote! { 'a }),
418-
},
419-
_ => ImplBlockKey {
420-
ty: parse_quote! { # ty },
421-
lifetime: None,
422-
},
423-
};
424397
Box::new(ImplBlockDetails {
425398
item: ImplItem::Fn(parse_quote! {
426399
#(#doc_attrs)*
427400
pub #unsafety fn #rust_name #lifetime_tokens ( #wrapper_params ) #ret_type {
428401
#call_body
429402
}
430403
}),
431-
ty,
404+
ty: parse_quote! { # ty },
432405
})
433406
}
434407

@@ -471,7 +444,7 @@ impl<'a> FnGenerator<'a> {
471444
};
472445
Box::new(ImplBlockDetails {
473446
item: ImplItem::Fn(parse_quote! { #stuff }),
474-
ty: ImplBlockKey { ty, lifetime: None },
447+
ty,
475448
})
476449
}
477450

engine/src/conversion/codegen_rs/lifetime.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use syn::{
3232
/// built-in type
3333
/// 3) Any parameter is any form of reference, and we're returning an `impl New`
3434
/// 3a) an 'impl ValueParam' counts as a reference.
35-
/// 4) If we're using CppRef<'a, T> as a return type
35+
/// 4) If we're using CppRef<'a, T> as a param or return type
3636
pub(crate) fn add_explicit_lifetime_if_necessary<'r>(
3737
param_details: &[ArgumentAnalysis],
3838
mut params: Punctuated<FnArg, Comma>,
@@ -56,6 +56,13 @@ pub(crate) fn add_explicit_lifetime_if_necessary<'r>(
5656
RustConversionType::FromValueParamToPtr
5757
)
5858
});
59+
60+
let any_param_is_cppref = param_details.iter().any(|pd| {
61+
matches!(
62+
pd.conversion.rust_conversion,
63+
RustConversionType::FromReferenceWrapperToPointer
64+
)
65+
});
5966
let return_type_is_impl = return_type_is_impl(&ret_type);
6067
let return_type_is_cppref = matches!(
6168
ret_conversion,
@@ -71,7 +78,8 @@ pub(crate) fn add_explicit_lifetime_if_necessary<'r>(
7178
if !(has_mutable_receiver
7279
|| hits_1024_bug
7380
|| returning_impl_with_a_reference_param
74-
|| return_type_is_cppref)
81+
|| return_type_is_cppref
82+
|| any_param_is_cppref)
7583
{
7684
return (None, params, ret_type);
7785
}
@@ -97,18 +105,15 @@ pub(crate) fn add_explicit_lifetime_if_necessary<'r>(
97105
#rarrow #old_tyit + 'a
98106
})
99107
}
100-
Type::Ptr(_) if return_type_is_cppref => {
101-
// The ptr will be converted to CppRef<'a, T> elsewhere, so we
102-
// just need to return the return type as-is such that the
103-
// next match statement adds <'a> to the function.
104-
Some(ret_type.clone().into_owned())
105-
}
106108
_ => None,
107109
},
108110
_ => None,
109111
};
110112

111113
match new_return_type {
114+
None if return_type_is_cppref || any_param_is_cppref => {
115+
(Some(quote! { <'a> }), params, ret_type)
116+
}
112117
None => (None, params, ret_type),
113118
Some(new_return_type) => {
114119
for FnArg::Typed(PatType { ty, .. }) | FnArg::Receiver(syn::Receiver { ty, .. }) in

engine/src/conversion/codegen_rs/mod.rs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ use itertools::Itertools;
2323
use proc_macro2::{Span, TokenStream};
2424
use syn::{
2525
parse_quote, punctuated::Punctuated, token::Comma, Attribute, Expr, FnArg, ForeignItem,
26-
ForeignItemFn, Ident, ImplItem, Item, ItemForeignMod, ItemMod, Lifetime, TraitItem, Type,
27-
TypePath,
26+
ForeignItemFn, Ident, ImplItem, Item, ItemForeignMod, ItemMod, TraitItem, Type, TypePath,
2827
};
2928

3029
use crate::{
@@ -59,16 +58,10 @@ use super::{
5958
use super::{convert_error::ErrorContext, ConvertErrorFromCpp};
6059
use quote::quote;
6160

62-
#[derive(Clone, Hash, PartialEq, Eq)]
63-
struct ImplBlockKey {
64-
ty: Type,
65-
lifetime: Option<Lifetime>,
66-
}
67-
6861
/// An entry which needs to go into an `impl` block for a given type.
6962
struct ImplBlockDetails {
7063
item: ImplItem,
71-
ty: ImplBlockKey,
64+
ty: Type,
7265
}
7366

7467
struct TraitImplBlockDetails {
@@ -412,10 +405,8 @@ impl<'a> RsCodeGenerator<'a> {
412405
}
413406
}
414407
for (ty, entries) in impl_entries_by_type.into_iter() {
415-
let lt = ty.lifetime.map(|lt| quote! { < #lt > });
416-
let ty = ty.ty;
417408
output_items.push(Item::Impl(parse_quote! {
418-
impl #lt #ty {
409+
impl #ty {
419410
#(#entries)*
420411
}
421412
}))
@@ -494,7 +485,6 @@ impl<'a> RsCodeGenerator<'a> {
494485
analysis,
495486
cpp_call_name,
496487
non_pod_types,
497-
self.config,
498488
),
499489
Api::Const { const_item, .. } => RsCodegenResult {
500490
bindgen_mod_items: vec![Item::Const(const_item.into())],
@@ -1095,10 +1085,7 @@ impl<'a> RsCodeGenerator<'a> {
10951085
fn #method(_uhoh: autocxx::BindingGenerationFailure) {
10961086
}
10971087
},
1098-
ty: ImplBlockKey {
1099-
ty: parse_quote! { #self_ty },
1100-
lifetime: None,
1101-
},
1088+
ty: parse_quote! { #self_ty },
11021089
})),
11031090
None,
11041091
None,

0 commit comments

Comments
 (0)