Skip to content

Commit 835b7a5

Browse files
ui: improve suggestion test by addig the help message
Signed-off-by: Vincenzo Palazzo <[email protected]>
1 parent 86c1a73 commit 835b7a5

File tree

4 files changed

+39
-28
lines changed

4 files changed

+39
-28
lines changed

compiler/rustc_typeck/src/astconv/mod.rs

+2-13
Original file line numberDiff line numberDiff line change
@@ -3015,7 +3015,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
30153015
let param_name = generics.params.next_type_param_name(None);
30163016

30173017
let add_generic_sugg = if let Some(span) = generics.span_for_param_suggestion() {
3018-
let param_name = generics.params.next_type_param_name(Some(&impl_trait_name));
30193018
(span, format!(", {}: {}", param_name, impl_trait_name))
30203019
} else {
30213020
(generics.span, format!("<{}: {}>", param_name, impl_trait_name))
@@ -3047,11 +3046,6 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
30473046
.map_or(false, |s| s.trim_end().ends_with('<'));
30483047

30493048
let is_global = poly_trait_ref.trait_ref.path.is_global();
3050-
let is_local = if let Some(def_id) = poly_trait_ref.trait_ref.trait_def_id() {
3051-
def_id.is_local()
3052-
} else {
3053-
false
3054-
};
30553049
let sugg = Vec::from_iter([
30563050
(
30573051
self_ty.span.shrink_to_lo(),
@@ -3077,9 +3071,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
30773071
rustc_errors::struct_span_err!(tcx.sess, self_ty.span, E0782, "{}", msg);
30783072
diag.multipart_suggestion_verbose(label, sugg, Applicability::MachineApplicable);
30793073
// check if the impl trait that we are considering is a impl of a local trait
3080-
if is_local {
3081-
self.maybe_lint_blanket_trait_impl(&self_ty, &mut diag);
3082-
}
3074+
self.maybe_lint_blanket_trait_impl(&self_ty, &mut diag);
30833075
diag.emit();
30843076
} else {
30853077
let msg = "trait objects without an explicit `dyn` are deprecated";
@@ -3094,10 +3086,7 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
30943086
sugg,
30953087
Applicability::MachineApplicable,
30963088
);
3097-
// check if the impl trait that we are considering is a impl of a local trait
3098-
if is_local {
3099-
self.maybe_lint_blanket_trait_impl::<()>(&self_ty, &mut diag);
3100-
}
3089+
self.maybe_lint_blanket_trait_impl::<()>(&self_ty, &mut diag);
31013090
diag.emit();
31023091
},
31033092
);

src/test/ui/suggestions/suggest-blanket-impl-local-trait.rs

+14
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,47 @@ trait GenericTrait<T> {}
1212

1313
impl LocalTraitTwo for LocalTraitOne {}
1414
//~^ ERROR trait objects must include the `dyn` keyword
15+
//~| HELP add `dyn` keyword before this trait
16+
//~| HELP alternatively use a blanket implementation to implement `LocalTraitTwo` for all types that also implement `LocalTraitOne`
1517

1618
impl fmt::Display for LocalTraitOne {
1719
//~^ ERROR trait objects must include the `dyn` keyword
20+
//~| HELP add `dyn` keyword before this trait
1821
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
1922
todo!();
2023
}
2124
}
2225

2326
impl fmt::Display for LocalTraitTwo + Send {
2427
//~^ ERROR trait objects must include the `dyn` keyword
28+
//~| HELP add `dyn` keyword before this trait
2529
fn fmt(&self, _f: &mut fmt::Formatter<'_>) -> fmt::Result {
2630
todo!();
2731
}
2832
}
2933

3034
impl LocalTraitOne for fmt::Display {}
3135
//~^ ERROR trait objects must include the `dyn` keyword
36+
//~| HELP add `dyn` keyword before this trait
37+
//~| HELP alternatively use a blanket implementation to implement `LocalTraitOne` for all types that also implement `fmt::Display`
38+
3239

3340
impl LocalTraitOne for fmt::Display + Send {}
3441
//~^ ERROR trait objects must include the `dyn` keyword
42+
//~| HELP add `dyn` keyword before this trait
43+
//~| HELP alternatively use a blanket implementation to implement `LocalTraitOne` for all types that also implement `fmt::Display + Send`
44+
3545

3646
impl<E> GenericTrait<E> for LocalTraitOne {}
3747
//~^ ERROR trait objects must include the `dyn` keyword
48+
//~| HELP add `dyn` keyword before this trait
49+
//~| HELP alternatively use a blanket implementation to implement `GenericTrait<E>` for all types that also implement `LocalTraitOne`
3850

3951
trait GenericTraitTwo<T> {}
4052

4153
impl<T, E> GenericTraitTwo<E> for GenericTrait<T> {}
4254
//~^ ERROR trait objects must include the `dyn` keyword
55+
//~| HELP add `dyn` keyword before this trait
56+
//~| HELP alternatively use a blanket implementation to implement `GenericTraitTwo<E>` for all types that also implement `GenericTrait<T>`
4357

4458
fn main() {}

src/test/ui/suggestions/suggest-blanket-impl-local-trait.stderr

+23-15
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ help: add `dyn` keyword before this trait
88
|
99
LL - impl LocalTraitTwo for LocalTraitOne {}
1010
LL + impl LocalTraitTwo for dyn LocalTraitOne {}
11-
|
11+
|
1212
help: alternatively use a blanket implementation to implement `LocalTraitTwo` for all types that also implement `LocalTraitOne`
1313
|
1414
LL | impl<T: LocalTraitOne> LocalTraitTwo for T {}
1515
| ++++++++++++++++++ ~
1616

1717
error[E0782]: trait objects must include the `dyn` keyword
18-
--> $DIR/suggest-blanket-impl-local-trait.rs:16:23
18+
--> $DIR/suggest-blanket-impl-local-trait.rs:18:23
1919
|
2020
LL | impl fmt::Display for LocalTraitOne {
2121
| ^^^^^^^^^^^^^
@@ -24,10 +24,10 @@ help: add `dyn` keyword before this trait
2424
|
2525
LL - impl fmt::Display for LocalTraitOne {
2626
LL + impl fmt::Display for dyn LocalTraitOne {
27-
|
27+
|
2828

2929
error[E0782]: trait objects must include the `dyn` keyword
30-
--> $DIR/suggest-blanket-impl-local-trait.rs:23:23
30+
--> $DIR/suggest-blanket-impl-local-trait.rs:26:23
3131
|
3232
LL | impl fmt::Display for LocalTraitTwo + Send {
3333
| ^^^^^^^^^^^^^^^^^^^^
@@ -36,10 +36,10 @@ help: add `dyn` keyword before this trait
3636
|
3737
LL - impl fmt::Display for LocalTraitTwo + Send {
3838
LL + impl fmt::Display for dyn LocalTraitTwo + Send {
39-
|
39+
|
4040

4141
error[E0782]: trait objects must include the `dyn` keyword
42-
--> $DIR/suggest-blanket-impl-local-trait.rs:30:24
42+
--> $DIR/suggest-blanket-impl-local-trait.rs:34:24
4343
|
4444
LL | impl LocalTraitOne for fmt::Display {}
4545
| ^^^^^^^^^^^^
@@ -48,10 +48,14 @@ help: add `dyn` keyword before this trait
4848
|
4949
LL - impl LocalTraitOne for fmt::Display {}
5050
LL + impl LocalTraitOne for dyn fmt::Display {}
51-
|
51+
|
52+
help: alternatively use a blanket implementation to implement `LocalTraitOne` for all types that also implement `fmt::Display`
53+
|
54+
LL | impl<T: fmt::Display> LocalTraitOne for T {}
55+
| +++++++++++++++++ ~
5256

5357
error[E0782]: trait objects must include the `dyn` keyword
54-
--> $DIR/suggest-blanket-impl-local-trait.rs:33:24
58+
--> $DIR/suggest-blanket-impl-local-trait.rs:40:24
5559
|
5660
LL | impl LocalTraitOne for fmt::Display + Send {}
5761
| ^^^^^^^^^^^^^^^^^^^
@@ -60,10 +64,14 @@ help: add `dyn` keyword before this trait
6064
|
6165
LL - impl LocalTraitOne for fmt::Display + Send {}
6266
LL + impl LocalTraitOne for dyn fmt::Display + Send {}
63-
|
67+
|
68+
help: alternatively use a blanket implementation to implement `LocalTraitOne` for all types that also implement `fmt::Display + Send`
69+
|
70+
LL | impl<T: fmt::Display + Send> LocalTraitOne for T {}
71+
| ++++++++++++++++++++++++ ~
6472

6573
error[E0782]: trait objects must include the `dyn` keyword
66-
--> $DIR/suggest-blanket-impl-local-trait.rs:36:29
74+
--> $DIR/suggest-blanket-impl-local-trait.rs:46:29
6775
|
6876
LL | impl<E> GenericTrait<E> for LocalTraitOne {}
6977
| ^^^^^^^^^^^^^
@@ -72,14 +80,14 @@ help: add `dyn` keyword before this trait
7280
|
7381
LL - impl<E> GenericTrait<E> for LocalTraitOne {}
7482
LL + impl<E> GenericTrait<E> for dyn LocalTraitOne {}
75-
|
83+
|
7684
help: alternatively use a blanket implementation to implement `GenericTrait<E>` for all types that also implement `LocalTraitOne`
7785
|
78-
LL | impl<E, L: LocalTraitOne> GenericTrait<E> for T {}
86+
LL | impl<E, T: LocalTraitOne> GenericTrait<E> for T {}
7987
| ++++++++++++++++++ ~
8088

8189
error[E0782]: trait objects must include the `dyn` keyword
82-
--> $DIR/suggest-blanket-impl-local-trait.rs:41:35
90+
--> $DIR/suggest-blanket-impl-local-trait.rs:53:35
8391
|
8492
LL | impl<T, E> GenericTraitTwo<E> for GenericTrait<T> {}
8593
| ^^^^^^^^^^^^^^^
@@ -88,10 +96,10 @@ help: add `dyn` keyword before this trait
8896
|
8997
LL - impl<T, E> GenericTraitTwo<E> for GenericTrait<T> {}
9098
LL + impl<T, E> GenericTraitTwo<E> for dyn GenericTrait<T> {}
91-
|
99+
|
92100
help: alternatively use a blanket implementation to implement `GenericTraitTwo<E>` for all types that also implement `GenericTrait<T>`
93101
|
94-
LL | impl<T, E, G: GenericTrait<T>> GenericTraitTwo<E> for U {}
102+
LL | impl<T, E, U: GenericTrait<T>> GenericTraitTwo<E> for U {}
95103
| ++++++++++++++++++++ ~
96104

97105
error: aborting due to 7 previous errors

suggest-blanket-impl-local-trait

468 KB
Binary file not shown.

0 commit comments

Comments
 (0)