Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/nightly' into fix-details-in-s…
Browse files Browse the repository at this point in the history
…tring-helpers
  • Loading branch information
martinvuyk committed Nov 21, 2024
2 parents 7f64beb + 4cd0762 commit e3f1beb
Show file tree
Hide file tree
Showing 84 changed files with 15,244 additions and 7,983 deletions.
77 changes: 75 additions & 2 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ what we publish.
return a
```

- `ref` function arguments without an origin clause are now treated as
`ref [_]`, which is more syntactically convenient and consistent:

```mojo
fn takes_and_return_ref(ref a: String) -> ref [a] String:
return a
```

- `Slice.step` is now an `Optional[Int]`, matching the optionality of
`slice.step` in Python.
([PR #3160](https://github.com/modularml/mojo/pull/3160) by
Expand Down Expand Up @@ -239,6 +247,19 @@ what we publish.
allocate additional capacity.
([PR #3755](https://github.com/modularml/mojo/pull/3755) by [@thatstoasty](https://github.com/thatstoasty)).

- Introduced a new `Deque` (double-ended queue) collection type, based on a
dynamically resizing circular buffer for efficient O(1) additions and removals
at both ends as well as O(1) direct access to all elements.

The `Deque` supports the full Python `collections.deque` API, ensuring that all
expected deque operations perform as in Python.

Enhancements to the standard Python API include `peek()` and `peekleft()`
methods for non-destructive access to the last and first elements, and advanced
constructor options (`capacity`, `min_capacity`, and `shrink`) for customizing
memory allocation and performance. These options allow for optimized memory usage
and reduced buffer reallocations, providing flexibility based on application requirements.

### 🦋 Changed

- The argument convention for `__init__` methods has been changed from `inout`
Expand Down Expand Up @@ -423,7 +444,7 @@ what we publish.
These operators can't be evaluated at runtime, as a `StringLiteral` must be
written into the binary during compilation.

- You can now index into `UnsafePointer` using SIMD scalar integral types:
- You can now index into `UnsafePointer` using SIMD scalar integral types:

```mojo
p = UnsafePointer[Int].alloc(1)
Expand All @@ -432,7 +453,7 @@ what we publish.
print(p[i])
```

- Float32 and Float64 are now printed and converted to strings with roundtrip
- Float32 and Float64 are now printed and converted to strings with roundtrip
guarantee and shortest representation:

```plaintext
Expand All @@ -449,13 +470,62 @@ what we publish.
Float64(1.234 * 10**16) 12340000000000000.0 1.234e+16
```

- Single argument constructors now require a `@implicit` decorator to allow
for implicit conversions. Previously you could define an `__init__` that
takes a single argument:

```mojo
struct Foo:
var value: Int
fn __init__(out self, value: Int):
self.value = value
```

And this would allow you to pass an `Int` in the position of a `Foo`:

```mojo
fn func(foo: Foo):
print("implicitly converted Int to Foo:", foo.value)
fn main():
func(Int(42))
```

This can result in complicated errors that are difficult to debug. By default
this implicit behavior is now turned off, so you have to explicitly construct
`Foo`:

```mojo
fn main():
func(Foo(42))
```

You can still opt into implicit conversions by adding the `@implicit`
decorator. For example, to enable implicit conversions from `Int` to `Foo`:

```mojo
struct Foo:
var value: Int
@implicit
fn __init__(out self, value: Int):
self.value = value
```

### ❌ Removed

- The `UnsafePointer.bitcast` overload for `DType` has been removed. Wrap your
`DType` in a `Scalar[my_dtype]` to call the only overload of `bitcast` now.

### 🛠️ Fixed

- Lifetime tracking is now fully field sensitive, which makes the uninitialized
variable checker more precise.

- [Issue #1310](https://github.com/modularml/mojo/issues/1310) - Mojo permits
the use of any constructor for implicit conversions

- [Issue #1632](https://github.com/modularml/mojo/issues/1632) - Mojo produces
weird error when inout function is used in non mutating function

Expand Down Expand Up @@ -497,3 +567,6 @@ what we publish.
```plaintext
... cannot be converted from 'UnsafePointer[UInt]' to 'UnsafePointer[Int]'
```

- Tooling now prints the origins of `ref` arguments and results correctly, and
prints `self` instead of `self: Self` in methods.
2 changes: 1 addition & 1 deletion docs/manual/decorators/nonmaterializable.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
" var x: Int\n",
"\n",
" @always_inline(\"nodebug\")\n",
" fn __add__(self: Self, rhs: Self) -> Self:\n",
" fn __add__(self, rhs: Self) -> Self:\n",
" return NmStruct(self.x + rhs.x)\n",
"\n",
"alias still_nm_struct = NmStruct(1) + NmStruct(2)\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/manual/values/lifetimes.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@
" can think of the underscore as a wildcard that will accept any origin:\n",
"\n",
" ```mojo\n",
" def add_ref(ref [_] a: Int, b: Int) -> Int:\n",
" def add_ref(ref a: Int, b: Int) -> Int:\n",
" return a+b\n",
" ```"
]
Expand Down Expand Up @@ -427,7 +427,7 @@
" for name in names:\n",
" self.names.append(name[])\n",
"\n",
" def __getitem__(ref [_] self: Self, index: Int) ->\n",
" def __getitem__(ref self, index: Int) ->\n",
" ref [self.names] String:\n",
" if (index >=0 and index < len(self.names)):\n",
" return self.names[index]\n",
Expand Down Expand Up @@ -530,7 +530,7 @@
"above:\n",
"\n",
"```mojo\n",
"def __getitem__(ref [_] self: Self, index: Int) ->\n",
"def __getitem__(ref self, index: Int) ->\n",
" ref [self] String:\n",
"```\n",
"\n",
Expand Down
Loading

0 comments on commit e3f1beb

Please sign in to comment.