Skip to content

Commit ba20a8b

Browse files
committed
Add #[init(val = ...)], deprecating #[init(default = ...)]
1 parent a20bb85 commit ba20a8b

File tree

4 files changed

+39
-10
lines changed

4 files changed

+39
-10
lines changed

godot-core/src/deprecated.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,19 @@ macro_rules! emit_deprecated_warning {
3535
pub use crate::emit_deprecated_warning;
3636

3737
// ----------------------------------------------------------------------------------------------------------------------------------------------
38-
// Concrete deprecations
38+
// Library-side deprecations
39+
40+
#[deprecated = "\nThe attribute key #[init(val = ...)] replaces #[init(default = ...)].\n\
41+
More information on https://github.com/godot-rust/gdext/pull/844."]
42+
pub const fn init_default() {}
43+
44+
// ----------------------------------------------------------------------------------------------------------------------------------------------
45+
// Godot-side deprecations
3946

4047
// This is a Godot-side deprecation. Since it's the only way in Godot 4.1, we keep compatibility for now.
4148
#[cfg_attr(
4249
since_api = "4.2",
43-
deprecated = "Use #[export(range = (radians_as_degrees))] and not #[export(range = (radians))]. \n\
50+
deprecated = "\nUse #[export(range = (radians_as_degrees))] and not #[export(range = (radians))].\n\
4451
More information on https://github.com/godotengine/godot/pull/82195."
4552
)]
4653
pub const fn export_range_radians() {}

godot-macros/src/class/data_models/field.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use proc_macro2::{Ident, TokenStream};
1111
pub struct Field {
1212
pub name: Ident,
1313
pub ty: venial::TypeExpr,
14-
pub default: Option<TokenStream>,
14+
pub default_val: Option<TokenStream>,
1515
pub var: Option<FieldVar>,
1616
pub export: Option<FieldExport>,
1717
pub is_onready: bool,
@@ -24,7 +24,7 @@ impl Field {
2424
Self {
2525
name: field.name.clone(),
2626
ty: field.ty.clone(),
27-
default: None,
27+
default_val: None,
2828
var: None,
2929
export: None,
3030
is_onready: false,
@@ -40,4 +40,8 @@ pub struct Fields {
4040

4141
/// The field with type `Base<T>`, if available.
4242
pub base_field: Option<Field>,
43+
44+
/// Deprecation warnings.
45+
pub deprecations: Vec<TokenStream>,
46+
4347
}

godot-macros/src/class/derive_godot_class.rs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ pub fn derive_godot_class(item: venial::Item) -> ParseResult<TokenStream> {
7575
let mut create_fn = quote! { None };
7676
let mut recreate_fn = quote! { None };
7777
let mut is_instantiable = true;
78+
let deprecations = &fields.deprecations;
7879

7980
match struct_cfg.init_strategy {
8081
InitStrategy::Generated => {
@@ -137,6 +138,7 @@ pub fn derive_godot_class(item: venial::Item) -> ParseResult<TokenStream> {
137138
#godot_exports_impl
138139
#user_class_impl
139140
#init_expecter
141+
#( #deprecations )*
140142

141143
::godot::sys::plugin_add!(__GODOT_PLUGIN_REGISTRY in #prv; #prv::ClassPlugin {
142144
class_name: #class_name_obj,
@@ -214,7 +216,7 @@ fn make_godot_init_impl(class_name: &Ident, fields: &Fields) -> TokenStream {
214216
let rest_init = fields.all_fields.iter().map(|field| {
215217
let field_name = field.name.clone();
216218
let value_expr = field
217-
.default
219+
.default_val
218220
.clone()
219221
.unwrap_or_else(|| quote! { ::std::default::Default::default() });
220222

@@ -399,6 +401,7 @@ fn parse_fields(
399401
) -> ParseResult<Fields> {
400402
let mut all_fields = vec![];
401403
let mut base_field = Option::<Field>::None;
404+
let mut deprecations = vec![];
402405

403406
// Attributes on struct fields
404407
for (named_field, _punct) in named_fields {
@@ -425,9 +428,23 @@ fn parse_fields(
425428
);
426429
}
427430

431+
// #[init(val = expr)]
432+
if let Some(default) = parser.handle_expr("val")? {
433+
field.default_val = Some(default);
434+
}
435+
428436
// #[init(default = expr)]
429437
if let Some(default) = parser.handle_expr("default")? {
430-
field.default = Some(default);
438+
if field.default_val.is_some() {
439+
return bail!(
440+
parser.span(),
441+
"Cannot use both `val` and `default` keys in #[init]; prefer using `val`"
442+
);
443+
}
444+
field.default_val = Some(default);
445+
deprecations.push(quote! {
446+
::godot::__deprecated::emit_deprecated_warning!(init_default);
447+
})
431448
}
432449

433450
// #[init(node = "NodePath")]
@@ -442,7 +459,7 @@ fn parse_fields(
442459
);
443460
}
444461

445-
if field.default.is_some() {
462+
if field.default_val.is_some() {
446463
return bail!(
447464
parser.span(),
448465
"The key `node` in attribute #[init] is mutually exclusive with the key `default`\n\
@@ -452,7 +469,7 @@ fn parse_fields(
452469
);
453470
}
454471

455-
field.default = Some(quote! {
472+
field.default_val = Some(quote! {
456473
OnReady::node(#node_path)
457474
});
458475
}
@@ -491,7 +508,7 @@ fn parse_fields(
491508
if field.is_onready
492509
|| field.var.is_some()
493510
|| field.export.is_some()
494-
|| field.default.is_some()
511+
|| field.default_val.is_some()
495512
{
496513
return bail!(
497514
named_field,
@@ -516,6 +533,7 @@ fn parse_fields(
516533
Ok(Fields {
517534
all_fields,
518535
base_field,
536+
deprecations
519537
})
520538
}
521539

godot-macros/src/docs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub fn member(member: &Field) -> Option<String> {
167167
let docs = make_docs_from_attributes(&member.attributes)?;
168168
let name = &member.name;
169169
let ty = member.ty.to_token_stream().to_string();
170-
let default = member.default.to_token_stream().to_string();
170+
let default = member.default_val.to_token_stream().to_string();
171171
Some(format!(
172172
r#"<member name="{name}" type="{ty}" default="{default}">{docs}</member>"#
173173
))

0 commit comments

Comments
 (0)