Skip to content

Commit

Permalink
improve and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
arielb1 committed Sep 24, 2019
1 parent 5d79e8c commit 9a94ecd
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 1 deletion.
File renamed without changes.
@@ -1,5 +1,5 @@
error[E0119]: conflicting implementations of trait `MyTrait` for type `MyFoo`:
--> $DIR/never-impl-is-reserved.rs:10:1
--> $DIR/never-from-impl-is-reserved.rs:10:1
|
LL | impl MyTrait for MyFoo {}
| ---------------------- first implementation here
Expand Down
@@ -0,0 +1,16 @@
// compile-fail

// check that reservation impls are accounted for in negative reasoning.

#![feature(rustc_attrs)]

trait MyTrait {}
#[rustc_reservation_impl]
impl MyTrait for () {}

trait OtherTrait {}
impl OtherTrait for () {}
impl<T: MyTrait> OtherTrait for T {}
//~^ ERROR conflicting implementations

fn main() {}
@@ -0,0 +1,11 @@
error[E0119]: conflicting implementations of trait `OtherTrait` for type `()`:
--> $DIR/reservation-impl-coherence-conflict.rs:13:1
|
LL | impl OtherTrait for () {}
| ---------------------- first implementation here
LL | impl<T: MyTrait> OtherTrait for T {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ conflicting implementation for `()`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0119`.
14 changes: 14 additions & 0 deletions src/test/ui/traits/reservation-impls/reservation-impl-no-use.rs
@@ -0,0 +1,14 @@
// compile-fail

// check that reservation impls can't be used as normal impls in positive reasoning.

#![feature(rustc_attrs)]

trait MyTrait { fn foo(&self); }
#[rustc_reservation_impl]
impl MyTrait for () { fn foo(&self) {} }

fn main() {
<() as MyTrait>::foo(&());
//~^ ERROR the trait bound `(): MyTrait` is not satisfied
}
@@ -0,0 +1,15 @@
error[E0277]: the trait bound `(): MyTrait` is not satisfied
--> $DIR/reservation-impl-no-use.rs:12:5
|
LL | trait MyTrait { fn foo(&self); }
| -------------- required by `MyTrait::foo`
...
LL | <() as MyTrait>::foo(&());
| ^^^^^^^^^^^^^^^^^^^^ the trait `MyTrait` is not implemented for `()`
|
= help: the following implementations were found:
<() as MyTrait>

error: aborting due to previous error

For more information about this error, try `rustc --explain E0277`.
28 changes: 28 additions & 0 deletions src/test/ui/traits/reservation-impls/reservation-impl-ok.rs
@@ -0,0 +1,28 @@
// run-pass

// rpass test for reservation impls. Not 100% required because `From` uses them,
// but still.

#![feature(rustc_attrs)]

use std::mem;

trait MyTrait<S> {
fn foo(&self, s: S) -> usize;
}

#[rustc_reservation_impl]
impl<T> MyTrait<u64> for T {
fn foo(&self, _x: u64) -> usize { 0 }
}

// reservation impls don't create coherence conflicts, even with
// non-chain overlap.
impl<S> MyTrait<S> for u32 {
fn foo(&self, _x: S) -> usize { mem::size_of::<S>() }
}

fn main() {
// ...and the non-reservation impl gets picked.XS
assert_eq!(0u32.foo(0u64), mem::size_of::<u64>());
}

0 comments on commit 9a94ecd

Please sign in to comment.