Skip to content

Invalid suggestion for missing lifetime specifier when using const generics #95616

Closed
@plaes

Description

@plaes

Given the following code: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=de3e0cab5a0a3a72f2840b824066f6f1

// OK: fn buggy_const<'a, const N: usize>(_a: &'a Option<[u8; N]>, _f: &str) -> &'a str {
fn buggy_const<const N: usize>(_a: &Option<[u8; N]>, _f: &str) -> &str {
    return "";
}

fn main() {
    buggy_const(&Some([69,69,69,69,0]), "test");
}

The current output is:

2 | fn buggy_const<const N: usize>(_a: &Option<[u8; N]>, _f: &str) -> &str {
  |                                    ----------------      ----     ^ expected named lifetime parameter
  |
  = help: this function's return type contains a borrowed value, but the signature does not say whether it is borrowed from `arr` or `field`
help: consider introducing a named lifetime parameter
  |
2 | fn buggy_const<const 'a, N: usize>(_a: &'a Option<[u8; N]>, _f: &'a str) -> &'a str {
  |                      +++                ++                       ++          ++

Ideally the output should look like:

2 | fn buggy_const<'a, const N: usize>(_a: &'a Option<[u8; N]>, _f: &'a str) -> &'a str {
  |                +++                      ++                       ++          ++

This issue is present with both stable (1.59) and nightly: 1.61.0-nightly -(2022-04-02 76d770ac21d9521db6a9)

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions