Skip to content

Commit 70ccbac

Browse files
authored
Fire error in case of rand functions with single arg (#1327)
1 parent 502c89c commit 70ccbac

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

compiler/pipes/final-check.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ void check_to_mixed_call(VertexAdaptor<op_func_call> call) {
315315
to_mixed_on_class(type->class_type());
316316
}
317317

318+
void check_rand_call(VertexAdaptor<op_func_call> call) {
319+
const auto args_size = call->args().size();
320+
kphp_error(args_size == 0 || args_size == 2, fmt_format("{}() should have 0 or 2 arguments", call->func_id->name));
321+
}
322+
318323
void check_estimate_memory_usage_call(VertexAdaptor<op_func_call> call) {
319324
const auto *type = tinf::get_type(call->args()[0]);
320325
std::unordered_set<ClassPtr> classes_inside;
@@ -898,6 +903,8 @@ void FinalCheckPass::check_op_func_call(VertexAdaptor<op_func_call> call) {
898903
check_header_register_callback(call);
899904
} else if (function_name == "to_mixed") {
900905
check_to_mixed_call(call);
906+
} else if (function_name == "mt_rand" || function_name == "rand") {
907+
check_rand_call(call);
901908
} else if (vk::string_view{function_name}.starts_with("rpc_tl_query") || vk::string_view{function_name}.starts_with("rpc_send_request")) {
902909
G->set_untyped_rpc_tl_used();
903910
} else if (vk::string_view{function_name}.starts_with("FFI$$")) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
@kphp_should_fail
2+
/rand\(\) should have 0 or 2 arguments/
3+
/mt_rand\(\) should have 0 or 2 arguments/
4+
<?php
5+
6+
$x = rand(1);
7+
$y = mt_rand(2);

0 commit comments

Comments
 (0)