Bug 4702 – Long Postfix not working with cross-module overloading

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-08-21T08:37:00Z
Last change time
2012-10-27T11:03:02Z
Assigned to
nobody
Creator
andrej.mitrovich

Comments

Comment #0 by andrej.mitrovich — 2010-08-21T08:37:39Z
widget.d: void fun(int x) { } widget.d: void fun(int x) { } main.d: import widget; import io; void main() { long y = 10L; fun(y); // fine, no errors, goes to io.fun writeln(typeid(10L)); // writes long fun(10L); // error: io.fun at io.d(4) conflicts with widget.fun at widget.d(4) }
Comment #1 by andrej.mitrovich — 2010-12-30T14:12:29Z
OOPS! That example code is completely wrong, please diregard it. This is the proper one which should work but doesn't: main.d: import std.stdio : writeln; import foo; // void fun(int x) import bar; // void fun(long x) void main() { auto y = 10L; fun(y); // ok, goes to bar.fun writeln(typeid(10L)); // writes long fun(10L); // error: bar.fun conflicts with foo.fun } foo.d: void fun(int x) { } bar.d: void fun(long x) { } This only happens with literals and when the two fun methods are defined in separate modules. If the fun methods are defined directly in main(), there's no error.
Comment #2 by andrej.mitrovich — 2012-01-21T17:53:28Z
I think what's going on is DMD figures out the literal can fit into an int and does the optimization where it converts it into an int behind the scenes. Proof is in the pudding: import std.stdio; import foo; import bar; void main() { long x; fun(cast(long)2147483648); // ok, int.max is 2147483647, overflows to long fun(cast(long)2147483647); // error, no overflow and literal stored as int }
Comment #3 by andrej.mitrovich — 2012-10-27T11:02:29Z
The problem is there are two overload sets and they're not explicitly merged. The fix is: import foo; // void fun(int x) import bar; // void fun(long x) alias foo.fun fun; // added alias bar.fun fun; // added void main() { auto y = 10L; fun(y); // ok, goes to bar.fun fun(10L); // error: bar.fun conflicts with foo.fun }
Comment #4 by andrej.mitrovich — 2012-10-27T11:03:02Z
(In reply to comment #3) > fun(10L); // error: bar.fun conflicts with foo.fun Disregard the comment it's a leftover from OP sample, the code will work now.