You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Respect variable type ascription when initializer has type Never (#6911)
## Description
Fixes#6391.
This PR fixes an issue with variable declarations whose initializers
diverge because they contain `return`, `continue` or `break`
expressions. In this case the initializer typechecks to `Never`, and the
variable is inferred to have type `Never` in the subsequent
(unreachable) code:
```
let mut x = return;
x = 42; // Illegal, since x has type Never
```
The problem is that this is the case even when the variable declaration
has a type ascription:
```
let mut x : u32 = return;
x = 42; // Should be legal, since x has the type ascription u32
```
As part of the fix there is another slight change of behavior for code
blocks that diverge. Until now the type of a code block has been
determined based on whether it contains a diverging expression:
```
match { return; true } // Considered to have type Never because of `return;`
{
// Type Never does not have a constructor
}
```
We now determine the type of a code block based on its implicit return:
```
match { return; true }
{
// Type Boolean has two constructors
true => ...,
false => ...,
}
```
In principle this is a breaking change, but the only change in behavior
is in code blocks that contain a `return`, `break` or `continue`, and
then contains dead code after that expression, so I can't imagine anyone
will be affected by it.
## 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).
- [ ] If my change requires substantial documentation changes, I have
[requested support from the DevRel
team](https://github.com/FuelLabs/devrel-requests/issues/new/choose)
- [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.
---------
Co-authored-by: Joshua Batty <joshpbatty@gmail.com>
Co-authored-by: IGI-111 <igi-111@protonmail.com>
0 commit comments