From bf8f996212323e37c81432dd7268a3bd448dceed Mon Sep 17 00:00:00 2001 From: Danil Alexeev Date: Thu, 15 Jun 2023 08:02:42 +0300 Subject: [PATCH] GDScript: Fix conflict between property and group names --- modules/gdscript/gdscript_compiler.cpp | 7 ++++--- modules/gdscript/gdscript_parser.h | 4 +++- ...rt_group_no_name_conflict_with_properties.gd | 17 +++++++++++++++++ ...t_group_no_name_conflict_with_properties.out | 5 +++++ 4 files changed, 29 insertions(+), 4 deletions(-) create mode 100644 modules/gdscript/tests/scripts/runtime/features/export_group_no_name_conflict_with_properties.gd create mode 100644 modules/gdscript/tests/scripts/runtime/features/export_group_no_name_conflict_with_properties.out diff --git a/modules/gdscript/gdscript_compiler.cpp b/modules/gdscript/gdscript_compiler.cpp index 327e24ef11f6..4f60e736c459 100644 --- a/modules/gdscript/gdscript_compiler.cpp +++ b/modules/gdscript/gdscript_compiler.cpp @@ -2581,20 +2581,21 @@ Error GDScriptCompiler::_populate_class_members(GDScript *p_script, const GDScri case GDScriptParser::ClassNode::Member::GROUP: { const GDScriptParser::AnnotationNode *annotation = member.annotation; - StringName name = annotation->export_info.name; + // Avoid name conflict. See GH-78252. + StringName name = vformat("@group_%d_%s", p_script->members.size(), annotation->export_info.name); // This is not a normal member, but we need this to keep indices in order. GDScript::MemberInfo minfo; minfo.index = p_script->member_indices.size(); PropertyInfo prop_info; - prop_info.name = name; + prop_info.name = annotation->export_info.name; prop_info.usage = annotation->export_info.usage; prop_info.hint_string = annotation->export_info.hint_string; p_script->member_info[name] = prop_info; p_script->member_indices[name] = minfo; - p_script->members.insert(name); + p_script->members.insert(Variant()); } break; default: diff --git a/modules/gdscript/gdscript_parser.h b/modules/gdscript/gdscript_parser.h index 8f0265510f72..32769a147aa7 100644 --- a/modules/gdscript/gdscript_parser.h +++ b/modules/gdscript/gdscript_parser.h @@ -749,7 +749,9 @@ class GDScriptParser { members.push_back(Member(p_enum_value)); } void add_member_group(AnnotationNode *p_annotation_node) { - members_indices[p_annotation_node->export_info.name] = members.size(); + // Avoid name conflict. See GH-78252. + StringName name = vformat("@group_%d_%s", members.size(), p_annotation_node->export_info.name); + members_indices[name] = members.size(); members.push_back(Member(p_annotation_node)); } diff --git a/modules/gdscript/tests/scripts/runtime/features/export_group_no_name_conflict_with_properties.gd b/modules/gdscript/tests/scripts/runtime/features/export_group_no_name_conflict_with_properties.gd new file mode 100644 index 000000000000..e46f24cc5f33 --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/export_group_no_name_conflict_with_properties.gd @@ -0,0 +1,17 @@ +extends RefCounted # TODO: Fix standalone annotations parsing. + +# GH-73843 +@export_group("Resource") + +# GH-78252 +@export var prop_1: int +@export_category("prop_1") +@export var prop_2: int + +func test(): + var resource := Resource.new() + prints("Not shadowed:", resource.get_class()) + + for property in get_property_list(): + if property.name in ["prop_1", "prop_2"]: + print(property) diff --git a/modules/gdscript/tests/scripts/runtime/features/export_group_no_name_conflict_with_properties.out b/modules/gdscript/tests/scripts/runtime/features/export_group_no_name_conflict_with_properties.out new file mode 100644 index 000000000000..96ae84e986ff --- /dev/null +++ b/modules/gdscript/tests/scripts/runtime/features/export_group_no_name_conflict_with_properties.out @@ -0,0 +1,5 @@ +GDTEST_OK +Not shadowed: Resource +{ "name": "prop_1", "class_name": &"", "type": 2, "hint": 0, "hint_string": "int", "usage": 4102 } +{ "name": "prop_1", "class_name": &"", "type": 0, "hint": 0, "hint_string": "", "usage": 128 } +{ "name": "prop_2", "class_name": &"", "type": 2, "hint": 0, "hint_string": "int", "usage": 4102 }