diff options
| author | Josh White <jwhite927@gmail.com> | 2020-02-07 12:44:31 -0500 |
|---|---|---|
| committer | Josh White <jwhite927@gmail.com> | 2020-02-07 12:44:31 -0500 |
| commit | 8b77f8688e9436c6b35d5746e3bb79e20c67567d (patch) | |
| tree | cda0107d99d2c7e590625fd381dc1f57845506d0 /src/librustc_error_codes/error_codes | |
| parent | 78df44655aa8547baa25ee9ca49699ad82c7f76d (diff) | |
performed --bless of 15 ui tests affected
Diffstat (limited to 'src/librustc_error_codes/error_codes')
| -rw-r--r-- | src/librustc_error_codes/error_codes/E0637.md | 69 |
1 files changed, 50 insertions, 19 deletions
diff --git a/src/librustc_error_codes/error_codes/E0637.md b/src/librustc_error_codes/error_codes/E0637.md index 978cf273c94..ba81e42ce08 100644 --- a/src/librustc_error_codes/error_codes/E0637.md +++ b/src/librustc_error_codes/error_codes/E0637.md @@ -1,31 +1,62 @@ -An underscore `_` character or a numeric literal `u8`, `i32`, `f64`, etc has -been used as the identifier for a lifetime. +An underscore `_` character has been used as the identifier for a lifetime, +or a const generic has been borrowed without an explicit lifetime. -Erroneous code example 1: +Erroneous example with an underscore: ``` -fn some_function<'_>(str1: &'_ str, str2: &'_ str) -> &'_ str { - //Some code -} +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 <'a>(str1: &'a str, str2: &'a str) -> &'a str {} ``` -or Erroneous code example 2: + +Erroneous example with const generic: ``` -fn some_function<'u8>(str1: &'u8 str, str2: &'u8 str) -> &'u8 str { - //Some code +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 +} + +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 } ``` -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. Numeric literals are -also invalid lifetime identifiers and will cause this error to be thrown. To fix -this, use 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]. +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 code example: +Corrected const generic example: ``` -fn some_function<'a>(str1: &'a str, str2: &'a str) -> &'a str { - //Some code +struct A<const N: &'a u8>; + +trait B {} + +impl<const N: &'a u8> A<N> { + fn foo<const M: &'a u8>(&self) {} +} + +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 |
