Skip to content

Commit 82b26c1

Browse files
authored
Rollup merge of rust-lang#124746 - OliverKillane:E0582-explain-assoc-types-improvement, r=pnkfelix
`rustc --explain E0582` additional example ## Context *From rust-lang#124744* Expands the example for E0582, an error ensuring that lifetime in a function's return type is sufficiently constrained (e.g. actually tied to some input type), to show an additional example where one sees the lifetime occurring syntactically among the relevant function input types, but is nonetheless rejected by rustc because a syntactic occurrence is not always sufficient.
2 parents 8387315 + f3dcf65 commit 82b26c1

File tree

1 file changed

+34
-0
lines changed
  • compiler/rustc_error_codes/src/error_codes

1 file changed

+34
-0
lines changed

compiler/rustc_error_codes/src/error_codes/E0582.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,40 @@ fn bar<F, G>(t: F, u: G)
2727
fn main() { }
2828
```
2929

30+
This error also includes the use of associated types with lifetime parameters.
31+
```compile_fail,E0582
32+
trait Foo {
33+
type Assoc<'a>;
34+
}
35+
36+
struct Bar<X, F>
37+
where
38+
X: Foo,
39+
F: for<'a> Fn(X::Assoc<'a>) -> &'a i32
40+
{
41+
x: X,
42+
f: F
43+
}
44+
```
45+
This is as `Foo::Assoc<'a>` could be implemented by a type that does not use
46+
the `'a` parameter, so there is no guarentee that `X::Assoc<'a>` actually uses
47+
`'a`.
48+
49+
To fix this we can pass a dummy parameter:
50+
```
51+
# trait Foo {
52+
# type Assoc<'a>;
53+
# }
54+
struct Bar<X, F>
55+
where
56+
X: Foo,
57+
F: for<'a> Fn(X::Assoc<'a>, /* dummy */ &'a ()) -> &'a i32
58+
{
59+
x: X,
60+
f: F
61+
}
62+
```
63+
3064
Note: The examples above used to be (erroneously) accepted by the
3165
compiler, but this was since corrected. See [issue #33685] for more
3266
details.

0 commit comments

Comments
 (0)