mirror of
				https://github.com/godotengine/godot.git
				synced 2025-11-03 23:21:15 +00:00 
			
		
		
		
	GDScript: Prevent constructing and inheriting engine singletons
This commit is contained in:
		
							parent
							
								
									fe5b1c8d49
								
							
						
					
					
						commit
						10b00bc2ea
					
				
					 5 changed files with 22 additions and 0 deletions
				
			
		| 
						 | 
					@ -459,6 +459,10 @@ Error GDScriptAnalyzer::resolve_class_inheritance(GDScriptParser::ClassNode *p_c
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				base = info_parser->get_parser()->head->get_datatype();
 | 
									base = info_parser->get_parser()->head->get_datatype();
 | 
				
			||||||
			} else if (class_exists(name)) {
 | 
								} else if (class_exists(name)) {
 | 
				
			||||||
 | 
									if (Engine::get_singleton()->has_singleton(name)) {
 | 
				
			||||||
 | 
										push_error(vformat(R"(Cannot inherit native class "%s" because it is an engine singleton.)", name), id);
 | 
				
			||||||
 | 
										return ERR_PARSE_ERROR;
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
				base.kind = GDScriptParser::DataType::NATIVE;
 | 
									base.kind = GDScriptParser::DataType::NATIVE;
 | 
				
			||||||
				base.native_type = name;
 | 
									base.native_type = name;
 | 
				
			||||||
			} else {
 | 
								} else {
 | 
				
			||||||
| 
						 | 
					@ -3203,6 +3207,12 @@ void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_a
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	bool is_constructor = (base_type.is_meta_type || (p_call->callee && p_call->callee->type == GDScriptParser::Node::IDENTIFIER)) && p_call->function_name == SNAME("new");
 | 
						bool is_constructor = (base_type.is_meta_type || (p_call->callee && p_call->callee->type == GDScriptParser::Node::IDENTIFIER)) && p_call->function_name == SNAME("new");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (is_constructor && Engine::get_singleton()->has_singleton(base_type.native_type)) {
 | 
				
			||||||
 | 
							push_error(vformat(R"(Cannot construct native class "%s" because it is an engine singleton.)", base_type.native_type), p_call);
 | 
				
			||||||
 | 
							p_call->set_datatype(call_type);
 | 
				
			||||||
 | 
							return;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (get_function_signature(p_call, is_constructor, base_type, p_call->function_name, return_type, par_types, default_arg_count, method_flags)) {
 | 
						if (get_function_signature(p_call, is_constructor, base_type, p_call->function_name, return_type, par_types, default_arg_count, method_flags)) {
 | 
				
			||||||
		// If the method is implemented in the class hierarchy, the virtual flag will not be set for that MethodInfo and the search stops there.
 | 
							// If the method is implemented in the class hierarchy, the virtual flag will not be set for that MethodInfo and the search stops there.
 | 
				
			||||||
		// Virtual check only possible for super() calls because class hierarchy is known. Node/Objects may have scripts attached we don't know of at compile-time.
 | 
							// Virtual check only possible for super() calls because class hierarchy is known. Node/Objects may have scripts attached we don't know of at compile-time.
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					func test():
 | 
				
			||||||
 | 
						Time.new()
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					GDTEST_ANALYZER_ERROR
 | 
				
			||||||
 | 
					Cannot construct native class "Time" because it is an engine singleton.
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,6 @@
 | 
				
			||||||
 | 
					# GH-82081
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					extends Time
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func test():
 | 
				
			||||||
 | 
						pass
 | 
				
			||||||
| 
						 | 
					@ -0,0 +1,2 @@
 | 
				
			||||||
 | 
					GDTEST_ANALYZER_ERROR
 | 
				
			||||||
 | 
					Cannot inherit native class "Time" because it is an engine singleton.
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue