GDScript: Prevent native class shadowing

This commit is contained in:
Danil Alexeev 2023-07-06 20:32:30 +03:00 committed by Relintai
parent 8ddf92681e
commit a70e3ce4b3

View File

@ -3739,7 +3739,7 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
return;
}
if (ClassDB::class_exists(p_class->name)) {
if (ClassDB::class_exists(p_class->name) || ClassDB::class_exists("_" + p_class->name.operator String())) {
_set_error("The class \"" + p_class->name + "\" shadows a native class.");
return;
}
@ -4819,6 +4819,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
member.line = tokenizer->get_token_line();
member.usages = 0;
// GH-57496
if (ClassDB::class_exists(member.identifier) || ClassDB::class_exists("_" + member.identifier.operator String())) {
_set_error("Variable \"" + String(member.identifier) + "\" shadows a native class.");
return;
}
if (current_class->constant_expressions.has(member.identifier)) {
_set_error("A constant named \"" + String(member.identifier) + "\" already exists in this class (at line: " +
itos(current_class->constant_expressions[member.identifier].expression->line) + ").");
@ -5106,6 +5112,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
StringName const_id = tokenizer->get_token_literal();
int line = tokenizer->get_token_line();
// GH-57496
if (ClassDB::class_exists(const_id) || ClassDB::class_exists("_" + const_id.operator String())) {
_set_error("Constant \"" + String(const_id) + "\" shadows a native class.");
return;
}
if (current_class->constant_expressions.has(const_id)) {
_set_error("Constant \"" + String(const_id) + "\" already exists in this class (at line " +
itos(current_class->constant_expressions[const_id].expression->line) + ").");
@ -5182,6 +5194,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
if (tokenizer->is_token_literal(0, true)) {
enum_name = tokenizer->get_token_literal();
// GH-57496
if (ClassDB::class_exists(enum_name) || ClassDB::class_exists("_" + enum_name)) {
_set_error("Enumeration \"" + enum_name + "\" shadows a native class.");
return;
}
if (current_class->constant_expressions.has(enum_name)) {
_set_error("A constant named \"" + String(enum_name) + "\" already exists in this class (at line " +
itos(current_class->constant_expressions[enum_name].expression->line) + ").");
@ -5274,6 +5292,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
if (enum_name != "") {
enum_dict[const_id] = enum_value_expr->value;
} else {
// GH-57496
if (ClassDB::class_exists(const_id) || ClassDB::class_exists("_" + const_id.operator String())) {
_set_error("Constant \"" + String(const_id) + "\" shadows a native class.");
return;
}
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) + ").");