Skip to content

link all removes interface implementation causing InvalidCastException #9566

Closed
@rolfbjarne

Description

@rolfbjarne

Steps to Reproduce

Test code:

interface I {
	void Foo ();
	void Bar ();
}

class A : I {
	public void Foo () { }
	public void Bar () { }
}

class B : I {
	public void Foo () { }
	public void Bar () { }
}

public class InterfaceTest {
	static Type type_a = typeof (A);
	static Type type_b = typeof (B);
	static Type type_i = typeof (I);

	static void F (I i)
	{
		i.Bar ();
	}

	public static void Bug10866 ()
	{
		var a = new A ();
		a.Foo ();
		a.Bar ();
		F (new A ());
		I [] Ifaces = (I []) (object) new B [0];
	}
}

We also have a unit test for this: 93eb592

The problem is that the linker removed the I interface from the B class, when it's not safe to do so.

Ref: dotnet/linker#1459
Ref: dotnet/linker#1305

Expected Behavior

No exception

Actual Behavior

System.InvalidCastException: Specified cast is not valid.
  at InterfaceTest.Bug10866 () [0x00020] in /Users/rolf/Projects/TestApp/test-app/AppDelegate.cs:78 
  at AppDelegate.Tapped () [0x00001] in /Users/rolf/Projects/TestApp/test-app/AppDelegate.cs:91 
  at AppDelegate.<FinishedLaunching>b__4_0 (Foundation.NSTimer v) [0x00000] in /Users/rolf/Projects/TestApp/test-app/AppDelegate.cs:100 
  at Foundation.NSTimerActionDispatcher.Fire (Foundation.NSTimer timer) [0x00000] in /Users/rolf/work/maccore/msbuild/xamarin-macios/src/Foundation/NSAction.cs:105 
  at (wrapper managed-to-native) UIKit.UIApplication.UIApplicationMain(int,string[],intptr,intptr)
  at UIKit.UIApplication.Main (System.String[] args, System.IntPtr principal, System.IntPtr delegate) [0x00005] in /Users/rolf/work/maccore/msbuild/xamarin-macios/src/UIKit/UIApplication.cs:86 
  at UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0000e] in /Users/rolf/work/maccore/msbuild/xamarin-macios/src/UIKit/UIApplication.cs:65 
  at AppDelegate.Main (System.String[] args) [0x00001] in /Users/rolf/Projects/TestApp/test-app/AppDelegate.cs:121

Environment

xamarin-macios: 9a746d6

Example Project

TestApp-7b81c7d.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIf an issue is a bug or a pull request a bug fixiOSIssues affecting iOSmacOSIssues affecting macOS

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions