diff options
| author | Josh White <jwhite927@gmail.com> | 2020-02-08 14:24:35 -0500 |
|---|---|---|
| committer | Josh White <jwhite927@gmail.com> | 2020-02-08 14:24:35 -0500 |
| commit | 8c351182de955b3dea73681a1e3b3eb6afb0edca (patch) | |
| tree | f3f5e3f62230ef2c6806218b8a062924606266eb | |
| parent | a804d476a670e29f2ac55d97c72ae0bd456b3f84 (diff) | |
| download | rust-8c351182de955b3dea73681a1e3b3eb6afb0edca.tar.gz rust-8c351182de955b3dea73681a1e3b3eb6afb0edca.zip | |
Corrected E0637.md based on test failures
| -rw-r--r-- | src/librustc_error_codes/error_codes/E0637.md | 73 |
1 files changed, 23 insertions, 50 deletions
diff --git a/src/librustc_error_codes/error_codes/E0637.md b/src/librustc_error_codes/error_codes/E0637.md index a79a2fda1a0..f21b1749ec7 100644 --- a/src/librustc_error_codes/error_codes/E0637.md +++ b/src/librustc_error_codes/error_codes/E0637.md @@ -1,59 +1,32 @@ -An underscore `_` character has been used as the identifier for a lifetime, -or a const generic has been borrowed without an explicit lifetime. +An underscore `_` character has been used as the identifier for a lifetime. -Erroneous example with an underscore: +Erroneous example: ```compile_fail,E0106,E0637 -fn foo<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str {} - // ^^ `'_` is a reserved lifetime name -``` -Lifetimes are named with `'ident`, where ident is the name of the lifetime or -loop. The `_` character, which represents the ignore pattern, cannot be used -as the identifier because it is a reserved lifetime name. To fix -this, use a lowercase letter, or a series of lowercase letters as the lifetime -identifier. Often a single lowercase letter, such as `'a`, is sufficient. For -more information, see [the book][bk-no]. - -Corrected underscore example: -``` -fn foo<'a>(str1: &'a str, str2: &'a str) -> &'a str {} -``` - -Erroneous example with const generic: -```compile_fail,E0261,E0637,E0658 -struct A<const N: &u8>; -//~^ ERROR `&` without an explicit lifetime name cannot be used here -trait B {} - -impl<const N: &u8> A<N> { -//~^ ERROR `&` without an explicit lifetime name cannot be used here - fn foo<const M: &u8>(&self) {} - //~^ ERROR `&` without an explicit lifetime name cannot be used here +fn longest<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str { + //^^ `'_` is a reserved lifetime name + if str1.len() > str2.len() { + str1 + } else { + str2 + } } - -impl<const N: &u8> B for A<N> {} -//~^ ERROR `&` without an explicit lifetime name cannot be used here - -fn bar<const N: &u8>() {} -//~^ ERROR `&` without an explicit lifetime name cannot be used here ``` +`'_`, cannot be used as a lifetime identifier because it is a reserved for the +anonymous lifetime. To fix this, use a lowercase letter such as 'a, or a series +of lowercase letters such as `'foo`. For more information, see [the book][bk-no]. +For more information on using the anonymous lifetime in rust nightly, see [the +nightly book][bk-al]. -Const generics cannot be borrowed without specifying a lifetime.The -compiler handles memory allocation of constants differently than that of -variables and it cannot infer the lifetime of the borrowed constant. -To fix this, explicitly specify a lifetime for the const generic. - -Corrected const generic example: +Corrected example: ``` -struct A<const N: &'a u8>; - -trait B {} - -impl<const N: &'a u8> A<N> { - fn foo<const M: &'a u8>(&self) {} +fn longest<'a>(str1: &'a str, str2: &'a str) -> &'a str { + if str1.len() > str2.len() { + str1 + } else { + str2 + } } - -impl<const N: &'a u8> B for A<N> {} - -fn bar<const N: &'a u8>() {} ``` + [bk-no]: https://doc.rust-lang.org/book/appendix-02-operators.html#non-operator-symbols +[bk-al]: https://doc.rust-lang.org/nightly/edition-guide/rust-2018/ownership-and-lifetimes/the-anonymous-lifetime.html |
