Skip to content

False negative for "the trait bound K: Ord is not satisfied" #88244

Closed
@cbarrick

Description

@cbarrick

I tried this code:

#![feature(const_btree_new)]

use std::collections::BTreeMap;

trait Proto: Ord {
    const DEFAULT: Self;
}

#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
struct Map<K, V>(BTreeMap<K, V>);

impl<K: Proto, V: Proto> Proto for Map<K, V> {
    const DEFAULT: Map<K, V> = Map(BTreeMap::new());
}

https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=77d9f2d244245a27faedba8a04c6ca14

I expected it to compile.

Instead I get the error:

   Compiling playground v0.0.1 (/playground)
error[E0277]: the trait bound `K: Ord` is not satisfied
   --> src/lib.rs:13:36
    |
13  |     const DEFAULT: Map<K, V> = Map(BTreeMap::new());
    |                                    ^^^^^^^^^^^^^^^
    |                                    |
    |                                    expected an implementor of trait `Ord`
    |                                    help: consider borrowing here: `&BTreeMap::new()`
    |
note: required by `BTreeMap::<K, V>::new`

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

It complains that K: Ord is not satisfied.

But K: Proto and Proto: Ord therefore K: Ord.

The code also fails if I explicitly add the K: Ord trait bound to the impl.

Meta

I tried using the most recent nightly on macOS 11.3 (Big Sur) and on the playground.

$ rustc --version --verbose
rustc 1.56.0-nightly (d3e2578c3 2021-08-21)
binary: rustc
commit-hash: d3e2578c31688619ddc0a10ddf8543bf4ebcba5b
commit-date: 2021-08-21
host: x86_64-apple-darwin
release: 1.56.0-nightly
LLVM version: 13.0.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-associated-itemsArea: Associated items (types, constants & functions)C-bugCategory: This is a bug.F-const_trait_impl`#![feature(const_trait_impl)]`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