Skip to content

Commit 4156214

Browse files
committed
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.
1 parent b18b67d commit 4156214

File tree

6 files changed

+100
-23
lines changed

6 files changed

+100
-23
lines changed

src/librustc/metadata/decoder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1366,7 +1366,7 @@ pub fn get_dylib_dependency_formats(cdata: Cmd)
13661366
if spec.len() == 0 { continue }
13671367
let cnum = spec.split(':').nth(0).unwrap();
13681368
let link = spec.split(':').nth(1).unwrap();
1369-
let cnum = cnum.parse().unwrap();
1369+
let cnum: ast::CrateNum = cnum.parse().unwrap();
13701370
let cnum = match cdata.cnum_map.get(&cnum) {
13711371
Some(&n) => n,
13721372
None => panic!("didn't find a crate in the cnum_map")

src/librustc/middle/traits/select.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,10 +736,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
736736
let trait_def_id = match poly_trait_predicate.0.trait_ref.self_ty().sty {
737737
ty::ty_projection(ref data) => data.trait_ref.def_id,
738738
ty::ty_infer(ty::TyVar(_)) => {
739-
// TODO ignore potential ambiguity so that we can do
740-
// better inference, need to get our story
741-
// straight(er) here, I think.
742-
// candidates.ambiguous = true;
739+
// If the self-type is an inference variable, then it MAY wind up
740+
// being a projected type, so induce an ambiguity.
741+
//
742+
// FIXME(#20297) -- being strict about this can cause
743+
// inference failures with BorrowFrom, which is
744+
// unfortunate. Can we do better here?
745+
candidates.ambiguous = true;
743746
return;
744747
}
745748
_ => { return; }

src/librustc_typeck/check/method/confirm.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
use super::probe;
1212

13-
use check::{mod, FnCtxt, NoPreference, PreferMutLvalue, callee};
13+
use check::{mod, FnCtxt, NoPreference, PreferMutLvalue, callee, demand};
14+
use middle::mem_categorization::Typer;
1415
use middle::subst::{mod};
1516
use middle::traits;
1617
use middle::ty::{mod, Ty};
@@ -541,7 +542,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
541542
// Don't retry the first one or we might infinite loop!
542543
if i != 0 {
543544
match expr.node {
544-
ast::ExprIndex(ref base_expr, _) => {
545+
ast::ExprIndex(ref base_expr, ref index_expr) => {
545546
let mut base_adjustment =
546547
match self.fcx.inh.adjustments.borrow().get(&base_expr.id) {
547548
Some(&ty::AdjustDerefRef(ref adr)) => (*adr).clone(),
@@ -577,14 +578,22 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
577578
&**base_expr,
578579
Some(&ty::AdjustDerefRef(base_adjustment.clone())));
579580

580-
check::try_index_step(
581+
let result = check::try_index_step(
581582
self.fcx,
582583
MethodCall::expr(expr.id),
583584
*expr,
584585
&**base_expr,
585586
adjusted_base_ty,
586587
base_adjustment,
587588
PreferMutLvalue);
589+
590+
if let Some((input_ty, return_ty)) = result {
591+
let index_expr_ty = self.fcx.expr_ty(&**index_expr);
592+
demand::suptype(self.fcx, index_expr.span, input_ty, index_expr_ty);
593+
594+
let expr_ty = self.fcx.expr_ty(&**expr);
595+
demand::suptype(self.fcx, expr.span, expr_ty, return_ty);
596+
}
588597
}
589598
ast::ExprUnary(ast::UnDeref, ref base_expr) => {
590599
// if this is an overloaded deref, then re-evaluate with

src/test/run-pass/multidispatch-infer-from-single-impl.rs renamed to src/test/compile-fail/associated-types-bound-failure.rs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,32 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
// Test that we correctly infer that `E` must be `()` here. This is
12-
// known because there is just one impl that could apply where
13-
// `Self=()`.
11+
// Test equality constraints on associated types in a where clause.
1412

15-
pub trait FromError<E> {
16-
fn from_error(err: E) -> Self;
13+
#![feature(associated_types)]
14+
15+
pub trait ToInt {
16+
fn to_int(&self) -> int;
17+
}
18+
19+
pub trait GetToInt
20+
{
21+
type R;
22+
23+
fn get(&self) -> <Self as GetToInt>::R;
1724
}
1825

19-
impl<E> FromError<E> for E {
20-
fn from_error(err: E) -> E {
21-
err
22-
}
26+
fn foo<G>(g: G) -> int
27+
where G : GetToInt
28+
{
29+
ToInt::to_int(&g.get()) //~ ERROR not implemented
2330
}
2431

25-
fn test() -> Result<(), ()> {
26-
Err(FromError::from_error(()))
32+
fn bar<G : GetToInt>(g: G) -> int
33+
where G::R : ToInt
34+
{
35+
ToInt::to_int(&g.get()) // OK
2736
}
2837

29-
fn main() {
30-
let result = (|| Err(FromError::from_error(())))();
31-
let foo: () = result.unwrap_or(());
38+
pub fn main() {
3239
}

src/test/run-pass/issue-12028.rs renamed to src/test/compile-fail/issue-12028.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
// Test an example where we fail to infer the type parameter H. This
12+
// is because there is really nothing constraining it. At one time, we
13+
// would infer based on the where clauses in scope, but that no longer
14+
// works.
15+
1116
trait Hash<H> {
1217
fn hash2(&self, hasher: &H) -> u64;
1318
}
@@ -30,7 +35,7 @@ trait StreamHash<S: Stream, H: StreamHasher<S>>: Hash<H> {
3035
impl<S: Stream, H: StreamHasher<S>> Hash<H> for u8 {
3136
fn hash2(&self, hasher: &H) -> u64 {
3237
let mut stream = hasher.stream();
33-
self.input_stream(&mut stream);
38+
self.input_stream(&mut stream); //~ ERROR type annotations required
3439
stream.result()
3540
}
3641
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test equality constraints on associated types in a where clause.
12+
13+
#![feature(associated_types)]
14+
15+
pub trait ToInt {
16+
fn to_int(&self) -> int;
17+
}
18+
19+
impl ToInt for int {
20+
fn to_int(&self) -> int { *self }
21+
}
22+
23+
impl ToInt for uint {
24+
fn to_int(&self) -> int { *self as int }
25+
}
26+
27+
pub trait GetToInt
28+
{
29+
type R : ToInt;
30+
31+
fn get(&self) -> <Self as GetToInt>::R;
32+
}
33+
34+
impl GetToInt for int {
35+
type R = int;
36+
fn get(&self) -> int { *self }
37+
}
38+
39+
impl GetToInt for uint {
40+
type R = uint;
41+
fn get(&self) -> uint { *self }
42+
}
43+
44+
fn foo<G>(g: G) -> int
45+
where G : GetToInt
46+
{
47+
ToInt::to_int(&g.get())
48+
}
49+
50+
pub fn main() {
51+
assert_eq!(foo(22i), 22i);
52+
assert_eq!(foo(22u), 22i);
53+
}

0 commit comments

Comments
 (0)