Skip to content

Commit bdda07b

Browse files
committed
Set OriginOfTyParam for methods.
Fixes bug with default fallback, and small refactorings to default fallback.
1 parent f27f976 commit bdda07b

File tree

9 files changed

+138
-17
lines changed

9 files changed

+138
-17
lines changed

src/librustc/traits/fulfill.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,9 @@ impl<'a, 'gcx, 'tcx> FulfillmentContext<'tcx> {
180180
self.predicates.pending_obligations()
181181
}
182182

183-
pub fn vars_in_unsolved_obligations(&self) -> FxHashSet<ty::TyVid> {
183+
pub fn vars_in_obligations(&self) -> FxHashSet<ty::TyVid> {
184184
let mut vars = FxHashSet();
185-
for p in self.predicates.unsolved_obligations() {
185+
for p in self.predicates.obligations() {
186186
for ty in p.obligation.predicate.walk_tys() {
187187
if let TypeVariants::TyInfer(InferTy::TyVar(vid)) = ty.sty {
188188
vars.insert(vid);

src/librustc_data_structures/obligation_forest/mod.rs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -244,16 +244,11 @@ impl<O: ForestObligation> ObligationForest<O> {
244244
.collect()
245245
}
246246

247-
/// Returns the set of obligations that are in a pending or waiting state.
248-
pub fn unsolved_obligations(&self) -> Vec<O>
247+
/// Returns all obligations.
248+
pub fn obligations(&self) -> Vec<O>
249249
where O: Clone
250250
{
251-
self.nodes
252-
.iter()
253-
.filter(|n| n.state.get() == NodeState::Pending ||
254-
n.state.get() == NodeState::Waiting)
255-
.map(|n| n.obligation.clone())
256-
.collect()
251+
self.nodes.iter().map(|n| n.obligation.clone()).collect()
257252
}
258253

259254
/// Perform a pass through the obligation list. This must

src/librustc_typeck/check/mod.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,7 +2201,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
22012201
// Collect variables that are unsolved and support fallback,
22022202
// grouped in bags by subtyping equivalence.
22032203
let mut bags = FxHashMap();
2204-
for vid in self.fulfillment_cx.borrow().vars_in_unsolved_obligations() {
2204+
for vid in self.fulfillment_cx.borrow().vars_in_obligations() {
22052205
let mut type_variables = self.infcx.type_variables.borrow_mut();
22062206
let not_known = type_variables.probe(vid).is_none();
22072207
let supports_fallback = match type_variables.var_origin(vid) {
@@ -2236,17 +2236,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
22362236
// For future-proofing against conflicting defaults,
22372237
// if one of the variables has no default, nothing is unified.
22382238
for &vid in &bag {
2239-
match self.infcx.type_variables.borrow().default(vid) {
2240-
&Default::User(_) => {},
2239+
let default = self.infcx.type_variables.borrow().default(vid).clone();
2240+
debug!("apply_user_type_parameter_fallback: \
2241+
checking var: {:?} with default: {:?}", vid, default);
2242+
match default {
2243+
Default::User(_) => {},
22412244
_ => continue 'bags,
22422245
}
22432246
}
22442247
for &vid in &bag {
22452248
// If future-proof, then all vars have defaults.
22462249
let default = self.infcx.type_variables.borrow().default(vid).clone();
22472250
let ty = self.tcx.mk_var(vid);
2248-
debug!("apply_user_type_parameter_fallback: \
2249-
ty: {:?} with default: {:?}", ty, default);
2251+
debug!("apply_user_type_parameter_fallback: applying fallback to var: {:?} \
2252+
with ty: {:?} with default: {:?}", vid, ty, default);
22502253
if let Default::User(user_default) = default {
22512254
let normalized_default = self.normalize_associated_types_in(
22522255
user_default.origin_span,

src/librustc_typeck/collect.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,14 +878,20 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
878878
let (ast_generics, opt_inputs) = match node {
879879
NodeTraitItem(item) => {
880880
match item.node {
881-
TraitItemKind::Method(ref sig, _) => (&item.generics, Some(&sig.decl.inputs)),
881+
TraitItemKind::Method(ref sig, _) => {
882+
origin = ty::OriginOfTyParam::Fn;
883+
(&item.generics, Some(&sig.decl.inputs))
884+
}
882885
_ => (&item.generics, None)
883886
}
884887
}
885888

886889
NodeImplItem(item) => {
887890
match item.node {
888-
ImplItemKind::Method(ref sig, _) => (&item.generics, Some(&sig.decl.inputs)),
891+
ImplItemKind::Method(ref sig, _) => {
892+
origin = ty::OriginOfTyParam::Fn;
893+
(&item.generics, Some(&sig.decl.inputs))
894+
}
889895
_ => (&item.generics, None)
890896
}
891897
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
#![feature(default_type_parameter_fallback)]
12+
13+
struct Foo;
14+
15+
impl Foo {
16+
fn method<A:Default=String>(&self) -> A {
17+
A::default()
18+
}
19+
}
20+
21+
fn main() {
22+
let f = Foo.method();
23+
println!("{}", f);
24+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
#![feature(default_type_parameter_fallback)]
12+
13+
struct Foo<A>(A);
14+
15+
impl<A:Default=i32> Foo<A> {
16+
fn new() -> Foo<A> {
17+
Foo(A::default())
18+
}
19+
}
20+
21+
fn main() {
22+
let foo = Foo::new();
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
#![feature(default_type_parameter_fallback)]
12+
13+
// Another example from the RFC
14+
trait Foo { }
15+
trait Bar { }
16+
17+
impl<T:Bar=usize> Foo for Vec<T> {}
18+
impl Bar for usize {}
19+
20+
fn takes_foo<F:Foo>(f: F) {}
21+
22+
fn main() {
23+
let x = Vec::new(); // x: Vec<$0>
24+
takes_foo(x); // adds oblig Vec<$0> : Foo
25+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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+
#![feature(default_type_parameter_fallback)]
12+
13+
// An example from the RFC
14+
trait Foo { fn takes_foo(&self); }
15+
trait Bar { }
16+
17+
impl<T:Bar=usize> Foo for Vec<T> {
18+
fn takes_foo(&self) {}
19+
}
20+
21+
impl Bar for usize {}
22+
23+
fn main() {
24+
let x = Vec::new(); // x: Vec<$0>
25+
x.takes_foo(); // adds oblig Vec<$0> : Foo
26+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
#![feature(default_type_parameter_fallback)]
12+
13+
use std::collections::HashMap;
14+
15+
type IntMap<K=usize> = HashMap<K, usize>;
16+
17+
fn main() {
18+
let x = IntMap::new();
19+
}

0 commit comments

Comments
 (0)