From 3c2f2ee5ef2a1035fffcc0b7e7ec20b290d4d5fc Mon Sep 17 00:00:00 2001 From: Ivan Bondarev Date: Sun, 2 May 2021 12:19:45 +0200 Subject: [PATCH] #2479 --- TestSuite/yieldrecord1.pas | 20 +++++++++++++++++++ .../TreeConversion/compilation_context.cs | 20 ++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 TestSuite/yieldrecord1.pas diff --git a/TestSuite/yieldrecord1.pas b/TestSuite/yieldrecord1.pas new file mode 100644 index 000000000..48af5324b --- /dev/null +++ b/TestSuite/yieldrecord1.pas @@ -0,0 +1,20 @@ +type + t1 = record + i: integer :=1; + end; + +function f1: sequence of t1; +begin + var o: t1; + yield o; +end; + +begin + var i := 0; + foreach var it in f1 do + begin + assert(it.i = 1); + Inc(i); + end; + assert(i = 1); +end. \ No newline at end of file diff --git a/TreeConverter/TreeConversion/compilation_context.cs b/TreeConverter/TreeConversion/compilation_context.cs index ccc8dc2b3..0df1c95c5 100644 --- a/TreeConverter/TreeConversion/compilation_context.cs +++ b/TreeConverter/TreeConversion/compilation_context.cs @@ -2117,8 +2117,18 @@ namespace PascalABCCompiler.TreeConverter if (CurrentStatementList != null) { //Инициализировать надо в текущем стейтменте - location lid = ((local_block_variable)vdn).loc; - local_block_variable_reference lbvr = new local_block_variable_reference((local_block_variable)vdn, lid); + expression_node expr = null; + location lid = null; + if (vdn is local_block_variable) + { + lid = ((local_block_variable)vdn).loc; + expr = new local_block_variable_reference((local_block_variable)vdn, lid); + } + else if (vdn is class_field) + { + lid = vdn.location; + expr = new class_field_reference(vdn as class_field, new this_node((vdn as class_field).cont_type, lid), lid); + } if (vdn.type.type_special_kind == SemanticTree.type_special_kind.set_type) { userInitalValue = syntax_tree_visitor.get_init_call_for_set_as_constr(vdn, userInitalValue); @@ -2140,12 +2150,12 @@ namespace PascalABCCompiler.TreeConverter } } } - CurrentStatementList.statements.AddElement(syntax_tree_visitor.find_operator(compiler_string_consts.assign_name, lbvr, userInitalValue, lid)); + CurrentStatementList.statements.AddElement(syntax_tree_visitor.find_operator(compiler_string_consts.assign_name, expr, userInitalValue, lid)); if (vdn.type.type_special_kind == SemanticTree.type_special_kind.set_type) { - lbvr.type = SystemLibrary.SystemLibInitializer.TypedSetType.sym_info as type_node; + expr.type = SystemLibrary.SystemLibInitializer.TypedSetType.sym_info as type_node; } - if (vdn.type.is_value_type /*&& userInitalValue is common_constructor_call*/) + if (vdn.type.is_value_type /*&& userInitalValue is common_constructor_call*/ && !(expr is class_field_reference)) { return new default_operator_node(vdn.type, lid); }