Thread overview
[Issue 11261] New: Can't infer types without explicit slice in foreach
Oct 14, 2013
Jesse Phillips
Oct 14, 2013
Jesse Phillips
Oct 15, 2013
Kenji Hara
Oct 15, 2013
Kenji Hara
Oct 15, 2013
Walter Bright
October 14, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11261

           Summary: Can't infer types without explicit slice in foreach
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: regression
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: Jesse.K.Phillips+D@gmail.com


--- Comment #0 from Jesse Phillips <Jesse.K.Phillips+D@gmail.com> 2013-10-14 10:54:11 PDT ---
When using a range to proved a key-value tuple, the compiler is unable to infer
the key-value in a foreach when slice is not present (2). Using an array
continues to work (1). This functioned in 2.63.

import std.typecons;
import std.range;

class Container {
    alias Tuple!(string, "key", string, "value") Key;

    InputRange!Key opSlice() {
    // 1 Key[] opSlice() {
        Range r;
        return inputRangeObject(r);
        // 1 return [Key("myKey", "My Value")];
    }
    private struct Range {
        enum empty = false;
        auto popFront() {
        }
        auto front() {
            return Key("myKey", "myValue");
        }
    }
}

void main() {
    auto container = new Container();
    foreach(k,v; container)
    // 2 foreach(k,v; container[])
    { break; }
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 14, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11261


Jesse Phillips <Jesse.K.Phillips+D@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |Jesse.K.Phillips+D@gmail.co
                   |                            |m


--- Comment #1 from Jesse Phillips <Jesse.K.Phillips+D@gmail.com> 2013-10-14 11:36:46 PDT ---
While the version with an explicit slice does infer types, it is inferring the wrong ones.

k = uint
v = Tuple!(string, "key", string, "value")

This is consistent with 2.63, without the explicit slice the types are

k = string
v = string

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11261


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull, rejects-valid


--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2013-10-14 20:39:43 PDT ---
https://github.com/D-Programming-Language/dmd/pull/2668

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11261



--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2013-10-14 20:44:15 PDT ---
(In reply to comment #1)
> While the version with an explicit slice does infer types, it is inferring the wrong ones.
> 
> k = uint
> v = Tuple!(string, "key", string, "value")
> 
> This is consistent with 2.63, without the explicit slice the types are
> 
> k = string
> v = string

In the first case (foreach (k, v; container)), both k and v should be inferred
to string.

However in the second case (foreach (k, v; container[])) you _cannot_ get same
inference result. Because std.range.InputRange interface defines an opApply
with indexing (size_t + KeyType), and it is preferentially used for foreach
argument type deduction.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11261



--- Comment #4 from github-bugzilla@puremagic.com 2013-10-15 00:58:43 PDT ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/800631a95964ff15a4dadb15b031f80222449031 fix Issue 11261 - Can't infer types without explicit slice in foreach

https://github.com/D-Programming-Language/dmd/commit/339deaa9be455c98aa00c4f0741c76b8774b0e41 Merge pull request #2668 from 9rnsr/fix11261

[REG2.064a] Issue 11261 - Can't infer types without explicit slice in foreach

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11261



--- Comment #5 from github-bugzilla@puremagic.com 2013-10-15 00:59:36 PDT ---
Commit pushed to 2.064 at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/718dfbb02f8ca7356e8f46637b2e90e2f81d96d0 Merge pull request #2668 from 9rnsr/fix11261

[REG2.064a] Issue 11261 - Can't infer types without explicit slice in foreach

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
October 15, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=11261


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


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