From ad7c2b06609c0da21ebdab6e45135cf07290f585 Mon Sep 17 00:00:00 2001 From: Oliver Killane Date: Sun, 5 May 2024 13:54:33 +0100 Subject: [PATCH 1/4] Updated error code explanation --- .../src/error_codes/E0582.md | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/compiler/rustc_error_codes/src/error_codes/E0582.md b/compiler/rustc_error_codes/src/error_codes/E0582.md index e50cc60ea3302..ea32e4f9f33f1 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0582.md +++ b/compiler/rustc_error_codes/src/error_codes/E0582.md @@ -27,6 +27,40 @@ fn bar(t: F, u: G) fn main() { } ``` +This error also includes the use of associated types with lifetime parameters. +```compile_fail,E0582 +trait Foo { + type Assoc<'a>; +} + +struct Bar +where + X: Foo, + F: for<'a> Fn(X::Assoc<'a>) -> &'a i32 +{ + x: X, + f: F +} +``` +This is as `Foo::Assoc<'a>` could be implemented by a type that does not use +the `'a` parameter, so there is no guarentee that `X::Assoc<'a>` actually uses +`'a`. + +To fix this we can pass a dummy parameter: +``` +# trait Foo { +# type Assoc<'a>; +# } +struct Bar +where + X: Foo, + F: for<'a> Fn(X::Assoc<'a>, /* dummy */ &'a ()) -> &'a i32 +{ + x: X, + f: F +} +``` + Note: The examples above used to be (erroneously) accepted by the compiler, but this was since corrected. See [issue #33685] for more details. From f3dcf65985dd956595a3f14dcb2a3052daceeb73 Mon Sep 17 00:00:00 2001 From: Oliver Killane Date: Sun, 5 May 2024 14:55:16 +0100 Subject: [PATCH 2/4] fix whitespace --- compiler/rustc_error_codes/src/error_codes/E0582.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0582.md b/compiler/rustc_error_codes/src/error_codes/E0582.md index ea32e4f9f33f1..31927cd5fe34f 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0582.md +++ b/compiler/rustc_error_codes/src/error_codes/E0582.md @@ -42,8 +42,8 @@ where f: F } ``` -This is as `Foo::Assoc<'a>` could be implemented by a type that does not use -the `'a` parameter, so there is no guarentee that `X::Assoc<'a>` actually uses +This is as `Foo::Assoc<'a>` could be implemented by a type that does not use +the `'a` parameter, so there is no guarentee that `X::Assoc<'a>` actually uses `'a`. To fix this we can pass a dummy parameter: From a5d0988a88a7557d4ae507fecd4df3d9ed8ad839 Mon Sep 17 00:00:00 2001 From: Oliver Killane <44177991+OliverKillane@users.noreply.github.com> Date: Wed, 15 May 2024 00:07:21 +0100 Subject: [PATCH 3/4] Update compiler/rustc_error_codes/src/error_codes/E0582.md Co-authored-by: Felix S Klock II --- compiler/rustc_error_codes/src/error_codes/E0582.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0582.md b/compiler/rustc_error_codes/src/error_codes/E0582.md index 31927cd5fe34f..26b65b12bc912 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0582.md +++ b/compiler/rustc_error_codes/src/error_codes/E0582.md @@ -42,7 +42,7 @@ where f: F } ``` -This is as `Foo::Assoc<'a>` could be implemented by a type that does not use +The latter scenario encounters this error because `Foo::Assoc<'a>` could be implemented by a type that does not use the `'a` parameter, so there is no guarentee that `X::Assoc<'a>` actually uses `'a`. From 012288b922e5db0ca6a8bbb2d1704c86c0fd79ff Mon Sep 17 00:00:00 2001 From: Oliver Killane Date: Wed, 15 May 2024 01:15:36 +0100 Subject: [PATCH 4/4] tidy fix from suggestion --- compiler/rustc_error_codes/src/error_codes/E0582.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_error_codes/src/error_codes/E0582.md b/compiler/rustc_error_codes/src/error_codes/E0582.md index 26b65b12bc912..b2cdb509c95c0 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0582.md +++ b/compiler/rustc_error_codes/src/error_codes/E0582.md @@ -42,9 +42,9 @@ where f: F } ``` -The latter scenario encounters this error because `Foo::Assoc<'a>` could be implemented by a type that does not use -the `'a` parameter, so there is no guarentee that `X::Assoc<'a>` actually uses -`'a`. +The latter scenario encounters this error because `Foo::Assoc<'a>` could be +implemented by a type that does not use the `'a` parameter, so there is no +guarentee that `X::Assoc<'a>` actually uses `'a`. To fix this we can pass a dummy parameter: ```