Skip to content

Invalid transformation to const function with deref #14091

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

Closed
SUPERCILEX opened this issue Jan 28, 2025 · 4 comments · Fixed by #14294
Closed

Invalid transformation to const function with deref #14091

SUPERCILEX opened this issue Jan 28, 2025 · 4 comments · Fixed by #14294
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have

Comments

@SUPERCILEX
Copy link

SUPERCILEX commented Jan 28, 2025

Summary

struct BucketSlotGuard<'a> {
    id: u32,
    free_list: &'a mut Vec<u32>,
}

impl BucketSlotGuard<'_> {
    fn into_inner(self) -> u32 {
        let this = ManuallyDrop::new(self);
        this.id
    }
}
The following errors were reported:
error[E0015]: cannot perform non-const deref coercion on `std::mem::ManuallyDrop<allocator::BucketSlotGuard<'_>>` in constant functions
   --> server/src/allocator.rs:168:9
    |
168 |         this.id
    |         ^^^^^^^
    |
    = note: attempting to deref into `allocator::BucketSlotGuard<'_>`
note: deref defined here
   --> /home/asaveau/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/manually_drop.rs:262:5
    |
262 |     type Target = T;
    |     ^^^^^^^^^^^
note: impl defined here, but it is not `const`
   --> /home/asaveau/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/manually_drop.rs:261:1
    |
261 | impl<T: ?Sized> Deref for ManuallyDrop<T> {
    | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

warning: this could be a `const fn`
   --> server/src/allocator.rs:166:5
    |
166 | /     fn into_inner(self) -> u32 {
167 | |         let this = ManuallyDrop::new(self);
168 | |         this.id
169 | |     }
    | |_____^
    |
    = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
    = note: `-W clippy::missing-const-for-fn` implied by `-W clippy::nursery`
    = help: to override `-W clippy::nursery` add `#[allow(clippy::missing_const_for_fn)]`
help: make the function `const`
    |
166 |     const fn into_inner(self) -> u32 {
    |     +++++

Version

rustc 1.86.0-nightly (2f348cb7c 2025-01-27)
binary: rustc
commit-hash: 2f348cb7ce4063fa4eb40038e6ada3c5214717bd
commit-date: 2025-01-27
host: x86_64-unknown-linux-gnu
release: 1.86.0-nightly
LLVM version: 19.1.7

Error output

Backtrace

<backtrace>

@SUPERCILEX SUPERCILEX added C-bug Category: Clippy is not doing the correct thing I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️ labels Jan 28, 2025
@matthiaskrgr
Copy link
Member

another example from rustc

// Regression test for #30104

//@ check-pass
#![allow(unused)]

use std::ops::{Deref, DerefMut};

fn box_two_field(v: &mut Box<(i32, i32)>) {
    let _a = &mut v.0;
    let _b = &mut v.1;
}

fn box_destructure(v: &mut Box<(i32, i32)>) {
    let (ref mut _head, ref mut _tail) = **v;
}

struct Wrap<T>(T);

impl<T> Deref for Wrap<T> {
    type Target = T;
    fn deref(&self) -> &T {
        &self.0
    }
}

impl<T> DerefMut for Wrap<T> {
    fn deref_mut(&mut self) -> &mut T {
        &mut self.0
    }
}

fn smart_two_field(v: &mut Wrap<(i32, i32)>) {
    let _a = &mut v.0;
    let _b = &mut v.1;
}

fn smart_destructure(v: &mut Wrap<(i32, i32)>) {
    let (ref mut _head, ref mut _tail) = **v;
}

fn main() {}
warning: this could be a `const fn`
  --> ./tests/ui/nll/issue-30104.rs:32:1
   |
32 | / fn smart_two_field(v: &mut Wrap<(i32, i32)>) {
33 | |     let _a = &mut v.0;
34 | |     let _b = &mut v.1;
35 | | }
   | |_^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
   = note: requested on the command line with `--force-warn clippy::missing-const-for-fn`
help: make the function `const`
   |
32 | const fn smart_two_field(v: &mut Wrap<(i32, i32)>) {
   | +++++

warning: this could be a `const fn`
  --> ./tests/ui/nll/issue-30104.rs:37:1
   |
37 | / fn smart_destructure(v: &mut Wrap<(i32, i32)>) {
38 | |     let (ref mut _head, ref mut _tail) = **v;
39 | | }
   | |_^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_const_for_fn
help: make the function `const`
   |
37 | const fn smart_destructure(v: &mut Wrap<(i32, i32)>) {
   | +++++
error[E0015]: cannot call non-const method `<Wrap<(i32, i32)> as std::ops::DerefMut>::deref_mut` in constant functions
  --> ./tests/ui/nll/issue-30104.rs:34:19
   |
34 |     let _b = &mut v.1;
   |                   ^^^
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error[E0015]: cannot call non-const method `<Wrap<(i32, i32)> as std::ops::DerefMut>::deref_mut` in constant functions
  --> ./tests/ui/nll/issue-30104.rs:38:42
   |
38 |     let (ref mut _head, ref mut _tail) = **v;
   |                                          ^^^
   |
   = note: calls in constant functions are limited to constant functions, tuple structs and tuple variants

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0015`.

@y21 y21 added I-false-positive Issue: The lint was triggered on code it shouldn't have and removed I-ICE Issue: Clippy panicked, giving an Internal Compilation Error (ICE) ❄️ labels Feb 13, 2025
@larseggert
Copy link

We're seeing this false positive in a bunch of places in mozilla/neqo, e.g., in https://github.com/mozilla/neqo/blob/7005337d9bcf347d79f8cad28605f6c539083824/neqo-transport/src/connection/mod.rs#L198-L203

larseggert added a commit to larseggert/neqo that referenced this issue Feb 24, 2025
Ans suppress some false positives, see rust-lang/rust-clippy#14091
martinthomson pushed a commit to mozilla/neqo that referenced this issue Feb 24, 2025
Ans suppress some false positives, see rust-lang/rust-clippy#14091
@profetia
Copy link
Contributor

profetia commented Mar 6, 2025

@rustbot claim

@profetia
Copy link
Contributor

profetia commented Mar 6, 2025

Should also be fixed in #14294.

github-merge-queue bot pushed a commit that referenced this issue Mar 20, 2025
Closes #14020
Closes #14290
Closes #14091

Add checks for unstable const traits.

changelog: [`missing_const_for_fn`] fix FP on unstable const traits
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants