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

Improve Error Messaging for Unconstructed Structs and Enum Variants in Generic Contexts #92569

Merged
merged 2 commits into from
Apr 27, 2022

Conversation

George-lewis
Copy link
Contributor

@George-lewis George-lewis commented Jan 5, 2022

Improves error messaging for empty-tuple structs and enum variants in certain generic contexts. See new ui tests for examples.

Closes #87181

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 5, 2022
@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @davidtwco (or someone else) soon.

Please see the contribution instructions for more information.

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jan 5, 2022
@George-lewis
Copy link
Contributor Author

r? @estebank

@rust-highfive rust-highfive assigned estebank and unassigned davidtwco Jan 5, 2022
if let Some(snippet) = snippet {
if let ty::FnDef(def_id, _) = rcvr_ty.kind() {
let hir_id =
tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can we assume this won't fail here? Is it ok for it to fail?

@camelid
Copy link
Member

camelid commented Jan 5, 2022

Please also add a UI test (src/test/ui/...).

@camelid camelid added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. A-diagnostics Area: Messages for errors, warnings, and lints and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jan 5, 2022
@camelid camelid changed the title Improve Error Messaging for Unconstructed Empty-Tuple Members Improve Error Messaging for Unconstructed Empty-Tuple Fields Jan 5, 2022
Comment on lines 1 to 2
error[E0599]: no method named `foo` found for fn item `fn() -> Inner {Inner}` in the current scope
--> src/test/ui/typeck/issue-87181-empty-tuple-fields.rs:16:15
Copy link
Member

Choose a reason for hiding this comment

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

I think you need to run ./x.py test --bless src/test/ui/typeck/issue-87181-empty-tuple-fields.rs.

Copy link
Contributor Author

@George-lewis George-lewis Jan 5, 2022

Choose a reason for hiding this comment

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

I think I might be missing something? I ran your command and it generated the stderr file, but the test still fails, and it's showing me some JSON.

Edit: I figured it out, I needed a //~^ ERROR in there

@George-lewis
Copy link
Contributor Author

@camelid Did I pick an acceptable filename for the ui test? I couldn't find much guidance in the contribution guide

@camelid
Copy link
Member

camelid commented Jan 5, 2022

The name seems fine to me.

@rust-log-analyzer

This comment has been minimized.

@George-lewis
Copy link
Contributor Author

George-lewis commented Jan 5, 2022

Proposal: Go beyond the scope of the ticket to improve error handling to support tuple-structs with elements, and also add the check to member access rather than just to method calls

For instance we can cover this case as so:

struct Bar<T> {
    bar: T
}

struct Foo(u8);
impl Foo {
    fn foo() { }
}

fn main() {
    let thing = Bar { bar: Foo };
    thing.bar.0;
}
error[E0609]: no field `0` on type `fn(u8) -> Foo {Foo}`
  --> test.rs:12:15
   |
12 |     thing.bar.0;
   |     --------- ^
   |     |
   |     help: call the constructor: `(thing.bar)(_)`
   |
   = help: placeholder

error: aborting due to previous error

For more information about this error, try `rustc --explain E0609`.

Also, if the original code has unnecessary brackets, like (thing.0).0, the current code will suggest a superfluous pair of brackets. Should we worry about that or just let the unnecessary-bracket lint pick it up for us?

@camelid camelid added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jan 5, 2022
@George-lewis
Copy link
Contributor Author

I have expanded the scope a little in line with my proposal. Now, the same checks are applied to field accesses, and non-empty tuple constructors are handled. There are 2 new tests to cover these cases.

@George-lewis George-lewis changed the title Improve Error Messaging for Unconstructed Empty-Tuple Fields Improve Error Messaging for Unconstructed Structs and Enum Variants in Generic Contexts Jan 5, 2022
@JohnCSimon JohnCSimon added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 13, 2022
@rust-log-analyzer

This comment has been minimized.

@bors
Copy link
Contributor

bors commented Apr 26, 2022

💔 Test failed - checks-actions

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

curl: (6) Could not resolve host: ci-mirrors.rust-lang.org

It seems like there might have been a network error?

@estebank
Copy link
Contributor

@bors retry

@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 Apr 26, 2022
@bors
Copy link
Contributor

bors commented Apr 26, 2022

🔒 Merge conflict

This pull request and the master branch diverged in a way that cannot be automatically merged. Please rebase on top of the latest master branch, and let the reviewer approve again.

How do I rebase?

Assuming self is your fork and upstream is this repository, you can resolve the conflict following these steps:

  1. git checkout 87181 (switch to your branch)
  2. git fetch upstream master (retrieve the latest master)
  3. git rebase upstream/master -p (rebase on top of it)
  4. Follow the on-screen instruction to resolve conflicts (check git status if you got lost).
  5. git push self 87181 --force-with-lease (update this PR)

You may also read Git Rebasing to Resolve Conflicts by Drew Blessing for a short tutorial.

Please avoid the "Resolve conflicts" button on GitHub. It uses git merge instead of git rebase which makes the PR commit history more difficult to read.

Sometimes step 4 will complete without asking for resolution. This is usually due to difference between how Cargo.lock conflict is handled during merge and rebase. This is normal, and you should still perform step 5 to update this PR.

Error message
Auto-merging compiler/rustc_typeck/src/check/method/suggest.rs
CONFLICT (content): Merge conflict in compiler/rustc_typeck/src/check/method/suggest.rs
Auto-merging compiler/rustc_typeck/src/check/expr.rs
Automatic merge failed; fix conflicts and then commit the result.

@bors bors added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Apr 26, 2022
@estebank
Copy link
Contributor

It seems like you'll have to rebase. You might want to squash all your commits into one before doing that to make it easier to resolve the merge conflicts.

@George-lewis
Copy link
Contributor Author

It does seem that way. I'm looking at it now and it looks like some new diagnostics were implemented, so I'll have to figure out what it does so I can integrate it with my diagnostics 🤔

@bors
Copy link
Contributor

bors commented Apr 26, 2022

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

@George-lewis
Copy link
Contributor Author

Oh God

@George-lewis
Copy link
Contributor Author

George-lewis commented Apr 26, 2022

I think I messed up the rebase. At this point I just want to make a new branch

Edit: I'm in the process of making a new branch now. I guess I'll open a new PR when it's ready

@George-lewis
Copy link
Contributor Author

Unfortunately I had to force push, but I think it should be ok now

@estebank
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Apr 26, 2022

📌 Commit a6b570b has been approved by estebank

@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-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Apr 26, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 27, 2022
Rollup of 6 pull requests

Successful merges:

 - rust-lang#92569 (Improve Error Messaging for Unconstructed Structs and Enum Variants in Generic Contexts)
 - rust-lang#96370 (Cleanup `report_method_error` a bit)
 - rust-lang#96383 (Fix erased region escaping into wfcheck due to rust-lang#95395)
 - rust-lang#96385 (Recover most `impl Trait` and `dyn Trait` lifetime bound suggestions under NLL)
 - rust-lang#96410 (rustdoc: do not write `{{root}}` in `pub use ::foo` docs)
 - rust-lang#96430 (Fix handling of `!` in rustdoc search)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors
Copy link
Contributor

bors commented Apr 27, 2022

⌛ Testing commit a6b570b with merge 99b70ee...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Zero member tuple struct should warn on incorrect construction