Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions compiler/src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1794,7 +1794,7 @@ Expression resolveOpDollar(Scope* sc, ArrayExp ae, out Expression pe0)
if (i == 0)
pe0 = extractOpDollarSideEffect(sc, ae);

if (e.op == EXP.interval && !(slice && slice.isTemplateDeclaration()))
if (e.op == EXP.interval && !slice)
{
return fallback();
}
Expand All @@ -1821,21 +1821,27 @@ Expression resolveOpDollar(Scope* sc, ArrayExp ae, out Expression pe0)

if (auto ie = e.isIntervalExp())
{
Expression edim = new IntegerExp(ae.loc, i, Type.tsize_t);
edim = edim.expressionSemantic(sc);
auto tiargs = new Objects(edim);

auto fargs = new Expressions(ie.lwr, ie.upr);
if (slice.isTemplateDeclaration())
{
Expression edim = new IntegerExp(ae.loc, i, Type.tsize_t);
edim = edim.expressionSemantic(sc);
auto tiargs = new Objects(edim);

const xerrors = global.startGagging();
sc = sc.push();
FuncDeclaration fslice = resolveFuncCall(ae.loc, sc, slice, tiargs, ae.e1.type, ArgumentList(fargs), FuncResolveFlag.quiet);
sc = sc.pop();
global.endGagging(xerrors);
if (!fslice)
return fallback();
const xerrors = global.startGagging();
sc = sc.push();
FuncDeclaration fslice = resolveFuncCall(ae.loc, sc, slice, tiargs, ae.e1.type, ArgumentList(fargs), FuncResolveFlag.quiet);
sc = sc.pop();
global.endGagging(xerrors);
if (!fslice)
return fallback();

e = new DotTemplateInstanceExp(ae.loc, ae.e1, Id.opSlice, tiargs);
e = new DotTemplateInstanceExp(ae.loc, ae.e1, Id.opSlice, tiargs);
}
else
{
e = new DotIdExp(ae.loc, ae.e1, Id.opSlice);
}
e = new CallExp(ae.loc, e, fargs);
e = e.expressionSemantic(sc);
}
Expand Down
26 changes: 26 additions & 0 deletions compiler/test/runnable/opover3.d
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,31 @@ void test20927()
}

/**************************************/
// opSlice template argument for dimension 0 is optional
// https://github.com/dlang/dmd/issues/19447
struct Foo
{
int payload;
int opSlice(int start, int end)
{
return start * end;
}
auto opIndexAssign(T)(T elem, int slice)
{
this.payload = slice;
return this;
}
}

void test19447()
{
Foo foo;
foo[3 .. 5] = 15;
assert(foo.payload == 15);
}

/**************************************/


void main()
{
Expand All @@ -207,4 +232,5 @@ void main()
test4();
test12070();
test20927();
test19447();
}
8 changes: 6 additions & 2 deletions spec/operatoroverloading.dd
Original file line number Diff line number Diff line change
Expand Up @@ -774,8 +774,12 @@ $(H3 $(LNAME2 slice_assignment_operator, Slice Assignment Operator Overloading))
`opIndexAssign` member function that takes the return value of the
`opSlice` function as parameter(s).
Expressions of the form $(CODE a[)$(SLICE)$(D ] = c) are rewritten as
$(CODE a.opIndexAssign$(LPAREN)c,) $(D a.opSlice!0$(LPAREN))$(SLICE2)$(D $(RPAREN)$(RPAREN)),
and $(CODE a[] = c) as $(CODE a.opIndexAssign(c)).
$(CODE a.opIndexAssign$(LPAREN)c,) $(D a.opSlice!0$(LPAREN))$(SLICE2)$(D $(RPAREN)$(RPAREN))
when `opSlice` is a template, or
$(CODE a.opIndexAssign$(LPAREN)c,) $(D a.opSlice$(LPAREN))$(SLICE2)$(D $(RPAREN)$(RPAREN))
when `opSlice` is declared without the compile-time parameter
(for one-dimensional slicing only).
$(CODE a[] = c) is rewritten as $(CODE a.opIndexAssign(c)).
)

$(P See $(RELATIVE_LINK2 array-ops, Array
Expand Down
Loading