Thread overview
[Issue 8667] New: selective import breaks normal overload resolution
Sep 16, 2012
Dmitry Olshansky
Jan 10, 2013
Kenji Hara
Jan 10, 2013
Kenji Hara
Jul 10, 2013
Kenji Hara
September 16, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=8667

           Summary: selective import breaks normal overload resolution
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: critical
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: dmitry.olsh@gmail.com


--- Comment #0 from Dmitry Olshansky <dmitry.olsh@gmail.com> 2012-09-16 06:03:26 PDT ---
The consequences of this bug are commonly observed by unwary as spurious template instantation fails around std.string split and (previously) replace if std.regex is imported.

This happens because std.string publicly and selectively imports a bunch of functions from std.array and that brings about a pack of bad side effects.

(marked as critical as it cripples Phobos usage in a very unfriendly way)

The bug simplified:

//2 modules with unambigiuos template function
module m2;

void split(T)(T k)
    if(is(T : int)){}

//second one
module m;

void split(T)(T k)
    if(is(T : string))
{
}

//another one to call them
import m;
import m2: split; //removing : split makes it work

void main(){
    split("abc");
    split(123);
}

Output:
tryit.d(5): Error: template m2.split does not match any function template
declar
ation
tryit.d(5): Error: template m2.split(T) if (is(T : int)) cannot deduce template
function from argument types !()(string)

So, apparently, selectively imported symbol hides all others. Tested on DMD v2.060, was there since at least 2.056.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8667



--- Comment #1 from Kenji Hara <k.hara.pg@gmail.com> 2013-01-09 17:58:21 PST ---
With current dmd implementation, this is an expected behavior. (But, I'm not sure whether is an expected language design.)

A selective import adds an alias declaration in importing module. So:

  import m;
  import m2: split; //removing : split makes it work

is same as:

  import m;
  import m2;
  alias split = m2.split;
  // in here, the name `split` has just one overload m2.split
  // (does not contain m1.split)

Therefore, in main, split("abc") does not match any function template
declaration.

===

In addition, renamed import works as same way. With current implementation,

  import m : x = split;

behaves same as:

  import m;
  alias x = m.split;

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
January 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8667



--- Comment #2 from Kenji Hara <k.hara.pg@gmail.com> 2013-01-09 18:11:34 PST ---
1. If the current behavior is correct,

     import mod : name;

   is just equal to

     import mod : name = name;

2. If this bug report is correct (== current behavior is incorrect),

     import mod : name;

   just makes `name` visible in symbol look up. And,

     import mod : name = name;

   merges overload set in the importing module.
   Then the two are different.

I think #2 is more flexible and controllable design.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 10, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=8667


Kenji Hara <k.hara.pg@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #3 from Kenji Hara <k.hara.pg@gmail.com> 2013-07-09 23:26:07 PDT ---
https://github.com/D-Programming-Language/dmd/pull/2256

I finally concluded that the current selective import behavior is not good. Then I fixed this issue in the pull request, but it's a breaking change. Dmitry, could you please comment your opinion in github discussion?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------