From b0fd4f6e56d1e39ba7e0efce1c464ce33aa7f6dc Mon Sep 17 00:00:00 2001 From: Gabriel de Marmiesse Date: Tue, 7 May 2024 11:15:07 -0500 Subject: [PATCH] [External] [stdlib] Make the use of the unsafe constructor of List explicit (#39465) [External] [stdlib] Make the use of the unsafe constructor of List explicit This PR is a small piece of https://github.com/modularml/mojo/pull/2507 We want unsafe things to be explicit, as stated in [the vision guide](https://github.com/modularml/mojo/blob/nightly/stdlib/docs/vision.md#objectives-and-goals) Co-authored-by: Gabriel de Marmiesse Closes modularml/mojo#2523 MODULAR_ORIG_COMMIT_REV_ID: 861433431c0fc7fd6ab1d905dfe09f3e3a5792c1 --- stdlib/src/builtin/file.mojo | 4 +++- stdlib/src/builtin/string.mojo | 12 ++++++++---- stdlib/src/collections/list.mojo | 10 +++++++--- stdlib/test/collections/test_list.mojo | 4 ++-- 4 files changed, 20 insertions(+), 10 deletions(-) diff --git a/stdlib/src/builtin/file.mojo b/stdlib/src/builtin/file.mojo index e18ee787e6..2b16d16f71 100644 --- a/stdlib/src/builtin/file.mojo +++ b/stdlib/src/builtin/file.mojo @@ -341,7 +341,9 @@ struct FileHandle: if err_msg: raise (err_msg^).consume_as_error() - var list = List[Int8](buf, size=int(size_copy), capacity=int(size_copy)) + var list = List[Int8]( + unsafe_pointer=buf, size=int(size_copy), capacity=int(size_copy) + ) return list diff --git a/stdlib/src/builtin/string.mojo b/stdlib/src/builtin/string.mojo index ac950ce1af..ed4896663e 100644 --- a/stdlib/src/builtin/string.mojo +++ b/stdlib/src/builtin/string.mojo @@ -511,7 +511,9 @@ struct String( var length = len(impl) var capacity = impl.capacity self._buffer = List[Int8]( - impl.steal_data().bitcast[Int8](), size=length, capacity=capacity + unsafe_pointer=impl.steal_data().bitcast[Int8](), + size=length, + capacity=capacity, ) @always_inline @@ -574,7 +576,9 @@ struct String( """ # we don't know the capacity of ptr, but we'll assume it's the same or # larger than len - self = Self(Self._buffer_type(ptr, size=len, capacity=len)) + self._buffer = Self._buffer_type( + unsafe_pointer=ptr, size=len, capacity=len + ) @always_inline fn __init__(inout self, ptr: UnsafePointer[UInt8], len: Int): @@ -589,8 +593,8 @@ struct String( """ # we don't know the capacity of ptr, but we'll assume it's the same or # larger than len - self = Self( - Self._buffer_type(ptr.bitcast[Int8](), size=len, capacity=len) + self._buffer = Self._buffer_type( + unsafe_pointer=ptr.bitcast[Int8](), size=len, capacity=len ) @always_inline diff --git a/stdlib/src/collections/list.mojo b/stdlib/src/collections/list.mojo index 19a1a4fa44..1b054a7f5a 100644 --- a/stdlib/src/collections/list.mojo +++ b/stdlib/src/collections/list.mojo @@ -128,16 +128,20 @@ struct List[T: CollectionElement](CollectionElement, Sized, Boolable): self.append(value[]) fn __init__( - inout self: Self, data: UnsafePointer[T], *, size: Int, capacity: Int + inout self: Self, + *, + unsafe_pointer: UnsafePointer[T], + size: Int, + capacity: Int, ): """Constructs a list from a pointer, its size, and its capacity. Args: - data: The pointer to the data. + unsafe_pointer: The pointer to the data. size: The number of elements in the list. capacity: The capacity of the list. """ - self.data = data + self.data = unsafe_pointer self.size = size self.capacity = capacity diff --git a/stdlib/test/collections/test_list.mojo b/stdlib/test/collections/test_list.mojo index 4f9f9ac0cb..a65d070c44 100644 --- a/stdlib/test/collections/test_list.mojo +++ b/stdlib/test/collections/test_list.mojo @@ -645,7 +645,7 @@ def test_constructor_from_pointer(): new_pointer[2] = 2 # rest is not initialized - var some_list = List[Int8](new_pointer, size=3, capacity=5) + var some_list = List[Int8](unsafe_pointer=new_pointer, size=3, capacity=5) assert_equal(some_list[0], 0) assert_equal(some_list[1], 1) assert_equal(some_list[2], 2) @@ -660,7 +660,7 @@ def test_constructor_from_other_list_through_pointer(): var size = len(initial_list) var capacity = initial_list.capacity var some_list = List[Int8]( - initial_list.steal_data(), size=size, capacity=capacity + unsafe_pointer=initial_list.steal_data(), size=size, capacity=capacity ) assert_equal(some_list[0], 0) assert_equal(some_list[1], 1)