Issue 9724 - Range predicates are not restrictive enough to justify assumptions made in Phobos code
Summary: Range predicates are not restrictive enough to justify assumptions made in Ph...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: phobos (show other issues)
Version: D2
Hardware: All All
: P3 normal
Assignee: monarchdodra
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-03-14 15:22 UTC by timon.gehr
Modified: 2024-12-01 16:16 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description timon.gehr 2013-03-14 15:22:16 UTC
DMD/Phobos 2.062:

Eg. the following breaks most of std.range, and most of std.algorithm could likely be broken too, but I am too lazy to investigate.

import std.range, std.algorithm;

struct TrollFace{
    @property string front()const{ return "troll"; }
    @property string back()const{ return "face"; }
    @property bool empty()const{ return true; }
    void popFront()const{ }
    void popBack()const{ }
    @property inout(TrollFace) save()inout{ return this; }
    auto opIndex(size_t index){ return front; }
    @property size_t length()inout{ return 0; }
    int* x;
}
struct TrollierFace{
    TrollFace face;
    alias face this;
    @disable this(this);
    @property inout(TrollierFace) save()inout{ return inout(TrollierFace)(face); }
}

void main(){
    immutable TrollFace a,b,c;
    a.retro();
    a.stride(2);
    chain(a,b,c);
    roundRobin(a,b,c);
    a.radial();
    a.radial(0);
    a.take(2);
    (immutable(TrollierFace)()).takeExactly(2);
    (immutable(TrollierFace)()).takeOne();
    (immutable(TrollierFace)()).takeNone();
    (immutable(TrollierFace)()).drop(2);
    (immutable(TrollierFace)()).dropExactly(0);
    (immutable(TrollierFace)()).dropOne();
    (immutable(TrollierFace)()).repeat();
    a.cycle();
    a.zip(b);
    lockstep((immutable(TrollierFace)()),b);
    a.frontTransversal();
    a.transversal(0);
    a.indexed([0]);
    a.chunks(5);
    a.filter!(a=>true);
    a.map!(a=>a);
    // ... (and so on)
}
Comment 1 monarchdodra 2013-03-15 04:16:33 UTC
Predicates? Did you mean "traits" or "restrictions" ?

Either way, I don't think that's the problem, as TrollFace (and immutable TrollFace) are both 100% legit Ranges.

The problem lies in the implementation that attempts to be immutable aware, and tries to cast away immutability via copying. Which it can't.

The solution is to simply strip range of all its unqual code.
Comment 2 timon.gehr 2013-03-15 04:54:07 UTC
(In reply to comment #1)
> Predicates? Did you mean "traits" or "restrictions" ?

They are predicates, mappings from types to bool.

>
> Either way, I don't think that's the problem, as TrollFace (and immutable
> TrollFace) are both 100% legit Ranges.
> 
> The problem lies in the implementation that attempts to be immutable aware, and
> tries to cast away immutability via copying. Which it can't.
> 
> The solution is to simply strip range of all its unqual code.

I think TrollierFace will still break many of them.
Comment 3 monarchdodra 2013-03-15 06:21:39 UTC
(In reply to comment #2)
> (In reply to comment #1)
> > Predicates? Did you mean "traits" or "restrictions" ?
> 
> They are predicates, mappings from types to bool.

Ah OK, I was confused, given the term is often used in algorithms for a specific context.

> > Either way, I don't think that's the problem, as TrollFace (and immutable
> > TrollFace) are both 100% legit Ranges.
> > 
> > The problem lies in the implementation that attempts to be immutable aware, and
> > tries to cast away immutability via copying. Which it can't.
> > 
> > The solution is to simply strip range of all its unqual code.
> 
> I think TrollierFace will still break many of them.

Hum. My testing shows that once you "fix" the ranges, then the code mostly just fails at the call side, since all these functions take by value. But had they taken by ref, then the error would be inside the implementation, so good catch.

I think there is a bug in isForwardRange. It should test at the very least that the result of save can be used to instantiate a new Range (eg: R r2 = r.save;). If we don't have this, then being able to call save is mostly pretty irrelevant...

...either that, or too restrictive to be useful anyways.

I had a fix prepared for another un-related bug in isForwardRange, so I'll also do this.
Comment 4 dlangBugzillaToGithub 2024-12-01 16:16:57 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

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

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