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

diagnostics: account for self type when looking for source of unsolved type variable #109957

Merged
merged 1 commit into from
Apr 7, 2023

Conversation

fmease
Copy link
Member

@fmease fmease commented Apr 5, 2023

Fixes #109905.

When searching for the source of an unsolved infer var inside of a list of generic args, we look through the tcx.generics_of(…).own_substs(…) which skips the self type if present. However, the computed argument_index is later1 used to index into tcx.generics_of(…).params which may still contain the self type. In such case, we are off by one when indexing into the parameters.

From now on, we account for this immediately after calling own_substs which keeps things local.

This also fixes the wrong output in the preexisting UI test inference/need_type_info/concrete-impl.rs which was overlooked. It used to claim that the type of type parameter Self couldn't be inferred in <Struct as Ambiguous<_>>::method() which of course isn't true: Self equals Struct here, A couldn't be inferred.

@rustbot label A-diagnostics

Footnotes

  1. https://github.com/rust-lang/rust/blob/f98a2718141593fbb8dbad10acc537786d748156/compiler/rustc_infer/src/infer/error_reporting/need_type_info.rs#L471

@rustbot
Copy link
Collaborator

rustbot commented Apr 5, 2023

r? @petrochenkov

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 5, 2023
@rustbot
Copy link
Collaborator

rustbot commented Apr 5, 2023

Some changes occurred in need_type_info.rs

cc @lcnr

@rustbot rustbot added the A-diagnostics Area: Messages for errors, warnings, and lints label Apr 5, 2023
@fmease fmease changed the title Account for self type when looking for type parameter that couldn't be inferred Account for self type when looking for source of unsolved type variable Apr 5, 2023
@fmease fmease changed the title Account for self type when looking for source of unsolved type variable diagnostics: account for self type when looking for source of unsolved type variable Apr 5, 2023
@petrochenkov petrochenkov 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-review Status: Awaiting review from the assignee but also interested parties. labels Apr 5, 2023
@rust-log-analyzer

This comment has been minimized.

@fmease
Copy link
Member Author

fmease commented Apr 5, 2023

Is the situation generics.parent.is_some() && generics.has_self possible?

The CI test failures answer it lol. Going back to checking the parent

@fmease
Copy link
Member Author

fmease commented Apr 5, 2023

@rustbot review

@rustbot rustbot 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 Apr 5, 2023
@fmease
Copy link
Member Author

fmease commented Apr 6, 2023

Is the situation generics.parent.is_some() && generics.has_self possible?

I've looked at the implementation of tcx.generics_of. has_self can also be true if the parent has_self in which case the (own) parameters (generics.params) don't contain the self type and we shouldn't offset the index in this case.

Looking at (vec![1,2,3]).into_iter().sum() in test tests/ui/span/type-annotations-needed-expr.rs for example where we cannot infer param S on method sum, generics.parent is Some(Iterator_def_id) but generics.params only contains param S and notably not the self type. Instead, the parent (trait Iterator) has the self type and thus generics.has_self is true.

relevant definitions
  • generics.parent: DefId(2:7268 ~ core[280a]::iter::traits::iterator::Iterator)
  • generics.has_self: true
  • generics.params: ["S"] (w/o self type)
  • substs: [std::vec::IntoIter<i32>, _#20t]
  • generics.own_substs_no_defaults(substs): [_#20t]

@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented Apr 6, 2023

📌 Commit b904ce9 has been approved by petrochenkov

It is now in the queue for this repository.

@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 6, 2023
bors added a commit to rust-lang-ci/rust that referenced this pull request Apr 7, 2023
…iaskrgr

Rollup of 6 pull requests

Successful merges:

 - rust-lang#109806 (Workaround rust-lang#109797 on windows-gnu)
 - rust-lang#109957 (diagnostics: account for self type when looking for source of unsolved type variable)
 - rust-lang#109960 (Fix buffer overrun in bootstrap and (test-only) symlink_junction)
 - rust-lang#110013 (Label `non_exhaustive` attribute on privacy errors from non-local items)
 - rust-lang#110016 (Run collapsed GUI test in mobile mode as well)
 - rust-lang#110022 (fix: fix regression in rust-lang#109203)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit e63586f into rust-lang:master Apr 7, 2023
@rustbot rustbot added this to the 1.70.0 milestone Apr 7, 2023
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.

cannot infer type of the type parameter 'a declared on the trait Baz
6 participants