Jelajahi Sumber

GDScript: Forbid enum values to shadow constants

- Don't allow constants to shadow parent members.
- Fix a spelling mistake.

Fix #13175
George Marques 7 tahun lalu
induk
melakukan
e6a6ea65c7
1 mengubah file dengan 20 tambahan dan 0 penghapusan
  1. 20 0
      modules/gdscript/gdscript_parser.cpp

+ 20 - 0
modules/gdscript/gdscript_parser.cpp

@@ -4863,6 +4863,20 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
 
 						StringName const_id = tokenizer->get_token_literal();
 
+						if (current_class->constant_expressions.has(const_id)) {
+							_set_error("A constant named '" + String(const_id) + "' already exists in this class (at line: " +
+									   itos(current_class->constant_expressions[const_id].expression->line) + ").");
+							return;
+						}
+
+						for (int i = 0; i < current_class->variables.size(); i++) {
+							if (current_class->variables[i].identifier == const_id) {
+								_set_error("A variable named '" + String(const_id) + "' already exists in this class (at line: " +
+										   itos(current_class->variables[i].line) + ").");
+								return;
+							}
+						}
+
 						tokenizer->advance();
 
 						if (tokenizer->get_token() == GDScriptTokenizer::TK_OP_ASSIGN) {
@@ -7216,6 +7230,12 @@ void GDScriptParser::_check_class_level_types(ClassNode *p_class) {
 		expr.is_constant = true;
 		c.type = expr;
 		c.expression->set_datatype(expr);
+
+		DataType tmp;
+		if (_get_member_type(p_class->base_type, E->key(), tmp)) {
+			_set_error("Member '" + String(E->key()) + "' already exists in parent class.", c.expression->line);
+			return;
+		}
 	}
 
 	// Function declarations