Skip to content

Commit 73bd5cf

Browse files
committed
Also fix #22066
1 parent 8303441 commit 73bd5cf

File tree

5 files changed

+53
-17
lines changed

5 files changed

+53
-17
lines changed

src/librustc/middle/traits/project.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,7 @@ fn assemble_candidates_from_trait_def<'cx,'tcx>(
561561
};
562562

563563
// If so, extract what we know from the trait and try to come up with a good answer.
564-
let trait_predicates = ty::lookup_predicates(selcx.tcx(), trait_ref.def_id);
565-
let bounds = trait_predicates.instantiate(selcx.tcx(), trait_ref.substs);
564+
let bounds = selcx.instantiate_trait_predicates(&trait_ref);
566565
assemble_candidates_from_predicates(selcx, obligation, obligation_trait_ref,
567566
candidate_set, bounds.predicates.into_vec());
568567
}

src/librustc/middle/traits/select.rs

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,15 @@ use super::{util};
3333

3434
use middle::fast_reject;
3535
use middle::subst::{Subst, Substs, TypeSpace, VecPerParamSpace};
36-
use middle::ty::{self, AssociatedType, RegionEscape, ToPolyTraitRef, Ty};
36+
use middle::ty::{self, AssociatedType, RegionEscape, ToPolyTraitRef, TraitRef, Ty};
3737
use middle::infer;
3838
use middle::infer::{InferCtxt, TypeFreshener};
3939
use middle::ty_fold::TypeFoldable;
4040
use std::cell::RefCell;
4141
use std::collections::hash_map::HashMap;
4242
use std::rc::Rc;
43-
use syntax::{abi, ast};
43+
use syntax::abi;
44+
use syntax::ast::{self, DefId};
4445
use util::common::ErrorReported;
4546
use util::ppaux::Repr;
4647

@@ -2201,19 +2202,33 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
22012202
-> Ty<'tcx>
22022203
{
22032204
let poly_ty = ty::lookup_item_type(self.tcx(), assoc_ty.def_id);
2204-
let substs =
2205-
if self.param_env().def_id == assoc_ty.container.id() {
2206-
// If the associated type we are about to instantiate is
2207-
// defined in the item current parameter environment is
2208-
// associated with, we are obliged to use free substitutions.
2209-
&self.param_env().free_substs
2210-
} else {
2211-
impl_substs
2212-
};
2213-
2205+
let substs = self.maybe_pick_free_substs(assoc_ty.container.id(), impl_substs);
22142206
poly_ty.ty.subst(self.tcx(), substs)
22152207
}
22162208

2209+
pub fn instantiate_trait_predicates(&self,
2210+
trait_ref: &Rc<TraitRef<'tcx>>)
2211+
-> ty::InstantiatedPredicates<'tcx>
2212+
{
2213+
let trait_predicates = ty::lookup_predicates(self.tcx(), trait_ref.def_id);
2214+
let substs = self.maybe_pick_free_substs(trait_ref.def_id, trait_ref.substs);
2215+
trait_predicates.instantiate(self.tcx(), substs)
2216+
}
2217+
2218+
fn maybe_pick_free_substs(&self,
2219+
target_def_id: DefId,
2220+
substs: &'cx Substs<'tcx>)
2221+
-> &'cx Substs<'tcx>
2222+
{
2223+
// The free substitutions take precedence over the given one if
2224+
// current parameter environment is associated with `target_def_id`.
2225+
if self.param_env().def_id == target_def_id {
2226+
&self.param_env().free_substs
2227+
} else {
2228+
substs
2229+
}
2230+
}
2231+
22172232
fn push_stack<'o,'s:'o>(&mut self,
22182233
previous_stack: Option<&'s TraitObligationStack<'s, 'tcx>>,
22192234
obligation: &'o TraitObligation<'tcx>)

src/test/compile-fail/regions-assoc-type-region-bound-in-trait-not-met.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ impl<'a> Foo<'a> for &'a i16 {
2222
}
2323

2424
impl<'a> Foo<'static> for &'a i32 {
25-
//~^ ERROR cannot infer
25+
//~^ ERROR does not fulfill the required lifetime
2626
type Value = &'a i32;
2727
}
2828

2929
impl<'a,'b> Foo<'b> for &'a i64 {
30-
//~^ ERROR cannot infer
30+
//~^ ERROR does not fulfill the required lifetime
3131
type Value = &'a i32;
3232
}
3333

src/test/compile-fail/regions-assoc-type-static-bound-in-trait-not-met.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ trait Foo {
1717
}
1818

1919
impl<'a> Foo for &'a i32 {
20-
//~^ ERROR cannot infer
20+
//~^ ERROR does not fulfill the required lifetime
2121
type Value = &'a i32;
2222
}
2323

src/test/run-pass/issue-22066.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
pub trait LineFormatter<'a> {
12+
type Iter: Iterator<Item=&'a str> + 'a;
13+
fn iter(&'a self, line: &'a str) -> Self::Iter;
14+
15+
fn dimensions(&'a self, line: &'a str) {
16+
for grapheme in self.iter(line) {
17+
let _ = grapheme.len();
18+
}
19+
}
20+
}
21+
22+
fn main() {}

0 commit comments

Comments
 (0)