Skip to content

negative trait bounds give nonsense error #125297

Closed as not planned
Closed as not planned
@lolbinarycat

Description

I tried this code:

#![feature(negative_bounds)]

struct Foo<T> {
	v: T,
}

impl<T: SomeTrait + Default> Foo<T> {
	fn new() -> Self {
		Foo{ v: T::default() }
	}
}

trait SomeTrait: !SomeOtherTrait {}
trait SomeOtherTrait {}
impl SomeOtherTrait for u8 {}
impl<T: !SomeOtherTrait> SomeTrait for T {}

fn main() {
    let _ = Foo::<u8>::new();
}

I expected to see this happen: the code should build successfully.
Instead, this happened: nonsense compiler error

also note that if new() is not available due to any trait bounds, the compiler will recommend calling new().

Meta

rustc --version --verbose:

rustc 1.80.0-nightly (b1ec1bd65 2024-05-18)
binary: rustc
commit-hash: b1ec1bd65f89c1375d2cf2fb733a87ef390276d3
commit-date: 2024-05-18
host: x86_64-unknown-linux-gnu
release: 1.80.0-nightly
LLVM version: 18.1.4
Backtrace

   Compiling negative-bound-bug v0.1.0 (/home/binarycat/src/rs/negative-bound-bug)
warning: the feature `negative_bounds` is internal to the compiler or standard library
 --> src/main.rs:1:12
  |
1 | #![feature(negative_bounds)]
  |            ^^^^^^^^^^^^^^^
  |
  = note: using it is strongly discouraged
  = note: `#[warn(internal_features)]` on by default

error[E0599]: the function or associated item `new` exists for struct `Foo<u8>`, but its trait bounds were not satisfied
  --> src/main.rs:19:24
   |
3  | struct Foo<T> {
   | ------------- function or associated item `new` not found for this struct
...
19 |     let _ = Foo::<u8>::new();
   |                        ^^^ function or associated item cannot be called on `Foo<u8>` due to unsatisfied trait bounds
   |
note: if you're trying to build a new `Foo<u8>`, consider using `Foo::<T>::new` which returns `Foo<_>`
  --> src/main.rs:8:2
   |
8  |     fn new() -> Self {
   |     ^^^^^^^^^^^^^^^^
note: trait bound `u8: SomeOtherTrait` was not satisfied
  --> src/main.rs:16:9
   |
16 | impl<T: !SomeOtherTrait> SomeTrait for T {}
   |         ^^^^^^^^^^^^^^^  ---------     -
   |         |
   |         unsatisfied trait bound introduced here

For more information about this error, try `rustc --explain E0599`.
warning: `negative-bound-bug` (bin "negative-bound-bug") generated 1 warning
error: could not compile `negative-bound-bug` (bin "negative-bound-bug") due to 1 previous error; 1 warning emitted

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions