-
Notifications
You must be signed in to change notification settings - Fork 534
add docs for never type #206
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -93,52 +93,6 @@ sufficient context to determine the type parameters. For example, | |
|
||
[path]: paths.html | ||
|
||
## Diverging functions | ||
|
||
A special kind of function can be declared with a `!` character where the | ||
output type would normally be. For example: | ||
|
||
```rust | ||
fn my_err(s: &str) -> ! { | ||
println!("{}", s); | ||
panic!(); | ||
} | ||
``` | ||
|
||
We call such functions "diverging" because they never return a value to the | ||
caller. Every control path in a diverging function must end with a `panic!()`, | ||
a loop expression without an associated break expression, or a call to another | ||
diverging function on every control path. The `!` annotation does *not* denote | ||
a type. | ||
|
||
It might be necessary to declare a diverging function because as mentioned | ||
previously, the typechecker checks that every control path in a function ends | ||
with a [`return`] or diverging expression. So, if `my_err` were declared | ||
without the `!` annotation, the following code would not typecheck: | ||
|
||
[`return`]: expressions/return-expr.html | ||
|
||
```rust | ||
# fn my_err(s: &str) -> ! { panic!() } | ||
|
||
fn f(i: i32) -> i32 { | ||
if i == 42 { | ||
return 42; | ||
} | ||
else { | ||
my_err("Bad number!"); | ||
} | ||
} | ||
``` | ||
|
||
This will not compile without the `!` annotation on `my_err`, since the `else` | ||
branch of the conditional in `f` does not return an `i32`, as required by the | ||
signature of `f`. Adding the `!` annotation to `my_err` informs the typechecker | ||
that, should control ever enter `my_err`, no further type judgments about `f` | ||
need to hold, since control will never resume in any context that relies on | ||
those judgments. Thus the return type on `f` only needs to reflect the `if` | ||
branch of the conditional. | ||
|
||
## Extern functions | ||
|
||
Extern functions are part of Rust's foreign function interface, providing the | ||
|
@@ -169,4 +123,4 @@ As non-Rust calling conventions do not support unwinding, unwinding past the end | |
of an extern function will cause the process to abort. In LLVM, this is | ||
implemented by executing an illegal instruction. | ||
|
||
[external blocks]: items/external-blocks.html | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unrelated to this PR, but why does this keep showing up? And why this time in a file that is otherwise untouched? |
||
[external blocks]: items/external-blocks.html |
This file contains hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why remove this? The information about what constructs produce the
!
return type remains useful. Also, the new section below doesn't use "diverges", "diverge", or "divergent", though we make use of "diverges" in the section aboutloop {}
s.I say just keep this section in tact, with adjustments to account for new thinking about the Never Type.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's not really a good reason to have information on divergence in the function items page. Perhaps mention in the function call and method call operators that calling a function that returns
!
diverges? Divergence sort of needs its own section in the Expressions page itself, I think? I haven't thought too much about whether it's a property of expressions, statements, or both? And that can be done separately from this.It would be nice to pull the example into the Never type docs though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @Havvy here. Divergence isn't specifically a property of functions, it's its own thing entirely and really needs its own section. I don't fully understand the semantics of it though, so I'm not confident that I'd be able to write that section.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, put the information where ever you think it fits best, but I don't want to lose the information.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe someone like @arielb1 can help here. Is this an accurate-enough description of type-checking w.r.t divergence and
!
?!
cause everything in the computation graph after the!
to be marked unreachable.!
.I know it's not exactly implemented like this in the compiler, but have I left out any relevant caveats? Do you think the reference needs a section on divergence separate from
!
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll make sure that divergence information doesn't get lost before the next beta, even if this PR loses it.