Thread overview
Array operation with overlapping
Nov 08, 2016
Oleg Gorbunov
Nov 09, 2016
Oleg Gorbunov
November 08, 2016
I am new in D, but saw some strage behavour.

Overlapping is detected for + and * array operation, but not for - and /.


import std.stdio;

void main()
{
	int[] slice = [2, 2, 2, 2];
	int[] slice2 = slice[0 .. $ - 1];
	int[] slice3 = slice[1 .. $];

	writeln("slice  before: ", slice);
	writeln("slice2 before: ", slice2);
	writeln("slice3 before: ", slice3);

	const auto cmd = "slice3[] = slice2[] + slice3[];";

	writeln( "cmd: ", cmd );
	
	mixin( cmd );

	writeln("slice  after : ", slice);
	writeln("slice2 after : ", slice2);
	writeln("slice3 after : ", slice3);
}

When this code is run at 'dlang.org'

slice  before: [2, 2, 2, 2]
slice2 before: [2, 2, 2]
slice3 before: [2, 2, 2]
cmd: slice3[] = slice2[] + slice3[];
object.Error@(0): Overlapping arrays in vector operation: 8 byte(s) overlap of 12
----------------
??:? nothrow @safe void rt.util.array._enforceNoOverlap(const(char[]), ulong, ulong, const(ulong)) [0x44f7f3]
??:? _arraySliceSliceAddSliceAssign_i [0x44c1b6]
??:? _Dmain [0x4408bb]
??:? _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv [0x44c8a6]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x44c7f0]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll() [0x44c862]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate()) [0x44c7f0]
??:? _d_run_main [0x44c761]
??:? main [0x44a389]
??:? __libc_start_main [0x40d80a14]



If change + to -

	const auto cmd = "slice3[] = slice2[] - slice3[];";

When it is run at 'dlang.org'

slice  before: [2, 2, 2, 2]
slice2 before: [2, 2, 2]
slice3 before: [2, 2, 2]
cmd: slice3[] = slice2[] - slice3[];
slice  after : [2, 0, -2, -4]
slice2 after : [2, 0, -2]
slice3 after : [0, -2, -4]

November 08, 2016
On 11/8/16 3:29 AM, Oleg Gorbunov wrote:
> I am new in D, but saw some strage behavour.
>
> Overlapping is detected for + and * array operation, but not for - and /.
>
>

I would say this is a bug. The behavior should be consistent.

-Steve
November 09, 2016
On Tuesday, 8 November 2016 at 14:52:41 UTC, Steven Schveighoffer wrote:
> On 11/8/16 3:29 AM, Oleg Gorbunov wrote:
>> I am new in D, but saw some strage behavour.
>>
>> Overlapping is detected for + and * array operation, but not for - and /.
>>
>>
>
> I would say this is a bug. The behavior should be consistent.
>
> -Steve

OK, I'll report this bug at https://issues.dlang.org