Skip to content

Commit e58f1bd

Browse files
committed
Normalize predicates during method winnowing.
Fixes #20604. Fixes #20378.
1 parent 2a1ba2f commit e58f1bd

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

src/librustc_typeck/check/method/probe.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -807,26 +807,26 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
807807
match probe.kind {
808808
InherentImplCandidate(impl_def_id, ref substs) |
809809
ExtensionImplCandidate(impl_def_id, _, ref substs, _) => {
810+
let selcx = &mut traits::SelectionContext::new(self.infcx(), self.fcx);
811+
let cause = traits::ObligationCause::misc(self.span, self.fcx.body_id);
812+
810813
// Check whether the impl imposes obligations we have to worry about.
811814
let impl_generics = ty::lookup_item_type(self.tcx(), impl_def_id).generics;
812815
let impl_bounds = impl_generics.to_bounds(self.tcx(), substs);
813-
// FIXME(#20378) assoc type normalization here?
814-
815-
// Erase any late-bound regions bound in the impl
816-
// which appear in the bounds.
817-
let impl_bounds = self.erase_late_bound_regions(&ty::Binder(impl_bounds));
816+
let traits::Normalized { value: impl_bounds,
817+
obligations: norm_obligations } =
818+
traits::normalize(selcx, cause.clone(), &impl_bounds);
818819

819820
// Convert the bounds into obligations.
820821
let obligations =
821-
traits::predicates_for_generics(
822-
self.tcx(),
823-
traits::ObligationCause::misc(self.span, self.fcx.body_id),
824-
&impl_bounds);
822+
traits::predicates_for_generics(self.tcx(),
823+
cause.clone(),
824+
&impl_bounds);
825825
debug!("impl_obligations={}", obligations.repr(self.tcx()));
826826

827827
// Evaluate those obligations to see if they might possibly hold.
828-
let mut selcx = traits::SelectionContext::new(self.infcx(), self.fcx);
829-
obligations.all(|o| selcx.evaluate_obligation(o))
828+
obligations.all(|o| selcx.evaluate_obligation(o)) &&
829+
norm_obligations.iter().all(|o| selcx.evaluate_obligation(o))
830830
}
831831

832832
ObjectCandidate(..) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Copyright 2015 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 that we handle projection types which wind up important for
12+
// resolving methods. This test was reduced from a larger example; the
13+
// call to `foo()` at the end was failing to resolve because the
14+
// winnowing stage of method resolution failed to handle an associated
15+
// type projection.
16+
17+
#![feature(associated_types)]
18+
19+
trait Hasher {
20+
type Output;
21+
fn finish(&self) -> Self::Output;
22+
}
23+
24+
trait Hash<H: Hasher> {
25+
fn hash(&self, h: &mut H);
26+
}
27+
28+
trait HashState {
29+
type Wut: Hasher;
30+
fn hasher(&self) -> Self::Wut;
31+
}
32+
33+
struct SipHasher;
34+
impl Hasher for SipHasher {
35+
type Output = u64;
36+
fn finish(&self) -> u64 { 4 }
37+
}
38+
39+
impl Hash<SipHasher> for int {
40+
fn hash(&self, h: &mut SipHasher) {}
41+
}
42+
43+
struct SipState;
44+
impl HashState for SipState {
45+
type Wut = SipHasher;
46+
fn hasher(&self) -> SipHasher { SipHasher }
47+
}
48+
49+
struct Map<S> {
50+
s: S,
51+
}
52+
53+
impl<S> Map<S>
54+
where S: HashState,
55+
<S as HashState>::Wut: Hasher<Output=u64>,
56+
{
57+
fn foo<K>(&self, k: K) where K: Hash< <S as HashState>::Wut> {}
58+
}
59+
60+
fn foo<K: Hash<SipHasher>>(map: &Map<SipState>) {
61+
map.foo(22);
62+
}
63+
64+
fn main() {}
65+

0 commit comments

Comments
 (0)