Skip to content

const_fn_trait_bound causes rustc not to recognize trait bounds #88071

Closed
@js2xxx

Description

@js2xxx

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-bugCategory: This is a bug.E-needs-testCall for participation: An issue has been fixed and does not reproduce, but no test has been added.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.requires-nightlyThis issue requires a nightly compiler in some way.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions