Skip to content

Inference failure sometimes results in things being considered functions. #89995

Closed
@kythyria

Description

@kythyria

Given the following code (playground):

use std::collections::HashSet;
use std::rc::Rc;

fn intern(cache: &mut HashSet<Rc<str>>, data: &str) -> Rc<str> {
   match cache.get(data) {
        Some(s) => s.clone(),
        None => {
            let d = Rc::from(data);
            cache.insert(d.clone());
            d
        }
    }
}

The current output is:

error[E0282]: type annotations needed
 --> src/lib.rs:9:28
  |
9 |             cache.insert(d.clone());
  |                            ^^^^^ cannot infer type
  |
  = note: type must be known at this point

error[E0599]: no method named `clone` found for struct `Rc<_>` in the current scope
 --> src/lib.rs:9:28
  |
9 |             cache.insert(d.clone());
  |                            ^^^^^ method not found in `Rc<_>`
  |
  = note: `d` is a function, perhaps you wish to call it

I'm surprised it can't infer the type of d correctly, considering there are multiple indications it must be Rc<str> (and R-A does so). Maybe it's confused by Rc<&str> being a thing?

The final note is straight up perplexing, however: where on earth is it getting the idea that that's a function from? That should probably be squashed.

Edit: even shorter reproduction (playground):

use std::rc::Rc;
fn intern(data: &str) -> Rc<str> {
    Rc::from(data).clone()
}

Metadata

Metadata

Assignees

No one assigned

    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