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

Trait resolution with index operator and mixed lifetimes #92603

Closed
Jarcho opened this issue Jan 6, 2022 · 1 comment
Closed

Trait resolution with index operator and mixed lifetimes #92603

Jarcho opened this issue Jan 6, 2022 · 1 comment
Labels
A-borrow-checker Area: The borrow checker A-lifetimes Area: Lifetimes / regions A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Jarcho
Copy link
Contributor

Jarcho commented Jan 6, 2022

As far as I can tell the error generated by this is wrong.

fn index_op<'a>(map: &std::collections::HashMap<&'static str, ()>, key: &'a str) {
    map[&key];
}
error[E0759]: `key` has lifetime `'a` but it needs to satisfy a `'static` lifetime requirement
 --> src/main.rs:2:9
  |
1 | fn index_op<'a>(map: &std::collections::HashMap<&'static str, ()>, key: &'a str) {
  |                                                                         ------- this data with lifetime `'a`...
2 |     map[&key];
  |         ^^^^ ...is captured here, requiring it to live as long as `'static`

If the lifetime comes from an impl block, a completely different error message is given.

trait Foo<'a> {
    type Key;
    fn index_op(map: &std::collections::HashMap<&'static str, ()>, key: Self::Key);
}
impl<'a> Foo<'a> for () {
    type Key = &'a str;
    fn index_op(map: &std::collections::HashMap<&'static str, ()>, key: Self::Key) {
        map[&key];
    }
}
error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements
 --> src/main.rs:7:84
  |
7 |       fn index_op(map: &std::collections::HashMap<&'static str, ()>, key: Self::Key) {
  |  ____________________________________________________________________________________^
8 | |         map[&key];
9 | |     }
  | |_____^
  |
note: first, the lifetime cannot outlive the lifetime `'a` as defined here...
 --> src/main.rs:5:6
  |
5 | impl<'a> Foo<'a> for () {
  |      ^^
note: ...so that the types are compatible
 --> src/main.rs:7:84
  |
7 |       fn index_op(map: &std::collections::HashMap<&'static str, ()>, key: Self::Key) {
  |  ____________________________________________________________________________________^
8 | |         map[&key];
9 | |     }
  | |_____^
  = note: expected `Foo<'a>`
             found `Foo<'_>`
  = note: but, the lifetime must be valid for the static lifetime...
note: ...so that the types are compatible
 --> src/main.rs:8:9
  |
8 |         map[&key];
  |         ^^^^^^^^^
  = note: expected `Borrow<&str>`
             found `Borrow<&'static str>`

Which is saying &'static str doesn't implement Borrow<&'a str>, which is technically true. The closest matching impl is impl<T> Borrow<T> for T which would mean it implements Borrow<&'static str> not Borrow<&'a str>. My understanding is trait resolution is not supposed to take lifetimes into account when determining if a type implements a trait which would make this error incorrect.

When either key is a &'static str, or the index method is used directly the errors do not occur.

use std::ops::Index;

trait Foo<'a> {
    type Key;
    fn index_op(map: &std::collections::HashMap<&'static str, ()>, key: Self::Key);
}
impl<'a> Foo<'a> for () {
    type Key = &'static str;
    fn index_op(map: &std::collections::HashMap<&'static str, ()>, key: Self::Key) {
        map[&key];
    }
}

fn index_op(map: &std::collections::HashMap<&'static str, ()>, key: &'static str) {
    map[&key];
}

and

use std::ops::Index;

trait Foo<'a> {
    type Key;
    fn index_op(map: &std::collections::HashMap<&'static str, ()>, key: Self::Key);
}
impl<'a> Foo<'a> for () {
    type Key = &'a str;
    fn index_op(map: &std::collections::HashMap<&'static str, ()>, key: Self::Key) {
        map.index(&key);
    }
}

fn index_op<'a>(map: &std::collections::HashMap<&'static str, ()>, key: &'a str) {
    map.index(&key);
}

This came up while looking at rust-lang/rust-clippy#1725

Tested on the playground (stable 1.57)

@Jarcho Jarcho added the C-bug Category: This is a bug. label Jan 6, 2022
@fmease fmease added A-lifetimes Area: Lifetimes / regions A-borrow-checker Area: The borrow checker T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-NLL Area: Non-lexical lifetimes (NLL) and removed needs-triage-legacy labels Jan 24, 2024
@fmease
Copy link
Member

fmease commented Jan 24, 2024

Both snippets now compile successfully, closing as fixed. Please notify me if I missed something and I should reopen the issue.

@fmease fmease closed this as completed Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-borrow-checker Area: The borrow checker A-lifetimes Area: Lifetimes / regions A-NLL Area: Non-lexical lifetimes (NLL) C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants