[3.x] GDScript: Prevent native class shadowing

This commit is contained in:
Danil Alexeev 2023-07-06 20:32:30 +03:00
parent ac5d7dc821
commit 3d98247678
No known key found for this signature in database
GPG key ID: 124453E157DA8DC7

View file

@ -3758,7 +3758,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;
}
@ -4922,6 +4922,12 @@ void GDScriptParser::_parse_class(ClassNode *p_class) {
member.usages = 0;
member.rpc_mode = rpc_mode;
// 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) + ").");
@ -5180,6 +5186,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) + ").");
@ -5256,6 +5268,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) + ").");
@ -5348,6 +5366,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) + ").");