-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
revise fn sig and ty representation so that bot is not a type. #14973
Comments
I would be willing to mentor this. |
On it :) |
@pnkfelix I've been looking around the code, and I have to admit that I don't really know by what to start, would you happen to have any advice ? |
This is roughly what I had in mind:
I'm not sure what the fallout will be -- I can imagine that some of those new inference variables will wind up being unconstrained, leading to type checking errors. We can tackle that problem when we come to it I guess. For one thing, at some point, we should probably allow unconstrained type variables anyway, since they don't really hurt anybody afaik. But even if we don't want to do that more generally, we could at least allow these type variables to be unconstrained (that is, unify them with (*) This is actually how the AST used to be structured. For all I know, it may still be structured that way. |
I should say I investigated removing ty_bot from the type system in the past and found that it may be rather impractical. Specifically, in the following program: fn f(v: Vec<uint>) {
for x in v.iter() {
let y: uint = if x > 0 {
break;
} else {
42u
};
println!("{}", y);
}
} there'd be no obvious way of unifying the two branches ( I think it'd be possible to make it so that inference would look for all "diverging" constructs to decide the types of which branches it needs to unify but that seems to defeat the purpose. Thoughts, @nikomatsakis? |
On Sat, Sep 20, 2014 at 10:43:08AM -0700, Jakub Wieczorek wrote:
The usual way is to create a fresh type variable as the result of the |
But there's ambiguity in whether a type variable originates in computation diverging or if it's truly unconstrained. For example, with your plan implemented the following two programs would type check the same way: if x > 0 {
Default::default();
()
} else {
42u
} if x > 0 {
break;
} else {
42u
} Whereas it seems the first one should not type check. Specifically, any time the type of a statement in a block is unconstrained, the block itself would unify with anything. That seems like it'd be way too permissive and dangerous. I may be completely off here but it doesn't seem like type variables give us the sort of nice propagation rules that ty_bot has. Not that I'm particularly fond of ty_bot... |
Soooooo ... |
@gamazeps it seems to me that if you understand the conversation above, and want to still look into following the path outlined by @nikomatsakis in the comments above, then you should feel free to try. I myself would not want to close this issue until I have a chance to look into @jakub- 's examples carefully (I do not yet see why he claims that his first example would type-check under the plan). But nonetheless, feel free to move on to another issue, since this one clearly may be more difficult than I had anticipated. |
@jakub- I do not understand why you say that this program would type check:
Can you clarify? For one thing, there is no bottom type involved here? |
I think there is some misunderstanding. The bottom type is only "created" in the following situations:
It reflects the fact that control flow will never reach that point. I am proposing that we use a fresh type variable in these situations. The bottom type is already considered, well, the bottom of the type lattice. This means that it is assignable to any other type (that is, |
After an IRC discussion with @nikomatsakis I can happily say I've been proven wrong and that the plan is workable. One thing that remains to be seen is how the new rules will affect expressions such as @gamazeps, I think this is definitely doable and my alarm was unwarranted. :) Please feel free to proceed if you're still interested in implementing this and I'm more than happy to assist. |
I don't think there is any reason that those programs have to break, but it is perhaps more refactoring than I imagined initially to keep them working. |
In particular, the type checker would potentially have to track whether something diverges or not separately from its type, though of course we could take a different approach like marking the type variables in question in some special way. |
Spawned off from #13847: @nikomatsakis says:
The text was updated successfully, but these errors were encountered: