-
Notifications
You must be signed in to change notification settings - Fork 502
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
Add description for LUB Coercion #808
Conversation
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.
First off, thanks for writing this! That said, it looks very rushed and doesn't actually describe what these coercions actually are. I don't actually know the algorithm for the coercion, so just including that would be tremendously helpful, but I've listed extensive notes on my thoughts on the contents of this PR. How much you want to turn them into work for yourself is up to you, except again, we do need the actual algorithm.
Notice I added explanation for LubCoerce with expected type in the end. That's because it's really how rustc currently works. You can check that by test these: This compiles: let esc: &[u8] = match true {
true => b"ab", // &[u8; 2]
false => b"c", // &[u8; 1]
}; This failes to compile: let esc = match true {
true => b"ab",
false => b"c",
}; Why?
|
I've sent my review as a new PR @ ldm0#1 |
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.
Looking at @ehuss for final approval.
(Also, I think the algorithm might be better expressed in text than code after looking at it, but that can be done 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.
I also generally prefer to not introduce ad-hoc pseudo-code, since it is undefined and open to interpretation. However, I think something is better than nothing, so it's probably fine for now. I also suspect a truly formal definition would be a much greater effort.
I'm not qualified to comment on the actual rules, since I know nothing about type coercion. I would be more comfortable with a lang team member to approve.
Waiting on a lang team member to approve. :D To check the correctness of the pseudo code, reviewers can check |
This comment has been minimized.
This comment has been minimized.
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.
Gave this a first read. Sorry for the long delay. I'll try to offer some more concrete suggestions in a bit. I am wondering if we can do something less "pseduo-code" and a bit higher-level -- that said, the algorithm is kind of "operational" though.
src/type-coercions.md
Outdated
``` | ||
|
||
In this example, both `foo` and `bar` has the type | ||
`LubCoerce(typeof(a), typeof(a), typeof(c))` and `baz` has the 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.
Hmm this notation is kind of ad-hoc -- I expected to see an actual type here. I guess I might say something like:
The type of foo
and bar
is the result of applying the LUB coercion to the types of a
, b
, and c
.
src/type-coercions.md
Outdated
|
||
LUB coercion is performed by the following algorithm: | ||
|
||
```txt |
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 wonder btw if we should incorporate mdbook-mermaid
into the reference ... then we could use a flow-chart here -- not sure how much better that would be ;)
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.
cc @ehuss
src/type-coercions.md
Outdated
LUB coercion is performed by the following algorithm: | ||
|
||
```txt | ||
Lub(a: Type, b: 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.
Hmm, the actual code seems to have a bit more of a "bias", i.e., it has a notion of "old and new", and ordering I think maybe can matter. I wonder if we can write this section with a bit less specificity -- let me check how the coercion section is written.
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.
@nikomatsakis Thanks for reviewing. Does the "old and new" means the prev_ty
and new_ty
in the code? I think it's represented in this part:
LubCoerce(vars...):
result = vars.get(0)
for var in vars[1..]:
result = Lub(result, var)
return result
Where result
represents the prev_ty
and var
represents the new_ty
. If you mean other things with "old and new". Could you elaborate more?
But for the ordering, I re-thinked about it and found it do matters in some cases(some edge cases). I will remove the property 1 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.
Yes, old and new does correspond to prev and new.
So I guess an alternative would be to describe the LUB coercion more informally and less precisely. Something like the following (@ldm0 you would obviously want to amend this to include the coercion from FnDef/closures to fn pointers that started all of this). I'm curious @ehuss or others what you think of these two options. I think I mildly prefer an informal description like this one, because I think that it is probably sufficient to give readers an intuition for what the compiler is trying to do, and I suspect that the more precise description is ultimately not sufficiently precise to be "authoratative" and hence we're better off leaving that job to the code itself. In some contexts, the compiler must coerce together multiple types to try and find the most general type. This is called a "Least Upper Bound" coercion. Some examples where the LUB coercion algorithm is used are:
In each such case, there are a set of types
ExamplesXXX give some interesting examples CaveatThis description is obviously informal. Making it more precise is expected to proceed as part of a general effort to specify the Rust type checker more precisely. |
The list of where LUB coercions happen should at least be an exhaustive list, not just "examples where it is performed". |
Where are we with this? |
Sorry for the long delay. Rewrite done. Exhaustive list added |
Thanks! |
Update books ## reference 5 commits in 56a13c082ee90736c08d6abdcd90462517b703d3..1b78182e71709169dc0f1c3acdc4541b6860e1c4 2020-09-14 23:20:16 -0700 to 2020-10-11 13:53:47 -0700 - Specify that SSE4.1 includes SSSE3 instead of SSE3 (rust-lang/reference#892) - Fix mutable expressions that can be dereferenced (rust-lang/reference#890) - Fix grammar in memory model (rust-lang/reference#889) - Add style checks. (rust-lang/reference#886) - Add description for LUB Coercion (rust-lang/reference#808) ## book 1 commits in cb28dee95e5e50b793e6ba9291c5d1568d3ad72e..451a1e30f2dd137aa04e142414eafb8d05f87f84 2020-09-09 10:06:00 -0500 to 2020-10-05 09:11:18 -0500 - clarify description of when ? can be used (rust-lang/book#2471) ## rust-by-example 1 commits in 7d3ff1c12db08a847a57a054be4a7951ce532d2d..152475937a8d8a1f508d8eeb57db79139bc803d9 2020-09-28 15:54:25 -0300 to 2020-10-09 09:29:50 -0300 - Add 1.45.0 cast documentation (rust-lang/rust-by-example#1384) ## embedded-book 2 commits in dd310616308e01f6cf227f46347b744aa56b77d9..79ab7776929c66db83203397958fa7037d5d9a30 2020-09-26 08:54:08 +0000 to 2020-10-12 08:00:05 +0000 - llvm-objdump: Use two hyphens in flags to objdump (rust-embedded/book#270) - Start/hardware: clarify which file needs tweaking (rust-embedded/book#266)
As requested in rust-lang/rust#71599. Also references the rust-lang/rust#48109 (comment)