Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d860e5b

Browse files
committed
Mention type parameter in more cases and don't suggest ~const bound already there
1 parent 3f2a63a commit d860e5b

File tree

53 files changed

+106
-172
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+106
-172
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,6 +1450,7 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
14501450
ty::Param(param_ty) => Ok((
14511451
generics.type_param(param_ty, tcx),
14521452
predicate.trait_ref.print_trait_sugared().to_string(),
1453+
Some(predicate.trait_ref.def_id),
14531454
)),
14541455
_ => Err(()),
14551456
}
@@ -1463,9 +1464,9 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
14631464
tcx,
14641465
hir_generics,
14651466
err,
1466-
predicates
1467-
.iter()
1468-
.map(|(param, constraint)| (param.name.as_str(), &**constraint, None)),
1467+
predicates.iter().map(|(param, constraint, def_id)| {
1468+
(param.name.as_str(), &**constraint, *def_id)
1469+
}),
14691470
None,
14701471
);
14711472
}

compiler/rustc_const_eval/src/check_consts/ops.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl<'tcx> NonConstOp<'tcx> for FnCallNonConst<'tcx> {
140140
err,
141141
param_ty.name.as_str(),
142142
&constraint,
143-
None,
143+
Some(trait_ref.def_id),
144144
None,
145145
);
146146
}

compiler/rustc_middle/src/ty/diagnostics.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ pub fn suggest_arbitrary_trait_bound<'tcx>(
162162
true
163163
}
164164

165-
#[derive(Debug)]
165+
#[derive(Debug, Clone, Copy)]
166166
enum SuggestChangingConstraintsMessage<'a> {
167167
RestrictBoundFurther,
168168
RestrictType { ty: &'a str },
@@ -319,6 +319,11 @@ pub fn suggest_constraining_type_params<'a>(
319319
suggest_changing_unsized_bound(generics, &mut suggestions, param, def_id);
320320
}
321321
}
322+
let bound_message = if constraints.iter().any(|(_, def_id, _)| def_id.is_none()) {
323+
SuggestChangingConstraintsMessage::RestrictBoundFurther
324+
} else {
325+
SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name }
326+
};
322327

323328
// in the scenario like impl has stricter requirements than trait,
324329
// we should not suggest restrict bound on the impl, here we double check
@@ -389,23 +394,11 @@ pub fn suggest_constraining_type_params<'a>(
389394
format!(" {constraint}")
390395
};
391396

392-
use SuggestChangingConstraintsMessage::RestrictBoundFurther;
393-
394397
if let Some(open_paren_sp) = open_paren_sp {
395-
suggestions.push((
396-
open_paren_sp,
397-
post.clone(),
398-
"(".to_string(),
399-
RestrictBoundFurther,
400-
));
401-
suggestions.push((
402-
span,
403-
post.clone(),
404-
format!("){suggestion}"),
405-
RestrictBoundFurther,
406-
));
398+
suggestions.push((open_paren_sp, post.clone(), "(".to_string(), bound_message));
399+
suggestions.push((span, post.clone(), format!("){suggestion}"), bound_message));
407400
} else {
408-
suggestions.push((span, post.clone(), suggestion, RestrictBoundFurther));
401+
suggestions.push((span, post.clone(), suggestion, bound_message));
409402
}
410403
};
411404

tests/ui/associated-types/issue-59324.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | |
77
LL | | Service<AssocType = <Bug as Foo>::OnlyFoo>
88
| |______________________________________________^ the trait `Foo` is not implemented for `Bug`
99
|
10-
help: consider further restricting this bound with trait `Foo`
10+
help: consider further restricting type parameter `Bug` with trait `Foo`
1111
|
1212
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
1313
| +++++
@@ -24,7 +24,7 @@ LL | |
2424
LL | | }
2525
| |_^ the trait `Foo` is not implemented for `Bug`
2626
|
27-
help: consider further restricting this bound with trait `Foo`
27+
help: consider further restricting type parameter `Bug` with trait `Foo`
2828
|
2929
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
3030
| +++++
@@ -38,7 +38,7 @@ LL | | &self,
3838
LL | | ) -> Self::AssocType;
3939
| |_________________________^ the trait `Foo` is not implemented for `Bug`
4040
|
41-
help: consider further restricting this bound with trait `Foo`
41+
help: consider further restricting type parameter `Bug` with trait `Foo`
4242
|
4343
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
4444
| +++++
@@ -61,7 +61,7 @@ error[E0277]: the trait bound `Bug: Foo` is not satisfied
6161
LL | ) -> Self::AssocType;
6262
| ^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `Bug`
6363
|
64-
help: consider further restricting this bound with trait `Foo`
64+
help: consider further restricting type parameter `Bug` with trait `Foo`
6565
|
6666
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
6767
| +++++

tests/ui/async-await/issue-86507.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ note: captured value is not `Send` because `&` references cannot be sent unless
1414
LL | let x = x;
1515
| ^ has type `&T` which is not `Send`, because `T` is not `Sync`
1616
= note: required for the cast from `Pin<Box<{async block@$DIR/issue-86507.rs:18:17: 18:27}>>` to `Pin<Box<(dyn Future<Output = ()> + Send + 'async_trait)>>`
17-
help: consider further restricting this bound with trait `Sync`
17+
help: consider further restricting type parameter `T` with trait `Sync`
1818
|
1919
LL | fn bar<'me, 'async_trait, T: Send + std::marker::Sync>(x: &'me T)
2020
| +++++++++++++++++++

tests/ui/auto-traits/typeck-auto-trait-no-supertraits-2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ LL | fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
3030
| ^ - you could clone this value
3131
| |
3232
| consider constraining this type parameter with `Clone`
33-
help: consider further restricting this bound with trait `Copy`
33+
help: consider further restricting type parameter `T` with trait `Copy`
3434
|
3535
LL | fn copy<T: Magic + Copy>(x: T) -> (T, T) { (x, x) }
3636
| ++++++

tests/ui/binop/binop-consume-args.stderr

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ LL | lhs + rhs;
1717
| --- you could clone this value
1818
note: calling this operator moves the left-hand side
1919
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
20-
help: consider further restricting this bound with trait `Copy`
20+
help: consider further restricting type parameter `A` with trait `Copy`
2121
|
2222
LL | fn add<A: Add<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
2323
| ++++++
@@ -64,7 +64,7 @@ LL | lhs - rhs;
6464
| --- you could clone this value
6565
note: calling this operator moves the left-hand side
6666
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
67-
help: consider further restricting this bound with trait `Copy`
67+
help: consider further restricting type parameter `A` with trait `Copy`
6868
|
6969
LL | fn sub<A: Sub<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
7070
| ++++++
@@ -111,7 +111,7 @@ LL | lhs * rhs;
111111
| --- you could clone this value
112112
note: calling this operator moves the left-hand side
113113
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
114-
help: consider further restricting this bound with trait `Copy`
114+
help: consider further restricting type parameter `A` with trait `Copy`
115115
|
116116
LL | fn mul<A: Mul<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
117117
| ++++++
@@ -158,7 +158,7 @@ LL | lhs / rhs;
158158
| --- you could clone this value
159159
note: calling this operator moves the left-hand side
160160
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
161-
help: consider further restricting this bound with trait `Copy`
161+
help: consider further restricting type parameter `A` with trait `Copy`
162162
|
163163
LL | fn div<A: Div<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
164164
| ++++++
@@ -205,7 +205,7 @@ LL | lhs % rhs;
205205
| --- you could clone this value
206206
note: calling this operator moves the left-hand side
207207
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
208-
help: consider further restricting this bound with trait `Copy`
208+
help: consider further restricting type parameter `A` with trait `Copy`
209209
|
210210
LL | fn rem<A: Rem<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
211211
| ++++++
@@ -252,7 +252,7 @@ LL | lhs & rhs;
252252
| --- you could clone this value
253253
note: calling this operator moves the left-hand side
254254
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
255-
help: consider further restricting this bound with trait `Copy`
255+
help: consider further restricting type parameter `A` with trait `Copy`
256256
|
257257
LL | fn bitand<A: BitAnd<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
258258
| ++++++
@@ -299,7 +299,7 @@ LL | lhs | rhs;
299299
| --- you could clone this value
300300
note: calling this operator moves the left-hand side
301301
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
302-
help: consider further restricting this bound with trait `Copy`
302+
help: consider further restricting type parameter `A` with trait `Copy`
303303
|
304304
LL | fn bitor<A: BitOr<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
305305
| ++++++
@@ -346,7 +346,7 @@ LL | lhs ^ rhs;
346346
| --- you could clone this value
347347
note: calling this operator moves the left-hand side
348348
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
349-
help: consider further restricting this bound with trait `Copy`
349+
help: consider further restricting type parameter `A` with trait `Copy`
350350
|
351351
LL | fn bitxor<A: BitXor<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
352352
| ++++++
@@ -393,7 +393,7 @@ LL | lhs << rhs;
393393
| --- you could clone this value
394394
note: calling this operator moves the left-hand side
395395
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
396-
help: consider further restricting this bound with trait `Copy`
396+
help: consider further restricting type parameter `A` with trait `Copy`
397397
|
398398
LL | fn shl<A: Shl<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
399399
| ++++++
@@ -440,7 +440,7 @@ LL | lhs >> rhs;
440440
| --- you could clone this value
441441
note: calling this operator moves the left-hand side
442442
--> $SRC_DIR/core/src/ops/bit.rs:LL:COL
443-
help: consider further restricting this bound with trait `Copy`
443+
help: consider further restricting type parameter `A` with trait `Copy`
444444
|
445445
LL | fn shr<A: Shr<B, Output=()> + Copy, B>(lhs: A, rhs: B) {
446446
| ++++++

tests/ui/binop/binop-move-semantics.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ LL | x
2020
| - you could clone this value
2121
note: calling this operator moves the left-hand side
2222
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL
23-
help: consider further restricting this bound with trait `Copy`
23+
help: consider further restricting type parameter `T` with trait `Copy`
2424
|
2525
LL | fn double_move<T: Add<Output=()> + Copy>(x: T) {
2626
| ++++++
@@ -40,7 +40,7 @@ help: consider cloning the value if the performance cost is acceptable
4040
|
4141
LL | x.clone()
4242
| ++++++++
43-
help: consider further restricting this bound with trait `Copy`
43+
help: consider further restricting type parameter `T` with trait `Copy`
4444
|
4545
LL | fn move_then_borrow<T: Add<Output=()> + Clone + Copy>(x: T) {
4646
| ++++++

tests/ui/binop/issue-93927.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | val == val
66
| |
77
| MyType<T>
88
|
9-
help: consider further restricting this bound with trait `Eq`
9+
help: consider further restricting type parameter `T` with trait `Eq`
1010
|
1111
LL | fn cond<T: PartialEq + std::cmp::Eq>(val: MyType<T>) -> bool {
1212
| ++++++++++++++

tests/ui/borrowck/clone-on-ref.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LL |
1212
LL | drop(cloned_items);
1313
| ------------ immutable borrow later used here
1414
|
15-
help: consider further restricting this bound with trait `Clone`
15+
help: consider further restricting type parameter `T` with trait `Clone`
1616
|
1717
LL | fn foo<T: Default + Clone>(list: &mut Vec<T>) {
1818
| +++++++
@@ -39,7 +39,7 @@ LL | fn bar<T: std::fmt::Display>(x: T) {
3939
| ^ consider constraining this type parameter with `Clone`
4040
LL | let a = &x;
4141
| - you could clone this value
42-
help: consider further restricting this bound with trait `Clone`
42+
help: consider further restricting type parameter `T` with trait `Clone`
4343
|
4444
LL | fn bar<T: std::fmt::Display + Clone>(x: T) {
4545
| +++++++

0 commit comments

Comments
 (0)