D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 20258 - alias this produces an infinite range when used with ranges
Summary: alias this produces an infinite range when used with ranges
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-10-02 02:07 UTC by Andrej Mitrovic
Modified: 2024-12-13 19:05 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Andrej Mitrovic 2019-10-02 02:07:19 UTC
-----
import std.stdio;

struct Set
{
    int[int] set;
    auto walk () { return this.set.byKey(); }
    alias walk this;
}

void main()
{
    Set set;
    set.set[1] = 10;
    writefln("Set contents: %s", set.set.byKey());  // ok
    writefln("Set contents: %s", set);  // prints "1" infinitely
}
-----

Tested with v2.086.1 and v2.088.0
Comment 1 berni44 2019-12-03 10:09:45 UTC
I think, the bug is to be found in dmd and byKey is innocent. If not, the correct component is not phobos but druntime, because byKey is located there.
Comment 2 Andrej Mitrovic 2022-07-07 13:25:29 UTC
Simpler example:

-----
import std.stdio;
import std.range;

struct Set
{
    auto walk () { return [].only; }
    alias walk this;
}

void main()
{
    Set set;
    writeln(set);
}
-----

It has to do with ranges, not specifically hashmaps as previously thought.

I think somewhere in the formatter it's probably trying to invoke walk() multiple times, implicitly through that 'alias this'.
Comment 3 dlangBugzillaToGithub 2024-12-13 19:05:45 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/19625

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB