www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - betterC and noboundscheck

reply Oleg B <code.viator gmail.com> writes:
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/o
js/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?
Nov 22 2017
next sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
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.
Nov 22 2017
prev sibling next sibling parent reply Joakim <dlang joakim.fea.st> writes:
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/o
js/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
Nov 22 2017
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
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).
Nov 22 2017
prev sibling next sibling parent reply codephantom <me noyb.com> writes:
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
Nov 23 2017
parent reply Basile B. <b2.temp gmx.com> writes:
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"
Nov 23 2017
parent codephantom <me noyb.com> writes:
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 ;-)
Nov 23 2017
prev sibling parent Michael V. Franklin <slavo5150 yahoo.com> writes:
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/o
js/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
Nov 24 2017