Adjust tests for inferenceGet more conservative about inference for now. Seems better to err on the side of being more correct rather than less. Fix a bug in typing index expressions that was exposed as a result, and add one type annotation that is not required. Delete some random tests that were relying on old behavior and don't seem to add anything anymore.
This commit is contained in:
parent
c197911c60
commit
05eb2eeb61
6 changed files with 100 additions and 23 deletions
|
|
@ -8,25 +8,32 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test that we correctly infer that `E` must be `()` here. This is
|
||||
// known because there is just one impl that could apply where
|
||||
// `Self=()`.
|
||||
// Test equality constraints on associated types in a where clause.
|
||||
|
||||
pub trait FromError<E> {
|
||||
fn from_error(err: E) -> Self;
|
||||
#![feature(associated_types)]
|
||||
|
||||
pub trait ToInt {
|
||||
fn to_int(&self) -> int;
|
||||
}
|
||||
|
||||
impl<E> FromError<E> for E {
|
||||
fn from_error(err: E) -> E {
|
||||
err
|
||||
}
|
||||
pub trait GetToInt
|
||||
{
|
||||
type R;
|
||||
|
||||
fn get(&self) -> <Self as GetToInt>::R;
|
||||
}
|
||||
|
||||
fn test() -> Result<(), ()> {
|
||||
Err(FromError::from_error(()))
|
||||
fn foo<G>(g: G) -> int
|
||||
where G : GetToInt
|
||||
{
|
||||
ToInt::to_int(&g.get()) //~ ERROR not implemented
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let result = (|| Err(FromError::from_error(())))();
|
||||
let foo: () = result.unwrap_or(());
|
||||
fn bar<G : GetToInt>(g: G) -> int
|
||||
where G::R : ToInt
|
||||
{
|
||||
ToInt::to_int(&g.get()) // OK
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
}
|
||||
|
|
@ -8,6 +8,11 @@
|
|||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test an example where we fail to infer the type parameter H. This
|
||||
// is because there is really nothing constraining it. At one time, we
|
||||
// would infer based on the where clauses in scope, but that no longer
|
||||
// works.
|
||||
|
||||
trait Hash<H> {
|
||||
fn hash2(&self, hasher: &H) -> u64;
|
||||
}
|
||||
|
|
@ -30,7 +35,7 @@ trait StreamHash<S: Stream, H: StreamHasher<S>>: Hash<H> {
|
|||
impl<S: Stream, H: StreamHasher<S>> Hash<H> for u8 {
|
||||
fn hash2(&self, hasher: &H) -> u64 {
|
||||
let mut stream = hasher.stream();
|
||||
self.input_stream(&mut stream);
|
||||
self.input_stream(&mut stream); //~ ERROR type annotations required
|
||||
stream.result()
|
||||
}
|
||||
}
|
||||
53
src/test/run-pass/associated-types-bound.rs
Normal file
53
src/test/run-pass/associated-types-bound.rs
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test equality constraints on associated types in a where clause.
|
||||
|
||||
#![feature(associated_types)]
|
||||
|
||||
pub trait ToInt {
|
||||
fn to_int(&self) -> int;
|
||||
}
|
||||
|
||||
impl ToInt for int {
|
||||
fn to_int(&self) -> int { *self }
|
||||
}
|
||||
|
||||
impl ToInt for uint {
|
||||
fn to_int(&self) -> int { *self as int }
|
||||
}
|
||||
|
||||
pub trait GetToInt
|
||||
{
|
||||
type R : ToInt;
|
||||
|
||||
fn get(&self) -> <Self as GetToInt>::R;
|
||||
}
|
||||
|
||||
impl GetToInt for int {
|
||||
type R = int;
|
||||
fn get(&self) -> int { *self }
|
||||
}
|
||||
|
||||
impl GetToInt for uint {
|
||||
type R = uint;
|
||||
fn get(&self) -> uint { *self }
|
||||
}
|
||||
|
||||
fn foo<G>(g: G) -> int
|
||||
where G : GetToInt
|
||||
{
|
||||
ToInt::to_int(&g.get())
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
assert_eq!(foo(22i), 22i);
|
||||
assert_eq!(foo(22u), 22i);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue