Bug 10866 - [PATCH] Linker does not remove unused method when implements unused interface method
Summary: [PATCH] Linker does not remove unused method when implements unused interface...
Status: RESOLVED FIXED
Alias: None
Product: iOS
Classification: Xamarin
Component: Tools ()
Version: 6.3.x
Hardware: PC Mac OS
: --- normal
Target Milestone: Untriaged
Assignee: Sebastien Pouliot
URL:
Depends on:
Blocks:
 
Reported: 2013-03-04 02:41 UTC by Marek Safar
Modified: 2013-03-05 11:06 UTC (History)
1 user (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 Developer Community or GitHub 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 Marek Safar 2013-03-04 02:41:20 UTC
Sample which reproduces the problem

using System;

interface I
{
	void Foo ();
}

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

class B : I
{
	// This method is not used but it's not removed
	public void Foo ()
	{
	}
}

public class Test
{
    public static void Main ()
	{
		new A ().Foo ();
		new B ();
	}
}

Suggested fix, which helps reduce mscorlib size further (tested with the latest mscorlib)


index 804d9d6..b484b3d 100644
--- a/mcs/tools/linker/Mono.Linker.Steps/MarkStep.cs
+++ b/mcs/tools/linker/Mono.Linker.Steps/MarkStep.cs
@@ -843,6 +843,9 @@ namespace Mono.Linker.Steps {
                                return;
 
                        foreach (MethodDefinition base_method in base_methods) {
+                               if (base_method.DeclaringType.IsInterface)
+                                       continue;
+                               
                                MarkMethod (base_method);
                                MarkBaseMethods (base_method);
                        }
Comment 1 Sebastien Pouliot 2013-03-04 17:08:34 UTC
Maybe I do not completely follow but:

1) Applying the above patch does not remove B::Foo;

2) It would be invalid. IOW if `Foo` is removed from `B` then it won't be implementing `I`;

3) Even if the patch worked (to remove B::Foo) it would be easy to break, e.g. add:

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

and call:

	F (new B ());

Scanning `F` will mark `I.Foo`, not B.Foo, so the code will be invalid.
Comment 2 Marek Safar 2013-03-04 17:12:40 UTC
I am not sure how you ran it or maybe you applied it incorrectly. The patch removes B::Foo and obviously it removes I:Foo too otherwise the code would be invalid. So the output binary has only A:Foo and empty I interface.
Comment 3 Marek Safar 2013-03-04 17:16:31 UTC
Forget to comment about 3.

That would not break the patch at all. It'd mark I:Foo because you are calling it from inside F so the linker see I:Foo used.
Comment 4 Sebastien Pouliot 2013-03-04 17:28:13 UTC
My bad, B::Foo was not removed because the unit test I wrote preserved the `F` method (1) and since I::Foo is not marked it means `I` is satisfied (2) and if it's marked by `F` (like my mistake) then `B:Foo` is kept (3) and valid.

Please commit and bump - I'll commit the unit tests (to linkall.app) afterward.
Comment 5 Marek Safar 2013-03-05 11:06:17 UTC
Fixed in master, with extra check for broken compilers