diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2020-04-05 14:55:06 -0700 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2020-04-11 14:34:01 -0700 |
| commit | 1e3bdc08c955a55f69a4c2df4a829a52f5b0c21c (patch) | |
| tree | 87c48a36e174fc8da9146365b7d86611875a2691 | |
| parent | 01169572a23dc599a5a4c2f338afe68e62b295fa (diff) | |
| download | rust-1e3bdc08c955a55f69a4c2df4a829a52f5b0c21c.tar.gz rust-1e3bdc08c955a55f69a4c2df4a829a52f5b0c21c.zip | |
Try to use the first char in the trait name as type param
| -rw-r--r-- | src/librustc_hir/hir.rs | 9 | ||||
| -rw-r--r-- | src/librustc_trait_selection/traits/error_reporting/suggestions.rs | 4 | ||||
| -rw-r--r-- | src/librustc_typeck/collect.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/suggestions/impl-trait-with-missing-bounds.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr | 10 |
5 files changed, 15 insertions, 12 deletions
diff --git a/src/librustc_hir/hir.rs b/src/librustc_hir/hir.rs index b4744a7d6db..f26fc402a9a 100644 --- a/src/librustc_hir/hir.rs +++ b/src/librustc_hir/hir.rs @@ -438,13 +438,15 @@ impl GenericParam<'hir> { } pub trait NextTypeParamName { - fn next_type_param_name(&self) -> &'static str; + fn next_type_param_name(&self, name: Option<&str>) -> String; } impl NextTypeParamName for &[GenericParam<'_>] { - fn next_type_param_name(&self) -> &'static str { + fn next_type_param_name(&self, name: Option<&str>) -> String { // This is the whitelist of possible parameter names that we might suggest. - let possible_names = ["T", "U", "V", "X", "Y", "Z", "A", "B", "C", "D", "E", "F", "G"]; + let name = name.and_then(|n| n.chars().next()).map(|c| c.to_string().to_uppercase()); + let name = name.as_ref().map(|s| s.as_str()); + let possible_names = [name.unwrap_or("T"), "T", "U", "V", "X", "Y", "Z", "A", "B", "C"]; let used_names = self .iter() .filter_map(|p| match p.name { @@ -457,6 +459,7 @@ impl NextTypeParamName for &[GenericParam<'_>] { .iter() .find(|n| !used_names.contains(&Symbol::intern(n))) .unwrap_or(&"ParamName") + .to_string() } } diff --git a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs index 16bdfe5d0d1..152b4fb7c56 100644 --- a/src/librustc_trait_selection/traits/error_reporting/suggestions.rs +++ b/src/librustc_trait_selection/traits/error_reporting/suggestions.rs @@ -211,14 +211,14 @@ fn suggest_restriction( } } - let type_param_name = generics.params.next_type_param_name(); + let type_param_name = generics.params.next_type_param_name(Some(&name)); // The type param `T: Trait` we will suggest to introduce. let type_param = format!("{}: {}", type_param_name, name); // FIXME: modify the `trait_ref` instead of string shenanigans. // Turn `<impl Trait as Foo>::Bar: Qux` into `<T as Foo>::Bar: Qux`. let pred = trait_ref.without_const().to_predicate().to_string(); - let pred = pred.replace(&impl_name, type_param_name); + let pred = pred.replace(&impl_name, &type_param_name); let mut sugg = vec![ match generics .params diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index eb8f46e83bb..47754c3704c 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -135,7 +135,7 @@ crate fn placeholder_type_error( if placeholder_types.is_empty() { return; } - let type_name = generics.next_type_param_name(); + let type_name = generics.next_type_param_name(None); let mut sugg: Vec<_> = placeholder_types.iter().map(|sp| (*sp, (*type_name).to_string())).collect(); diff --git a/src/test/ui/suggestions/impl-trait-with-missing-bounds.rs b/src/test/ui/suggestions/impl-trait-with-missing-bounds.rs index 6947bc0a734..6e9e8821cfe 100644 --- a/src/test/ui/suggestions/impl-trait-with-missing-bounds.rs +++ b/src/test/ui/suggestions/impl-trait-with-missing-bounds.rs @@ -24,7 +24,7 @@ fn baz(t: impl std::fmt::Debug, constraints: impl Iterator) { } } -fn bat<K, T: std::fmt::Debug>(t: T, constraints: impl Iterator, _: K) { +fn bat<I, T: std::fmt::Debug>(t: T, constraints: impl Iterator, _: I) { for constraint in constraints { qux(t); qux(constraint); diff --git a/src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr b/src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr index 2d48be42233..e1c40e2537b 100644 --- a/src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr +++ b/src/test/ui/suggestions/impl-trait-with-missing-bounds.stderr @@ -10,7 +10,7 @@ LL | fn qux(_: impl std::fmt::Debug) {} = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item` help: introduce a type parameter with a trait bound instead of using `impl Trait` | -LL | fn foo<T: Iterator>(constraints: T) where <T as std::iter::Iterator>::Item: std::fmt::Debug { +LL | fn foo<I: Iterator>(constraints: I) where <I as std::iter::Iterator>::Item: std::fmt::Debug { | ^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` @@ -25,7 +25,7 @@ LL | fn qux(_: impl std::fmt::Debug) {} = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item` help: introduce a type parameter with a trait bound instead of using `impl Trait` | -LL | fn bar<T, U: Iterator>(t: T, constraints: U) where T: std::fmt::Debug, <U as std::iter::Iterator>::Item: std::fmt::Debug { +LL | fn bar<T, I: Iterator>(t: T, constraints: I) where T: std::fmt::Debug, <I as std::iter::Iterator>::Item: std::fmt::Debug { | ^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` @@ -40,7 +40,7 @@ LL | fn qux(_: impl std::fmt::Debug) {} = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item` help: introduce a type parameter with a trait bound instead of using `impl Trait` | -LL | fn baz<T: Iterator>(t: impl std::fmt::Debug, constraints: T) where <T as std::iter::Iterator>::Item: std::fmt::Debug { +LL | fn baz<I: Iterator>(t: impl std::fmt::Debug, constraints: I) where <I as std::iter::Iterator>::Item: std::fmt::Debug { | ^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0277]: `<impl Iterator as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` @@ -55,7 +55,7 @@ LL | fn qux(_: impl std::fmt::Debug) {} = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator as std::iter::Iterator>::Item` help: introduce a type parameter with a trait bound instead of using `impl Trait` | -LL | fn bat<K, T: std::fmt::Debug, U: Iterator>(t: T, constraints: U, _: K) where <U as std::iter::Iterator>::Item: std::fmt::Debug { +LL | fn bat<I, T: std::fmt::Debug, U: Iterator>(t: T, constraints: U, _: I) where <U as std::iter::Iterator>::Item: std::fmt::Debug { | ^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error[E0277]: `<impl Iterator + std::fmt::Debug as std::iter::Iterator>::Item` doesn't implement `std::fmt::Debug` @@ -70,7 +70,7 @@ LL | fn qux(_: impl std::fmt::Debug) {} = help: the trait `std::fmt::Debug` is not implemented for `<impl Iterator + std::fmt::Debug as std::iter::Iterator>::Item` help: introduce a type parameter with a trait bound instead of using `impl Trait` | -LL | fn bak<T: Iterator + std::fmt::Debug>(constraints: T) where <T as std::iter::Iterator>::Item: std::fmt::Debug { +LL | fn bak<I: Iterator + std::fmt::Debug>(constraints: I) where <I as std::iter::Iterator>::Item: std::fmt::Debug { | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ error: aborting due to 5 previous errors |
