Bug 8667 – selective import breaks normal overload resolution
Status
RESOLVED
Resolution
DUPLICATE
Severity
critical
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-09-16T06:03:00Z
Last change time
2015-04-11T16:47:44Z
Keywords
pull
Assigned to
nobody
Creator
dmitry.olsh
Comments
Comment #0 by dmitry.olsh — 2012-09-16T06:03:26Z
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.
Comment #1 by k.hara.pg — 2013-01-09T17:58:21Z
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;
Comment #2 by k.hara.pg — 2013-01-09T18:11:34Z
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.
Comment #3 by k.hara.pg — 2013-07-09T23:26:07Z
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?
Comment #4 by issues.dlang — 2013-12-02T23:34:21Z
I expect that bug# 314 is related to this.
Comment #5 by k.hara.pg — 2014-04-16T04:34:05Z
(In reply to Jonathan M Davis from comment #4)
> I expect that bug# 314 is related to this.
Now this is a dup of issue 12359. For that, I'm opening a pull request to remove hidden alias declarations introduced by selective imports.
Comment #6 by code — 2015-04-11T16:47:44Z
*** This issue has been marked as a duplicate of issue 12359 ***