Skip to content

Commit 3376ede

Browse files
committed
typeck/kind -- stop using old trait framework.
- Unify the "well-formedness" checking that typeck was already doing with what was taking place in kind. - Move requirements that things be sized into typeck. - I left the checking on upvars in kind, though I think it should eventually be refactored into regionck (which would perhaps be renamed). This reflects a general plan to convert typeck so that it registers obligations or other pending things for conditions it cannot check eventually. This makes it easier to identify all the conditions that apply to an AST expression, but can also influence inference in somec cases (e.g., `Send` implies `'static`, so I already had to promote a lot of the checking that `kind.rs` was doing into typeck, this branch just continues the process).
1 parent a0855a8 commit 3376ede

File tree

10 files changed

+1270
-1798
lines changed

10 files changed

+1270
-1798
lines changed

src/librustc/middle/kind.rs

Lines changed: 46 additions & 437 deletions
Large diffs are not rendered by default.

src/librustc/middle/typeck/check/_match.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -555,11 +555,9 @@ pub fn check_pat(pcx: &pat_ctxt, pat: &ast::Pat, expected: ty::t) {
555555
None);
556556
match tcx.def_map.borrow().find(&pat.id) {
557557
Some(def) => {
558-
let item_type = ty::lookup_item_type(tcx, def.def_id());
559-
let substitutions = fcx.infcx().fresh_substs_for_type(
560-
pat.span, &item_type.generics);
558+
let struct_ty = fcx.instantiate_item_type(pat.span, def.def_id());
561559
check_struct_pat(pcx, pat.span, fields.as_slice(),
562-
etc, def.def_id(), &substitutions);
560+
etc, def.def_id(), &struct_ty.substs);
563561
}
564562
None => {
565563
tcx.sess.span_bug(pat.span,

src/librustc/middle/typeck/check/method.rs

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ obtained the type `Foo`, we would never match this method.
8282

8383
use middle::subst;
8484
use middle::subst::Subst;
85+
use middle::traits;
8586
use middle::ty::*;
8687
use middle::ty;
8788
use middle::typeck::astconv::AstConv;
@@ -91,7 +92,6 @@ use middle::typeck::infer;
9192
use middle::typeck::MethodCallee;
9293
use middle::typeck::{MethodOrigin, MethodParam};
9394
use middle::typeck::{MethodStatic, MethodStaticUnboxedClosure, MethodObject};
94-
use middle::typeck::{param_index};
9595
use middle::typeck::check::regionmanip::replace_late_bound_regions_in_fn_sig;
9696
use middle::typeck::TypeAndSubsts;
9797
use util::common::indenter;
@@ -537,14 +537,12 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
537537
return
538538
}
539539

540-
let vcx = self.fcx.vtable_context();
541-
542540
// Get the tupled type of the arguments.
543541
let arguments_type = *closure_function_type.sig.inputs.get(0);
544542
let return_type = closure_function_type.sig.output;
545543

546544
let closure_region =
547-
vcx.infcx.next_region_var(infer::MiscVariable(self.span));
545+
self.fcx.infcx().next_region_var(infer::MiscVariable(self.span));
548546
let unboxed_closure_type = ty::mk_unboxed_closure(self.tcx(),
549547
closure_did,
550548
closure_region);
@@ -554,7 +552,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
554552
rcvr_substs: subst::Substs::new_trait(
555553
vec![arguments_type, return_type],
556554
vec![],
557-
*vcx.infcx.next_ty_vars(1).get(0)),
555+
*self.fcx.infcx().next_ty_vars(1).get(0)),
558556
method_ty: method,
559557
origin: MethodStaticUnboxedClosure(closure_did),
560558
});
@@ -617,7 +615,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
617615

618616
self.push_inherent_candidates_from_bounds_inner(
619617
&[trait_ref.clone()],
620-
|_this, new_trait_ref, m, method_num, _bound_num| {
618+
|_this, new_trait_ref, m, method_num| {
621619
let vtable_index =
622620
get_method_index(tcx, &*new_trait_ref,
623621
trait_ref.clone(), method_num);
@@ -632,7 +630,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
632630
rcvr_substs: new_trait_ref.substs.clone(),
633631
method_ty: Rc::new(m),
634632
origin: MethodObject(MethodObject {
635-
trait_id: new_trait_ref.def_id,
633+
trait_ref: new_trait_ref,
636634
object_trait_id: did,
637635
method_num: method_num,
638636
real_index: vtable_index
@@ -651,22 +649,20 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
651649
rcvr_ty,
652650
param_ty.space,
653651
param_ty.idx,
654-
restrict_to,
655-
param_index { space: param_ty.space, index: param_ty.idx });
652+
restrict_to);
656653
}
657654

658655

659656
fn push_inherent_candidates_from_bounds(&mut self,
660657
self_ty: ty::t,
661658
space: subst::ParamSpace,
662659
index: uint,
663-
restrict_to: Option<DefId>,
664-
param: param_index) {
660+
restrict_to: Option<DefId>) {
665661
let bounds =
666662
self.fcx.inh.param_env.bounds.get(space, index).trait_bounds
667663
.as_slice();
668664
self.push_inherent_candidates_from_bounds_inner(bounds,
669-
|this, trait_ref, m, method_num, bound_num| {
665+
|this, trait_ref, m, method_num| {
670666
match restrict_to {
671667
Some(trait_did) => {
672668
if trait_did != trait_ref.def_id {
@@ -692,10 +688,8 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
692688
rcvr_substs: trait_ref.substs.clone(),
693689
method_ty: m,
694690
origin: MethodParam(MethodParam {
695-
trait_id: trait_ref.def_id,
691+
trait_ref: trait_ref,
696692
method_num: method_num,
697-
param_num: param,
698-
bound_num: bound_num,
699693
})
700694
})
701695
})
@@ -709,15 +703,16 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
709703
mk_cand: |this: &mut LookupContext,
710704
tr: Rc<TraitRef>,
711705
m: Rc<ty::Method>,
712-
method_num: uint,
713-
bound_num: uint|
714-
-> Option<Candidate>) {
706+
method_num: uint|
707+
-> Option<Candidate>)
708+
{
715709
let tcx = self.tcx();
716-
let mut next_bound_idx = 0; // count only trait bounds
717-
718-
ty::each_bound_trait_and_supertraits(tcx, bounds, |bound_trait_ref| {
719-
let this_bound_idx = next_bound_idx;
720-
next_bound_idx += 1;
710+
let mut cache = HashSet::new();
711+
for bound_trait_ref in traits::transitive_bounds(tcx, bounds) {
712+
// Already visited this trait, skip it.
713+
if !cache.insert(bound_trait_ref.def_id) {
714+
continue;
715+
}
721716

722717
let trait_items = ty::trait_items(tcx, bound_trait_ref.def_id);
723718
match trait_items.iter().position(|ti| {
@@ -736,8 +731,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
736731
match mk_cand(self,
737732
bound_trait_ref,
738733
method,
739-
pos,
740-
this_bound_idx) {
734+
pos) {
741735
Some(cand) => {
742736
debug!("pushing inherent candidate for param: {}",
743737
cand.repr(self.tcx()));
@@ -752,8 +746,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
752746
// check next trait or bound
753747
}
754748
}
755-
true
756-
});
749+
}
757750
}
758751

759752

@@ -764,7 +757,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
764757

765758
let impl_items = self.tcx().impl_items.borrow();
766759
for impl_infos in self.tcx().inherent_impls.borrow().find(&did).iter() {
767-
for impl_did in impl_infos.borrow().iter() {
760+
for impl_did in impl_infos.iter() {
768761
let items = impl_items.get(impl_did);
769762
self.push_candidates_from_impl(*impl_did,
770763
items.as_slice(),
@@ -816,11 +809,10 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
816809
// determine the `self` of the impl with fresh
817810
// variables for each parameter:
818811
let span = self.self_expr.map_or(self.span, |e| e.span);
819-
let vcx = self.fcx.vtable_context();
820812
let TypeAndSubsts {
821813
substs: impl_substs,
822814
ty: impl_ty
823-
} = impl_self_ty(&vcx, span, impl_did);
815+
} = impl_self_ty(self.fcx, span, impl_did);
824816

825817
let candidates = if is_extension {
826818
&mut self.extension_candidates
@@ -861,7 +853,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
861853
adjustment {:?} for {}", adjustment, self.ty_to_string(self_ty));
862854
match adjustment {
863855
Some((self_expr_id, adj)) => {
864-
self.fcx.write_adjustment(self_expr_id, adj);
856+
self.fcx.write_adjustment(self_expr_id, self.span, adj);
865857
}
866858
None => {}
867859
}
@@ -1093,7 +1085,9 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
10931085

10941086
ty_err => None,
10951087

1096-
ty_infer(TyVar(_)) => {
1088+
ty_infer(TyVar(_)) |
1089+
ty_infer(SkolemizedTy(_)) |
1090+
ty_infer(SkolemizedIntTy(_)) => {
10971091
self.bug(format!("unexpected type: {}",
10981092
self.ty_to_string(self_ty)).as_slice());
10991093
}
@@ -1134,6 +1128,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
11341128
Some(self_expr_id) => {
11351129
self.fcx.write_adjustment(
11361130
self_expr_id,
1131+
self.span,
11371132
ty::AutoDerefRef(ty::AutoDerefRef {
11381133
autoderefs: autoderefs,
11391134
autoref: Some(kind(region, *mutbl))
@@ -1193,7 +1188,7 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
11931188

11941189
// return something so we don't get errors for every mutability
11951190
return Some(MethodCallee {
1196-
origin: relevant_candidates.get(0).origin,
1191+
origin: relevant_candidates.get(0).origin.clone(),
11971192
ty: ty::mk_err(),
11981193
substs: subst::Substs::empty()
11991194
});
@@ -1221,12 +1216,14 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
12211216
candidate_b.repr(self.tcx()));
12221217
match (&candidate_a.origin, &candidate_b.origin) {
12231218
(&MethodParam(ref p1), &MethodParam(ref p2)) => {
1224-
let same_trait = p1.trait_id == p2.trait_id;
1225-
let same_method = p1.method_num == p2.method_num;
1226-
let same_param = p1.param_num == p2.param_num;
1227-
// The bound number may be different because
1228-
// multiple bounds may lead to the same trait
1229-
// impl
1219+
let same_trait =
1220+
p1.trait_ref.def_id == p2.trait_ref.def_id;
1221+
let same_method =
1222+
p1.method_num == p2.method_num;
1223+
// it's ok to compare self-ty with `==` here because
1224+
// they are always a TyParam
1225+
let same_param =
1226+
p1.trait_ref.self_ty() == p2.trait_ref.self_ty();
12301227
same_trait && same_method && same_param
12311228
}
12321229
_ => false
@@ -1353,13 +1350,13 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
13531350
}
13541351
}
13551352

1356-
self.fcx.add_region_obligations_for_parameters(
1357-
self.span,
1353+
self.fcx.add_obligations_for_parameters(
1354+
traits::ObligationCause::misc(self.span),
13581355
&all_substs,
13591356
&candidate.method_ty.generics);
13601357

13611358
MethodCallee {
1362-
origin: candidate.origin,
1359+
origin: candidate.origin.clone(),
13631360
ty: fty,
13641361
substs: all_substs
13651362
}
@@ -1436,10 +1433,10 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
14361433
MethodStaticUnboxedClosure(_) => bad = false,
14371434
// FIXME: does this properly enforce this on everything now
14381435
// that self has been merged in? -sully
1439-
MethodParam(MethodParam { trait_id: trait_id, .. }) |
1440-
MethodObject(MethodObject { trait_id: trait_id, .. }) => {
1436+
MethodParam(MethodParam { trait_ref: ref trait_ref, .. }) |
1437+
MethodObject(MethodObject { trait_ref: ref trait_ref, .. }) => {
14411438
bad = self.tcx().destructor_for_type.borrow()
1442-
.contains_key(&trait_id);
1439+
.contains_key(&trait_ref.def_id);
14431440
}
14441441
}
14451442

@@ -1583,10 +1580,10 @@ impl<'a, 'tcx> LookupContext<'a, 'tcx> {
15831580
self.report_static_candidate(idx, did)
15841581
}
15851582
MethodParam(ref mp) => {
1586-
self.report_param_candidate(idx, (*mp).trait_id)
1583+
self.report_param_candidate(idx, mp.trait_ref.def_id)
15871584
}
15881585
MethodObject(ref mo) => {
1589-
self.report_trait_candidate(idx, mo.trait_id)
1586+
self.report_trait_candidate(idx, mo.trait_ref.def_id)
15901587
}
15911588
}
15921589
}

0 commit comments

Comments
 (0)