dmd/compiler/test/runnable/testbounds_safeonly.d
2022-07-09 18:53:07 +02:00

27 lines
646 B
D

// REQUIRED_ARGS: -boundscheck=safeonly
// PERMUTE_ARGS: -inline -g -O
import core.exception : RangeError;
// Check for RangeError is thrown
bool thrown(T)(lazy T cond)
{
import core.exception;
bool f = false;
try { cond(); } catch (RangeError e) { f = true; }
return f;
}
@safe int safeIndex (int[] arr) { return arr[2]; }
@trusted int trustedIndex(int[] arr) { return arr[2]; }
@system int systemIndex (int[] arr) { return arr[2]; }
void main()
{
int[3] data = [1,2,3];
int[] arr = data[0..2];
assert(arr. safeIndex().thrown);
assert(arr.trustedIndex() == 3);
assert(arr. systemIndex() == 3);
}