Bug 6385 – isInputRange!(ubyte[2u]) is a failure when used in global scope
Status
RESOLVED
Resolution
DUPLICATE
Severity
regression
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2011-07-26T11:18:00Z
Last change time
2011-09-05T14:37:06Z
Keywords
rejects-valid
Assigned to
nobody
Creator
schveiguy
Comments
Comment #0 by schveiguy — 2011-07-26T11:18:02Z
The following code incorrectly produces an error:
import std.range;
pragma(msg, isInputRange!(ubyte[2u]).stringof);
Error: template instance std.array.front!(ubyte[2u]) incompatible arguments for template instantiation
The code of isInputRange looks like this:
template isInputRange(R)
{
enum bool isInputRange = is(typeof(
{
R r; // can define a range object
if (r.empty) {} // can test for empty
r.popFront(); // can invoke popFront()
auto h = r.front; // can get the front of the range
}()));
}
It was my impression that is(typeof(...)) should just return false on compilation failure. I don't think isInputRange is incorrectly implemented.
The error is eliminated if you put the isInputRange call inside a function:
import std.range;
void main() {
pragma(msg, isInputRange!(ubyte[2u]).stringof);
}
outputs:
false
during compilation, and successfully compiles.
See related discussion here: http://www.digitalmars.com/webnews/newsgroups.php?art_group=digitalmars.D.learn&article_id=28422
Comment #1 by schveiguy — 2011-07-26T11:21:28Z
BTW, this compiles on 2.051, fails on 2.052
Comment #2 by clugdbug — 2011-08-26T04:37:04Z
Reduced test case. It seems this bug is quite complicated, it requires two semantic errors, plus uniform function call syntax, and a template constraint:
template template6385(T)
{
enum template6385 = false;
}
void bug6385(A)(A a) if (template6385!A)
{
this_is_an_error();
}
const bool bool6385 =
is(typeof(
{
ubyte[2] r;
this_is_another_error();
r.bug6385;
}()
));
The test case was accidentally fixed by commit:
https://github.com/D-Programming-Language/dmd/commit/dc83c
but I don't really understand why. It seems to be just luck.
Comment #3 by yebblies — 2011-08-27T04:06:24Z
(In reply to comment #2)
> The test case was accidentally fixed by commit:
> https://github.com/D-Programming-Language/dmd/commit/dc83c
> but I don't really understand why. It seems to be just luck.
dc83c prevents dmd from re-evaluating the template constraint when the args match an instance that already exists. Previously the constraint would be run unless all template args had a spec. You can see this by changing 'void bug6385(A)(A a) if (template6385!A)' to 'void bug6385(A : ubyte[2])(A a) if (template6385!A)'.
Slightly smaller test case:
void templ6385(A)(A a) if ( { return false; }() )
{
this_is_an_error();
}
const bool bool6385 =
is(typeof(
{
this_is_another_error();
templ6385(1);
}()
));
void main() {}
Using an older commit, it seems that the cause is re-running semantic on templ6385, as it's been added to the list of module members. This seems to be similar to issue 4302 (caused by the fix to bug 4042) but missed by the patch in 4302.
Changing
- if (!(sc->flags & SCOPEstaticif))
+ if (!(sc->flags & SCOPEstaticif) && !sc->intypeof)
from that patch seems to fix it, but I have no idea if there are any other missing cases.
My guess at a correct solution is that http://www.dsource.org/projects/dmd/changeset/477 is incorrect, and should instead remove the TemplateInstance from the module's list of members.
Comment #4 by code — 2011-09-05T14:31:14Z
*** This issue has been marked as a duplicate of issue 6602 ***
Comment #5 by code — 2011-09-05T14:37:06Z
I think the problem described above didn't occur if the isInputRange instantiation is inside a function, because then the instance was added to the global list later, during semantic3, and TemplateInstance::semantic3 immediately exists if this.error is set, whereas TemplateInstance::semantic doesn't bail out early.