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

rustc_typeck: improve diagnostics for -> _ fn return type #62694

Merged
merged 2 commits into from
Jul 19, 2019

Conversation

lundibundi
Copy link
Contributor

@lundibundi lundibundi commented Jul 15, 2019

This should implement IIUC the mentioned issue.

I'm not sure if there is a better way than get_infer_ret_ty to get/check the return type without code duplication.

Also, is this unwrap be okay ty::Binder::bind(*tables.liberated_fn_sigs().get(hir_id).unwrap())?

r? @eddyb
Closes: #56132

@rust-highfive

This comment has been minimized.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 15, 2019
| ^
| |
| not allowed in type signatures
| help: replace `_` with the correct return type: `i32`

error[E0121]: the type placeholder `_` is not allowed within types on item signatures
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Heh, you could also implement it for const/static with type _.

Copy link
Contributor

@Centril Centril Jul 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could also add parser recovery for when no type is provided syntactically, for example: const FOO = 42;. I think more people would attempt that. But this could be done in a follow up.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Help for const FOO = 42; looks nice, that's how I usually forget about it in const's, though I'm not sure what'd be the correct parser recovery, replacing the type with Infer and then failing here with proper help?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, basically delegate to the AST representation of const $name: _ = $expr; and make sure to error in the parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Centril But would we have the correct type to suggest in the parser? (I assume you implied the type of self.parse_expr() on the right side of const declaration)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct type in the parser would be Infer.

Copy link
Contributor Author

@lundibundi lundibundi Jul 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then the suggestion for const FOO = 42 would be the same as for const FOO: _ = 42 - to replace the _ even though the user hasn't written it, could we perhaps denote some sort of type-absence to handle that properly? Or am I misunderstanding something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can skip this for now, tbh.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let's follow up later.

@lundibundi We should take care not to suggest replacing _ with something since the user did not write that. Instead we should indicate that the type is missing and that we think the type is so and so.

Copy link
Member

@eddyb eddyb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great! I only had minor nits.

);
}
diag.emit();
ty::Binder::bind(*tables.liberated_fn_sigs().get(hir_id).unwrap())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, you could also get the return type from here, instead of from the body. Not sure which is better, cc @matthewjasper @nikomatsakis

Copy link
Contributor Author

@lundibundi lundibundi Jul 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be at least more 'correct' since we are suggesting the return type of this function and not the 'body' and since we already have it might as well use it.

Edit: Pushed as a separate commit.

@rust-highfive

This comment has been minimized.

@lundibundi
Copy link
Contributor Author

Merged fixups and rebased on master as there were quite many fixups! and GitHub displayed the commits in the wrong order anyway.

@eddyb
Copy link
Member

eddyb commented Jul 18, 2019

@bors r+

@bors
Copy link
Contributor

bors commented Jul 18, 2019

📌 Commit f8681f0 has been approved by eddyb

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 18, 2019
@bors
Copy link
Contributor

bors commented Jul 19, 2019

⌛ Testing commit f8681f0 with merge f9477a7...

bors added a commit that referenced this pull request Jul 19, 2019
rustc_typeck: improve diagnostics for -> _ fn return type

This should implement IIUC the mentioned issue.

~~I'm not sure if there is a better way than `get_infer_ret_ty` to get/check the return type without code duplication.~~

~~Also, is this unwrap be okay `ty::Binder::bind(*tables.liberated_fn_sigs().get(hir_id).unwrap())`?~~

r? @eddyb
Closes: #56132
@bors
Copy link
Contributor

bors commented Jul 19, 2019

☀️ Test successful - checks-azure
Approved by: eddyb
Pushing f9477a7 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jul 19, 2019
@bors bors merged commit f8681f0 into rust-lang:master Jul 19, 2019
@lundibundi lundibundi deleted the help-infer-fn-ret branch July 19, 2019 15:31
Centril added a commit to Centril/rust that referenced this pull request Jul 21, 2019
… r=eddyb

rustc_typeck: improve diagnostics for _ const/static declarations

This continues rust-lang#62694 and adds type suggestions to const/static declarations with `_` type.

r? @eddyb
Centril added a commit to Centril/rust that referenced this pull request Jul 22, 2019
… r=eddyb

rustc_typeck: improve diagnostics for _ const/static declarations

This continues rust-lang#62694 and adds type suggestions to const/static declarations with `_` type.

r? @eddyb
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this pull request Jul 23, 2019
… r=eddyb

rustc_typeck: improve diagnostics for _ const/static declarations

This continues rust-lang#62694 and adds type suggestions to const/static declarations with `_` type.

r? @eddyb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Permit -> _ return types for improved diagnostics
6 participants