diff --git a/NETGenerator/NETGenerator.cs b/NETGenerator/NETGenerator.cs index 3c6cf4802..453723ca5 100644 --- a/NETGenerator/NETGenerator.cs +++ b/NETGenerator/NETGenerator.cs @@ -11046,7 +11046,10 @@ namespace PascalABCCompiler.NETGenerator is_dot_expr = tmp_is_dot_expr; is_addr = tmp_is_addr; il.Emit(OpCodes.Brtrue, NullLabel); - il.Emit(OpCodes.Ldloc, tmp_lb); + if (value.condition.type.is_nullable_type) + il.Emit(OpCodes.Ldloca, tmp_lb); + else + il.Emit(OpCodes.Ldloc, tmp_lb); TypeInfo ti = helper.GetTypeReference(value.condition.type); if (ti != null) EmitBox(value.condition, ti.tp); diff --git a/TestSuite/nullable14.pas b/TestSuite/nullable14.pas new file mode 100644 index 000000000..095b2bdbf --- /dev/null +++ b/TestSuite/nullable14.pas @@ -0,0 +1,13 @@ +function f1 := 1; + +function p1(n: integer?): integer; +begin + Result := (n??f1).Value; +end; + +begin + // OK + assert(p1(nil) = 1); + // AccessViolationException + assert(p1(5) = 5); +end. \ No newline at end of file