Skip to content

Confusing error when comparing references to primitives in a const context #90870

Closed
@WaffleLapkin

Description

@WaffleLapkin

Given the following code: (playground)

const fn f(a: &u8, b: &u8) -> bool {
    a == b
}

The current output is:

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
 --> src/lib.rs:2:5
  |
2 |     a == b
  |     ^^^^^^

Ideally the output should look like:

error[E0015]: calls in constant functions are limited to constant functions, tuple structs and tuple variants
 --> src/lib.rs:2:5
  |
2 |     a == b
  |     ^^^^^^
 note: `a` has type `&u8`, `b` has type `&u8`
 help: consider dereferencing `a` and `b`
  |
2 |     *a == *b
  |     +     +

I've encountered this "in the wild" while trying to implement const version of str::strip_prefix: (playground). Another, a bit simpler example (const fn &[u8] eq): (playground). In both cases references were created implicitly by matching on a slice.

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