Provide suggestions for type parameters missing bounds for associated types
When implementing the binary operator traits it is easy to forget to restrict the `Output` associated type. `rustc` now accounts for different cases to lead users in the right direction to add the necessary restrictions. The structured suggestions in the following output are new:
```
error: equality constraints are not yet supported in `where` clauses
--> $DIR/missing-bounds.rs:37:33
|
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
| ^^^^^^^^^^^^^^^^^^^^^^ not supported
|
= note: see issue #20041 <https://github.com/rust-lang/rust/issues/20041> for more information
help: if `Output` is an associated type you're trying to set, use the associated type binding syntax
|
LL | impl<B: Add> Add for E<B> where B: Add<Output = B> {
| ^^^^^^^^^^^^^^^^^
error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:11:11
|
7 | impl<B> Add for A<B> where B: Add {
| - this type parameter
...
11 | A(self.0 + rhs.0)
| ^^^^^^^^^^^^^^ expected type parameter `B`, found associated type
|
= note: expected type parameter `B`
found associated type `<B as std::ops::Add>::Output`
help: consider further restricting this bound
|
7 | impl<B> Add for A<B> where B: Add + std::ops::Add<Output = B> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0369]: cannot add `B` to `B`
--> $DIR/missing-bounds.rs:31:21
|
31 | Self(self.0 + rhs.0)
| ------ ^ ----- B
| |
| B
|
help: consider restricting type parameter `B`
|
27 | impl<B: std::ops::Add<Output = B>> Add for D<B> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
That output is given for the following cases:
```rust
struct A<B>(B);
impl<B> Add for A<B> where B: Add {
type Output = Self;
fn add(self, rhs: Self) -> Self {
A(self.0 + rhs.0) //~ ERROR mismatched types
}
}
struct D<B>(B);
impl<B> Add for D<B> {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0) //~ ERROR cannot add `B` to `B`
}
}
struct E<B>(B);
impl<B: Add> Add for E<B> where <B as Add>::Output = B {
type Output = Self;
fn add(self, rhs: Self) -> Self {
Self(self.0 + rhs.0)
}
}
```
|
||
|---|---|---|
| .. | ||
| bootstrap | ||
| build_helper | ||
| ci | ||
| doc | ||
| etc | ||
| liballoc | ||
| libarena | ||
| libcore | ||
| libfmt_macros | ||
| libgraphviz | ||
| libpanic_abort | ||
| libpanic_unwind | ||
| libproc_macro | ||
| libprofiler_builtins | ||
| librustc_apfloat | ||
| librustc_ast | ||
| librustc_ast_lowering | ||
| librustc_ast_passes | ||
| librustc_ast_pretty | ||
| librustc_attr | ||
| librustc_builtin_macros | ||
| librustc_codegen_llvm | ||
| librustc_codegen_ssa | ||
| librustc_data_structures | ||
| librustc_driver | ||
| librustc_error_codes | ||
| librustc_errors | ||
| librustc_expand | ||
| librustc_feature | ||
| librustc_fs_util | ||
| librustc_hir | ||
| librustc_hir_pretty | ||
| librustc_incremental | ||
| librustc_index | ||
| librustc_infer | ||
| librustc_interface | ||
| librustc_lexer | ||
| librustc_lint | ||
| librustc_llvm | ||
| librustc_macros | ||
| librustc_metadata | ||
| librustc_middle | ||
| librustc_mir | ||
| librustc_mir_build | ||
| librustc_parse | ||
| librustc_passes | ||
| librustc_plugin_impl | ||
| librustc_privacy | ||
| librustc_query_system | ||
| librustc_resolve | ||
| librustc_save_analysis | ||
| librustc_session | ||
| librustc_span | ||
| librustc_symbol_mangling | ||
| librustc_target | ||
| librustc_trait_selection | ||
| librustc_traits | ||
| librustc_ty | ||
| librustc_typeck | ||
| librustdoc | ||
| libserialize | ||
| libstd | ||
| libterm | ||
| libtest | ||
| libunwind | ||
| llvm-project@3ba91917e5 | ||
| rtstartup | ||
| rustc | ||
| rustllvm | ||
| stdarch@d10eefc622 | ||
| test | ||
| tools | ||
| README.md | ||
| stage0.txt | ||
This directory contains the source code of the rust project, including:
rustcand its testslibstd- Various submodules for tools, like rustdoc, rls, etc.
For more information on how various parts of the compiler work, see the rustc dev guide.