Issue 20309 - Passing alias this with function returning chain to tempCString hangs up the program
Summary: Passing alias this with function returning chain to tempCString hangs up the ...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: x86_64 Linux
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-10-21 10:55 UTC by Jan Jurzitza
Modified: 2024-12-01 16:35 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 Jan Jurzitza 2019-10-21 10:55:33 UTC
The following minimal test case will result in a binary which hangs up:

import std.range : chain;
import std.internal.cstring : tempCString;

struct Test {
    auto filename() const { return chain("a", "b"); }
    alias filename this;
}

void main() {
    auto name = Test();
    auto namez = name.tempCString!char();
    // should return fine or crash if doesn't exist, but not hang
}

See https://run.dlang.io/is/8AlJO5

This is a bug because with `name.filename.tempCString!char()` it works fine, so it has an issue with the alias this on that range.

This causes calls to the std.file functions like isFile or getAttributes to hang up if called with a struct like this.

This only happens when it is indirected via an alias this on the argument which is passed.

Another test to test it with public APIs: `std.file.getAttributes(Test())`
Comment 1 berni44 2019-12-08 09:50:30 UTC
Reduced example:

```
import std.stdio;
import std.range : chain;

struct Test 
{
    auto filename() const { return chain("a", "b"); }
    alias filename this;
}

void main() 
{
    auto name = Test();
    writeln(name.front);
    name.popFront();
    writeln(name.front);
}
```

name.front and name.popFront always call filename(), which returns a new range object... This is IMHO not a phobos bug.
Comment 2 Jan Jurzitza 2019-12-08 12:46:26 UTC
you are right, it makes sense that it creates a new object for each instantiation. However this would mean that maybe the isInputRange check or whatever tempCString and others are using, should not allow such a construct which always returns a new instance
Comment 3 berni44 2019-12-08 13:11:36 UTC
IMHO this is a problem burried deep into alias this. My solution would be, to remove alias this completely. Creates a lot of problems...
Comment 4 dlangBugzillaToGithub 2024-12-01 16:35:52 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/phobos/issues/9782

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