Skip to content

Commit

Permalink
Rollup merge of rust-lang#124746 - OliverKillane:E0582-explain-assoc-…
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
jieyouxu committed May 14, 2024
2 parents 8387315 + f3dcf65 commit 82b26c1
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0582.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@ fn bar<F, G>(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<X, F>
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<X, F>
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.
Expand Down

0 comments on commit 82b26c1

Please sign in to comment.