Bug 7571 - JIT miscompiles generics code where array access uses explicit interface implementation from value type
Summary: JIT miscompiles generics code where array access uses explicit interface impl...
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: JIT ()
Version: unspecified
Hardware: PC Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-10-01 12:19 UTC by Rodrigo Kumpera
Modified: 2012-10-03 07:26 UTC (History)
4 users (show)

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

Description Rodrigo Kumpera 2012-10-01 12:19:02 UTC
The JIT miscompiles the following piece of code:


using System;

struct Record : Foo<Record>.IRecord {
	int idx;
	int Foo<Record>.IRecord.Key { get { return idx; } }
}

class Foo<T> where T : Foo<T>.IRecord {
	public interface IRecord {
		int Key { get; }
	}

	public static int Extract (T[] t) {
		int a = t[0].Key;
		if (a > 10)
			return 100;
		else
			return 200;
	}
}


class Driver {
	static void Main () {
		Console.WriteLine (Foo<Record>.Extract (new Record [10]));
	}
}



What gets miscompiled is that Extract does box Record after we load it from the array.

If you move IRecord to be a top level type this doesn't happen.
Comment 1 Zoltan Varga 2012-10-02 20:53:44 UTC
What happens is if IRecord is global, the iface call gets resolved at JIT time and gets inlined, so there is no need to box.
Comment 2 Zoltan Varga 2012-10-03 03:34:45 UTC
This isn't a bug imho, we need to box the value in order to be able to call its iface method.
Comment 3 Marek Safar 2012-10-03 03:38:36 UTC
It's a bug. Here is longer version from Rodrigo which prints 

0
1

on .net



using System;


struct Record : Foo<Record>.IRecord {
	int counter;
	int Foo<Record>.IRecord.DoSomething () {
		return counter++;
	}
}

class Foo<T> where T : Foo<T>.IRecord {
	public interface IRecord {
		int DoSomething ();
	}

	public static int Extract (T[] t) {
		return t[0].DoSomething ();
	}
}


class Driver {
	static void Main () {
		var r = new Record [10];
		Console.WriteLine (Foo<Record>.Extract (r));
		Console.WriteLine (Foo<Record>.Extract (r));
	}
}
Comment 4 Zoltan Varga 2012-10-03 07:26:55 UTC
Fixed in master.