-
-
Notifications
You must be signed in to change notification settings - Fork 250
Bugfix – initialize&cache proper return value for generic, typed array. #1357
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ use quote::{format_ident, quote}; | |
|
||
use crate::generator::functions_common; | ||
use crate::generator::functions_common::{ | ||
make_arg_expr, make_param_or_field_type, FnArgExpr, FnCode, FnKind, FnParamDecl, FnParamTokens, | ||
make_arg_expr, make_param_or_field_type, FnArgExpr, FnCode, FnKind, FnParamDecl, | ||
}; | ||
use crate::models::domain::{FnParam, FnQualifier, Function, RustTy, TyName}; | ||
use crate::util::{ident, safe_ident}; | ||
|
@@ -59,15 +59,6 @@ pub fn make_function_definition_with_defaults( | |
&default_fn_params, | ||
); | ||
|
||
// ExBuilder::new() constructor signature. | ||
let FnParamTokens { | ||
func_general_lifetime: simple_fn_lifetime, | ||
.. | ||
} = fns::make_params_exprs( | ||
required_fn_params.iter().cloned(), | ||
FnKind::ExBuilderConstructor, | ||
); | ||
Comment on lines
-62
to
-69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change is unrelated (the dead code you mentioned), right? Why exactly? Could you extract it to its own commit so it's easier to isolate the changes in case of regressions? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
let return_decl = &sig.return_value().decl; | ||
|
||
// If either the builder has a lifetime (non-static/global method), or one of its parameters is a reference, | ||
|
@@ -119,7 +110,7 @@ pub fn make_function_definition_with_defaults( | |
// Lifetime is set if any parameter is a reference. | ||
#[doc = #default_parameter_usage] | ||
#[inline] | ||
#vis fn #simple_fn_name #simple_fn_lifetime ( | ||
#vis fn #simple_fn_name ( | ||
#simple_receiver_param | ||
#( #class_method_required_params, )* | ||
) #return_decl { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -300,28 +300,40 @@ pub trait Function: fmt::Display { | |
fn name(&self) -> &str { | ||
&self.common().name | ||
} | ||
|
||
/// Rust name as `Ident`. Might be cached in future. | ||
fn name_ident(&self) -> Ident { | ||
safe_ident(self.name()) | ||
} | ||
|
||
fn godot_name(&self) -> &str { | ||
&self.common().godot_name | ||
} | ||
|
||
fn params(&self) -> &[FnParam] { | ||
&self.common().parameters | ||
} | ||
|
||
fn return_value(&self) -> &FnReturn { | ||
&self.common().return_value | ||
} | ||
|
||
fn is_vararg(&self) -> bool { | ||
self.common().is_vararg | ||
} | ||
|
||
fn is_private(&self) -> bool { | ||
self.common().is_private | ||
} | ||
|
||
fn is_virtual(&self) -> bool { | ||
matches!(self.direction(), FnDirection::Virtual { .. }) | ||
} | ||
|
||
fn is_generic(&self) -> bool { | ||
matches!(self.return_value().type_, Some(RustTy::GenericArray)) | ||
} | ||
Comment on lines
+333
to
+335
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Always empty lines between multi-line |
||
|
||
fn direction(&self) -> FnDirection { | ||
self.common().direction | ||
} | ||
|
@@ -621,6 +633,13 @@ impl FnReturn { | |
Self::with_enum_replacements(return_value, &[], ctx) | ||
} | ||
|
||
pub fn with_generic_builtin(generic_type: RustTy) -> Self { | ||
Self { | ||
decl: generic_type.return_decl(), | ||
type_: Some(generic_type), | ||
} | ||
} | ||
|
||
pub fn with_enum_replacements( | ||
return_value: &Option<JsonMethodReturn>, | ||
replacements: EnumReplacements, | ||
|
@@ -669,6 +688,14 @@ impl FnReturn { | |
} | ||
} | ||
|
||
pub fn generic_params(&self) -> Option<TokenStream> { | ||
self.type_.as_ref()?.generic_params() | ||
} | ||
|
||
pub fn where_clause(&self) -> Option<TokenStream> { | ||
self.type_.as_ref()?.where_clause() | ||
} | ||
|
||
pub fn call_result_decl(&self) -> TokenStream { | ||
let ret = self.type_tokens(); | ||
quote! { -> Result<#ret, crate::meta::error::CallError> } | ||
|
@@ -705,6 +732,11 @@ pub enum RustTy { | |
/// Note that untyped arrays are mapped as `BuiltinIdent("Array")`. | ||
BuiltinArray { elem_type: TokenStream }, | ||
|
||
/// Will be included as `Array<T>` in the generated source. | ||
/// | ||
/// Set by [`builtin_method_generic_ret`](crate::special_cases::builtin_method_generic_ret) | ||
GenericArray, | ||
|
||
/// C-style raw pointer to a `RustTy`. | ||
RawPointer { inner: Box<RustTy>, is_const: bool }, | ||
|
||
|
@@ -755,10 +787,30 @@ impl RustTy { | |
pub fn return_decl(&self) -> TokenStream { | ||
match self { | ||
Self::EngineClass { tokens, .. } => quote! { -> Option<#tokens> }, | ||
Self::GenericArray => quote! { -> Array<Ret> }, | ||
other => quote! { -> #other }, | ||
} | ||
} | ||
|
||
pub fn generic_params(&self) -> Option<TokenStream> { | ||
if matches!(self, Self::GenericArray) { | ||
Some(quote! { < Ret > }) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn where_clause(&self) -> Option<TokenStream> { | ||
if matches!(self, Self::GenericArray) { | ||
Some(quote! { | ||
where | ||
Ret: crate::meta::ArrayElement, | ||
}) | ||
} else { | ||
None | ||
} | ||
} | ||
|
||
pub fn is_integer(&self) -> bool { | ||
let RustTy::BuiltinIdent { ty, .. } = self else { | ||
return false; | ||
|
@@ -789,6 +841,7 @@ impl ToTokens for RustTy { | |
RustTy::EngineEnum { tokens: path, .. } => path.to_tokens(tokens), | ||
RustTy::EngineClass { tokens: path, .. } => path.to_tokens(tokens), | ||
RustTy::ExtenderReceiver { tokens: path } => path.to_tokens(tokens), | ||
RustTy::GenericArray => quote! { Array<Ret> }.to_tokens(tokens), | ||
} | ||
} | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is quite nice, but does anyone ever see these docs?
I guess the show up in IDE hints? 🤔
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(currently) contributors are the only ones seeing it.
Still nice to have and might come in handy one day – I think it might be useful providing some context while debugging some UB (if any ever happens) too
screenshoot from previous version