Skip to content

Confusing interaction between deref coercion and unknown type #56036

Closed
@euclio

Description

@euclio

I wrote something like the following code:

// fn function_that_doesnt_exist() -> String {
//    String::new()
// }

fn test(s: &str) {
    println!("{}", s);
}

fn test2(s: String) {
    println!("{}", s);
}

fn main() {
    let s = function_that_doesnt_exist();
    test(&s);
    test2(s);
}

https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=4be367b5c1d5a1d3d6eb20001c171825

Notice that the actual error is the missing function_that_doesnt_exist, but the compiler confusingly reports errors about str not having a size known at compile time, presumably because it infers the type of s as str. When I was writing this code, I missed the message about the missing function, so I was left scratching my head for a bit. It might be nice to silence such errors when the type of the variable is unknowable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.D-papercutDiagnostics: An error or lint that needs small tweaks.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-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