Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
plaes opened this issue Apr 3, 2022 · 1 comment · Fixed by #95654
Closed

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

plaes opened this issue Apr 3, 2022 · 1 comment · Fixed by #95654
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@plaes
Copy link

plaes commented Apr 3, 2022

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)

@plaes plaes added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 3, 2022
@plaes
Copy link
Author

plaes commented Apr 3, 2022

So, this is where the suggestion comes from. Unfortunately I can't yet find a place to move the value before the const:

diff --git a/compiler/rustc_resolve/src/late/diagnostics.rs b/compiler/rustc_resolve/src/late/diagnostics.rs
index b05ec654997..af487381702 100644
--- a/compiler/rustc_resolve/src/late/diagnostics.rs
+++ b/compiler/rustc_resolve/src/late/diagnostics.rs
@@ -2166,7 +2166,15 @@ impl<'tcx> LifetimeContext<'_, 'tcx> {
                                     }
                             )
                         }) {
-                            (param.span.shrink_to_lo(), "'a, ".to_string())
+                            match param.kind {
+                                hir::GenericParamKind::Const { .. }  => {
+                                    println!("TODO: {:#?}", param);
+                                    (param.span.shrink_to_lo(), "'a, ".to_string())
+                                },
+                                _ => {
+                                    (param.span.shrink_to_lo(), "'a, ".to_string())
+                                }
+                            }
                         } else {
                             (generics.span, "<'a>".to_string())
                         }

@notriddle notriddle self-assigned this Apr 4, 2022
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 5, 2022
…davidtwco

diagnostics: use correct span for const generics

Fixes rust-lang#95616
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 5, 2022
…davidtwco

diagnostics: use correct span for const generics

Fixes rust-lang#95616
Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue Apr 5, 2022
…davidtwco

diagnostics: use correct span for const generics

Fixes rust-lang#95616
@bors bors closed this as completed in 6ece80f Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants