Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
431c500
Print env var in --print=deployment-target
madsmtm Nov 14, 2024
72d2db7
Implement lint against `Symbol::intern` on a string literal
clubby789 Nov 27, 2024
71b698c
Replace `Symbol::intern` calls with preinterned symbols
clubby789 Nov 27, 2024
0554993
Don't suggest restricting bound with unstable traits on stable
estebank Nov 27, 2024
e3dfae8
reword trait bound suggestion message to include the bounds
estebank Nov 28, 2024
7e38a45
Add test for lack of suggestion in stable
estebank Nov 28, 2024
c34079a
Use trait name instead of full constraint in suggestion message
estebank Nov 28, 2024
5c9e5d1
Mention type parameter in more cases and don't suggest ~const bound a…
estebank Nov 28, 2024
442ec3d
Account for `impl Trait` in "add bound" suggestion message
estebank Nov 28, 2024
055dbc5
fix rustdoc test
estebank Nov 28, 2024
6b758e2
Do not talk about "trait `<Foo = Bar>`"
estebank Nov 28, 2024
05e6714
Use run-make `diff` output for stable output test
estebank Nov 29, 2024
2d61c09
reduce false positives on some common cases from if-let-rescope
dingxiangfei2009 Dec 2, 2024
69a9312
Stabilize noop_waker
eholk Nov 16, 2024
2b88e4c
stabilize const_{size,align}_of_val
RalfJung Dec 2, 2024
2f45ea5
rustc_const_stable not needed for consts
eholk Dec 2, 2024
0609b99
Structurally resolve in probe_adt
compiler-errors Nov 28, 2024
ebb9a38
document -Zrandomize-layout in the unstable book
the8472 Dec 2, 2024
2807ba7
Use correct `hir_id` for array const arg infers
BoxyUwU Dec 3, 2024
3b2ff90
Add ui test for const evaluation fail when type is too big.
LFS6502 Dec 3, 2024
9c5b8ea
Rollup merge of #133041 - madsmtm:print-deployment-target-env-var, r=…
jhpratt Dec 3, 2024
36b14a2
Rollup merge of #133089 - eholk:stabilize-noop-waker, r=dtolnay
jhpratt Dec 3, 2024
23703d6
Rollup merge of #133522 - estebank:dont-suggest-unstable-trait, r=com…
jhpratt Dec 3, 2024
90b30d4
Rollup merge of #133545 - clubby789:symbol-intern-lit, r=jieyouxu
jhpratt Dec 3, 2024
dd03d84
Rollup merge of #133558 - compiler-errors:structurally-resolve-probe-…
jhpratt Dec 3, 2024
712e0e3
Rollup merge of #133753 - dingxiangfei2009:reduce-false-positive-if-l…
jhpratt Dec 3, 2024
df66045
Rollup merge of #133762 - RalfJung:const-size-of-val, r=workingjubilee
jhpratt Dec 3, 2024
c541427
Rollup merge of #133777 - the8472:document-randomize-layout, r=jieyouxu
jhpratt Dec 3, 2024
3976c91
Rollup merge of #133779 - BoxyUwU:array_const_arg_infer_hir_id, r=com…
jhpratt Dec 3, 2024
02ae277
Rollup merge of #133785 - HypheX:add-ui-test, r=compiler-errors
jhpratt Dec 3, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
reword trait bound suggestion message to include the bounds
  • Loading branch information
estebank committed Nov 28, 2024
commit e3dfae8844804ae19823479e4c807602b68912a0
45 changes: 31 additions & 14 deletions compiler/rustc_middle/src/ty/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ enum SuggestChangingConstraintsMessage<'a> {

fn suggest_changing_unsized_bound(
generics: &hir::Generics<'_>,
suggestions: &mut Vec<(Span, String, SuggestChangingConstraintsMessage<'_>)>,
suggestions: &mut Vec<(Span, String, String, SuggestChangingConstraintsMessage<'_>)>,
param: &hir::GenericParam<'_>,
def_id: Option<DefId>,
) {
Expand Down Expand Up @@ -206,7 +206,8 @@ fn suggest_changing_unsized_bound(
continue;
}

let mut push_suggestion = |sp, msg| suggestions.push((sp, String::new(), msg));
let mut push_suggestion =
|sp, msg| suggestions.push((sp, "Sized".to_string(), String::new(), msg));

if predicate.bounds.len() == unsized_bounds.len() {
// All the bounds are unsized bounds, e.g.
Expand Down Expand Up @@ -348,10 +349,20 @@ pub fn suggest_constraining_type_params<'a>(
use SuggestChangingConstraintsMessage::RestrictBoundFurther;

if let Some(open_paren_sp) = open_paren_sp {
suggestions.push((open_paren_sp, "(".to_string(), RestrictBoundFurther));
suggestions.push((span, format!("){suggestion}"), RestrictBoundFurther));
suggestions.push((
open_paren_sp,
constraint.clone(),
"(".to_string(),
RestrictBoundFurther,
));
suggestions.push((
span,
constraint.clone(),
format!("){suggestion}"),
RestrictBoundFurther,
));
} else {
suggestions.push((span, suggestion, RestrictBoundFurther));
suggestions.push((span, constraint.clone(), suggestion, RestrictBoundFurther));
}
};

Expand Down Expand Up @@ -409,6 +420,7 @@ pub fn suggest_constraining_type_params<'a>(
// - insert: `, X: Bar`
suggestions.push((
generics.tail_span_for_predicate_suggestion(),
constraint.clone(),
constraints.iter().fold(String::new(), |mut string, &(constraint, _)| {
write!(string, ", {param_name}: {constraint}").unwrap();
string
Expand Down Expand Up @@ -438,6 +450,7 @@ pub fn suggest_constraining_type_params<'a>(
// default (`<T=Foo>`), so we suggest adding `where T: Bar`.
suggestions.push((
generics.tail_span_for_predicate_suggestion(),
constraint.clone(),
format!("{where_prefix} {param_name}: {constraint}"),
SuggestChangingConstraintsMessage::RestrictTypeFurther { ty: param_name },
));
Expand All @@ -451,6 +464,7 @@ pub fn suggest_constraining_type_params<'a>(
if let Some(colon_span) = param.colon_span {
suggestions.push((
colon_span.shrink_to_hi(),
constraint.clone(),
format!(" {constraint}"),
SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
));
Expand All @@ -463,6 +477,7 @@ pub fn suggest_constraining_type_params<'a>(
// - help: consider restricting this type parameter with `T: Foo`
suggestions.push((
param.span.shrink_to_hi(),
constraint.clone(),
format!(": {constraint}"),
SuggestChangingConstraintsMessage::RestrictType { ty: param_name },
));
Expand All @@ -471,12 +486,16 @@ pub fn suggest_constraining_type_params<'a>(
// FIXME: remove the suggestions that are from derive, as the span is not correct
suggestions = suggestions
.into_iter()
.filter(|(span, _, _)| !span.in_derive_expansion())
.filter(|(span, _, _, _)| !span.in_derive_expansion())
.collect::<Vec<_>>();

if suggestions.len() == 1 {
let (span, suggestion, msg) = suggestions.pop().unwrap();
let post = if unstable_suggestion { " but it is an `unstable` trait" } else { "" };
let (span, constraint, suggestion, msg) = suggestions.pop().unwrap();
let post = format!(
" with {}trait{} `{constraint}`",
if unstable_suggestion { "unstable " } else { "" },
if constraint.contains('+') { "s" } else { "" },
);
let msg = match msg {
SuggestChangingConstraintsMessage::RestrictBoundFurther => {
format!("consider further restricting this bound{post}")
Expand All @@ -488,21 +507,19 @@ pub fn suggest_constraining_type_params<'a>(
format!("consider further restricting type parameter `{ty}`{post}")
}
SuggestChangingConstraintsMessage::RemoveMaybeUnsized => {
format!(
"consider removing the `?Sized` bound to make the type parameter `Sized`{post}"
)
format!("consider removing the `?Sized` bound to make the type parameter `Sized`")
}
SuggestChangingConstraintsMessage::ReplaceMaybeUnsizedWithSized => {
format!("consider replacing `?Sized` with `Sized`{post}")
format!("consider replacing `?Sized` with `Sized`")
}
};

err.span_suggestion_verbose(span, msg, suggestion, applicability);
} else if suggestions.len() > 1 {
let post = if unstable_suggestion { " but some of them are `unstable` traits" } else { "" };
let post = if unstable_suggestion { " (some of them are unstable traits)" } else { "" };
err.multipart_suggestion_verbose(
format!("consider restricting type parameters{post}"),
suggestions.into_iter().map(|(span, suggestion, _)| (span, suggestion)).collect(),
suggestions.into_iter().map(|(span, _, suggestion, _)| (span, suggestion)).collect(),
applicability,
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/rustdoc-ui/issues/issue-96287.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0220]: associated type `Assoc` not found for `V`
LL | pub type Foo<V> = impl Trait<V::Assoc>;
| ^^^^^ there is an associated type `Assoc` in the trait `TraitWithAssoc`
|
help: consider restricting type parameter `V`
help: consider restricting type parameter `V` with trait `TraitWithAssoc`
|
LL | pub type Foo<V: TraitWithAssoc> = impl Trait<V::Assoc>;
| ++++++++++++++++
Expand All @@ -16,7 +16,7 @@ LL | pub type Foo<V> = impl Trait<V::Assoc>;
| ^^^^^ there is an associated type `Assoc` in the trait `TraitWithAssoc`
|
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
help: consider restricting type parameter `V`
help: consider restricting type parameter `V` with trait `TraitWithAssoc`
|
LL | pub type Foo<V: TraitWithAssoc> = impl Trait<V::Assoc>;
| ++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: the trait bound `C: Bar<5>` is not satisfied
LL | pub struct Structure<C: Tec> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Bar<5>` is not implemented for `C`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Bar<5>`
|
LL | pub struct Structure<C: Tec + Bar<5>> {
| ++++++++
Expand All @@ -15,7 +15,7 @@ error[E0277]: the trait bound `C: Bar<5>` is not satisfied
LL | _field: C::BarType,
| ^^^^^^^^^^ the trait `Bar<5>` is not implemented for `C`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Bar<5>`
|
LL | pub struct Structure<C: Tec + Bar<5>> {
| ++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: the trait bound `T: Get` is not satisfied
LL | fn uhoh<T>(foo: <T as Get>::Value) {}
| ^^^^^^^^^^^^^^^^^ the trait `Get` is not implemented for `T`
|
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `Get`
|
LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}
| +++++
Expand All @@ -15,7 +15,7 @@ error[E0277]: the trait bound `T: Get` is not satisfied
LL | fn uhoh<T>(foo: <T as Get>::Value) {}
| ^^ the trait `Get` is not implemented for `T`
|
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `Get`
|
LL | fn uhoh<T: Get>(foo: <T as Get>::Value) {}
| +++++
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/defaults-suitability.current.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ note: required by a bound in `Foo::Bar`
|
LL | type Bar: Clone = Vec<T>;
| ^^^^^ required by this bound in `Foo::Bar`
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `std::clone::Clone`
|
LL | trait Foo<T: std::clone::Clone> {
| +++++++++++++++++++
Expand Down Expand Up @@ -132,7 +132,7 @@ LL | Self::Baz: Clone,
...
LL | type Baz = T;
| --- required by a bound in this associated type
help: consider further restricting type parameter `T`
help: consider further restricting type parameter `T` with trait `std::clone::Clone`
|
LL | Self::Baz: Clone, T: std::clone::Clone
| ~~~~~~~~~~~~~~~~~~~~~~
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/associated-types/defaults-suitability.next.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ note: required by a bound in `Foo::Bar`
|
LL | type Bar: Clone = Vec<T>;
| ^^^^^ required by this bound in `Foo::Bar`
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `std::clone::Clone`
|
LL | trait Foo<T: std::clone::Clone> {
| +++++++++++++++++++
Expand Down Expand Up @@ -132,7 +132,7 @@ LL | Self::Baz: Clone,
...
LL | type Baz = T;
| --- required by a bound in this associated type
help: consider further restricting type parameter `T`
help: consider further restricting type parameter `T` with trait `std::clone::Clone`
|
LL | Self::Baz: Clone, T: std::clone::Clone
| ~~~~~~~~~~~~~~~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ error[E0277]: the trait bound `for<'b> T: X<'b, T>` is not satisfied
LL | impl<S, T> X<'_, T> for (S,) {
| ^^^^^^^^ the trait `for<'b> X<'b, T>` is not implemented for `T`
|
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `for<'b> X<'b, T>`
|
LL | impl<S, T: for<'b> X<'b, T>> X<'_, T> for (S,) {
| ++++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ LL | trait UnsafeCopy<'a, T: Copy>
LL | where
LL | for<'b> <Self as UnsafeCopy<'b, T>>::Item: std::ops::Deref<Target = T>,
| ^^^^^^^^^^ required by this bound in `UnsafeCopy`
help: consider further restricting this bound
help: consider further restricting this bound with trait `<Target = T>`
|
LL | impl<T: Copy + std::ops::Deref<Target = T>> UnsafeCopy<'_, T> for T {
| ++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ note: required by a bound in `copy`
|
LL | fn copy<U: Setup + ?Sized>(from: &U::From) -> U::From {
| ^^^^^ required by this bound in `copy`
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `std::marker::Copy`
|
LL | pub fn copy_any<T: std::marker::Copy>(t: &T) -> T {
| +++++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ note: required by a bound in `Complete::Assoc`
|
LL | type Assoc: Partial<Self>;
| ^^^^^^^^^^^^^ required by this bound in `Complete::Assoc`
help: consider restricting type parameter `T`
help: consider restricting type parameter `T` with trait `std::marker::Copy`
|
LL | impl<T: std::marker::Copy> Complete for T {
| +++++++++++++++++++
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/associated-types/issue-59324.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | |
LL | | Service<AssocType = <Bug as Foo>::OnlyFoo>
| |______________________________________________^ the trait `Foo` is not implemented for `Bug`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Foo`
|
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++
Expand All @@ -24,7 +24,7 @@ LL | |
LL | | }
| |_^ the trait `Foo` is not implemented for `Bug`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Foo`
|
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++
Expand All @@ -38,7 +38,7 @@ LL | | &self,
LL | | ) -> Self::AssocType;
| |_________________________^ the trait `Foo` is not implemented for `Bug`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Foo`
|
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++
Expand All @@ -61,7 +61,7 @@ error[E0277]: the trait bound `Bug: Foo` is not satisfied
LL | ) -> Self::AssocType;
| ^^^^^^^^^^^^^^^ the trait `Foo` is not implemented for `Bug`
|
help: consider further restricting this bound
help: consider further restricting this bound with trait `Foo`
|
LL | pub trait ThriftService<Bug: NotFoo + Foo>:
| +++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/issue-70818.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ note: captured value is not `Send`
|
LL | async { (ty, ty1) }
| ^^^ has type `U` which is not `Send`
help: consider restricting type parameter `U`
help: consider restricting type parameter `U` with trait `std::marker::Send`
|
LL | fn foo<T: Send, U: std::marker::Send>(ty: T, ty1: U) -> impl Future<Output = (T, U)> + Send {
| +++++++++++++++++++
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/async-await/issue-86507.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ note: captured value is not `Send` because `&` references cannot be sent unless
LL | let x = x;
| ^ has type `&T` which is not `Send`, because `T` is not `Sync`
= 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)>>`
help: consider further restricting this bound
help: consider further restricting this bound with trait `std::marker::Sync`
|
LL | fn bar<'me, 'async_trait, T: Send + std::marker::Sync>(x: &'me T)
| +++++++++++++++++++
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ LL | fn copy<T: Magic>(x: T) -> (T, T) { (x, x) }
| ^ - you could clone this value
| |
| consider constraining this type parameter with `Clone`
help: consider further restricting this bound
help: consider further restricting this bound with trait `Copy`
|
LL | fn copy<T: Magic + Copy>(x: T) -> (T, T) { (x, x) }
| ++++++
Expand Down
Loading
Loading