Skip to content

~const bounds do not work when impling const Drop #92881

Closed
@beepster4096

Description

@beepster4096
Contributor

I encountered this issue while trying to remove box_free.

I tried this code:

#![feature(const_trait_impl, const_fn_trait_bound, const_mut_refs)]

trait X {
    fn x();
}

struct Y<T: X>(T);

impl<T: ~const X> const Drop for Y<T> {
    fn drop(&mut self) {
        T::x();
    }
}

I expected the code to compile.

Instead, I got the following error:

error[E0367]: `Drop` impl requires `T: X` but the struct it is implemented for does not
 --> src/lib.rs:9:9
  |
9 | impl<T: ~const X> const Drop for Y<T> {
  |         ^^^^^^^^
  |
note: the implementor must specify the same requirement
 --> src/lib.rs:7:1
  |
7 | struct Y<T: X>(T);
  | ^^^^^^^^^^^^^^^^^^

Meta

rustc --version --verbose:

rustc 1.60.0-nightly (22e491ac7 2022-01-13)
binary: rustc
commit-hash: 22e491ac7ed454d34669151a8b6464cb643c9b41
commit-date: 2022-01-13
host: x86_64-pc-windows-msvc
release: 1.60.0-nightly
LLVM version: 13.0.0

Activity

lilasta

lilasta commented on Jan 14, 2022

@lilasta
Contributor

workaround: play

#![feature(const_trait_impl, const_fn_trait_bound, const_mut_refs)]

trait X {
    fn x();
}

struct Y<T: X>(T);

impl<T: X> const Drop for Y<T> {
    fn drop(&mut self)
    where
        T: ~const X
    {
        T::x();
    }
}
compiler-errors

compiler-errors commented on Jan 14, 2022

@compiler-errors
Member

Might be enough to just replace ConstIfConst with NotConst when comparing the list of predicates in dropck, if someone wants to try that.

crlf0710

crlf0710 commented on Jan 14, 2022

@crlf0710
Member
beepster4096

beepster4096 commented on Jan 15, 2022

@beepster4096
ContributorAuthor

workaround: play

This workaround causes the type to impl ~const Drop when it shouldn't causing errors when interpreting instead of during type checking. (playground) This needs to be fixed too.

Might be enough to just replace ConstIfConst with NotConst when comparing the list of predicates in dropck, if someone wants to try that.

This isn't enough. The constness of the bound seems to not get checked at all.

beepster4096

beepster4096 commented on Jan 15, 2022

@beepster4096
ContributorAuthor

I'm not exactly sure how trait selection works, but I think the problem may be here, where only the constness of the impl is checked, and not any of the bounds.

compiler-errors

compiler-errors commented on Jan 15, 2022

@compiler-errors
Member

@drmeepster you wouldn't believe that the same exact line was on my clipboard, and I was going to say that it was somewhere around assemble_const_drop_candidates 😆

Yeah, it seems like we need to enforce that the ~const bounds on the drop impl are actually const during confirmation.

compiler-errors

compiler-errors commented on Jan 15, 2022

@compiler-errors
Member
compiler-errors

compiler-errors commented on Jan 18, 2022

@compiler-errors
Member

I'll take a stab at this, if nobody else is planning on it already!
@rustbot claim

cc @fee1-dead:
I'm planning on rewriting assemble_const_drop_candidates to prefer selecting a user-provided impl const Drop if it is provided (e.g. by an ADT), and fleshing out a real confirm_const_drop_candidate so we make sure to check those obligations that come from the user-provided impl const Drop.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

C-bugCategory: This is a bug.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

    Participants

    @crlf0710@compiler-errors@beepster4096@lilasta

    Issue actions

      ~const bounds do not work when impling const Drop · Issue #92881 · rust-lang/rust