www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 8478] New: Turn some undefined pointer comparisons into compile-time errors

http://d.puremagic.com/issues/show_bug.cgi?id=8478

           Summary: Turn some undefined pointer comparisons into
                    compile-time errors
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid, diagnostic
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: bearophile_hugs eml.cc



Often the D language shares the semantic of C language. This D program compiles
with no errors or warnings:


struct Foo { int a, b; }
void main() {
    Foo f;
    if (&f.a < &f.b) {} // first comparison
    int a, b;
    if (&a < &b) {} // second comparison
}


The first comparison is correct, the addresses of a and b are guaranteed to be
in order by the C standard, see §6.5.8:5  (ISO/IEC JTC 1, SC 22, WG 14. ISO/IEC
9899:201x: Programming languages—C. Technical Report n1570, Intl. Organization
for Standardization, August 2011).

The second comparison is undefined in C according to the C standard §6.5.8:5.
Because semantics is given to relational pointer comparisons only where the two
addresses share a common base.

I think a compiler is free to reorder the position of the variables a and b on
the stack.

So on that D program I suggest the D compiler to give error similar to:

Line 6: Error: cannot apply '<' to different base objects.

See:
http://c-semantics.googlecode.com/files/2012-C-Semantics-Ellison-Rosu-POPL.pdf

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jul 30 2012