Bug 20040 - Issue with variable reuse in generated IL
Summary: Issue with variable reuse in generated IL
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: 3.2.x
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2014-05-26 07:04 UTC by jameswilddev
Modified: 2014-05-26 13:05 UTC (History)
1 user (show)

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:

Description jameswilddev 2014-05-26 07:04:34 UTC

I've been writing a vector mathematics library as a project and noticed that a test that passes on Visual Studio fails on Xamarin Studio.  Indeed, it appears that the result of a calculation is completely out.  On review of the generated code, it would appear the Mono compiler is overwriting a variable with intent to use it later on:

Code entered:
var closest = ray.Start.Add( ray.Normal.Multiply( along ) );

Generated code according to DotPeek:
TVector vector1 = ray.Start; 
// ISSUE: explicit reference operation 
// ISSUE: variable of a reference type 
TVector& local1 = @vector1; 
vector1 = ray.Normal; 
TVector to = vector1.Multiply(multiply1); 
// ISSUE: explicit reference operation 
float num1 = VectorExtensions.Magnitude((^local1).Add(to));

If my understanding is correct here, it is storing ray.Start in vector1, taking a reference to vector1, overwriting vector1 with ray.Normal, then expecting the referenced value to still equal ray.Start later on.

On the other hand, if I split this out into two statements which should be functionally identical:
var offset = ray.Normal.Multiply( along ); 
var closest = ray.Start.Add( offset );

The generated code according to DotPeek is much simpler:
float multiply1 = VectorExtensions.Dot(new TVector().Subtract(ray.Start), ray.Difference) / ray.Length; 
TVector to1 = ray.Normal.Multiply(multiply1); 
float num1 = VectorExtensions.Magnitude(ray.Start.Add(to1));

And the result matches that in Visual Studio.

I can't reproduce this issue outside of this method, but I've branched specifically for it, and have noticed it happening elsewhere in the same method.

The test which fails as a result of this bug is SUNRUSE.Geometry.RoundTests.EntryButStopsBeforeOrigin.

Thanks for reading.
Comment 1 Marek Safar 2014-05-26 12:51:28 UTC
Fixed in master
Comment 2 jameswilddev 2014-05-26 13:05:54 UTC
Thank you!  (https://github.com/mono/mono/commit/122a67e4c88064d4a67434e59fd9191260bfcd91)  I'm on the beta channel for Xamarin Studio, which seems to get updates fairly often.  I'll be sure to retest this the next time an update comes through for it.

Thanks for all the great work you guys are doing on this platform.