Skip to content

Commit bd9bf23

Browse files
authored
Merge pull request #1558 from godot-rust/qol/oneditor-init-error
Collect uninitialized `OnEditor` fields into single panic message
2 parents b746be4 + 6fc73e5 commit bd9bf23

2 files changed

Lines changed: 21 additions & 22 deletions

File tree

godot-macros/src/class/derive_godot_class.rs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -450,46 +450,45 @@ fn make_oneditor_panic_inits(class_name: &Ident, all_fields: &[Field]) -> TokenS
450450
// Despite its name OnEditor shouldn't panic in the editor for tool classes.
451451
let is_in_editor = quote! { <::godot::classes::Engine as ::godot::obj::Singleton>::singleton().is_editor_hint() };
452452

453-
let are_all_oneditor_fields_valid = quote! { are_all_oneditor_fields_valid };
454-
455-
// Informs the user which fields haven't been set, instead of panicking on the very first one. Useful for debugging.
456453
let on_editor_fields_checks = all_fields
457454
.iter()
458455
.filter(|&field| field.is_oneditor)
459456
.map(|field| {
460457
let field = &field.name;
461-
let warning_message =
462-
format! { "godot-rust: OnEditor field {field} hasn't been initialized."};
458+
let field_name_str = field.to_string();
463459

464460
quote! {
465461
if this.#field.is_invalid() {
466-
::godot::global::godot_warn!(#warning_message);
467-
#are_all_oneditor_fields_valid = false;
462+
uninitialized_fields.push(#field_name_str);
468463
}
469464
}
470465
})
471466
.collect::<Vec<_>>();
472467

473468
if !on_editor_fields_checks.is_empty() {
469+
let class_name_str = class_name.to_string();
470+
474471
quote! {
475-
// Triggers `clippy::useless_let_if_seq` lint if only one `#on_editor_fields_checks` is present.
476-
#[allow(clippy::useless_let_if_seq)]
477-
fn __are_oneditor_fields_initalized(this: &#class_name) -> bool {
472+
fn __check_oneditor_fields(this: &#class_name) {
478473
// Early return for `#[class(tool)]`.
479474
if #is_in_editor {
480-
return true;
475+
return;
481476
}
482477

483-
let mut #are_all_oneditor_fields_valid: bool = true;
478+
let mut uninitialized_fields: Vec<&str> = Vec::new();
484479

485480
#( #on_editor_fields_checks )*
486481

487-
#are_all_oneditor_fields_valid
482+
if !uninitialized_fields.is_empty() {
483+
panic!(
484+
"{}::ready(): OnEditor fields not initialized: {}",
485+
#class_name_str,
486+
uninitialized_fields.join(", "),
487+
);
488+
}
488489
}
489490

490-
if !__are_oneditor_fields_initalized(&self) {
491-
panic!("OnEditor fields must be properly initialized before ready.")
492-
}
491+
__check_oneditor_fields(&self);
493492
}
494493
} else {
495494
TokenStream::new()

itest/godot/.godot/global_script_class_cache.cfg

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ list=[{
77
"language": &"GDScript",
88
"path": "res://TestRunner.gd"
99
}, {
10-
"base": &"TestSuite",
11-
"class": &"TestSuiteSpecial",
10+
"base": &"RefCounted",
11+
"class": &"TestSuite",
1212
"icon": "",
1313
"is_abstract": false,
1414
"is_tool": false,
1515
"language": &"GDScript",
16-
"path": "res://TestSuiteSpecial.gd"
16+
"path": "res://TestSuite.gd"
1717
}, {
18-
"base": &"RefCounted",
19-
"class": &"TestSuite",
18+
"base": &"TestSuite",
19+
"class": &"TestSuiteSpecial",
2020
"icon": "",
2121
"is_abstract": false,
2222
"is_tool": false,
2323
"language": &"GDScript",
24-
"path": "res://TestSuite.gd"
24+
"path": "res://TestSuiteSpecial.gd"
2525
}]

0 commit comments

Comments
 (0)