[Issue 15041] Pointer slice with one negative index throws 'Range violation'

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Thu Oct 1 03:10:05 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=15041

--- Comment #2 from anoneuron at gmail.com ---
Hmm yes, it seems all operations with array indices are treated as unsigned,
and just happen to do the right thing in most cases because the integers are
modular (e.g. adding size_t.max is the same as subtracting 1).

I propose instead of silently casting signed values to unsigned, the compiler
should explicitly disallow use of signed values as array indices (or at least
emit a warning, so as to not break existing code).

(In reply to Vladimir Panteleev from comment #1)
> > 	assert(p[-1 .. 1] == "aa"); // 'Range violation'
> 
> What happens here is that -1 gets implicitly casted to size_t, which is
> unsigned, so this becomes p[size_t.max .. 1]. Slicing p[a..b] when a>b is
> nonsense, hence the RangeError.
> 
> The check is removed when -release is present.
> 
> I'm not sure this is a bug.

--


More information about the Digitalmars-d-bugs mailing list