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_fn_trait_bound causes rustc not to recognize trait bounds #88071

Closed
js2xxx opened this issue Aug 16, 2021 · 4 comments · Fixed by #89768
Closed

const_fn_trait_bound causes rustc not to recognize trait bounds #88071

js2xxx opened this issue Aug 16, 2021 · 4 comments · Fixed by #89768
Labels
C-bug Category: This is a bug. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@js2xxx
Copy link

js2xxx commented Aug 16, 2021

I replaced lib.rs with this code after creating an empty lib:

#![no_std]
#![feature(const_btree_new)]
#![feature(const_fn_trait_bound)]

use alloc::collections::BTreeMap;

extern crate alloc;

pub struct CustomMap<K, V>(BTreeMap<K, V>);

impl<K, V> CustomMap<K, V>
where
    K: Ord,
{
    pub const fn new() -> Self {
        CustomMap(BTreeMap::new())
    }
}

When running cargo build, I expected to see this happen: built successfully

Instead, this happened:

   Compiling failure-test v0.1.0 (/home/js2xxx/Projects/failure-test)
error[E0277]: the trait bound `K: Ord` is not satisfied
   --> src/lib.rs:16:19
    |
16  |         CustomMap(BTreeMap::new())
    |                   ^^^^^^^^^^^^^^^
    |                   |
    |                   expected an implementor of trait `Ord`
    |                   help: consider borrowing here: `&BTreeMap::new()`
    |
note: required by `BTreeMap::<K, V>::new`
   --> /home/js2xxx/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/alloc/src/collections/btree/map.rs:512:5
    |
512 | /     pub const fn new() -> BTreeMap<K, V>
513 | |     where
514 | |         K: Ord,
    | |_______________^

For more information about this error, try `rustc --explain E0277`.
error: could not compile `failure-test` due to previous error

And when I followed the suggestions it offered, it turned into a type mismatch error and wanted me to cancel the borrowing.

Meta

rustc --version --verbose:

rustc 1.56.0-nightly (8007b506a 2021-08-14)
binary: rustc
commit-hash: 8007b506ac5da629f223b755f5a5391edd5f6d01
commit-date: 2021-08-14
host: x86_64-unknown-linux-gnu
release: 1.56.0-nightly
LLVM version: 12.0.1

The output of RUST_BACKTRACE=1 cargo build is the same as above.

@js2xxx js2xxx added the C-bug Category: This is a bug. label Aug 16, 2021
@jyn514
Copy link
Member

jyn514 commented Aug 16, 2021

So this is odd: if you move the Ord bound to the function it works fine: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=1bec0846e3c540afc36b436b508947f1

and if you add both bounds it stops working, which is pretty incomprehensible to me: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=fcd4b29d4f01dba3eacaaacb0cdbae5e

@jyn514 jyn514 added requires-nightly This issue requires a nightly compiler in some way. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 16, 2021
@jyn514
Copy link
Member

jyn514 commented Aug 16, 2021

This is definitely related to const_fn_trait_bound, because removing const from the function makes it work fine.

@jyn514 jyn514 changed the title Rustc cannot recognize trait bounds const_fn_trait_bound causes rustc not to recognize trait bounds Aug 16, 2021
@Kazurin-775
Copy link

It seems that you don't even need to specify a trait bound in your code to cause this bug. The following code breaks for me:

pub struct PidPool {
    pool: BTreeSet<u32>,
}

impl PidPool {
    pub const fn new() -> PidPool {
        PidPool {
            pool: BTreeSet::new(),
        }
    }
}

which gives the following very weird output on nightly:

error[E0277]: the trait bound `u32: Ord` is not satisfied
   --> src/lib.rs:10:19
    |
10  |             pool: BTreeSet::new(),
    |                   ^^^^^^^^^^^^^ the trait `Ord` is not implemented for `u32`
    |
note: required by `BTreeSet::<T>::new`
help: consider introducing a `where` bound, but there might be an alternative better way to express this requirement
    |
7   | impl PidPool where u32: Ord {
    |              ++++++++++++++

(But anyway, there is a trait bound specified in BTreeSet's source code. Hope this code helps in diagnosing this problem.)

@Kazurin-775
Copy link

Update: I'm not quite sure what happened, but it seems that this problem is already fixed in the latest nightly, and both code snippets can now be compiled without problem. Maybe it's time to close this issue.

(This issue should be some kind of regression introduced at some point between nightly-2021-08-14 and nightly-2021-08-15, according to my tests.)

@jyn514 jyn514 added the E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. label Aug 31, 2021
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 13, 2021
add some more testcases

resolves rust-lang#52893
resolves rust-lang#68295
resolves rust-lang#87750
resolves rust-lang#88071

All these issues have been fixed according to glacier. Just adding a test so it can be closed.

Can anybody tell me why the github keywords do not work? 🤔
Please edit this post if you can fix it.
the8472 added a commit to the8472/rust that referenced this issue Oct 13, 2021
add some more testcases

resolves rust-lang#52893
resolves rust-lang#68295
resolves rust-lang#87750
resolves rust-lang#88071

All these issues have been fixed according to glacier. Just adding a test so it can be closed.

Can anybody tell me why the github keywords do not work? 🤔
Please edit this post if you can fix it.
@bors bors closed this as completed in 0caa616 Oct 13, 2021
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. E-needs-test Call for participation: An issue has been fixed and does not reproduce, but no test has been added. requires-nightly This issue requires a nightly compiler in some way. 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.

3 participants