Skip to content

Misleading message when attempting to call methods on a raw pointer #21596

Closed
@kornelski

Description

@kornelski

When trying to call a method on a raw pointer Rust compiler says there's no such method.

It would be better to say that there is such method, but the pointer has to be explicitly dereferenced first.

let x = 8u8;
let y = &x;
let z: *const u8 = &x;
println!("{} {}", 
    y.to_string(), // OK
    z.to_string()  // Not OK
);

error: type *const u8 does not implement any method in scope named to_string

This is technically correct, but unexpected and help hint for this error suggests wrong solution.

I've read this message as "yada yada no such method", so I went to check whether I've spelled the method correctly, whether I've put the method in the appropriate impl block, etc. — all completely irrelevant to this error.

I'd find something like that more helpful:

error: method to_string requires type u8, but *const u8 cannot be automatically dereferenced. hint: use as_ref()

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.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.P-lowLow priorityT-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