Bug 23676 - Null coalescing operator with implicit cast trys to get value from a null nullable
Summary: Null coalescing operator with implicit cast trys to get value from a null nul...
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Mac OS
: Normal normal
Target Milestone: ---
Assignee: Marek Safar
: 23796 ()
Depends on:
Reported: 2014-10-08 01:39 UTC by jason
Modified: 2014-10-15 06:47 UTC (History)
6 users (show)

Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description jason 2014-10-08 01:39:14 UTC
Sometimes the null coalescing operator that contains an implicit cast on the left hand side trys to get a value from a null nullable. Thus issue has been observed when running in the iOS simulator and only occurs sometimes. I'm assuming this occurs when the method is jitted, hence why it is a bit non-deterministic.

The exception thrown is System.InvalidOperationException. The exception message is "Nullable object must have a value".

Simplified reproducible code is below:

double defaultValue = 1000;
int? nullableValue = null;

double value = nullableValue ?? defaultValue;

The work around is to declare defaultValue as an int.
Comment 1 Udham Singh 2014-10-08 07:08:09 UTC
I have checked this issue with help of code provided in bug description and getting the same error mentioned in bug description. Please refer the screencast : http://www.screencast.com/t/JDf1a6eWa

Application Output : https://gist.github.com/Udham1/8acd243c5917aae50589

Environment Info : 

=== Xamarin Studio ===

Version 5.5 (build 227)
Installation UUID: ce927b2a-2c07-44c5-b186-09cfdafba6dc
	Mono 3.10.0 ((detached/47db868)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 310000019

=== Apple Developer Tools ===

Xcode 6.0.1 (6528)
Build 6A317

=== Xamarin.iOS ===

Version: (Business Edition)
Hash: 959c1e4
Build date: 2014-10-03 00:25:37-0400

=== Build Information ===

Release ID: 505000227
Git revision: 7b721eeec7a2fa4c4f4de0ecd2aed4dc25edac95
Build date: 2014-10-02 15:53:38-04
Xamarin addins: 99ed56b428b31eba1efaace4d82188d6f334e6ca

=== Operating System ===

Mac OS X 10.9.4
Darwin Xamarin76s-Mac-mini.local 13.3.0 Darwin Kernel Version 13.3.0
    Tue Jun  3 21:27:35 PDT 2014
    root:xnu-2422.110.17~1/RELEASE_X86_64 x86_64
Comment 2 Rolf Bjarne Kvinge [MSFT] 2014-10-13 06:52:41 UTC
This looks like an mcs bug:

> csharp
Mono C# Shell, type "help;" for help

Enter statements below.
csharp> double defaultValue = 1000;
csharp> int? nullableValue = null;
csharp> double value = nullableValue ?? defaultValue;
(1,34): warning CS0429: Unreachable expression code detected
System.InvalidOperationException: Nullable object must have a value.
  at System.Nullable`1[System.Int32].get_Value () [0x00000] in <filename unknown>:0 
  at <InteractiveExpressionClass>.Host (System.Object& $retval) [0x00000] in <filename unknown>:0 
  at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) [0x00000] in <filename unknown>:0 
  at Mono.CSharpShell.Evaluate (System.String input) [0x00000] in <filename unknown>:0
Comment 3 Rolf Bjarne Kvinge [MSFT] 2014-10-13 06:52:46 UTC
> mono --version
Mono JIT compiler version 3.10.0 ((detached/53e3161 Wed Sep 24 17:40:55 EDT 2014)
Comment 4 Rolf Bjarne Kvinge [MSFT] 2014-10-14 07:39:08 UTC
*** Bug 23796 has been marked as a duplicate of this bug. ***
Comment 6 Marek Safar 2014-10-15 06:47:50 UTC
Already fixed in mono master. A fix backported to 3.10