Bug 59400 - Wrong interface method is called (two interfaces, covariance)
Summary: Wrong interface method is called (two interfaces, covariance)
Status: CONFIRMED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT (show other bugs)
Version: master
Hardware: PC All
: --- major
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2017-09-11 20:24 UTC by nasonov256
Modified: 2017-10-13 08:07 UTC (History)
5 users (show)

See Also:
Tags: bugpool
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

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).

Note You need to log in before you can comment on or make changes to this bug.