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

Fix ambiguous call to constraints.as_ref() #22

Merged
merged 1 commit into from
Apr 27, 2024

Conversation

mfrischknecht
Copy link
Contributor

If fast-ssh is built with an up-to-date version of rustc, the compiler complains about constraints.as_ref() being ambiguous, demanding the types involved in the .constraints() call be clarified with a ::<&[Constraint]> turbofish annotation. Calling constraints.as_slice() instead removes the ambiguity.

As an example, see this failing Nixpkgs build: https://hydra.nixos.org/build/243071074
Log: https://hydra.nixos.org/build/243071074/nixlog/4

Reproduced locally with a current rust version:

[nix-shell:~/source/fast-ssh]$ cargo build
   Compiling fast-ssh v0.3.1 (/home/manuel/source/fast-ssh)
error[E0283]: type annotations needed
  --> src/layout.rs:60:10
   |
60 |         .constraints(constraints.as_ref())
   |          ^^^^^^^^^^^             ------ type must be known at this point
   |          |
   |          cannot infer type of the type parameter `C` declared on the method `constraints`
   |
   = note: multiple `impl`s satisfying `Vec<Constraint>: AsRef<_>` found in the `alloc` crate:
           - impl<T, A> AsRef<Vec<T, A>> for Vec<T, A>
             where A: Allocator;
           - impl<T, A> AsRef<[T]> for Vec<T, A>
             where A: Allocator;
help: consider specifying the generic argument
   |
60 |         .constraints::<&T>(constraints.as_ref())
   |                     ++++++

For more information about this error, try `rustc --explain E0283`.
error: could not compile `fast-ssh` (bin "fast-ssh") due to previous error

[nix-shell:~/source/fast-ssh]$ cargo --version
cargo 1.74.1 (ecb9851af 2023-10-18)

nix-shell:~/source/fast-ssh]$ rustc --version
rustc 1.74.1 (a28077b28 2023-12-04)

If `fast-ssh` is built with an up-to-date version of `rustc`,
the compiler complains about `constraints.as_ref()` being ambiguous,
demanding the types involved in the `.constraints()` call be clarified
with a `::<&[Constraint]>` turbofish annotation:

```
Compiling fast-ssh v0.3.1 (/build/source)
error[E0283]: type annotations needed
  --> src/layout.rs:60:10
   |
60 |         .constraints(constraints.as_ref())
   |          ^^^^^^^^^^^             ------ type must be known at this point
   |          |
   |          cannot infer type of the type parameter `C` declared on the method `constraints`
   |
   = note: multiple `impl`s satisfying `Vec<Constraint>: AsRef<_>` found in the `alloc` crate:
           - impl<T, A> AsRef<Vec<T, A>> for Vec<T, A>
             where A: Allocator;
           - impl<T, A> AsRef<[T]> for Vec<T, A>
             where A: Allocator;
help: consider specifying the generic argument
   |
60 |         .constraints::<&T>(constraints.as_ref())
   |                     ++++++

For more information about this error, try `rustc --explain E0283`.
error: could not compile `fast-ssh` (bin "fast-ssh") due to previous error
```

Luckily, if we use `constraints.as_slice()` instead, the types
involved are still apparent to the compiler, and the resulting
code still remains about as readable as before.
mfrischknecht added a commit to mfrischknecht/nixpkgs that referenced this pull request Jan 20, 2024
peterhoeg pushed a commit to peterhoeg/nixpkgs that referenced this pull request Jan 22, 2024
@Julien-R44
Copy link
Owner

Thank you! Sorry to have taken so long

@Julien-R44 Julien-R44 merged commit 0d81887 into Julien-R44:main Apr 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants