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

Regression in diagnostic quality for unimplemented traits on arrays. #92113

Closed
BGR360 opened this issue Dec 20, 2021 · 8 comments
Closed

Regression in diagnostic quality for unimplemented traits on arrays. #92113

BGR360 opened this issue Dec 20, 2021 · 8 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-trait-system Area: Trait system C-bug Category: This is a bug. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. P-low Low priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@BGR360
Copy link
Contributor

BGR360 commented Dec 20, 2021

Commit dcd716f (#86986) appears to have regressed the quality of compiler diagnostics produced when an array type does not satisfy a trait but its slice version does (there may be other scenarios that see similar regressions, but this is where I've seen it).

This is currently blocking #91314.

Motivating example (playground):

trait Read {}

impl Read for &[u8] {}

fn wants_read(_: impl Read) {}

fn main() {
    wants_read([0u8]);
}

When compiled on dcd716f~1 (83b32f2), rustc provides a help message telling the user that the trait is implemented for slices:

error[E0277]: the trait bound `[u8; 1]: Read` is not satisfied
 --> ../scratch/issue-90528-unsizing-suggestion-1.rs:8:16
  |
8 |     wants_read([0u8]);
  |     ---------- ^^^^^ the trait `Read` is not implemented for `[u8; 1]`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the following implementations were found:
            <&[u8] as Read>
note: required by a bound in `wants_read`
 --> ../scratch/issue-90528-unsizing-suggestion-1.rs:5:23
  |
5 | fn wants_read(_: impl Read) {}
  |                       ^^^^ required by this bound in `wants_read`

Compiled on dcd716f, this help message no longer appears:

error[E0277]: the trait bound `[u8; 1]: Read` is not satisfied
 --> ../scratch/issue-90528-unsizing-suggestion-1.rs:8:16
  |
8 |     wants_read([0u8]);
  |     ---------- ^^^^^ the trait `Read` is not implemented for `[u8; 1]`
  |     |
  |     required by a bound introduced by this call
  |
note: required by a bound in `wants_read`
 --> ../scratch/issue-90528-unsizing-suggestion-1.rs:5:23
  |
5 | fn wants_read(_: impl Read) {}
  |                       ^^^^ required by this bound in `wants_read`

Inspiration for regression tests can be taken from #91314.

@rustbot modify labels: +A-diagnostics +T-compiler +D-terse

cc @lcnr

@BGR360 BGR360 added C-bug Category: This is a bug. regression-untriaged Untriaged performance or correctness regression. labels Dec 20, 2021
@rustbot rustbot added I-prioritize Issue: Indicates that prioritization has been requested for this issue. A-diagnostics Area: Messages for errors, warnings, and lints D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Dec 20, 2021
@BGR360
Copy link
Contributor Author

BGR360 commented Dec 20, 2021

@rustbot modify labels: -regression-untriaged +regression-from-stable-to-nightly

@rustbot rustbot added regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. and removed regression-untriaged Untriaged performance or correctness regression. labels Dec 20, 2021
@lcnr
Copy link
Contributor

lcnr commented Dec 21, 2021

While I think mentioning <&[u8] as Read> here is useful, I do not believe that reverting the change in #86986 is the right fix here.

I think that we could actually change these diagnostics to more generally deal with stuff like deref impls by explicitly looking for potential impls instead of looking at the existing impl candidates.

So when encountering a "trait bound is not satisfied" error for some type T, we look at impls for type &T, &mut T and [U] (if T = [U; N]) and then mention these impls with a note like "considering using a reference".

Do you want to look at this yourself, if not I should hopefully be able to make some time for this over the next few weeks

cc @estebank

@BGR360
Copy link
Contributor Author

BGR360 commented Dec 21, 2021

I do not believe that reverting the change in #86986 is the right fix here.

Certainly not. I'm a little bummed that the work I did in #91314 is kaput, but I agree with you.

explicitly looking for potential impls instead of looking at the existing impl candidates

I like that idea, but it seems like we're lacking tests to tell us what else might have lost information as a result of #86986, which will make it harder to know exactly which types of potential impls we should look for. The work I was doing in #91314 obviously tells us one category of potential impls we need to check: [U] if T = [U; N], but how can we know what else may have regressed? I'll have to lean on your understanding of the work you did.

Do you want to look at this yourself

I was just poking around in there, but this seems like a larger time commitment compared to the very directed suggestion generation of #91314.

With guidance, I'd be happy to try out doing just the [U; N] -> [U] searching, and seeing how it goes from there. My main worry is that it sounds like this may not be something we've done before in rustc_trait_selection (that is, constructing a new type and querying for impls on that type). But if there is an example you can point me to, then that would probably make it a lot easier.

@rustbot label +A-traits

@rustbot rustbot added the A-trait-system Area: Trait system label Dec 21, 2021
@lcnr
Copy link
Contributor

lcnr commented Dec 21, 2021

alright 👍 i also believe that we aren't doing anything like that, but i think the required changes won't be too large though they do require more knowledge about how rustc works 😅 .

Going to write some instructions this week, feel free to ping me either here or on zulip in case i forget

@lcnr
Copy link
Contributor

lcnr commented Dec 22, 2021

so, we collect the impl candidates in

fn find_similar_impl_candidates(
&self,
trait_ref: ty::PolyTraitRef<'tcx>,
) -> Vec<ty::TraitRef<'tcx>> {

The reason we don't consider slice impls anymore is this comparision

if simp != imp_simp {
return None;
}

we want some fuzzy equality here instead of comparing their simplified types. How exactly that fuzzy equality should work requires a bit of fiddling so that's something you probably have to experiment with yourself. I would expect that directly using Ty here is better than using SimplifiedTypes. It should end up looking similar to

fn fuzzy_match_tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> bool {
which you can probably reuse 😅

@apiraino
Copy link
Contributor

Assigning priority as discussed in the Zulip thread of the Prioritization Working Group.

@rustbot label -I-prioritize +P-low

@rustbot rustbot added P-low Low priority and removed I-prioritize Issue: Indicates that prioritization has been requested for this issue. labels Dec 23, 2021
@steffahn
Copy link
Member

@rustbot label regression-from-stable-to-beta, -regression-from-stable-to-nightly

@rustbot rustbot added regression-from-stable-to-beta Performance or correctness regression from stable to beta. and removed regression-from-stable-to-nightly Performance or correctness regression from stable to nightly. labels Jan 19, 2022
@pietroalbini pietroalbini added regression-from-stable-to-stable Performance or correctness regression from one stable version to another. and removed regression-from-stable-to-beta Performance or correctness regression from stable to beta. labels Apr 6, 2022
@BGR360
Copy link
Contributor Author

BGR360 commented Sep 11, 2022

This is fixed on stable. Not sure exactly what fixed it, but I suspect #93298.

error[E0277]: the trait bound `[u8; 1]: Read` is not satisfied
 --> src/main.rs:8:16
  |
8 |     wants_read([0u8]);
  |     ---------- ^^^^^ the trait `Read` is not implemented for `[u8; 1]`
  |     |
  |     required by a bound introduced by this call
  |
  = help: the trait `Read` is implemented for `&[u8]`
note: required by a bound in `wants_read`
 --> src/main.rs:5:23
  |
5 | fn wants_read(_: impl Read) {}
  |                       ^^^^ required by this bound in `wants_read`

@BGR360 BGR360 closed this as completed Sep 11, 2022
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 A-trait-system Area: Trait system C-bug Category: This is a bug. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. P-low Low priority regression-from-stable-to-stable Performance or correctness regression from one stable version to another. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants