diff --git a/NETGenerator/NETGenerator.cs b/NETGenerator/NETGenerator.cs index fb7ab9a9e..3f3309278 100644 --- a/NETGenerator/NETGenerator.cs +++ b/NETGenerator/NETGenerator.cs @@ -2951,10 +2951,10 @@ namespace PascalABCCompiler.NETGenerator il.Emit(OpCodes.Stloc, lb); } in_var_init = true; - GenerateInitCode(var, il); - if (var.type.is_value_type && var.inital_value is ICommonConstructorCall) + + if (!(var.type.is_value_type && var.inital_value is IDefaultOperatorNode)) { - il.Emit(OpCodes.Stloc, lb); + GenerateInitCode(var, il); } in_var_init = false; } diff --git a/TestSuite/records16.pas b/TestSuite/records16.pas new file mode 100644 index 000000000..061fdf85e --- /dev/null +++ b/TestSuite/records16.pas @@ -0,0 +1,24 @@ +type + t0=class + static i := 0; + + constructor := + i += 1; + end; + r1=record + ni := t0.i; + end; + + t1=class(t0) + + constructor; + begin + var a: r1; + assert(a.ni = 1); + end; + + end; + +begin + new t1; +end. \ No newline at end of file diff --git a/TreeConverter/TreeConversion/compilation_context.cs b/TreeConverter/TreeConversion/compilation_context.cs index a9f65ad3f..0bd348773 100644 --- a/TreeConverter/TreeConversion/compilation_context.cs +++ b/TreeConverter/TreeConversion/compilation_context.cs @@ -2047,6 +2047,18 @@ namespace PascalABCCompiler.TreeConverter internal expression_node GetInitalValueForVariable(var_definition_node vdn, expression_node userInitalValue) { + if (userInitalValue == null && CurrentStatementList != null && vdn.type.is_value_type && vdn.type is common_type_node) + { + common_type_node ctn = vdn.type as common_type_node; + foreach (var meth in ctn.methods) + { + if (meth.is_constructor && meth.parameters.Count == 0) + { + userInitalValue = new common_constructor_call(meth, vdn.location); + break; + } + } + } if (userInitalValue != null) { if (CurrentStatementList != null) @@ -2075,13 +2087,15 @@ namespace PascalABCCompiler.TreeConverter } } } - else if (vdn.type.is_value_type && userInitalValue is common_constructor_call) - return userInitalValue; CurrentStatementList.statements.AddElement(syntax_tree_visitor.find_operator(compiler_string_consts.assign_name, lbvr, userInitalValue, lid)); if (vdn.type.type_special_kind == SemanticTree.type_special_kind.set_type) { lbvr.type = SystemLibrary.SystemLibInitializer.TypedSetType.sym_info as type_node; } + if (vdn.type.is_value_type && userInitalValue is common_constructor_call) + { + return new default_operator_node(vdn.type, lid); + } return null; } if (userInitalValue is constant_node)