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

Compiler suggestions send me into an endless loop #71394

Closed
Lehona opened this issue Apr 21, 2020 · 1 comment · Fixed by #71618
Closed

Compiler suggestions send me into an endless loop #71394

Lehona opened this issue Apr 21, 2020 · 1 comment · Fixed by #71618
Assignees
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-traits Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Lehona
Copy link

Lehona commented Apr 21, 2020

I tried this code:

fn main() {
    let data: &[u8] = &[0; 10][..];
    let points: &[i8] = data.into();
}

which outputs

error[E0277]: the trait bound `&[i8]: std::convert::From<&[u8]>` is not satisfied
 --> src/main.rs:3:25
  |
3 |     let points: &[i8] = data.into();
  |                         ^^^^^^^^^^^^^^^ the trait `std::convert::From<&[u8]>` is not implemented for `&[i8]`
  |
  = note: `std::convert::From<&[u8]>` is implemented for `&mut [i8]`, but not for `&[i8]`
  = note: required because of the requirements on the impl of `std::convert::Into<&[i8]>` for `&[u8]`
  = note: required by `std::convert::Into::into`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
error: could not compile `playground`.

To learn more, run the command again with --verbose.

Note the suggestion

  = note: `std::convert::From<&[u8]>` is implemented for `&mut [i8]`, but not for `&[i8]`

Changing the type of points to &mut [i8] will simply swap the suggestion to say From<&[u8]> is not implemented for &mut [i8], but it is implemented for &[i8], ultimately making me chase my own tail. I have scoured the docs and came to the conclusion that From<&[u8]> is not implemented for either of those types, so why does the compiler suggest it is?

This behaviour occurs across stable, beta and nightly on 1.42.

@Lehona Lehona added the C-bug Category: This is a bug. label Apr 21, 2020
@jonas-schievink jonas-schievink added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-traits Area: Trait system D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 21, 2020
@ecstatic-morse
Copy link
Contributor

@estebank

if let ty::Ref(region, t_type, mutability) = trait_ref.skip_binder().self_ty().kind {
let trait_type = match mutability {
hir::Mutability::Mut => self.tcx.mk_imm_ref(region, t_type),
hir::Mutability::Not => self.tcx.mk_mut_ref(region, t_type),
};
let new_obligation = self.mk_obligation_for_def_id(
trait_ref.skip_binder().def_id,
trait_type,
ObligationCause::dummy(),
obligation.param_env,
);
if self.evaluate_obligation_no_overflow(&new_obligation).must_apply_modulo_regions()

When we call mk_obligation_for_def_id here, are we forgetting that we are trying to prove From<&[u8]> for &mut [i8] and instead looking for any impl of From<T>?

@ecstatic-morse ecstatic-morse self-assigned this Apr 24, 2020
ecstatic-morse added a commit to ecstatic-morse/rust that referenced this issue May 22, 2020
ecstatic-morse added a commit to ecstatic-morse/rust that referenced this issue May 22, 2020
RalfJung added a commit to RalfJung/rust that referenced this issue May 23, 2020
…atsakis

Preserve substitutions when making trait obligations for suggestions

Resolves rust-lang#71394.

I *think* `map_bound_ref` is correct here. In any case, I think a lot of the diagnostic code is using `skip_binder` more aggressively than it should be, so I doubt that this is worse than the status quo. The assertion that `new_self_ty` has no escaping bound vars should be enough.

r? @estebank

cc @nikomatsakis Is the call to `skip_binder` on line 551 (and elsewhere in this file) appropriate? https://github.com/rust-lang/rust/blob/46ec74e60f238f694b46c976d6217e7cf8d4cf1a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs#L537-L565
Dylan-DPC-zz pushed a commit to Dylan-DPC-zz/rust that referenced this issue May 23, 2020
…atsakis

Preserve substitutions when making trait obligations for suggestions

Resolves rust-lang#71394.

I *think* `map_bound_ref` is correct here. In any case, I think a lot of the diagnostic code is using `skip_binder` more aggressively than it should be, so I doubt that this is worse than the status quo. The assertion that `new_self_ty` has no escaping bound vars should be enough.

r? @estebank

cc @nikomatsakis Is the call to `skip_binder` on line 551 (and elsewhere in this file) appropriate? https://github.com/rust-lang/rust/blob/46ec74e60f238f694b46c976d6217e7cf8d4cf1a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs#L537-L565
@bors bors closed this as completed in ff48fc9 May 24, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix`. A-traits Area: Trait system C-bug Category: This is a bug. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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.

3 participants