Skip to content

Commit

Permalink
[mini] Runtime check if boxing is needed for DIM constrained calls (m…
Browse files Browse the repository at this point in the history
…ono/mono#15729)

This is to handle the following example.  The issue is that in Check() we have
a constrained call where the called method is IAdder`1<!!U>::PlusPlus() which
is a default interface method, and we need to determine at runtime whether to
box the this argument.

```
using System;

public interface IAdder<T> {
	int Add (int x);

	int PlusPlus () {
		return Add (1);
	}
}

interface IGen3<T> { }

struct Adder<T> : IAdder<IGen3<T>> {
	int _field;
	public int Add (int x) {
		_field = x + _field;
		return _field;
	}
}

public class P {
	public static int Check<T, U>(T t) where T : IAdder<U> {
		return t.PlusPlus () + t.PlusPlus ();
	}

	public static void Main () {
		var x = new Adder<object> ();
		int y = Check<Adder<object>, IGen3<object>> (x);
		Console.WriteLine ("expect 2, got {0}", y);
	}
}
```

Commit migrated from mono/mono@3f55bfb
  • Loading branch information
lambdageek authored Jul 18, 2019
1 parent 614672c commit c7f5916
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/mono/mono/mini/method-to-ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -5636,10 +5636,12 @@ handle_constrained_call (MonoCompile *cfg, MonoMethod *cmethod, MonoMethodSignat
* A simple solution would be to box always and make a normal virtual call, but that would
* be bad performance wise.
*/
if (mono_class_is_interface (cmethod->klass) && mono_class_is_ginst (cmethod->klass)) {
if (mono_class_is_interface (cmethod->klass) && mono_class_is_ginst (cmethod->klass) &&
(cmethod->flags & METHOD_ATTRIBUTE_ABSTRACT)) {
/*
* The parent classes implement no generic interfaces, so the called method will be a vtype method, so no boxing neccessary.
*/
/* If the method is not abstract, it's a default interface method, and we need to box */
need_box = FALSE;
}

Expand Down
1 change: 0 additions & 1 deletion src/mono/mono/tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,6 @@ KNOWN_FAILING_TESTS = \
appdomain-marshalbyref-assemblyload.exe \
abort-try-holes.exe \
threads-init.exe \
dim-constrainedcall.exe \
gptail1.exe \
itaili1.exe \
sirtail1.exe \
Expand Down

0 comments on commit c7f5916

Please sign in to comment.