Skip to content

Commit dc41dba

Browse files
committed
Update unop path, fix tests
1 parent 4d0fe27 commit dc41dba

File tree

5 files changed

+51
-94
lines changed

5 files changed

+51
-94
lines changed

compiler/rustc_typeck/src/check/op.rs

+39-34
Original file line numberDiff line numberDiff line change
@@ -456,25 +456,32 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
456456
// the resulting predicate generates a more specific
457457
// suggestion for the user.
458458
let errors = self
459-
.lookup_op_method(lhs_ty, &[rhs_ty], Op::Binary(op, is_assign))
460-
.unwrap_err();
461-
let predicates = errors
462-
.into_iter()
463-
.filter_map(|error| error.obligation.predicate.to_opt_poly_trait_pred())
464-
.collect::<Vec<_>>();
465-
if !predicates.is_empty() {
466-
for pred in predicates {
467-
self.infcx.suggest_restricting_param_bound(&mut err,
468-
pred,
469-
self.body_id,
470-
);
459+
.lookup_op_method(
460+
lhs_ty,
461+
Some(rhs_ty),
462+
Some(rhs_expr),
463+
Op::Binary(op, is_assign),
464+
)
465+
.unwrap_err();
466+
let predicates = errors
467+
.into_iter()
468+
.filter_map(|error| error.obligation.predicate.to_opt_poly_trait_pred())
469+
.collect::<Vec<_>>();
470+
if !predicates.is_empty() {
471+
for pred in predicates {
472+
self.infcx.suggest_restricting_param_bound(
473+
&mut err,
474+
pred,
475+
self.body_id,
476+
);
477+
}
478+
} else if *ty != lhs_ty {
479+
// When we know that a missing bound is responsible, we don't show
480+
// this note as it is redundant.
481+
err.note(&format!(
482+
"the trait `{missing_trait}` is not implemented for `{lhs_ty}`"
483+
));
471484
}
472-
} else if *ty != lhs_ty {
473-
// When we know that a missing bound is responsible, we don't show
474-
// this note as it is redundant.
475-
err.note(&format!(
476-
"the trait `{missing_trait}` is not implemented for `{lhs_ty}`"
477-
));
478485
}
479486
}
480487
err.emit();
@@ -663,24 +670,22 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
663670
ex.span,
664671
format!("cannot apply unary operator `{}`", op.as_str()),
665672
);
666-
let missing_trait = match op {
667-
hir::UnOp::Deref => unreachable!("check unary op `-` or `!` only"),
668-
hir::UnOp::Not => "std::ops::Not",
669-
hir::UnOp::Neg => "std::ops::Neg",
670-
};
673+
671674
let mut visitor = TypeParamVisitor(vec![]);
672675
visitor.visit_ty(operand_ty);
673-
if let [ty] = &visitor.0[..] && let ty::Param(p) = *operand_ty.kind() {
674-
suggest_constraining_param(
675-
self.tcx,
676-
self.body_id,
677-
&mut err,
678-
*ty,
679-
operand_ty,
680-
missing_trait,
681-
p,
682-
true,
683-
);
676+
if let [_] = &visitor.0[..] && let ty::Param(_) = *operand_ty.kind() {
677+
let predicates = errors
678+
.iter()
679+
.filter_map(|error| {
680+
error.obligation.predicate.clone().to_opt_poly_trait_pred()
681+
});
682+
for pred in predicates {
683+
self.infcx.suggest_restricting_param_bound(
684+
&mut err,
685+
pred,
686+
self.body_id,
687+
);
688+
}
684689
}
685690

686691
let sp = self.tcx.sess.source_map().start_point(ex.span);

src/test/ui/generic-associated-types/missing-bounds.fixed

-46
This file was deleted.

src/test/ui/generic-associated-types/missing-bounds.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// run-rustfix
2-
31
use std::ops::Add;
42

53
struct A<B>(B);

src/test/ui/generic-associated-types/missing-bounds.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: equality constraints are not yet supported in `where` clauses
2-
--> $DIR/missing-bounds.rs:37:33
2+
--> $DIR/missing-bounds.rs:35:33
33
|
44
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
55
| ^^^^^^^^^^^^^^^^^^^^^^ not supported
@@ -11,7 +11,7 @@ LL | impl<B: Add> Add for E<B> where B: Add<Output = B> {
1111
| ~~~~~~~~~~~~~~~~~~
1212

1313
error[E0308]: mismatched types
14-
--> $DIR/missing-bounds.rs:11:11
14+
--> $DIR/missing-bounds.rs:9:11
1515
|
1616
LL | impl<B> Add for A<B> where B: Add {
1717
| - this type parameter
@@ -24,7 +24,7 @@ LL | A(self.0 + rhs.0)
2424
= note: expected type parameter `B`
2525
found associated type `<B as Add>::Output`
2626
note: tuple struct defined here
27-
--> $DIR/missing-bounds.rs:5:8
27+
--> $DIR/missing-bounds.rs:3:8
2828
|
2929
LL | struct A<B>(B);
3030
| ^
@@ -34,7 +34,7 @@ LL | impl<B> Add for A<B> where B: Add + Add<Output = B> {
3434
| +++++++++++++++++
3535

3636
error[E0308]: mismatched types
37-
--> $DIR/missing-bounds.rs:21:14
37+
--> $DIR/missing-bounds.rs:19:14
3838
|
3939
LL | impl<B: Add> Add for C<B> {
4040
| - this type parameter
@@ -47,7 +47,7 @@ LL | Self(self.0 + rhs.0)
4747
= note: expected type parameter `B`
4848
found associated type `<B as Add>::Output`
4949
note: tuple struct defined here
50-
--> $DIR/missing-bounds.rs:15:8
50+
--> $DIR/missing-bounds.rs:13:8
5151
|
5252
LL | struct C<B>(B);
5353
| ^
@@ -57,7 +57,7 @@ LL | impl<B: Add + Add<Output = B>> Add for C<B> {
5757
| +++++++++++++++++
5858

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

7272
error[E0308]: mismatched types
73-
--> $DIR/missing-bounds.rs:42:14
73+
--> $DIR/missing-bounds.rs:40:14
7474
|
7575
LL | impl<B: Add> Add for E<B> where <B as Add>::Output = B {
7676
| - this type parameter
@@ -83,7 +83,7 @@ LL | Self(self.0 + rhs.0)
8383
= note: expected type parameter `B`
8484
found associated type `<B as Add>::Output`
8585
note: tuple struct defined here
86-
--> $DIR/missing-bounds.rs:35:8
86+
--> $DIR/missing-bounds.rs:33:8
8787
|
8888
LL | struct E<B>(B);
8989
| ^

src/test/ui/type/type-check/missing_trait_impl.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ LL | let y = -x;
3232
|
3333
help: consider restricting type parameter `T`
3434
|
35-
LL | fn baz<T: std::ops::Neg<Output = T>>(x: T) {
36-
| +++++++++++++++++++++++++++
35+
LL | fn baz<T: std::ops::Neg>(x: T) {
36+
| +++++++++++++++
3737

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

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

0 commit comments

Comments
 (0)