Skip to content

Commit

Permalink
Update unop path, fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
willcrichton committed Apr 26, 2022
1 parent 4d0fe27 commit dc41dba
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 94 deletions.
73 changes: 39 additions & 34 deletions compiler/rustc_typeck/src/check/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,25 +456,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// the resulting predicate generates a more specific
// suggestion for the user.
let errors = self
.lookup_op_method(lhs_ty, &[rhs_ty], Op::Binary(op, is_assign))
.unwrap_err();
let predicates = errors
.into_iter()
.filter_map(|error| error.obligation.predicate.to_opt_poly_trait_pred())
.collect::<Vec<_>>();
if !predicates.is_empty() {
for pred in predicates {
self.infcx.suggest_restricting_param_bound(&mut err,
pred,
self.body_id,
);
.lookup_op_method(
lhs_ty,
Some(rhs_ty),
Some(rhs_expr),
Op::Binary(op, is_assign),
)
.unwrap_err();
let predicates = errors
.into_iter()
.filter_map(|error| error.obligation.predicate.to_opt_poly_trait_pred())
.collect::<Vec<_>>();
if !predicates.is_empty() {
for pred in predicates {
self.infcx.suggest_restricting_param_bound(
&mut err,
pred,
self.body_id,
);
}
} else if *ty != lhs_ty {
// When we know that a missing bound is responsible, we don't show
// this note as it is redundant.
err.note(&format!(
"the trait `{missing_trait}` is not implemented for `{lhs_ty}`"
));
}
} else if *ty != lhs_ty {
// When we know that a missing bound is responsible, we don't show
// this note as it is redundant.
err.note(&format!(
"the trait `{missing_trait}` is not implemented for `{lhs_ty}`"
));
}
}
err.emit();
Expand Down Expand Up @@ -663,24 +670,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
ex.span,
format!("cannot apply unary operator `{}`", op.as_str()),
);
let missing_trait = match op {
hir::UnOp::Deref => unreachable!("check unary op `-` or `!` only"),
hir::UnOp::Not => "std::ops::Not",
hir::UnOp::Neg => "std::ops::Neg",
};

let mut visitor = TypeParamVisitor(vec![]);
visitor.visit_ty(operand_ty);
if let [ty] = &visitor.0[..] && let ty::Param(p) = *operand_ty.kind() {
suggest_constraining_param(
self.tcx,
self.body_id,
&mut err,
*ty,
operand_ty,
missing_trait,
p,
true,
);
if let [_] = &visitor.0[..] && let ty::Param(_) = *operand_ty.kind() {
let predicates = errors
.iter()
.filter_map(|error| {
error.obligation.predicate.clone().to_opt_poly_trait_pred()
});
for pred in predicates {
self.infcx.suggest_restricting_param_bound(
&mut err,
pred,
self.body_id,
);
}
}

let sp = self.tcx.sess.source_map().start_point(ex.span);
Expand Down
46 changes: 0 additions & 46 deletions src/test/ui/generic-associated-types/missing-bounds.fixed

This file was deleted.

2 changes: 0 additions & 2 deletions src/test/ui/generic-associated-types/missing-bounds.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// run-rustfix

use std::ops::Add;

struct A<B>(B);
Expand Down
16 changes: 8 additions & 8 deletions src/test/ui/generic-associated-types/missing-bounds.stderr
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
error: equality constraints are not yet supported in `where` clauses
--> $DIR/missing-bounds.rs:37:33
--> $DIR/missing-bounds.rs:35:33
|
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
| ^^^^^^^^^^^^^^^^^^^^^^ not supported
Expand All @@ -11,7 +11,7 @@ LL | impl<B: Add> Add for E<B> where B: Add<Output = B> {
| ~~~~~~~~~~~~~~~~~~

error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:11:11
--> $DIR/missing-bounds.rs:9:11
|
LL | impl<B> Add for A<B> where B: Add {
| - this type parameter
Expand All @@ -24,7 +24,7 @@ LL | A(self.0 + rhs.0)
= note: expected type parameter `B`
found associated type `<B as Add>::Output`
note: tuple struct defined here
--> $DIR/missing-bounds.rs:5:8
--> $DIR/missing-bounds.rs:3:8
|
LL | struct A<B>(B);
| ^
Expand All @@ -34,7 +34,7 @@ LL | impl<B> Add for A<B> where B: Add + Add<Output = B> {
| +++++++++++++++++

error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:21:14
--> $DIR/missing-bounds.rs:19:14
|
LL | impl<B: Add> Add for C<B> {
| - this type parameter
Expand All @@ -47,7 +47,7 @@ LL | Self(self.0 + rhs.0)
= note: expected type parameter `B`
found associated type `<B as Add>::Output`
note: tuple struct defined here
--> $DIR/missing-bounds.rs:15:8
--> $DIR/missing-bounds.rs:13:8
|
LL | struct C<B>(B);
| ^
Expand All @@ -57,7 +57,7 @@ LL | impl<B: Add + Add<Output = B>> Add for C<B> {
| +++++++++++++++++

error[E0369]: cannot add `B` to `B`
--> $DIR/missing-bounds.rs:31:21
--> $DIR/missing-bounds.rs:29:21
|
LL | Self(self.0 + rhs.0)
| ------ ^ ----- B
Expand All @@ -70,7 +70,7 @@ LL | impl<B: std::ops::Add> Add for D<B> {
| +++++++++++++++

error[E0308]: mismatched types
--> $DIR/missing-bounds.rs:42:14
--> $DIR/missing-bounds.rs:40:14
|
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
| - this type parameter
Expand All @@ -83,7 +83,7 @@ LL | Self(self.0 + rhs.0)
= note: expected type parameter `B`
found associated type `<B as Add>::Output`
note: tuple struct defined here
--> $DIR/missing-bounds.rs:35:8
--> $DIR/missing-bounds.rs:33:8
|
LL | struct E<B>(B);
| ^
Expand Down
8 changes: 4 additions & 4 deletions src/test/ui/type/type-check/missing_trait_impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ LL | let y = -x;
|
help: consider restricting type parameter `T`
|
LL | fn baz<T: std::ops::Neg<Output = T>>(x: T) {
| +++++++++++++++++++++++++++
LL | fn baz<T: std::ops::Neg>(x: T) {
| +++++++++++++++

error[E0600]: cannot apply unary operator `!` to type `T`
--> $DIR/missing_trait_impl.rs:14:13
Expand All @@ -43,8 +43,8 @@ LL | let y = !x;
|
help: consider restricting type parameter `T`
|
LL | fn baz<T: std::ops::Not<Output = T>>(x: T) {
| +++++++++++++++++++++++++++
LL | fn baz<T: std::ops::Not>(x: T) {
| +++++++++++++++

error[E0614]: type `T` cannot be dereferenced
--> $DIR/missing_trait_impl.rs:15:13
Expand Down

0 comments on commit dc41dba

Please sign in to comment.