Bug 16473 – operator overloading is broken

Status
RESOLVED
Resolution
MOVED
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-09-06T13:26:11Z
Last change time
2020-03-21T03:56:38Z
Assigned to
No Owner
Creator
Илья Ярошенко

Comments

Comment #0 by ilyayaroshenko — 2016-09-06T13:26:11Z
------------------------------------------- import std.experimental.ndslice; void main() { auto sl = new double[10].sliced(2, 5); auto d = -sl[0, 1]; } ------------------------------------------- /d921/f269.d(6): Error: template std.experimental.ndslice.slice.Slice!(2LU, double*).Slice.opIndexUnary cannot deduce function from argument types !("-")(int, int), candidates are: /opt/compilers/dmd2/include/std/experimental/ndslice/slice.d(1980): std.experimental.ndslice.slice.Slice!(2LU, double*).Slice.opIndexUnary(string op, Indexes...)(Indexes _indexes) if (isFullPureIndex!Indexes && (op == "++" || op == "--")) /opt/compilers/dmd2/include/std/experimental/ndslice/slice.d(2008): std.experimental.ndslice.slice.Slice!(2LU, double*).Slice.opIndexUnary(string op, Slices...)(Slices slices) if (isFullPureSlice!Slices && (op == "++" || op == "--")) ------------- The are two prototypes in Slice. Both with if (op == "++" || op == "--") condition. So, I don't think we need to add all opIndexUnary variants. Instead, if expression -sl[0, 1] can not be expanded with opIndexUnary, then is should be expanded with -(sl.opIndex(0, 1)).
Comment #1 by clumsycodemonkey — 2016-09-25T01:50:48Z
I was able to fix it by adding `-` to the list of accepted strings in the if statement. So now the function signature looks like this -------------------- auto ref opIndexUnary(string op, Indexes...)(Indexes _indexes) if (isFullPureIndex!Indexes && (op == `++` || op == `--` || op == `-`)) -------------------- This works for the case reported in the original ticket. Not sure what to do about the overload that takes slices. The semantically typical thing to do would be return a new slice with a new array underneath it that has the negated values, so as to not modify the underlying array. But that would cause an allocation.
Comment #2 by ilyayaroshenko — 2016-09-25T12:07:38Z
(In reply to Ryan from comment #1) > I was able to fix it by adding `-` to the list of accepted strings in the if > statement. So now the function signature looks like this > -------------------- > auto ref opIndexUnary(string op, Indexes...)(Indexes _indexes) > if (isFullPureIndex!Indexes && (op == `++` || op == `--` || op > == `-`)) > -------------------- > > This works for the case reported in the original ticket. Not sure what to do > about the overload that takes slices. The semantically typical thing to do > would be return a new slice with a new array underneath it that has the > negated values, so as to not modify the underlying array. But that would > cause an allocation. This is a workaround, which generate more template bloat. We need DMD FE bug fix
Comment #3 by github-bugzilla — 2016-09-30T15:19:38Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/545dfd08d705574928eef96bffb914b646aa74e0 workaround for Issue 16473 https://github.com/dlang/phobos/commit/302632968f164867e4aa08f1cf8b1429a43cf2a6 Merge pull request #4820 from 9il/workaround16473 workaround for Issue 16473
Comment #4 by github-bugzilla — 2016-10-01T11:47:29Z