Bizarre find() error [D2]

Jonathan M Davis jmdavisProg at gmail.com
Tue Feb 23 19:10:55 PST 2010


bearophile wrote:

> Jonathan M Davis:
>> This is starting to look like a bug in DMD rather than my code. That is,
>> unless I'm just totally misunderstanding something about how imports
>> work. As it is, it just seems downright weird.
> 
> D2 module system has several bugs, but I can't know if the bug is in your
> code, in DMD, or it comes from misunderstanding of D. Can you please
> reduce the problem to some minimal, and show here the code of little
> modules that cause the bug? If it's a DMD bug it can then be filed in
> bugzilla.
> 
> Bye,
> bearophile

Okay. The first module:

module main;

import std.algorithm;
import std.stdio;
import other;

void main(string[] args)
{
    //auto f = find(args, "");
    writefln("%s", contains(args, "hello"));
}


The second module:

module other;

import std.algorithm;
import std.array;

bool contains(string[] list, string str)
{
    return find(list, str).empty;
}


If the commented out line in main() is uncommented, then the code compiles. Otherwise, you get this lovely error:

/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/typecons.d(424): Error: static assert  (is(Tuple!(string,float) == Tuple!(string,float))) is false
/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/typecons.d(413):        instantiated from here: Tuple!(string,float)
/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/typecons.d(423):        instantiated from here: slice!(1,3)
/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/algorithm.d(1090):        3 recursive instantiations from here: Tuple!(string[],uint)
/home/jmdavis/Downloaded_Files/dmd/dmd2/linux/bin/../../src/phobos/std/algorithm.d(1205):        instantiated from here: FindResult!(string[],string)
other.d(8):        instantiated from here: find!("a == b",string[],string)


If you move the function in other.d into main.d (and add the import for
std.array), then it works. It doesn't matter whether the commented out line
is before or after the function call, or even whether it's in the same function.
It doesn't matter whether the find is for the same types or not - trying to
find an int in an int[] in main.d fixes the problem as well. If I add yet another
module, like so:

module main;

import std.algorithm;
import std.stdio;
import other1;

void main(string[] args)
{
    //auto f = find(args, "");
    writefln("%s", contains(args, "hello"));
}

The second module:

module other1;

bool contains(string[] list, string str)
{
    return contains2(list, str);
}

The third module:

module other2;

import std.algorithm;
import std.array;

bool contains2(string[] list, string str)
{
    return find(list, str).empty;
}

then the main module still has to have the find call, or the code doesn't
compile. I can even make main() empty so that nothing is called, and it still
won't compile - even if I add a find call in other1 (it doesn't matter whether
main.d imports other1 either).

After messing around with my build flags though, I _can_ say that it seems to have
something to do with the -unittest flag. If that flag isn't used, then there's no
problem. If it is used, then I get the compilation error. From what I see, it looks
like this is a bug which needs to be reported as opposed to anything wrong with
my code.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list