I noticed that the following code passes compilation and returns a value that is out of the boundary of a list:
def main():
var lst = List[Int](1, 2, 3, 4, 5)
var item = lst[10]
print("Item at index 10:", item)
This prints:
Item at index 10: 5102179120
My memory might be wrong, but I remembered that in an earlier Mojo version, this would lead to a compilation error.
I also checked the standard library and see that there is indeed a boundary check for the List
type:
fn __getitem__[I: Indexer](ref self, idx: I) -> ref [self] T:
@parameter
if _type_is_eq[I, UInt]():
return (self.data + idx)[]
else:
var normalized_idx = Int(idx)
debug_assert(
-self._len <= normalized_idx < self._len,
"index: ",
normalized_idx,
" is out of bounds for `List` of length: ",
self._len,
)
if normalized_idx < 0:
normalized_idx += len(self)
return (self.data + normalized_idx)[]
Is there anything wrong with my code so the the boundary checks are by-passed?