Bug 3420 - Stepping over return goes to wrong next statement
Summary: Stepping over return goes to wrong next statement
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Linux
: --- major
Target Milestone: ---
Assignee: Marek Safar
Depends on:
Reported: 2012-02-13 09:39 UTC by Marek Safar
Modified: 2012-02-14 14:10 UTC (History)
3 users (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 Marek Safar 2012-02-13 09:39:55 UTC
using System;

class Program
	public static void Main ()
		Test test = new Test ();

		test.Run ((i) => {
			switch (i) {
			case 0:
				return 0;	// Set a breakpoint here and F10 (step over)

			case 1:
				return 1;


			throw new Exception ("Unknow value");	// Debugger continues here

class Test
	public delegate int RunDelegate (int val);

	public void Run (RunDelegate test)
		test (0);
Comment 1 Zoltan Varga 2012-02-13 13:45:17 UTC
After the single step, the pc is at IL 0x37, but that line has no line number info, so we choose the last line which has one:

Line number table for method Program:<Main>m__0 (int):
IL0 -> /home/zovarga/work/Hello/Hello/Main.cs:9
IL1 -> /home/zovarga/work/Hello/Hello/Main.cs:10
IL8 -> /home/zovarga/work/Hello/Hello/Main.cs:12
IL1b -> /home/zovarga/work/Hello/Hello/Main.cs:15
IL27 -> /home/zovarga/work/Hello/Hello/Main.cs:18
IL2c -> /home/zovarga/work/Hello/Hello/Main.cs:21
IL38 -> /home/zovarga/work/Hello/Hello/Main.cs:22
IL38 -> /home/zovarga/work/Hello/Hello/Main.cs:22
Comment 2 Marek Safar 2012-02-14 12:03:35 UTC
I don't understand it

Interesting code

IL_000a:  br         IL_0037
IL_0031:  newobj     instance void [mscorlib]System.Exception::.ctor(string)
IL_0036:  throw
IL_0037:  ldloc.0
IL_0038:  ret

Sequence points

        <entry il="0x0" row="9" file_ref="1" hidden="false" />
        <entry il="0x1" row="10" file_ref="1" hidden="false" />
        <entry il="0x8" row="12" file_ref="1" hidden="false" />
        <entry il="0x1b" row="15" file_ref="1" hidden="false" />
        <entry il="0x27" row="18" file_ref="1" hidden="false" />
        <entry il="0x2c" row="21" file_ref="1" hidden="false" />
        <entry il="0x38" row="22" file_ref="1" hidden="false" />

Jumping to 0037 should not use entry for 0x2c, I'll check if I can emit sequence point at 0x37 instead of 0x38 but I think better heuristics would work here too.
Comment 3 Zoltan Varga 2012-02-14 12:19:51 UTC
The current heuristics is that we do a seq point when the IL stack is empty, and it is empty at 0x37, not 0x38. So it would be better if mcs used 0x37 too, since this is where logically the 'return 0' statement begins.
Comment 4 Marek Safar 2012-02-14 14:10:30 UTC
Fixed in master. The sequence point start at 0x37 now