Skip to content
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

Partially implement type ascription #30184

Merged
merged 3 commits into from Dec 19, 2015
Merged

Partially implement type ascription #30184

merged 3 commits into from Dec 19, 2015

Conversation

@petrochenkov
Copy link
Contributor

@petrochenkov petrochenkov commented Dec 3, 2015

This PR is a rebase of the original PR by @eddyb #21836 with some unrebasable parts manually reapplied, feature gate added + type equality restriction added as described below.

This implementation is partial because the type equality restriction is applied to all type ascription expressions and not only those in lvalue contexts. Thus, all difficulties with detection of these contexts and translation of coercions having effect in runtime are avoided.
So, you can't write things with coercions like let slice = &[1, 2, 3]: &[u8];. It obviously makes type ascription less useful than it should be, but it's still much more useful than not having type ascription at all.
In particular, things like let v = something.iter().collect(): Vec<_>; and let u = t.into(): U; work as expected and I'm pretty happy with these improvements alone.

Part of #23416

@petrochenkov
Copy link
Contributor Author

@petrochenkov petrochenkov commented Dec 3, 2015

Hm, @rust-highfive failed to assign a reviewer.

r? @eddyb

@eddyb
Copy link
Member

@eddyb eddyb commented Dec 3, 2015

I wrote the original version of this code, so I'd rather have someone else look over it.
r? @nikomatsakis

@rust-highfive rust-highfive assigned nikomatsakis and unassigned eddyb Dec 3, 2015
@@ -2671,6 +2671,14 @@ fn check_lit<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
}
}

fn check_expr_eq_type<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
expr: &'tcx hir::Expr,

This comment has been minimized.

@oli-obk

oli-obk Dec 3, 2015
Contributor

wrong indentation

@bors
Copy link
Contributor

@bors bors commented Dec 4, 2015

The latest upstream changes (presumably #29850) made this pull request unmergeable. Please resolve the merge conflicts.

@petrochenkov petrochenkov force-pushed the petrochenkov:ascr branch from 8fd1579 to 96d2791 Dec 4, 2015
@petrochenkov
Copy link
Contributor Author

@petrochenkov petrochenkov commented Dec 4, 2015

Rebased.

@bors
Copy link
Contributor

@bors bors commented Dec 16, 2015

The latest upstream changes (presumably #30206) made this pull request unmergeable. Please resolve the merge conflicts.

eddyb and others added 2 commits Feb 1, 2015
@petrochenkov petrochenkov force-pushed the petrochenkov:ascr branch from 96d2791 to 7afc5e2 Dec 16, 2015
@petrochenkov
Copy link
Contributor Author

@petrochenkov petrochenkov commented Dec 16, 2015

Rebased.

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Dec 17, 2015

Sorry for the delay! I'll get to this soon.

@@ -320,6 +320,8 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
name: Field::new(index.node as usize) },
hir::ExprCast(ref source, _) =>
ExprKind::Cast { source: source.to_ref() },
hir::ExprType(ref source, _) =>
return source.make_mirror(cx),

This comment has been minimized.

@nikomatsakis

nikomatsakis Dec 18, 2015
Contributor

This doesn't seem right. I think we probably want to convert this to a ExprKind::Cast.

This comment has been minimized.

@nikomatsakis

nikomatsakis Dec 18, 2015
Contributor

/me reconsiders. Actually, let me read a bit more and get back to this point!

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Dec 18, 2015

@petrochenkov I didn't notice at first that you skipped over the coercion aspect. Makes sense for a first pass, this certainly simplifies things. The code looks great. The only thing I would want to add are some parsing tests that test the relative precedence of : with other operators. Can you do that?

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Dec 18, 2015

Some examples tests for precedence:

  • &[1, 2, 3]: &[u8; 3] => (&[1,2,3]): &[u8: 3]
  • x:u8+y => (22:u8)+42
  • x+y:u8 => x+(y:u8)

The only tricky part is how to write such at est. I envisioned testing this by setting up some scenarios that will fail to type-check if the precedence is not correct? I'm not sure if there is a better way.

@petrochenkov
Copy link
Contributor Author

@petrochenkov petrochenkov commented Dec 18, 2015

Sorry for the delay! I'll get to this soon.

Thanks! In fact my free time is coming to an end rapidly, so I have to wrap up my work on rustc ASAP (at least for now).
I'll add tests for operator precedence.

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Dec 18, 2015

@petrochenkov

Thanks! In fact my free time is coming to an end rapidly, so I have to wrap up my work on rustc ASAP (at least for now).

:( Let me know if there is work you think you will not have time for! I'd be happy to land this PR (or privacy) and then do the followup work myself.

+ Rebase fixes
@petrochenkov petrochenkov force-pushed the petrochenkov:ascr branch from 7afc5e2 to 95fdaf2 Dec 18, 2015
@petrochenkov
Copy link
Contributor Author

@petrochenkov petrochenkov commented Dec 18, 2015

Updated with the operator precedence tests.

@nikomatsakis
Copy link
Contributor

@nikomatsakis nikomatsakis commented Dec 19, 2015

@bors r+

Another nice PR. Thanks @petrochenkov!

@bors
Copy link
Contributor

@bors bors commented Dec 19, 2015

📌 Commit 95fdaf2 has been approved by nikomatsakis

@bors
Copy link
Contributor

@bors bors commented Dec 19, 2015

Testing commit 95fdaf2 with merge 440ef8b...

bors added a commit that referenced this pull request Dec 19, 2015
This PR is a rebase of the original PR by @eddyb #21836 with some unrebasable parts manually reapplied, feature gate added + type equality restriction added as described below.

This implementation is partial because the type equality restriction is applied to all type ascription expressions and not only those in lvalue contexts. Thus, all difficulties with detection of these contexts and translation of coercions having effect in runtime are avoided.
So, you can't write things with coercions like `let slice = &[1, 2, 3]: &[u8];`. It obviously makes type ascription less useful than it should be, but it's still much more useful than not having type ascription at all.
In particular, things like `let v = something.iter().collect(): Vec<_>;` and `let u = t.into(): U;` work as expected and I'm pretty happy with these improvements alone.

Part of #23416
@bors bors merged commit 95fdaf2 into rust-lang:master Dec 19, 2015
2 checks passed
2 checks passed
continuous-integration/travis-ci/pr The Travis CI build passed
Details
homu Test successful
Details
@petrochenkov
Copy link
Contributor Author

@petrochenkov petrochenkov commented Dec 19, 2015

This should probably be marked with relnotes

@eddyb eddyb added the relnotes label Dec 19, 2015
@petrochenkov petrochenkov deleted the petrochenkov:ascr branch Sep 21, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

6 participants