diff options
| author | Michael Goulet <michael@errs.io> | 2024-03-07 15:44:07 +0000 |
|---|---|---|
| committer | Michael Goulet <michael@errs.io> | 2024-03-08 19:08:13 +0000 |
| commit | ffd30e0a6939d7d25c4ac28bfac4b4e367adcd59 (patch) | |
| tree | 079aa5d6de0958fa94d642c53a3c8077baf65153 /compiler/rustc_error_codes | |
| parent | 74acabe9b042ea8c42862ee29aca2a8b7d333644 (diff) | |
| download | rust-ffd30e0a6939d7d25c4ac28bfac4b4e367adcd59.tar.gz rust-ffd30e0a6939d7d25c4ac28bfac4b4e367adcd59.zip | |
Improve error message for opaque captures
Diffstat (limited to 'compiler/rustc_error_codes')
| -rw-r--r-- | compiler/rustc_error_codes/src/error_codes/E0657.md | 59 |
1 files changed, 14 insertions, 45 deletions
diff --git a/compiler/rustc_error_codes/src/error_codes/E0657.md b/compiler/rustc_error_codes/src/error_codes/E0657.md index 7fe48c51147..477d8e8bb6d 100644 --- a/compiler/rustc_error_codes/src/error_codes/E0657.md +++ b/compiler/rustc_error_codes/src/error_codes/E0657.md @@ -1,57 +1,26 @@ -A lifetime bound on a trait implementation was captured at an incorrect place. +An `impl Trait` captured a higher-ranked lifetime, which is not supported. + +Currently, `impl Trait` types are only allowed to capture lifetimes from +their parent items, and not from any `for<'a>` binders in scope. Erroneous code example: ```compile_fail,E0657 -trait Id<T> {} -trait Lt<'a> {} - -impl<'a> Lt<'a> for () {} -impl<T> Id<T> for T {} - -fn free_fn_capture_hrtb_in_impl_trait() - -> Box<for<'a> Id<impl Lt<'a>>> // error! -{ - Box::new(()) -} +trait BorrowInto<'a> { + type Target; -struct Foo; -impl Foo { - fn impl_fn_capture_hrtb_in_impl_trait() - -> Box<for<'a> Id<impl Lt<'a>>> // error! - { - Box::new(()) - } + fn borrow_into(&'a self) -> Self::Target; } -``` -Here, you have used the inappropriate lifetime in the `impl Trait`, -The `impl Trait` can only capture lifetimes bound at the fn or impl -level. +impl<'a> BorrowInto<'a> for () { + type Target = &'a (); -To fix this we have to define the lifetime at the function or impl -level and use that lifetime in the `impl Trait`. For example you can -define the lifetime at the function: - -``` -trait Id<T> {} -trait Lt<'a> {} - -impl<'a> Lt<'a> for () {} -impl<T> Id<T> for T {} - -fn free_fn_capture_hrtb_in_impl_trait<'b>() - -> Box<for<'a> Id<impl Lt<'b>>> // ok! -{ - Box::new(()) + fn borrow_into(&'a self) -> Self::Target { + self + } } -struct Foo; -impl Foo { - fn impl_fn_capture_hrtb_in_impl_trait<'b>() - -> Box<for<'a> Id<impl Lt<'b>>> // ok! - { - Box::new(()) - } +fn opaque() -> impl for<'a> BorrowInto<'a, Target = impl Sized + 'a> { + () } ``` |
