From c3160fe819e251b65053ec3d6c80b1ea01e7c609 Mon Sep 17 00:00:00 2001 From: Ivan Bondarev Date: Fri, 24 Dec 2021 15:14:40 +0100 Subject: [PATCH] #2579 --- NETGenerator/NETGenerator.cs | 30 +++++++++++++++++++++++++----- TestSuite/double_question3.pas | 8 ++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 TestSuite/double_question3.pas diff --git a/NETGenerator/NETGenerator.cs b/NETGenerator/NETGenerator.cs index 076be33d9..3b92f9ffa 100644 --- a/NETGenerator/NETGenerator.cs +++ b/NETGenerator/NETGenerator.cs @@ -10757,11 +10757,31 @@ namespace PascalABCCompiler.NETGenerator else { value.condition.visit(this); - tmp_lb = il.DeclareLocal(helper.GetTypeReference(value.type).tp); - il.Emit(OpCodes.Stloc, tmp_lb); - il.Emit(OpCodes.Ldloc, tmp_lb); - il.Emit(OpCodes.Ldnull); - il.Emit(OpCodes.Ceq); + if (value.condition.type.is_nullable_type) + { + + tmp_lb = il.DeclareLocal(helper.GetTypeReference(value.type).tp); + il.Emit(OpCodes.Stloc, tmp_lb); + il.Emit(OpCodes.Ldloca, tmp_lb); + MethodInfo mi = null; + TypeInfo cond_ti = helper.GetTypeReference(value.condition.type); + if (value.condition.type is IGenericTypeInstance) + mi = TypeBuilder.GetMethod(cond_ti.tp, typeof(Nullable<>).GetMethod("get_HasValue")); + else + mi = cond_ti.tp.GetMethod("get_HasValue"); + il.Emit(OpCodes.Call, mi); + il.Emit(OpCodes.Ldc_I4_0); + il.Emit(OpCodes.Ceq); + } + else + { + tmp_lb = il.DeclareLocal(helper.GetTypeReference(value.type).tp); + il.Emit(OpCodes.Stloc, tmp_lb); + il.Emit(OpCodes.Ldloc, tmp_lb); + il.Emit(OpCodes.Ldnull); + il.Emit(OpCodes.Ceq); + } + } diff --git a/TestSuite/double_question3.pas b/TestSuite/double_question3.pas new file mode 100644 index 000000000..a09704683 --- /dev/null +++ b/TestSuite/double_question3.pas @@ -0,0 +1,8 @@ +begin + var i: integer?; + var x := i ?? 0; + assert(x.Value = 0); + i := 1; + x := i ?? 0; + assert(x.Value = 1); +end. \ No newline at end of file