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

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

Closed
WaffleLapkin opened this issue Nov 13, 2021 · 0 comments · Fixed by #91272
Closed

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

WaffleLapkin opened this issue Nov 13, 2021 · 0 comments · Fixed by #91272
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@WaffleLapkin
Copy link
Member

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.

@WaffleLapkin WaffleLapkin added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 13, 2021
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 6, 2021
…, r=wesleywiser

Print a suggestion when comparing references to primitive types in `const fn`

Fixes rust-lang#90870.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 7, 2021
…, r=wesleywiser

Print a suggestion when comparing references to primitive types in `const fn`

Fixes rust-lang#90870.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 8, 2021
…, r=wesleywiser

Print a suggestion when comparing references to primitive types in `const fn`

Fixes rust-lang#90870.
@bors bors closed this as completed in 871cf2b Dec 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant