From be9d9618845b8edbfb110927d8deab8c636c9e59 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 10 Jul 2024 02:28:09 +0000 Subject: [PATCH] More accurate span for type parameter suggestion After: ``` error[E0229]: associated item constraints are not allowed here --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:3:10 | LL | impl Foo for String {} | ^^^^^^^^^^ associated item constraint not allowed here | help: declare the type parameter right after the `impl` keyword | LL - impl Foo for String {} LL + impl Foo for String {} | ``` Before: ``` error[E0229]: associated item constraints are not allowed here --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:3:10 | LL | impl Foo for String {} | ^^^^^^^^^^ associated item constraint not allowed here | help: declare the type parameter right after the `impl` keyword | LL | impl Foo for String {} | ++++++++++++ ~ ``` --- .../src/hir_ty_lowering/errors.rs | 8 +++-- ...declared-in-wrong-spot-issue-113073.stderr | 30 +++++++++++-------- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index 5e595488ea7a8..e1c590c255184 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -1340,11 +1340,13 @@ pub fn prohibit_assoc_item_constraint( format!("<{lifetimes}{type_with_constraints}>"), ) }; - let suggestions = - vec![param_decl, (constraint.span, format!("{}", matching_param.name))]; + let suggestions = vec![ + param_decl, + (constraint.span.with_lo(constraint.ident.span.hi()), String::new()), + ]; err.multipart_suggestion_verbose( - format!("declare the type parameter right after the `impl` keyword"), + "declare the type parameter right after the `impl` keyword", suggestions, Applicability::MaybeIncorrect, ); diff --git a/tests/ui/generics/impl-block-params-declared-in-wrong-spot-issue-113073.stderr b/tests/ui/generics/impl-block-params-declared-in-wrong-spot-issue-113073.stderr index dfc6761e17e7f..c60c4c72a2133 100644 --- a/tests/ui/generics/impl-block-params-declared-in-wrong-spot-issue-113073.stderr +++ b/tests/ui/generics/impl-block-params-declared-in-wrong-spot-issue-113073.stderr @@ -14,8 +14,9 @@ LL | impl Foo for String {} | help: declare the type parameter right after the `impl` keyword | -LL | impl Foo for String {} - | ++++++++++++ ~ +LL - impl Foo for String {} +LL + impl Foo for String {} + | error[E0229]: associated item constraints are not allowed here --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:7:10 @@ -25,8 +26,9 @@ LL | impl Foo for u8 {} | help: declare the type parameter right after the `impl` keyword | -LL | impl<'a, T: 'a + Default> Foo for u8 {} - | +++++++++++++++++++++ ~ +LL - impl Foo for u8 {} +LL + impl<'a, T: 'a + Default> Foo for u8 {} + | error[E0229]: associated item constraints are not allowed here --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:13:13 @@ -36,8 +38,9 @@ LL | impl Foo for u16 {} | help: declare the type parameter right after the `impl` keyword | -LL | impl Foo for u16 {} - | ++++++++++++ ~ +LL - impl Foo for u16 {} +LL + impl Foo for u16 {} + | error[E0229]: associated item constraints are not allowed here --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:17:14 @@ -47,8 +50,9 @@ LL | impl<'a> Foo for u32 {} | help: declare the type parameter right after the `impl` keyword | -LL | impl<'a, 'a, T: 'a + Default> Foo for u32 {} - | +++++++++++++++++++++ ~ +LL - impl<'a> Foo for u32 {} +LL + impl<'a, 'a, T: 'a + Default> Foo for u32 {} + | error[E0229]: associated item constraints are not allowed here --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:23:10 @@ -58,8 +62,9 @@ LL | impl Bar for String {} | help: declare the type parameter right after the `impl` keyword | -LL | impl Bar for String {} - | ++++++++++++ ~ +LL - impl Bar for String {} +LL + impl Bar for String {} + | error[E0107]: trait takes 2 generic arguments but 1 generic argument was supplied --> $DIR/impl-block-params-declared-in-wrong-spot-issue-113073.rs:27:9 @@ -87,8 +92,9 @@ LL | impl Bar for u8 {} | help: declare the type parameter right after the `impl` keyword | -LL | impl Bar for u8 {} - | ++++++++++++ ~ +LL - impl Bar for u8 {} +LL + impl Bar for u8 {} + | error: aborting due to 8 previous errors