Bug 313498 (MONO52597) - [PATCH] We generate invalid code for using idisposable structs
Summary: [PATCH] We generate invalid code for using idisposable structs
Status: RESOLVED FIXED
Alias: MONO52597
Product: Mono: Compilers
Classification: Mono
Component: C# (show other bugs)
Version: unspecified
Hardware: Other Other
: P3 - Medium : Normal
Target Milestone: V1
Assignee: Mono Bugs
QA Contact: Mono Bugs
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2004-01-05 06:45 UTC by Ben Maurer
Modified: 2007-09-15 21:24 UTC (History)
0 users

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments
proposed patch which fixes the first testcase (978 bytes, patch)
2004-01-05 08:58 UTC, Thomas Wiest
Details | Diff
new patch which passes the tests (1.25 KB, patch)
2004-01-05 23:20 UTC, Thomas Wiest
Details | Diff
new patch which fixes the issues of the last patch (1.52 KB, patch)
2004-01-06 01:55 UTC, Thomas Wiest
Details | Diff
patch for the second problem (1.47 KB, patch)
2004-01-07 22:08 UTC, Thomas Wiest
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Thomas Wiest 2007-09-15 18:23:19 UTC


---- Reported by bmaurer@users.sf.net 2004-01-04 23:45:45 MST ----

using System;

struct Blah : System.IDisposable {
	public void Dispose () {
		Console.WriteLine ("foo");
	}
}

class B  {
	static void Main () {
		using (Blah b = new Blah ()) {
			Console.WriteLine ("...");
		}
	}
}

When compiled and run on Mono we get,

** ERROR **: Invalid IL code at IL001d in .B:Main (): IL_001d: ldloc.0

The IL in question is:

          IL_0017:  ldloc.0
          IL_0018:  brfalse IL_0023
 
          IL_001d:  ldloc.0
          IL_001e:  callvirt instance void class
[mscorlib]'System.IDisposable'::'Dispose'()
          IL_0023:  endfinally

MS generates:

      IL_0014:  ldloca.s   V_0
      IL_0016:  call       instance void Blah::Dispose()
      IL_001b:  endfinally



---- Additional Comments From bmaurer@users.sf.net 2004-01-04 23:53:22 MST ----

Related,

using System;
using System.Collections;

struct Blah : IEnumerable {
	IEnumerator IEnumerable.GetEnumerator () {
		return null;
	}
}

class B  {
	static void Main () {
		foreach (object o in new Blah ())
			;
	}
}

Makes the runtime freeze. It makes the MS runtime throw an
ExecutionEngineExceptioon. The IL is invalid, again we treat it like a
reference type.

We need a few test cases 
1) IDisposable struct -- regular impl, explicit impl
2) IEnumerable struct -- regulare / explicit impl
3) IEnumerable returning a strongly typed struct -- regular/explicit



---- Additional Comments From pcgod@gmx.net 2004-01-05 01:58:20 MST ----

Created an attachment (id=165420)
proposed patch which fixes the first testcase




---- Additional Comments From bmaurer@users.sf.net 2004-01-05 07:57:31 MST ----

Hmmm, this looks like it could cause regressions (if the object is 
not idisposable, it looks like it might thow an exception). Did it 
pass all of mcs/tests?

If we do not have tests for what we are currently able to do, we need 
to write those first, and then do the fix, so we dont regress 
anything.



---- Additional Comments From pcgod@gmx.net 2004-01-05 16:20:25 MST ----

Created an attachment (id=165421)
new patch which passes the tests




---- Additional Comments From bmaurer@users.sf.net 2004-01-05 16:35:54 MST ----

THis does not handle the case where the ValueType does not inherit
IDisposable.

Also, we do not handle the case where it does inherit IDisposable, but
only as an explicit impl.

You do not handle if there are multiple overloads for Dispose

You should not emit a callvirt on a struct



---- Additional Comments From pcgod@gmx.net 2004-01-05 18:55:29 MST ----

Created an attachment (id=165422)
new patch which fixes the issues of the last patch




---- Additional Comments From miguel@ximian.com 2004-01-07 00:36:48 MST ----

Fantastic job once again guys.  

I have now applied this patch.



---- Additional Comments From bmaurer@users.sf.net 2004-01-07 00:59:22 MST ----

Miguel, this only covers the using case. The IEnumerator one is not
fixed (same problem, different section of code).



---- Additional Comments From pcgod@gmx.net 2004-01-07 15:08:27 MST ----

Created an attachment (id=165423)
patch for the second problem




---- Additional Comments From martin@ximian.com 2004-04-29 13:19:07 MST ----

Fixed in CVS.

Imported an attachment (id=165420)
Imported an attachment (id=165421)
Imported an attachment (id=165422)
Imported an attachment (id=165423)

Unknown operating system unknown. Setting to default OS "Other".