How to avoid bounding check when index is surely in range?

Viewed 44

I found code like this will still trigger bounding check(by adding -gcflags="-d=ssa/check_bce/debug=1"):

// slice is guaranteed to be non empty elsewhere

idx := someUintVariable % len(slice)

slice[idx] = someValue // Found IsInBounds, how to avoid that?

Is there a way to force the compiler not to do bounding check in this case?

1 Answers

Is there a way to force the compiler not to do bounding check in this case?

Only by disabling all bound checks with gcflags=-B.

There is no "inline flag" to turn off bound checking in just a single case.

Note that the compiler sometimes can prove to itself that the bound check cannot fail and thus drops the bound check. There is no reliable way to trigger this as this is an evolving implementation detail.

Related