From 28a1a03e8dbccdff404c133145b5bffc63920cad Mon Sep 17 00:00:00 2001 From: Ivan Bondarev Date: Sun, 6 Apr 2025 11:35:09 +0200 Subject: [PATCH] #3251 --- TestSuite/lambdas27.pas | 14 ++++++++++++++ .../convertion_data_and_alghoritms.cs | 14 ++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 TestSuite/lambdas27.pas diff --git a/TestSuite/lambdas27.pas b/TestSuite/lambdas27.pas new file mode 100644 index 000000000..3b22473d1 --- /dev/null +++ b/TestSuite/lambdas27.pas @@ -0,0 +1,14 @@ +var i: integer; + +procedure p1(o: real; d: char->()); +begin + Inc(i); +end; + +procedure p1(o: byte; d: ()->word) := exit; + +begin + p1(0, procedure(x) ->x.ToString()); + p1(0, x -> x.ToString()); + assert(i = 2); +end. \ No newline at end of file diff --git a/TreeConverter/TreeConversion/convertion_data_and_alghoritms.cs b/TreeConverter/TreeConversion/convertion_data_and_alghoritms.cs index f0a34fb88..223cdc7f1 100644 --- a/TreeConverter/TreeConversion/convertion_data_and_alghoritms.cs +++ b/TreeConverter/TreeConversion/convertion_data_and_alghoritms.cs @@ -2477,7 +2477,7 @@ namespace PascalABCCompiler.TreeConverter } possible_type_convertions_list_list tcll = new possible_type_convertions_list_list(); - + Dictionary last_chance_list = new Dictionary(); for (int i = 0; i < set_of_possible_functions.Count; i++) { Errors.Error err = null; @@ -2495,6 +2495,7 @@ namespace PascalABCCompiler.TreeConverter if (fact.type is delegated_methods dm) { var fact_is_function_with_return_value = dm.proper_methods.Count > 0 && dm.proper_methods[0].function.return_value_type != null; + var fact_is_lambda = syntax_nodes_parameters != null && k < syntax_nodes_parameters.Count && syntax_nodes_parameters[k] is SyntaxTree.function_lambda_definition; var form_is_procedure = false; var form_is_delegate = false; @@ -2516,6 +2517,8 @@ namespace PascalABCCompiler.TreeConverter if (fact_is_function_with_return_value && form_is_procedure) { proc_func_or_lambdaAndNotDelegate_OK_flag = false; + if (dm.proper_methods[0].function.return_value_type is lambda_any_type_node) + last_chance_list[set_of_possible_functions[i]] = tc; break; } if (fact_is_lambda && !form_is_delegate) // лямбда вместо не делегата - исключает функцию из рассмотрения @@ -2579,6 +2582,12 @@ namespace PascalABCCompiler.TreeConverter } } + if (set_of_possible_functions.Count == 0 && indefinits.Count == 0 && last_chance_list.Count == 1) + { + tcll.AddElement(last_chance_list.First().Value); + set_of_possible_functions.Add(last_chance_list.First().Key); + } + if (set_of_possible_functions.Count == 0 && indefinits.Count == 0) { if (_is_assigment && parameters.Count == 2) @@ -2587,7 +2596,8 @@ namespace PascalABCCompiler.TreeConverter err_out = new OperatorCanNotBeAppliedToThisTypes(_tmp_bfn.name, parameters[0], parameters[1], loc); else if (is_op) err_out = new OperatorCanNotBeAppliedToThisTypes(first_function.name, parameters[0], parameters.Count > 1 ? parameters[1] : null, loc); - else err_out = new NoFunctionWithSameArguments(FunctionName, loc, is_alone_method_defined); + else + err_out = new NoFunctionWithSameArguments(FunctionName, loc, is_alone_method_defined); return set_of_possible_functions; }