Bug 16900 - Nullable cast does strict cast first causing NullReferenceException
Summary: Nullable cast does strict cast first causing NullReferenceException
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 3.2.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-12-19 15:46 UTC by Jason Morse
Modified: 2014-01-02 12:53 UTC (History)
1 user (show)

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


Attachments
Sample program illustration issue (140.34 KB, application/x-zip-compressed)
2013-12-30 15:33 UTC, Jason Morse
Details


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:
Status:
RESOLVED FIXED

Description Jason Morse 2013-12-19 15:46:12 UTC
Upcasting from interface to nullable struct implementation causes Mono compiler to perform strict cast *before* nullable cast. When executing against standard Microsoft .NET 2.0 CLR this causes NullReferenceException (unsure about behavior when used against Mono runtime).

Sample project attached. Solution configurations are available for Microsoft (Debug)and Mono (Debug-Mono).

            IMyStruct myStruct = null;
            MyStruct? structValue;

            structValue = (Nullable<MyStruct>)myStruct;  // <-- NullReferenceException when compiled with Mono

            // Equivalent generated by csc.exe (v4.0)
            structValue = (MyStruct?)myStruct;

            // Equivalent generated by msc.exe (v3.2.3)
            structValue = new MyStruct?((MyStruct)myStruct);

    public struct MyStruct : IMyStruct { }

    public interface IMyStruct { }
Comment 1 Jason Morse 2013-12-19 15:59:40 UTC
Verified mono runtime exhibits same behavior.
Comment 2 Jason Morse 2013-12-30 15:33:08 UTC
Created attachment 5740 [details]
Sample program illustration issue

Visual Studio 2010 project/solution.
Comment 3 Marek Safar 2014-01-02 12:53:22 UTC
Fixed in master