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

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

Closed
beepster4096 opened this issue Jan 14, 2022 · 8 comments · Fixed by #93028
Closed

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

beepster4096 opened this issue Jan 14, 2022 · 8 comments · Fixed by #93028
Assignees
Labels
C-bug Category: This is a bug.

Comments

@beepster4096
Copy link
Contributor

beepster4096 commented Jan 14, 2022

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

@beepster4096 beepster4096 added the C-bug Category: This is a bug. label Jan 14, 2022
@lilasta
Copy link
Contributor

lilasta commented Jan 14, 2022

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
Copy link
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
Copy link
Member

cc @fee1-dead

@beepster4096
Copy link
Contributor Author

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
Copy link
Contributor Author

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
Copy link
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
Copy link
Member

@compiler-errors
Copy link
Member

compiler-errors commented Jan 18, 2022

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
Labels
C-bug Category: This is a bug.
Projects
None yet
4 participants