Thread overview
betterC and noboundscheck
Nov 22, 2017
Oleg B
Nov 22, 2017
Adam D. Ruppe
Nov 22, 2017
Joakim
Nov 23, 2017
Nicholas Wilson
Nov 24, 2017
codephantom
Nov 24, 2017
Basile B.
Nov 24, 2017
codephantom
November 22, 2017
Hello. I try compile simple example:

import core.stdc.stdio;
import std.algorithm : min;

extern (C) void main()
{
    char[256] buf;
    buf[] = '\0';

    auto str = "hello world";
    auto ln = min(buf.length, str.length);
    buf[0..ln] = str[0..ln];
    printf("%s\n", buf.ptr);
}

rdmd -betterC bettercarray2.d

and get error:

/tmp/.rdmd-1000/rdmd-bettercarray2.d-435C14EC3DAF09FFABF8ED6919B624C1/objs/bettercarray2.o: In function `main':
bettercarray2.d:(.text.main[main]+0xbc): undefined reference to `_d_arraycopy'
collect2: error: ld returned 1 exit status
Error: linker exited with status 1

If I understand correctly _d_arraycopy is part of druntime and it check bounds of array access.

If I add -noboundscheck flag all works fine.

dmd version is 2.076.1

Why -betterC flag not 'include' -noboundscheck flag?
It's bug or in some cases it's useful?
November 22, 2017
On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:
> Why -betterC flag not 'include' -noboundscheck flag?

-noboundscheck is extremely harmful. If -betterC implied that, it would no longer be a better C, it would just be the same buggy C.

The compiler should perhaps inline the bounds check so it doesn't need the druntime function, but it certainly shouldn't skip it.

November 22, 2017
On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:
> Hello. I try compile simple example:
>
> import core.stdc.stdio;
> import std.algorithm : min;
>
> extern (C) void main()
> {
>     char[256] buf;
>     buf[] = '\0';
>
>     auto str = "hello world";
>     auto ln = min(buf.length, str.length);
>     buf[0..ln] = str[0..ln];
>     printf("%s\n", buf.ptr);
> }
>
> rdmd -betterC bettercarray2.d
>
> and get error:
>
> /tmp/.rdmd-1000/rdmd-bettercarray2.d-435C14EC3DAF09FFABF8ED6919B624C1/objs/bettercarray2.o: In function `main':
> bettercarray2.d:(.text.main[main]+0xbc): undefined reference to `_d_arraycopy'
> collect2: error: ld returned 1 exit status
> Error: linker exited with status 1
>
> If I understand correctly _d_arraycopy is part of druntime and it check bounds of array access.
>
> If I add -noboundscheck flag all works fine.
>
> dmd version is 2.076.1
>
> Why -betterC flag not 'include' -noboundscheck flag?
> It's bug or in some cases it's useful?

betterC is a new feature that's still being worked on and still has holes in it:

https://github.com/dlang/dmd/pull/7151

I suggest you open an issue for it on bugzilla, as this sounds like either a hole or at least something that should be documented better:

https://issues.dlang.org
November 23, 2017
On Wednesday, 22 November 2017 at 16:57:10 UTC, Joakim wrote:
> On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:
>> [...]
>
> betterC is a new feature that's still being worked on and still has holes in it:
>
> https://github.com/dlang/dmd/pull/7151
>
> I suggest you open an issue for it on bugzilla, as this sounds like either a hole or at least something that should be documented better:
>
> https://issues.dlang.org

see also https://github.com/ldc-developers/ldc/pull/2426 (also WiP).
November 24, 2017
On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:
> If I add -noboundscheck flag all works fine.
>
> dmd version is 2.076.1
>
> Why -betterC flag not 'include' -noboundscheck flag?
> It's bug or in some cases it's useful?


Interestingly, ldc2 will compile this ok, without the -noboundscheck flag

btw. you should start using: boundscheck=off/on/safeonly flag instead i believe.

https://dlang.org/dmd-windows.html#switch-boundscheck

November 24, 2017
On Friday, 24 November 2017 at 00:17:31 UTC, codephantom wrote:
> On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:
>> If I add -noboundscheck flag all works fine.
>>
>> dmd version is 2.076.1
>>
>> Why -betterC flag not 'include' -noboundscheck flag?
>> It's bug or in some cases it's useful?
>
>
> Interestingly, ldc2 will compile this ok, without the -noboundscheck flag
>
> btw. you should start using: boundscheck=off/on/safeonly flag instead i believe.
>
> https://dlang.org/dmd-windows.html#switch-boundscheck

It's b/c LDC2 doesn't have the most recent betterC features. It works just like it worked in older DMD release (e.g w 2.075), when betterC was "inexact" / "mostly unimplemented"
November 24, 2017
On Friday, 24 November 2017 at 05:01:17 UTC, Basile B. wrote:
> On Friday, 24 November 2017 at 00:17:31 UTC, codephantom wrote:
>> On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:
>>> If I add -noboundscheck flag all works fine.
>>>
>>> dmd version is 2.076.1
>>>
>>> Why -betterC flag not 'include' -noboundscheck flag?
>>> It's bug or in some cases it's useful?
>>
>>
>> Interestingly, ldc2 will compile this ok, without the -noboundscheck flag
>>
>> btw. you should start using: boundscheck=off/on/safeonly flag instead i believe.
>>
>> https://dlang.org/dmd-windows.html#switch-boundscheck
>
> It's b/c LDC2 doesn't have the most recent betterC features. It works just like it worked in older DMD release (e.g w 2.075), when betterC was "inexact" / "mostly unimplemented"


Thanks. I didn't take that into account (was using LDC 1.5.0 ..based on 2.075.1)

Getting used to monthly compiler updates is a real challenge.. not something I'm used to ;-)
November 25, 2017
On Wednesday, 22 November 2017 at 15:10:40 UTC, Oleg B wrote:

> import core.stdc.stdio;
> import std.algorithm : min;
>
> extern (C) void main()
> {
>     char[256] buf;
>     buf[] = '\0';
>
>     auto str = "hello world";
>     auto ln = min(buf.length, str.length);
>     buf[0..ln] = str[0..ln];
>     printf("%s\n", buf.ptr);
> }
>
> rdmd -betterC bettercarray2.d
>
> and get error:
>
> /tmp/.rdmd-1000/rdmd-bettercarray2.d-435C14EC3DAF09FFABF8ED6919B624C1/objs/bettercarray2.o: In function `main':
> bettercarray2.d:(.text.main[main]+0xbc): undefined reference to `_d_arraycopy'
> collect2: error: ld returned 1 exit status
> Error: linker exited with status 1


Bug submitted here: https://issues.dlang.org/show_bug.cgi?id=18010