]> git.lizzy.rs Git - rust.git/commitdiff
Add proper explanation of error code E0657
authorPankajChaudhary5 <pankajchaudhary172@gmail.com>
Mon, 30 Mar 2020 17:46:44 +0000 (23:16 +0530)
committerpankajchaudhary5 <pankajchaudhary172@gmail.com>
Mon, 13 Apr 2020 08:06:22 +0000 (13:36 +0530)
src/librustc_error_codes/error_codes.rs
src/librustc_error_codes/error_codes/E0657.md [new file with mode: 0644]
src/test/ui/error-codes/E0657.stderr

index 2f0a3fc1d1c386817112c4e17db4b087af0e62c3..6d87627ce3498c0af63232918ff06ad24dbe14e2 100644 (file)
 E0646: include_str!("./error_codes/E0646.md"),
 E0647: include_str!("./error_codes/E0647.md"),
 E0648: include_str!("./error_codes/E0648.md"),
+E0657: include_str!("./error_codes/E0657.md"),
 E0658: include_str!("./error_codes/E0658.md"),
 E0659: include_str!("./error_codes/E0659.md"),
 E0660: include_str!("./error_codes/E0660.md"),
            // used in argument position
     E0640, // infer outlives requirements
 //  E0645, // trait aliases not finished
-    E0657, // `impl Trait` can only capture lifetimes bound at the fn level
     E0667, // `impl Trait` in projections
     E0687, // in-band lifetimes cannot be used in `fn`/`Fn` syntax
     E0688, // in-band lifetimes cannot be mixed with explicit lifetime binders
diff --git a/src/librustc_error_codes/error_codes/E0657.md b/src/librustc_error_codes/error_codes/E0657.md
new file mode 100644 (file)
index 0000000..7fe48c5
--- /dev/null
@@ -0,0 +1,57 @@
+A lifetime bound on a trait implementation was captured at an incorrect place.
+
+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(())
+}
+
+struct Foo;
+impl Foo {
+    fn impl_fn_capture_hrtb_in_impl_trait()
+        -> Box<for<'a> Id<impl Lt<'a>>> // error!
+    {
+        Box::new(())
+    }
+}
+```
+
+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.
+
+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(())
+}
+
+struct Foo;
+impl Foo {
+    fn impl_fn_capture_hrtb_in_impl_trait<'b>()
+        -> Box<for<'a> Id<impl Lt<'b>>> // ok!
+    {
+        Box::new(())
+    }
+}
+```
index b24b413600c6c0d194c1bb6563349bbb7b24e173..df76b45a5891f9e5a18b702205c503fb789c672f 100644 (file)
@@ -12,3 +12,4 @@ LL |         -> Box<for<'a> Id<impl Lt<'a>>>
 
 error: aborting due to 2 previous errors
 
+For more information about this error, try `rustc --explain E0657`.