Skip to content

Commit 38d7e27

Browse files
Properly replace impl Trait in fn args, turn {integer} to i32
1 parent 55805ab commit 38d7e27

File tree

5 files changed

+76
-12
lines changed

5 files changed

+76
-12
lines changed

compiler/rustc_trait_selection/src/traits/error_reporting/suggestions.rs

+27-12
Original file line numberDiff line numberDiff line change
@@ -372,21 +372,34 @@ fn suggest_restriction<'tcx>(
372372
// but instead we choose to suggest replacing all instances of `impl Trait` with `T`
373373
// where `T: Trait`.
374374
let mut ty_spans = vec![];
375-
let impl_trait_str = format!("impl {}", bound_str);
376375
for input in fn_sig.decl.inputs {
377-
if let hir::TyKind::Path(hir::QPath::Resolved(
378-
None,
379-
hir::Path { segments: [segment], .. },
380-
)) = input.kind
381-
{
382-
if segment.ident.as_str() == impl_trait_str.as_str() {
383-
// `fn foo(t: impl Trait)`
384-
// ^^^^^^^^^^ get this to suggest `T` instead
376+
struct ReplaceImplTraitVisitor<'a> {
377+
ty_spans: &'a mut Vec<Span>,
378+
bound_str: &'a str,
379+
}
380+
impl<'a, 'hir> hir::intravisit::Visitor<'hir> for ReplaceImplTraitVisitor<'a> {
381+
fn visit_ty(&mut self, t: &'hir hir::Ty<'hir>) {
382+
if let hir::TyKind::Path(hir::QPath::Resolved(
383+
None,
384+
hir::Path { segments: [segment], .. },
385+
)) = t.kind
386+
{
387+
if segment.ident.as_str().strip_prefix("impl ").map(|s| s.trim_start())
388+
== Some(self.bound_str)
389+
{
390+
// `fn foo(t: impl Trait)`
391+
// ^^^^^^^^^^ get this to suggest `T` instead
385392

386-
// There might be more than one `impl Trait`.
387-
ty_spans.push(input.span);
393+
// There might be more than one `impl Trait`.
394+
self.ty_spans.push(t.span);
395+
return;
396+
}
397+
}
398+
hir::intravisit::walk_ty(self, t);
388399
}
389400
}
401+
ReplaceImplTraitVisitor { ty_spans: &mut ty_spans, bound_str: &bound_str }
402+
.visit_ty(input);
390403
}
391404

392405
let type_param_name = generics.params.next_type_param_name(Some(&bound_str));
@@ -396,7 +409,7 @@ fn suggest_restriction<'tcx>(
396409
// FIXME: modify the `trait_pred` instead of string shenanigans.
397410
// Turn `<impl Trait as Foo>::Bar: Qux` into `<T as Foo>::Bar: Qux`.
398411
let pred = trait_pred.to_predicate(tcx).to_string();
399-
let pred = pred.replace(&impl_trait_str, &type_param_name);
412+
let pred = pred.replace(&format!("impl {}", bound_str), &type_param_name);
400413
let mut sugg = vec![
401414
if let Some(span) = generics.span_for_param_suggestion() {
402415
(span, format!(", {}", type_param))
@@ -460,6 +473,8 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
460473
trait_pred: ty::PolyTraitPredicate<'tcx>,
461474
body_id: hir::HirId,
462475
) {
476+
let trait_pred = self.resolve_numeric_literals_with_default(trait_pred);
477+
463478
let self_ty = trait_pred.skip_binder().self_ty();
464479
let (param_ty, projection) = match self_ty.kind() {
465480
ty::Param(_) => (true, None),
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
fn add_ten<N>(n: N) -> N {
2+
n + 10
3+
//~^ ERROR cannot add `{integer}` to `N`
4+
}
5+
6+
fn main() {}
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
error[E0369]: cannot add `{integer}` to `N`
2+
--> $DIR/issue-97677.rs:2:7
3+
|
4+
LL | n + 10
5+
| - ^ -- {integer}
6+
| |
7+
| N
8+
|
9+
help: consider restricting type parameter `N`
10+
|
11+
LL | fn add_ten<N: std::ops::Add<i32>>(n: N) -> N {
12+
| ++++++++++++++++++++
13+
14+
error: aborting due to previous error
15+
16+
For more information about this error, try `rustc --explain E0369`.
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
pub fn print_values(values: &impl IntoIterator)
2+
where {
3+
for x in values.into_iter() {
4+
println!("{x}");
5+
//~^ ERROR <impl IntoIterator as IntoIterator>::Item` doesn't implement `std::fmt::Display
6+
}
7+
}
8+
9+
fn main() {}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error[E0277]: `<impl IntoIterator as IntoIterator>::Item` doesn't implement `std::fmt::Display`
2+
--> $DIR/issue-97760.rs:4:20
3+
|
4+
LL | println!("{x}");
5+
| ^ `<impl IntoIterator as IntoIterator>::Item` cannot be formatted with the default formatter
6+
|
7+
= help: the trait `std::fmt::Display` is not implemented for `<impl IntoIterator as IntoIterator>::Item`
8+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
9+
= note: this error originates in the macro `$crate::format_args_nl` (in Nightly builds, run with -Z macro-backtrace for more info)
10+
help: introduce a type parameter with a trait bound instead of using `impl Trait`
11+
|
12+
LL ~ pub fn print_values<I: IntoIterator>(values: &I)
13+
LL ~ where <I as IntoIterator>::Item: std::fmt::Display {
14+
|
15+
16+
error: aborting due to previous error
17+
18+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)