From 7d33094d3aa9c9ef2f376ed0dff026a6bc402dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Fri, 30 Jun 2023 00:30:50 +0000 Subject: [PATCH] Use structured suggestion when telling user about `for<'a>` ``` error[E0637]: `&` without an explicit lifetime name cannot be used here --> $DIR/E0637.rs:13:13 | LL | T: Into<&u32>, | ^ explicit lifetime name needed here | help: consider introducing a higher-ranked lifetime here | LL | T: for<'a> Into<&'a u32>, | +++++++ ++ ``` --- compiler/rustc_resolve/src/late.rs | 10 +++++++--- tests/ui/error-codes/E0637.stderr | 7 +++---- ...orrect-explicit-lifetime-name-needed.stderr | 7 +++---- ...ause-inherent-impl-ampersand-rust2015.fixed | 17 +++++++++++++++++ ...-clause-inherent-impl-ampersand-rust2015.rs | 17 +++++++++++++++++ ...se-inherent-impl-ampersand-rust2015.stderr} | 9 ++++----- ...ause-inherent-impl-ampersand-rust2018.fixed | 18 ++++++++++++++++++ ...-clause-inherent-impl-ampersand-rust2018.rs | 18 ++++++++++++++++++ ...se-inherent-impl-ampersand-rust2018.stderr} | 9 ++++----- .../where-clause-inherent-impl-ampersand.rs | 18 ------------------ .../where-clause-trait-impl-region-2015.fixed | 14 ++++++++++++++ .../where-clause-trait-impl-region-2015.rs | 14 ++++++++++++++ ...where-clause-trait-impl-region-2015.stderr} | 9 ++++----- .../where-clause-trait-impl-region-2018.fixed | 15 +++++++++++++++ .../where-clause-trait-impl-region-2018.rs | 15 +++++++++++++++ ...where-clause-trait-impl-region-2018.stderr} | 9 ++++----- .../where-clause-trait-impl-region.rs | 15 --------------- 17 files changed, 157 insertions(+), 64 deletions(-) create mode 100644 tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.fixed create mode 100644 tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.rs rename tests/ui/underscore-lifetime/{where-clause-trait-impl-region.rust2015.stderr => where-clause-inherent-impl-ampersand-rust2015.stderr} (56%) create mode 100644 tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.fixed create mode 100644 tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.rs rename tests/ui/underscore-lifetime/{where-clause-trait-impl-region.rust2018.stderr => where-clause-inherent-impl-ampersand-rust2018.stderr} (56%) delete mode 100644 tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rs create mode 100644 tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.fixed create mode 100644 tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.rs rename tests/ui/underscore-lifetime/{where-clause-inherent-impl-ampersand.rust2015.stderr => where-clause-trait-impl-region-2015.stderr} (55%) create mode 100644 tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.fixed create mode 100644 tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.rs rename tests/ui/underscore-lifetime/{where-clause-inherent-impl-ampersand.rust2018.stderr => where-clause-trait-impl-region-2018.stderr} (55%) delete mode 100644 tests/ui/underscore-lifetime/where-clause-trait-impl-region.rs diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 9f4573ea02594..744dcf0db846e 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -1632,9 +1632,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> { .. } = &rib.kind { - diag.span_help( - *span, - "consider introducing a higher-ranked lifetime here with `for<'a>`", + diag.multipart_suggestion( + "consider introducing a higher-ranked lifetime here", + vec![ + (span.shrink_to_lo(), "for<'a> ".into()), + (lifetime.ident.span.shrink_to_hi(), "'a ".into()), + ], + Applicability::MachineApplicable, ); break; } diff --git a/tests/ui/error-codes/E0637.stderr b/tests/ui/error-codes/E0637.stderr index 78341735e191a..d9db89ddb0c97 100644 --- a/tests/ui/error-codes/E0637.stderr +++ b/tests/ui/error-codes/E0637.stderr @@ -22,11 +22,10 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here LL | T: Into<&u32>, | ^ explicit lifetime name needed here | -help: consider introducing a higher-ranked lifetime here with `for<'a>` - --> $DIR/E0637.rs:13:8 +help: consider introducing a higher-ranked lifetime here | -LL | T: Into<&u32>, - | ^ +LL | T: for<'a> Into<&'a u32>, + | +++++++ ++ error: aborting due to 3 previous errors diff --git a/tests/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr b/tests/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr index faf4c9eb87275..bcd1fbc55edcd 100644 --- a/tests/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr +++ b/tests/ui/generics/issue-65285-incorrect-explicit-lifetime-name-needed.stderr @@ -4,11 +4,10 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here LL | fn should_error() where T : Into<&u32> {} | ^ explicit lifetime name needed here | -help: consider introducing a higher-ranked lifetime here with `for<'a>` - --> $DIR/issue-65285-incorrect-explicit-lifetime-name-needed.rs:5:32 +help: consider introducing a higher-ranked lifetime here | -LL | fn should_error() where T : Into<&u32> {} - | ^ +LL | fn should_error() where T : for<'a> Into<&'a u32> {} + | +++++++ ++ error[E0106]: missing lifetime specifier --> $DIR/issue-65285-incorrect-explicit-lifetime-name-needed.rs:9:20 diff --git a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.fixed b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.fixed new file mode 100644 index 0000000000000..5be6ff8e7e14e --- /dev/null +++ b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.fixed @@ -0,0 +1,17 @@ +// run-rustfix + +trait WithType {} +trait WithRegion<'a> { } + +#[allow(dead_code)] +struct Foo { + t: T +} + +impl Foo +where + T: for<'a> WithType<&'a u32> +//~^ ERROR `&` without an explicit lifetime name cannot be used here +{ } + +fn main() {} diff --git a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.rs b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.rs new file mode 100644 index 0000000000000..d7072aa118161 --- /dev/null +++ b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.rs @@ -0,0 +1,17 @@ +// run-rustfix + +trait WithType {} +trait WithRegion<'a> { } + +#[allow(dead_code)] +struct Foo { + t: T +} + +impl Foo +where + T: WithType<&u32> +//~^ ERROR `&` without an explicit lifetime name cannot be used here +{ } + +fn main() {} diff --git a/tests/ui/underscore-lifetime/where-clause-trait-impl-region.rust2015.stderr b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.stderr similarity index 56% rename from tests/ui/underscore-lifetime/where-clause-trait-impl-region.rust2015.stderr rename to tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.stderr index 63fc1a19b9383..3e197dc9a9d12 100644 --- a/tests/ui/underscore-lifetime/where-clause-trait-impl-region.rust2015.stderr +++ b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2015.stderr @@ -1,14 +1,13 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here - --> $DIR/where-clause-trait-impl-region.rs:11:17 + --> $DIR/where-clause-inherent-impl-ampersand-rust2015.rs:13:17 | LL | T: WithType<&u32> | ^ explicit lifetime name needed here | -help: consider introducing a higher-ranked lifetime here with `for<'a>` - --> $DIR/where-clause-trait-impl-region.rs:11:8 +help: consider introducing a higher-ranked lifetime here | -LL | T: WithType<&u32> - | ^ +LL | T: for<'a> WithType<&'a u32> + | +++++++ ++ error: aborting due to previous error diff --git a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.fixed b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.fixed new file mode 100644 index 0000000000000..0f1be586589e2 --- /dev/null +++ b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.fixed @@ -0,0 +1,18 @@ +// edition:2018 +// run-rustfix + +trait WithType {} +trait WithRegion<'a> { } + +#[allow(dead_code)] +struct Foo { + t: T +} + +impl Foo +where + T: for<'a> WithType<&'a u32> +//~^ ERROR `&` without an explicit lifetime name cannot be used here +{ } + +fn main() {} diff --git a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.rs b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.rs new file mode 100644 index 0000000000000..59f7e472e2d34 --- /dev/null +++ b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.rs @@ -0,0 +1,18 @@ +// edition:2018 +// run-rustfix + +trait WithType {} +trait WithRegion<'a> { } + +#[allow(dead_code)] +struct Foo { + t: T +} + +impl Foo +where + T: WithType<&u32> +//~^ ERROR `&` without an explicit lifetime name cannot be used here +{ } + +fn main() {} diff --git a/tests/ui/underscore-lifetime/where-clause-trait-impl-region.rust2018.stderr b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.stderr similarity index 56% rename from tests/ui/underscore-lifetime/where-clause-trait-impl-region.rust2018.stderr rename to tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.stderr index 63fc1a19b9383..08b4268e5d237 100644 --- a/tests/ui/underscore-lifetime/where-clause-trait-impl-region.rust2018.stderr +++ b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand-rust2018.stderr @@ -1,14 +1,13 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here - --> $DIR/where-clause-trait-impl-region.rs:11:17 + --> $DIR/where-clause-inherent-impl-ampersand-rust2018.rs:14:17 | LL | T: WithType<&u32> | ^ explicit lifetime name needed here | -help: consider introducing a higher-ranked lifetime here with `for<'a>` - --> $DIR/where-clause-trait-impl-region.rs:11:8 +help: consider introducing a higher-ranked lifetime here | -LL | T: WithType<&u32> - | ^ +LL | T: for<'a> WithType<&'a u32> + | +++++++ ++ error: aborting due to previous error diff --git a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rs b/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rs deleted file mode 100644 index 43de30944cacb..0000000000000 --- a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rs +++ /dev/null @@ -1,18 +0,0 @@ -// revisions: rust2015 rust2018 -//[rust2018] edition:2018 - -trait WithType {} -trait WithRegion<'a> { } - -struct Foo { - t: T -} - -impl Foo -where - T: WithType<&u32> -//[rust2015]~^ ERROR `&` without an explicit lifetime name cannot be used here -//[rust2018]~^^ ERROR `&` without an explicit lifetime name cannot be used here -{ } - -fn main() {} diff --git a/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.fixed b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.fixed new file mode 100644 index 0000000000000..55c7470960eb2 --- /dev/null +++ b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.fixed @@ -0,0 +1,14 @@ +// run-rustfix + +trait WithType {} +trait WithRegion<'a> { } + +trait Foo { } + +impl Foo for Vec +where + T: for<'a> WithType<&'a u32> +//~^ ERROR `&` without an explicit lifetime name cannot be used here +{ } + +fn main() {} diff --git a/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.rs b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.rs new file mode 100644 index 0000000000000..42a35b0216119 --- /dev/null +++ b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.rs @@ -0,0 +1,14 @@ +// run-rustfix + +trait WithType {} +trait WithRegion<'a> { } + +trait Foo { } + +impl Foo for Vec +where + T: WithType<&u32> +//~^ ERROR `&` without an explicit lifetime name cannot be used here +{ } + +fn main() {} diff --git a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rust2015.stderr b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.stderr similarity index 55% rename from tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rust2015.stderr rename to tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.stderr index f4d14b5f87bec..8c5bbb631b4c7 100644 --- a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rust2015.stderr +++ b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2015.stderr @@ -1,14 +1,13 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here - --> $DIR/where-clause-inherent-impl-ampersand.rs:13:17 + --> $DIR/where-clause-trait-impl-region-2015.rs:10:17 | LL | T: WithType<&u32> | ^ explicit lifetime name needed here | -help: consider introducing a higher-ranked lifetime here with `for<'a>` - --> $DIR/where-clause-inherent-impl-ampersand.rs:13:8 +help: consider introducing a higher-ranked lifetime here | -LL | T: WithType<&u32> - | ^ +LL | T: for<'a> WithType<&'a u32> + | +++++++ ++ error: aborting due to previous error diff --git a/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.fixed b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.fixed new file mode 100644 index 0000000000000..09b96fe5ea42a --- /dev/null +++ b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.fixed @@ -0,0 +1,15 @@ +// run-rustfix +// edition:2018 + +trait WithType {} +trait WithRegion<'a> { } + +trait Foo { } + +impl Foo for Vec +where + T: for<'a> WithType<&'a u32> +//~^ ERROR `&` without an explicit lifetime name cannot be used here +{ } + +fn main() {} diff --git a/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.rs b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.rs new file mode 100644 index 0000000000000..445f38cbee187 --- /dev/null +++ b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.rs @@ -0,0 +1,15 @@ +// run-rustfix +// edition:2018 + +trait WithType {} +trait WithRegion<'a> { } + +trait Foo { } + +impl Foo for Vec +where + T: WithType<&u32> +//~^ ERROR `&` without an explicit lifetime name cannot be used here +{ } + +fn main() {} diff --git a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rust2018.stderr b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.stderr similarity index 55% rename from tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rust2018.stderr rename to tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.stderr index f4d14b5f87bec..0268c59fa4af8 100644 --- a/tests/ui/underscore-lifetime/where-clause-inherent-impl-ampersand.rust2018.stderr +++ b/tests/ui/underscore-lifetime/where-clause-trait-impl-region-2018.stderr @@ -1,14 +1,13 @@ error[E0637]: `&` without an explicit lifetime name cannot be used here - --> $DIR/where-clause-inherent-impl-ampersand.rs:13:17 + --> $DIR/where-clause-trait-impl-region-2018.rs:11:17 | LL | T: WithType<&u32> | ^ explicit lifetime name needed here | -help: consider introducing a higher-ranked lifetime here with `for<'a>` - --> $DIR/where-clause-inherent-impl-ampersand.rs:13:8 +help: consider introducing a higher-ranked lifetime here | -LL | T: WithType<&u32> - | ^ +LL | T: for<'a> WithType<&'a u32> + | +++++++ ++ error: aborting due to previous error diff --git a/tests/ui/underscore-lifetime/where-clause-trait-impl-region.rs b/tests/ui/underscore-lifetime/where-clause-trait-impl-region.rs deleted file mode 100644 index 09e5bbd846d6c..0000000000000 --- a/tests/ui/underscore-lifetime/where-clause-trait-impl-region.rs +++ /dev/null @@ -1,15 +0,0 @@ -// revisions: rust2015 rust2018 -//[rust2018] edition:2018 - -trait WithType {} -trait WithRegion<'a> { } - -trait Foo { } - -impl Foo for Vec -where - T: WithType<&u32> -//[rust2015,rust2018]~^ ERROR `&` without an explicit lifetime name cannot be used here -{ } - -fn main() {}