forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#132583 - mejrs:tuples, r=compiler-errors
Suggest creating unary tuples when types don't match a trait When you want to have a variadic function, a common workaround to implement this is to create a trait and then implement that trait for various tuples. For example in `pyo3` there exists ```rust /// Calls the object with only positional arguments. pub fn call1(&self, args: impl IntoPy<Py<PyTuple>>) -> PyResult<&PyAny> { ... } ``` with various impls like ```rust impl<A: IntoPy<PyObject> IntoPy<Py<PyAny>> for (A,) impl<A: IntoPy<PyObject, B: IntoPy<PyObject> IntoPy<Py<PyAny>> for (A, B) ... etc ``` This means that if you want to call the method with a single item you have to create a unary tuple, like `(x,)`, rather than just `x`. This PR implements a suggestion to do that, if applicable.
- Loading branch information
Showing
11 changed files
with
155 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
pub trait Argument {} | ||
impl Argument for u8 {} | ||
impl Argument for i8 {} | ||
impl Argument for String {} | ||
impl Argument for &str {} | ||
|
||
pub trait TupleArgs {} | ||
impl<A: Argument> TupleArgs for (A,) {} | ||
impl<A: Argument, B: Argument> TupleArgs for (A, B) {} | ||
impl<A: Argument, B: Argument, C: Argument> TupleArgs for (A, B, C) {} | ||
|
||
fn convert_into_tuple(_x: impl TupleArgs) {} | ||
|
||
fn main() { | ||
convert_into_tuple(42_u8); | ||
//~^ ERROR E0277 | ||
//~| HELP the following other types implement trait `TupleArgs` | ||
//~| HELP use a unary tuple instead | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
error[E0277]: the trait bound `u8: TupleArgs` is not satisfied | ||
--> $DIR/suggest_tuple_wrap.rs:15:24 | ||
| | ||
LL | convert_into_tuple(42_u8); | ||
| ------------------ ^^^^^ the trait `TupleArgs` is not implemented for `u8` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following other types implement trait `TupleArgs`: | ||
(A, B) | ||
(A, B, C) | ||
(A,) | ||
note: required by a bound in `convert_into_tuple` | ||
--> $DIR/suggest_tuple_wrap.rs:12:32 | ||
| | ||
LL | fn convert_into_tuple(_x: impl TupleArgs) {} | ||
| ^^^^^^^^^ required by this bound in `convert_into_tuple` | ||
help: use a unary tuple instead | ||
| | ||
LL | convert_into_tuple((42_u8,)); | ||
| + ++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
26 changes: 26 additions & 0 deletions
26
tests/ui/on-unimplemented/suggest_tuple_wrap_root_obligation.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
struct Tuple; | ||
|
||
impl From<(u8,)> for Tuple { | ||
fn from(_: (u8,)) -> Self { | ||
todo!() | ||
} | ||
} | ||
impl From<(u8, u8)> for Tuple { | ||
fn from(_: (u8, u8)) -> Self { | ||
todo!() | ||
} | ||
} | ||
impl From<(u8, u8, u8)> for Tuple { | ||
fn from(_: (u8, u8, u8)) -> Self { | ||
todo!() | ||
} | ||
} | ||
|
||
fn convert_into_tuple(_x: impl Into<Tuple>) {} | ||
|
||
fn main() { | ||
convert_into_tuple(42_u8); | ||
//~^ ERROR E0277 | ||
//~| HELP use a unary tuple instead | ||
//~| HELP the following other types implement trait `From<T>` | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/ui/on-unimplemented/suggest_tuple_wrap_root_obligation.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
error[E0277]: the trait bound `Tuple: From<u8>` is not satisfied | ||
--> $DIR/suggest_tuple_wrap_root_obligation.rs:22:24 | ||
| | ||
LL | convert_into_tuple(42_u8); | ||
| ------------------ ^^^^^ the trait `From<u8>` is not implemented for `Tuple` | ||
| | | ||
| required by a bound introduced by this call | ||
| | ||
= help: the following other types implement trait `From<T>`: | ||
`Tuple` implements `From<(u8, u8)>` | ||
`Tuple` implements `From<(u8, u8, u8)>` | ||
`Tuple` implements `From<(u8,)>` | ||
= note: required for `u8` to implement `Into<Tuple>` | ||
note: required by a bound in `convert_into_tuple` | ||
--> $DIR/suggest_tuple_wrap_root_obligation.rs:19:32 | ||
| | ||
LL | fn convert_into_tuple(_x: impl Into<Tuple>) {} | ||
| ^^^^^^^^^^^ required by this bound in `convert_into_tuple` | ||
help: use a unary tuple instead | ||
| | ||
LL | convert_into_tuple((42_u8,)); | ||
| + ++ | ||
|
||
error: aborting due to 1 previous error | ||
|
||
For more information about this error, try `rustc --explain E0277`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters