forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements the Never
!
types as a TypeInfo bottom type. (FuelLabs#5602
) ## Description We now parse the `!` as a TypeInfo::Never, and remove the usage of empty enums as Never type in our code. This commit removes completely the DeterministicallyAborts and TypeCheckUnificationContext. The DeterministicallyAborts can be removed because the Never TypeInfo is now propagated using the type checker. Code blocks that return, break, continue, or call an expression that returns Never, are marked as Never. Partially fixes FuelLabs#5562. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
- Loading branch information
Showing
42 changed files
with
276 additions
and
410 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,53 @@ | ||
# Never Type | ||
|
||
The Never type `!` represents the type of computations which never resolve to any value at all. | ||
|
||
## Additional Information | ||
|
||
`break`, `continue` and `return` expressions also have type `!`. For example we are allowed to | ||
write: | ||
|
||
```sway | ||
let x: ! = { | ||
return 123 | ||
}; | ||
``` | ||
|
||
Although the `let` is pointless here, it illustrates the meaning of `!`. Since `x` is never | ||
assigned a value (because `return` returns from the entire function), `x` can be given type | ||
`Never`. We could also replace `return 123` with a `revert()` or a never-ending `loop` and this code | ||
would still be valid. | ||
|
||
A more realistic usage of `Never` is in this code: | ||
|
||
```sway | ||
let num: u32 = match get_a_number() { | ||
Some(num) => num, | ||
None => break, | ||
}; | ||
``` | ||
|
||
Both match arms must produce values of type [`u32`], but since `break` never produces a value | ||
at all we know it can never produce a value which isn't a [`u32`]. This illustrates another | ||
behaviour of the `!` type - expressions with type `!` will coerce into any other type. | ||
|
||
Note that `!` type coerces into any other type, another example of this would be: | ||
|
||
```sway | ||
let x: u32 = { | ||
return 123 | ||
}; | ||
``` | ||
|
||
Regardless of the type of `x`, the return block of type `Never` will always coerce into `x` type. | ||
|
||
## Examples | ||
|
||
```sway | ||
fn foo() { | ||
let num: u64 = match Option::None::<u64> { | ||
Some(num) => num, | ||
None => return, | ||
}; | ||
} | ||
``` |
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
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
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
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
Oops, something went wrong.