[Issue 6638] New: Suggestions/error messages for misuses of for/foreach

d-bugmail at puremagic.com d-bugmail at puremagic.com
Fri Sep 9 11:25:10 PDT 2011


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

           Summary: Suggestions/error messages for misuses of for/foreach
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: diagnostic
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-09-09 11:24:56 PDT ---
Languages and compilers copy each other all the time. This is a low-priority
diagnostic enhancement request inspired by Rust compiler:

https://github.com/graydon/rust/wiki/Error-reporting

> Use of for where for each was meant.
> 
> for (v in foo.iter()) // suggest "for each"


This is wrong D code (in my code the mistakes #5 and #7 are common enough):


void main() {
    for (i; 0 .. 10) {} // #1
    int[5] a;
    for (x; a) {} // #2
    foreach (int = 0; i < 10; i++) {} // #3
    foreach (i, 0 .. 10) {} // #4
    foreach (i, x, a) {} // #5
    foreach (i; x, a) {} // #6
    foreach (i; x; a) {} // #7
}


DMD 2.055 gives:

test.d(2): found '..' when expecting ';' following for condition
test.d(4): found ')' when expecting ';' following for condition
test.d(5): found 'foreach' when expecting ')'
test.d(5): found '=' when expecting '.' following int
test.d(5): found '0' when expecting identifier following 'int.'
test.d(5): found ';' when expecting ')'
test.d(5): found 'i' when expecting ';' following statement
test.d(5): found '<' instead of statement
test.d(5): found ')' when expecting ';' following statement
test.d(6): basic type expected, not 0
test.d(6): no identifier for declarator int
test.d(6): found '0' when expecting ';'
test.d(6): expression expected, not '..'
test.d(6): found '10' when expecting ')'
test.d(6): found ')' instead of statement
test.d(7): Declaration expected, not 'foreach'
test.d(8): no identifier for declarator x
test.d(8): semicolon expected, not ')'
test.d(8): Declaration expected, not ')'
test.d(9): no identifier for declarator x
test.d(9): no identifier for declarator a


In some (or all) such 7 situations the D compiler can generate specific better
error messages, with a suggestion.

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


More information about the Digitalmars-d-bugs mailing list