-
Notifications
You must be signed in to change notification settings - Fork 242
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description ## Problem\* ## Summary\* Adds a way for users to get around limitations in the type checker, particularly for arithmetic generics, by using a new `checked_transmute` builtin. This will cast any type to another but will assert during monomorphization that the types unify. ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [x] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ ] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: Michael J Klein <michaeljklein@users.noreply.github.com>
- Loading branch information
1 parent
6e40f62
commit 2618061
Showing
9 changed files
with
137 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
title: Memory Module | ||
description: | ||
This module contains functions which manipulate memory in a low-level way | ||
keywords: | ||
[ | ||
mem, memory, zeroed, transmute, checked_transmute | ||
] | ||
--- | ||
|
||
# `std::mem::zeroed` | ||
|
||
```rust | ||
fn zeroed<T>() -> T | ||
``` | ||
|
||
Returns a zeroed value of any type. | ||
This function is generally unsafe to use as the zeroed bit pattern is not guaranteed to be valid for all types. | ||
It can however, be useful in cases when the value is guaranteed not to be used such as in a BoundedVec library implementing a growable vector, up to a certain length, backed by an array. | ||
The array can be initialized with zeroed values which are guaranteed to be inaccessible until the vector is pushed to. | ||
Similarly, enumerations in noir can be implemented using this method by providing zeroed values for the unused variants. | ||
|
||
This function currently supports the following types: | ||
|
||
- Field | ||
- Bool | ||
- Uint | ||
- Array | ||
- Slice | ||
- String | ||
- Tuple | ||
- Functions | ||
|
||
Using it on other types could result in unexpected behavior. | ||
|
||
# `std::mem::checked_transmute` | ||
|
||
```rust | ||
fn checked_transmute<T, U>(value: T) -> U | ||
``` | ||
|
||
Transmutes a value of one type into the same value but with a new type `U`. | ||
|
||
This function is safe to use since both types are asserted to be equal later during compilation after the concrete values for generic types become known. | ||
This function is useful for cases where the compiler may fails a type check that is expected to pass where | ||
a user knows the two types to be equal. For example, when using arithmetic generics there are cases the compiler | ||
does not see as equal, such as `[Field; N*(A + B)]` and `[Field; N*A + N*B]`, which users may know to be equal. | ||
In these cases, `checked_transmute` can be used to cast the value to the desired type while also preserving safety | ||
by checking this equality once `N`, `A`, `B` are fully resolved. | ||
|
||
Note that since this safety check is performed after type checking rather than during, no error is issued if the function | ||
containing `checked_transmute` is never called. |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "checked_transmute" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.35.0" | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use std::mem::checked_transmute; | ||
|
||
fn main() { | ||
let _: [Field; 2] = transmute_fail([1]); | ||
} | ||
|
||
pub fn transmute_fail<let N: u32>(x: [Field; N]) -> [Field; N + 1] { | ||
checked_transmute(x) | ||
} |
7 changes: 7 additions & 0 deletions
7
test_programs/compile_success_empty/checked_transmute/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "checked_transmute" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.35.0" | ||
|
||
[dependencies] |
15 changes: 15 additions & 0 deletions
15
test_programs/compile_success_empty/checked_transmute/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use std::mem::checked_transmute; | ||
|
||
fn main() { | ||
// 1*(2 + 3) = 1*2 + 1*3 = 5 | ||
let _: [Field; 5] = distribute::<1, 2, 3>([1, 2, 3, 4, 5]); | ||
} | ||
|
||
pub fn distribute<let N: u32, let A: u32, let B: u32>(x: [Field; N * (A + B)]) -> [Field; N * A + N * B] { | ||
// asserts: [Field; N * (A + B)] = [Field; N * A + N * B] | ||
// -> N * A + B = N * A + N * B | ||
// | ||
// This assert occurs during monomorphization when the actual values for N, A, and B | ||
// become known. This also means if this function is not called, the assert will not trigger. | ||
checked_transmute(x) | ||
} |