Bug 59400 - Wrong interface method is called (two interfaces, covariance)
Summary: Wrong interface method is called (two interfaces, covariance)
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT (show other bugs)
Version: master
Hardware: PC All
: --- major
Target Milestone: ---
Assignee: Alexis Christoforides
URL:
Depends on:
Blocks:
 
Reported: 2017-09-11 20:24 UTC by nasonov256
Modified: 2018-03-19 13:37 UTC (History)
6 users (show)

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


Attachments
Code demonstrating bug effect (2.97 KB, text/plain)
2017-09-11 20:24 UTC, nasonov256
Details
Code demonstrating bug effect (2.96 KB, text/plain)
2017-09-11 20:55 UTC, nasonov256
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 nasonov256 2017-09-11 20:24:57 UTC
Created attachment 24687 [details]
Code demonstrating bug effect

I have a class implementing two interfaces: IEnumerator<ValueHolder<Value>> and IEnumerator<IValue<Value>>, where its arguments are declared as:

interface IValue<out TValue> {}
struct ValueHolder<TValue>: IValue<TValue> {}
class Value: ValueBase {}

The object is casted to IEnumerator<IValue<ValueBase>>. Only IEnumerator<IValue<Value>> can be used here, but when I call Current property, the corresponding property from IEnumerator<ValueHolder<Value>> is used resulting in unexpected program behavior. See attached file for more details.

The problem is reproduced with Mono 5.2 x64 system, both Windows and Linux. No problem occurs with Microsoft .NET.

=====================================================================================================

Mono JIT compiler version 4.2.1 (Debian 4.2.1.102+dfsg2-7ubuntu4)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
    TLS:           __thread
    SIGSEGV:       altstack
    Notifications: epoll
    Architecture:  amd64
    Disabled:      none
    Misc:          softdebug
    LLVM:          supported, not enabled.
    GC:            sgen

IEnumerator<IValue<TValue>>.Current is called (correct)
IEnumerator<IValue<TValue>>.Current is called (correct)
IEnumerator<IPair<TKey, TValue>>.Current is called (correct)
IEnumerator<IPair<TKey, TValue>>.Current is called (correct)

=====================================================================================================

Mono JIT compiler version 5.2.0.215 (tarball Mon Aug 14 15:46:12 UTC 2017)
Copyright (C) 2002-2014 Novell, Inc, Xamarin Inc and Contributors. www.mono-project.com
    TLS:           __thread
    SIGSEGV:       altstack
    Notifications: epoll
    Architecture:  amd64
    Disabled:      none
    Misc:          softdebug
    LLVM:          supported, not enabled.
    GC:            sgen (concurrent by default)

IEnumerator<ValueHolder<TValue>>.Current is called (incorrect)
IEnumerator<ValueHolder<TValue>>.Current is called (incorrect)
IEnumerator<Pair<TKey, TValue>>.Current is called (incorrect)

Unhandled Exception:
System.NullReferenceException: Object reference not set to an instance of an object
  at MonoBug.IncorrectEnumerator`2[TKey,TValue].System.Collections.Generic.IEnumerator<MonoBug.Pair<TKey,TValue>>.get_Current () [0x00027] in <68f182b83f4d4d13afbd236c63aa1a87>:0
  at MonoBug.Program.Main (System.String[] args) [0x0002f] in <68f182b83f4d4d13afbd236c63aa1a87>:0
[ERROR] FATAL UNHANDLED EXCEPTION: System.NullReferenceException: Object reference not set to an instance of an object
  at MonoBug.IncorrectEnumerator`2[TKey,TValue].System.Collections.Generic.IEnumerator<MonoBug.Pair<TKey,TValue>>.get_Current () [0x00027] in <68f182b83f4d4d13afbd236c63aa1a87>:0
  at MonoBug.Program.Main (System.String[] args) [0x0002f] in <68f182b83f4d4d13afbd236c63aa1a87>:0
Comment 1 nasonov256 2017-09-11 20:55:05 UTC
Created attachment 24688 [details]
Code demonstrating bug effect

Updated code
Comment 2 Ludovic Henry 2017-09-21 21:18:42 UTC
I can reproduce with Mono 5.6.0.83 (2017-08/48e94bd5f61) and Mono 5.7.0 (master/2e08526b14c).
Comment 3 Alexis Christoforides 2017-11-21 03:49:00 UTC
Candidate fix at https://github.com/mono/mono/pull/6034
Comment 4 Ludovic Henry 2018-02-21 15:46:51 UTC
Fixed in master with https://github.com/mono/mono/commit/a85911f480e675900c4584640f14c86321573454