Skip to content

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
| +++++++

tests/ui/builtin-superkinds/builtin-superkinds-double-superkind.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ note: required by a bound in `Foo`
1010
|
1111
LL | trait Foo : Send+Sync { }
1212
| ^^^^ required by this bound in `Foo`
13-
help: consider further restricting this bound with trait `Send`
13+
help: consider further restricting type parameter `T` with trait `Send`
1414
|
1515
LL | impl <T: Sync+'static + std::marker::Send> Foo for (T,) { }
1616
| +++++++++++++++++++
@@ -27,7 +27,7 @@ note: required by a bound in `Foo`
2727
|
2828
LL | trait Foo : Send+Sync { }
2929
| ^^^^ required by this bound in `Foo`
30-
help: consider further restricting this bound with trait `Sync`
30+
help: consider further restricting type parameter `T` with trait `Sync`
3131
|
3232
LL | impl <T: Send + std::marker::Sync> Foo for (T,T) { }
3333
| +++++++++++++++++++

tests/ui/builtin-superkinds/builtin-superkinds-in-metadata.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ note: required by a bound in `RequiresRequiresShareAndSend`
1414
|
1515
LL | pub trait RequiresRequiresShareAndSend : RequiresShare + Send { }
1616
| ^^^^ required by this bound in `RequiresRequiresShareAndSend`
17-
help: consider further restricting this bound with trait `Send`
17+
help: consider further restricting type parameter `T` with trait `Send`
1818
|
1919
LL | impl <T:Sync+'static + std::marker::Send> RequiresRequiresShareAndSend for X<T> { }
2020
| +++++++++++++++++++

tests/ui/builtin-superkinds/builtin-superkinds-typaram-not-send.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: required by a bound in `Foo`
99
|
1010
LL | trait Foo : Send { }
1111
| ^^^^ required by this bound in `Foo`
12-
help: consider further restricting this bound with trait `Send`
12+
help: consider further restricting type parameter `T` with trait `Send`
1313
|
1414
LL | impl <T: Sync+'static + std::marker::Send> Foo for T { }
1515
| +++++++++++++++++++

tests/ui/closures/closure-bounds-cant-promote-superkind-in-struct.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ note: required by a bound in `X`
99
|
1010
LL | struct X<F> where F: FnOnce() + 'static + Send {
1111
| ^^^^ required by this bound in `X`
12-
help: consider further restricting this bound with trait `Send`
12+
help: consider further restricting type parameter `F` with trait `Send`
1313
|
1414
LL | fn foo<F>(blk: F) -> X<F> where F: FnOnce() + 'static + std::marker::Send {
1515
| +++++++++++++++++++
@@ -25,7 +25,7 @@ note: required by a bound in `X`
2525
|
2626
LL | struct X<F> where F: FnOnce() + 'static + Send {
2727
| ^^^^ required by this bound in `X`
28-
help: consider further restricting this bound with trait `Send`
28+
help: consider further restricting type parameter `F` with trait `Send`
2929
|
3030
LL | fn foo<F>(blk: F) -> X<F> where F: FnOnce() + 'static + std::marker::Send {
3131
| +++++++++++++++++++

tests/ui/closures/closure-bounds-subtype.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ help: use parentheses to call this type parameter
1515
|
1616
LL | take_const_owned(f());
1717
| ++
18-
help: consider further restricting this bound with trait `Sync`
18+
help: consider further restricting type parameter `F` with trait `Sync`
1919
|
2020
LL | fn give_owned<F>(f: F) where F: FnOnce() + Send + std::marker::Sync {
2121
| +++++++++++++++++++

tests/ui/consts/fn_trait_refs.stderr

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,6 @@ LL | f()
212212
| ^^^
213213
|
214214
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
215-
help: consider further restricting this bound with trait `~const Fn()`
216-
|
217-
LL | T: ~const Fn<()> + ~const Destruct + ~const Fn(),
218-
| +++++++++++++
219215

220216
error[E0015]: cannot call non-const closure in constant functions
221217
--> $DIR/fn_trait_refs.rs:23:5
@@ -224,10 +220,6 @@ LL | f()
224220
| ^^^
225221
|
226222
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
227-
help: consider further restricting this bound with trait `~const FnMut()`
228-
|
229-
LL | T: ~const FnMut<()> + ~const Destruct + ~const FnMut(),
230-
| ++++++++++++++++
231223

232224
error[E0015]: cannot call non-const closure in constant functions
233225
--> $DIR/fn_trait_refs.rs:30:5
@@ -236,10 +228,6 @@ LL | f()
236228
| ^^^
237229
|
238230
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
239-
help: consider further restricting this bound with trait `~const FnOnce()`
240-
|
241-
LL | T: ~const FnOnce<()> + ~const FnOnce(),
242-
| +++++++++++++++++
243231

244232
error: aborting due to 25 previous errors
245233

tests/ui/consts/unstable-const-fn-in-libcore.stderr

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ LL | Opt::None => f(),
1919
| ^^^
2020
|
2121
= note: calls in constant functions are limited to constant functions, tuple structs and tuple variants
22-
help: consider further restricting this bound with trait `~const FnOnce()`
23-
|
24-
LL | const fn unwrap_or_else<F: ~const FnOnce() -> T + ~const FnOnce()>(self, f: F) -> T {
25-
| +++++++++++++++++
2622

2723
error[E0493]: destructor of `F` cannot be evaluated at compile-time
2824
--> $DIR/unstable-const-fn-in-libcore.rs:19:60

tests/ui/higher-ranked/trait-bounds/hrtb-higher-ranker-supertraits-transitive.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | fn want_bar_for_any_ccx<B>(b: &B)
1313
| -------------------- required by a bound in this function
1414
LL | where B : for<'ccx> Bar<'ccx>
1515
| ^^^^^^^^^^^^^^^^^^^ required by this bound in `want_bar_for_any_ccx`
16-
help: consider further restricting this bound with trait `Bar`
16+
help: consider further restricting type parameter `B` with trait `Bar`
1717
|
1818
LL | where B : Qux + for<'ccx> Bar<'ccx>
1919
| +++++++++++++++++++++

0 commit comments

Comments
 (0)