Bug 10306 - Unexpected source locations in Mono AST
Summary: Unexpected source locations in Mono AST
Status: RESOLVED FIXED
Alias: None
Product: Compilers
Classification: Mono
Component: C# ()
Version: unspecified
Hardware: PC Windows
: --- normal
Target Milestone: ---
Assignee: Marek Safar
URL:
Depends on:
Blocks:
 
Reported: 2013-02-14 14:32 UTC by Brett van Swelm
Modified: 2013-03-13 11:51 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 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:
Status:
RESOLVED FIXED

Description Brett van Swelm 2013-02-14 14:32:36 UTC
Similar to https://bugzilla.xamarin.com/show_bug.cgi?id=9885, we are seeing more examples of incorrect source locations in the Mono AST. Six examples are included below, with appropriate commentary:

===== SourceLocations.cs =====
using System;

public struct Ps {
    public int si;
}

public class SourceLocations
{
    // 1) The pseudo-statement that is a static field initializer is said to start at
    // the type "object", not at the "static" modifier:
    static object foo = new object(); // static field

    unsafe public void MyMethod(int i, int[] q, Ps* ps) {
        // 2) Local declarations are said to start at the local name "foo", not at the
        // type "object":
        object foo = new object(); // local

        // 3) parenthesized expressions used as the receiver of a call tend to give the
        // location of the statement as the location of the inner expression, not that of
        // the lparen. Examples:
        ((Action)(()=>{}))();
        (i+i).ToString();
        (i is int).ToString();
        (-i).ToString();
        // etc

        // 4) Array indexer expressions used as the receiver of the call give the location
        // of the left bracket, not the array:
        q[0].ToString();

        // 5) Pointer dereferences via the arrow operator give the location of the arrow
        // rather than the location of the pointer:
        ps->si = 123;

        // 6) postfix increment and decrement give the location of the ++ or -- rather
        // than the variable:
        i++;
        i--;
    }
}
===== end =====

Thanks,

Brett van Swelm | Senior Engineer
Coverity | 185 Berry Street | Suite 6500, Lobby 3 | San Francisco, CA 94107
The Leader in Development Testing
Read our profile in Forbes, Coverity Gets Code Right 25% Faster
Comment 1 Marek Safar 2013-02-20 05:24:52 UTC
I am not sure I understand the bug report. Is this about some internal mcs location properties or is it actually something what the end user will experience?
Comment 2 Brett van Swelm 2013-03-01 14:46:04 UTC
We are interested in using the internal location properties, but assumed these
could reproduce like bug 9885.

In the example above we can forget about #1 (we don't *really* care about that one) and #2 appears to be our bug, not yours. But the rest can be reproduced with compiler warnings:

===== Test.cs =====
public class Test {
    public void Foo2() {
        return;
        object // <- warning correctly reported here
        foo
        =
        new
        object();
    }
    public void Foo3a(object o) {
        return;
        (
        (System.Action)o)(); // <- warning incorrectly reported here
    }
    public void Foo3b(int i) {
        return;
        (
        i // <- warning incorrectly reported here
        +
        i
        )
        .ToString();
    }
    public void Foo3c(object i) {
        return;
        (
        i
        is // <- warning incorrectly reported here
        int
        )
        .ToString();
    }
    public void Foo3d(int i) {
        return;
        (
        - // <- warning incorrectly reported here
        i
        )
        .ToString();
    }
    public void Foo4(int[] q) {
        return;
        q
        [ // <- warning incorrectly reported here
        0
        ]
        .ToString();
    }
    public struct MyStruct { public int si; }
    public unsafe void Foo5(MyStruct *ps) {
        return;
        ps
        ->
        si // <- warning incorrectly reported here
        =
        123;
    }
    public void Foo6a(int i) {
        return;
        i
        ++; // <- warning incorrectly reported here
    }
    public void Foo6b(int i) {
        return;
        i
        --; // <- warning incorrectly reported here
    }
}
===== end =====

$ mcs /unsafe /target:library Test.cs
Test.cs(4,9): warning CS0162: Unreachable code detected
Test.cs(13,9): warning CS0162: Unreachable code detected
Test.cs(18,9): warning CS0162: Unreachable code detected
Test.cs(28,9): warning CS0162: Unreachable code detected
Test.cs(36,9): warning CS0162: Unreachable code detected
Test.cs(44,9): warning CS0162: Unreachable code detected
Test.cs(54,9): warning CS0162: Unreachable code detected
Test.cs(61,9): warning CS0162: Unreachable code detected
Test.cs(66,9): warning CS0162: Unreachable code detected
Compilation succeeded - 9 warning(s)

$ grep -n // Test.cs
4:        object // <- warning correctly reported here
13:        (System.Action)o)(); // <- warning incorrectly reported here
18:        i // <- warning incorrectly reported here
28:        is // <- warning incorrectly reported here
36:        - // <- warning incorrectly reported here
44:        [ // <- warning incorrectly reported here
54:        si // <- warning incorrectly reported here
61:        ++; // <- warning incorrectly reported here
66:        --; // <- warning incorrectly reported here
Comment 3 Marek Safar 2013-03-13 11:51:30 UTC
Fixed in master