From 8c351182de955b3dea73681a1e3b3eb6afb0edca Mon Sep 17 00:00:00 2001 From: Josh White Date: Sat, 8 Feb 2020 14:24:35 -0500 Subject: Corrected E0637.md based on test failures --- src/librustc_error_codes/error_codes/E0637.md | 73 +++++++++------------------ 1 file changed, 23 insertions(+), 50 deletions(-) (limited to 'src/librustc_error_codes/error_codes') 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; -//~^ ERROR `&` without an explicit lifetime name cannot be used here -trait B {} - -impl A { -//~^ ERROR `&` without an explicit lifetime name cannot be used here - fn foo(&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 B for A {} -//~^ ERROR `&` without an explicit lifetime name cannot be used here - -fn bar() {} -//~^ 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; - -trait B {} - -impl A { - fn foo(&self) {} +fn longest<'a>(str1: &'a str, str2: &'a str) -> &'a str { + if str1.len() > str2.len() { + str1 + } else { + str2 + } } - -impl B for A {} - -fn bar() {} ``` + [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 -- cgit 1.4.1-3-g733a5