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

Properly infer types with type casts #15271

Merged
merged 1 commit into from Jul 28, 2023
Merged

Conversation

lowr
Copy link
Contributor

@lowr lowr commented Jul 12, 2023

This PR reenables Expectation::Castable (previous attempt at #14104, reverted by #14120) and implements type cast checks, which enable us to infer a bit more.

Castable expectations are relatively weak -- they only influence the inference if we cannot infer the types by other means. Therefore, we need to defer possible type unification with the casted type until we type check all expressions of the body. This PR adds a struct and slots in InferenceContext for the deferred cast checks (c.f. CastCheck in rustc_hir_typeck).

I only implemented the bits that affect the inference result. It should be possible to return type adjustments for well-formed casts and report diagnostics for invalid casts, but I'm leaving them for future work for now.

Fixes #11571
Fixes #15246

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 12, 2023
@lowr
Copy link
Contributor Author

lowr commented Jul 12, 2023

I'm dumping analysis-stats results below. TL;DR is that none of the numbers regressed and several ?ty numbers improved a bit. All crates besides self are from rustc-perf@c52ee6 as usual.

crate 75ac37f this branch
self (75ac37f) exprs: 605528, ??ty: 97, ?ty: 171, !ty: 3
pats: 151212, ??ty: 20, ?ty: 8, !ty: 0
exprs: 605528, ??ty: 97, ?ty: 168, !ty: 3
pats: 151212, ??ty: 20, ?ty: 8, !ty: 0
diesel exprs: 161838, ??ty: 305, ?ty: 4900, !ty: 63
pats: 33418, ??ty: 122, ?ty: 1211, !ty: 0
exprs: 161838, ??ty: 305, ?ty: 4900, !ty: 63
pats: 33418, ??ty: 122, ?ty: 1211, !ty: 0
webrender/webrender exprs: 131728, ??ty: 93, ?ty: 42, !ty: 2
pats: 24082, ??ty: 13, ?ty: 15, !ty: 11
exprs: 131728, ??ty: 92, ?ty: 40, !ty: 2
pats: 24082, ??ty: 13, ?ty: 15, !ty: 11
ripgrep exprs: 72427, ??ty: 27, ?ty: 7, !ty: 0
pats: 12079, ??ty: 3, ?ty: 0, !ty: 0
exprs: 72427, ??ty: 27, ?ty: 7, !ty: 0
pats: 12079, ??ty: 3, ?ty: 0, !ty: 0

@HKalbasi
Copy link
Member

@bors r+

@bors
Copy link
Collaborator

bors commented Jul 28, 2023

📌 Commit 074488b has been approved by HKalbasi

It is now in the queue for this repository.

@bors
Copy link
Collaborator

bors commented Jul 28, 2023

⌛ Testing commit 074488b with merge 037844c...

@bors
Copy link
Collaborator

bors commented Jul 28, 2023

☀️ Test successful - checks-actions
Approved by: HKalbasi
Pushing 037844c to master...

@bors bors merged commit 037844c into rust-lang:master Jul 28, 2023
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect inferred type when first used as a pointer A test for cast propagation
4 participants