Skip to content

Commit 919975d

Browse files
committed
Address nits.
1 parent 518ec12 commit 919975d

File tree

12 files changed

+75
-45
lines changed

12 files changed

+75
-45
lines changed

src/librustc/middle/traits/fulfill.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ use middle::ty::{mod, AsPredicate, RegionEscape, Ty, ToPolyTraitRef};
1414
use std::collections::HashSet;
1515
use std::collections::hash_map::Entry::{Occupied, Vacant};
1616
use std::default::Default;
17-
use std::rc::Rc;
1817
use syntax::ast;
1918
use util::common::ErrorReported;
2019
use util::ppaux::Repr;
@@ -102,26 +101,30 @@ impl<'tcx> FulfillmentContext<'tcx> {
102101
}
103102
}
104103

105-
pub fn normalize_associated_type<'a>(&mut self,
104+
/// "Normalize" a projection type `<SomeType as SomeTrait>::X` by
105+
/// creating a fresh type variable `$0` as well as a projection
106+
/// predicate `<SomeType as SomeTrait>::X == $0`. When the
107+
/// inference engine runs, it will attempt to find an impl of
108+
/// `SomeTrait` or a where clause that lets us unify `$0` with
109+
/// something concrete. If this fails, we'll unify `$0` with
110+
/// `projection_ty` again.
111+
pub fn normalize_projection_type<'a>(&mut self,
106112
infcx: &InferCtxt<'a,'tcx>,
107-
trait_ref: Rc<ty::TraitRef<'tcx>>,
108-
item_name: ast::Name,
113+
projection_ty: ty::ProjectionTy<'tcx>,
109114
cause: ObligationCause<'tcx>)
110115
-> Ty<'tcx>
111116
{
112-
debug!("normalize_associated_type(trait_ref={}, item_name={})",
113-
trait_ref.repr(infcx.tcx),
114-
item_name.repr(infcx.tcx));
117+
debug!("normalize_associated_type(projection_ty={})",
118+
projection_ty.repr(infcx.tcx));
115119

116-
assert!(!trait_ref.has_escaping_regions());
120+
assert!(!projection_ty.has_escaping_regions());
117121

118122
// FIXME(#20304) -- cache
119123

120124
let ty_var = infcx.next_ty_var();
121125
let projection =
122126
ty::Binder(ty::ProjectionPredicate {
123-
projection_ty: ty::ProjectionTy { trait_ref: trait_ref,
124-
item_name: item_name },
127+
projection_ty: projection_ty,
125128
ty: ty_var
126129
});
127130
let obligation = Obligation::new(cause, projection.as_predicate());

src/librustc/middle/traits/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ pub enum ObligationCauseCode<'tcx> {
117117

118118
#[deriving(Clone)]
119119
pub struct DerivedObligationCause<'tcx> {
120-
/// Resolving this trait led to the current obligation
120+
/// The trait reference of the parent obligation that led to the
121+
/// current obligation. Note that only trait obligations lead to
122+
/// derived obligations, so we just store the trait reference here
123+
/// directly.
121124
parent_trait_ref: ty::PolyTraitRef<'tcx>,
122125

123126
/// The parent trait had this cause

src/librustc/middle/traits/project.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use super::VtableImplData;
2020
use middle::infer;
2121
use middle::subst::Subst;
2222
use middle::ty::{mod, AsPredicate, ToPolyTraitRef, Ty};
23-
use std::fmt;
2423
use util::ppaux::Repr;
2524

2625
pub type PolyProjectionObligation<'tcx> =
@@ -34,10 +33,17 @@ pub type ProjectionTyObligation<'tcx> =
3433

3534
/// When attempting to resolve `<T as TraitRef>::Name == U`...
3635
pub enum ProjectionError<'tcx> {
36+
/// ...we could not find any helpful information on what `Name`
37+
/// might be. This could occur, for example, if there is a where
38+
/// clause `T : TraitRef` but not `T : TraitRef<Name=V>`. When
39+
/// normalizing, this case is where we opt to normalize back to
40+
/// the projection type `<T as TraitRef>::Name`.
3741
NoCandidate,
42+
43+
/// ...we found multiple sources of information and couldn't resolve the ambiguity.
3844
TooManyCandidates,
3945

40-
///
46+
/// ...`<T as TraitRef::Name>` ws resolved to some type `V` that failed to unify with `U`
4147
MismatchedTypes(MismatchedProjectionTypes<'tcx>),
4248

4349
/// ...an error occurred matching `T : TraitRef`
@@ -380,12 +386,6 @@ fn confirm_candidate<'cx,'tcx>(
380386
Ok(projected_ty)
381387
}
382388

383-
impl<'tcx> Repr<'tcx> for super::MismatchedProjectionTypes<'tcx> {
384-
fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
385-
self.err.repr(tcx)
386-
}
387-
}
388-
389389
impl<'tcx> Repr<'tcx> for ProjectionError<'tcx> {
390390
fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
391391
match *self {
@@ -401,12 +401,6 @@ impl<'tcx> Repr<'tcx> for ProjectionError<'tcx> {
401401
}
402402
}
403403

404-
impl<'tcx> fmt::Show for super::MismatchedProjectionTypes<'tcx> {
405-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
406-
write!(f, "MismatchedProjectionTypes(..)")
407-
}
408-
}
409-
410404
impl<'tcx> Repr<'tcx> for ProjectionTyCandidate<'tcx> {
411405
fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
412406
match *self {

src/librustc/middle/traits/select.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,15 @@ enum SelectionCandidate<'tcx> {
150150
}
151151

152152
struct SelectionCandidateSet<'tcx> {
153+
// a list of candidates that definitely apply to the current
154+
// obligation (meaning: types unify).
153155
vec: Vec<SelectionCandidate<'tcx>>,
154-
ambiguous: bool
156+
157+
// if this is true, then there were candidates that might or might
158+
// not have applied, but we couldn't tell. This occurs when some
159+
// of the input types are type variables, in which case there are
160+
// various "builtin" rules that might or might not trigger.
161+
ambiguous: bool,
155162
}
156163

157164
enum BuiltinBoundConditions<'tcx> {

src/librustc/middle/traits/util.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,9 +385,16 @@ impl<'tcx> fmt::Show for super::FulfillmentErrorCode<'tcx> {
385385
}
386386
}
387387

388-
impl<'tcx> Repr<'tcx> for ty::type_err<'tcx> {
388+
impl<'tcx> Repr<'tcx> for super::MismatchedProjectionTypes<'tcx> {
389389
fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
390-
ty::type_err_to_str(tcx, self)
390+
self.err.repr(tcx)
391391
}
392392
}
393393

394+
impl<'tcx> fmt::Show for super::MismatchedProjectionTypes<'tcx> {
395+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
396+
write!(f, "MismatchedProjectionTypes(..)")
397+
}
398+
}
399+
400+

src/librustc/middle/ty.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,8 @@ pub enum Predicate<'tcx> {
17911791
/// where T : 'a
17921792
TypeOutlives(PolyTypeOutlivesPredicate<'tcx>),
17931793

1794-
///
1794+
/// where <T as TraitRef>::Name == X, approximately.
1795+
/// See `ProjectionPredicate` struct for details.
17951796
Projection(PolyProjectionPredicate<'tcx>),
17961797
}
17971798

@@ -1857,9 +1858,14 @@ impl<'tcx> PolyProjectionPredicate<'tcx> {
18571858
}
18581859
}
18591860

1861+
/// Represents the projection of an associated type. In explicit UFCS
1862+
/// form this would be written `<T as Trait<..>>::N`.
18601863
#[deriving(Clone, PartialEq, Eq, Hash, Show)]
18611864
pub struct ProjectionTy<'tcx> {
1865+
/// The trait reference `T as Trait<..>`.
18621866
pub trait_ref: Rc<ty::TraitRef<'tcx>>,
1867+
1868+
/// The name `N` of the associated type.
18631869
pub item_name: ast::Name,
18641870
}
18651871

@@ -2179,6 +2185,12 @@ impl<'tcx> ParameterEnvironment<'tcx> {
21792185
/// - `generics`: the set of type parameters and their bounds
21802186
/// - `ty`: the base types, which may reference the parameters defined
21812187
/// in `generics`
2188+
///
2189+
/// Note that TypeSchemes are also sometimes called "polytypes" (and
2190+
/// in fact this struct used to carry that name, so you may find some
2191+
/// stray references in a comment or something). We try to reserve the
2192+
/// "poly" prefix to refer to higher-ranked things, as in
2193+
/// `PolyTraitRef`.
21822194
#[deriving(Clone, Show)]
21832195
pub struct TypeScheme<'tcx> {
21842196
pub generics: Generics<'tcx>,
@@ -4680,6 +4692,12 @@ pub fn ty_sort_string<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> String {
46804692
}
46814693
}
46824694

4695+
impl<'tcx> Repr<'tcx> for ty::type_err<'tcx> {
4696+
fn repr(&self, tcx: &ty::ctxt<'tcx>) -> String {
4697+
ty::type_err_to_str(tcx, self)
4698+
}
4699+
}
4700+
46834701
/// Explains the source of a type err in a short, human readable way. This is meant to be placed
46844702
/// in parentheses after some larger message. You should also invoke `note_and_explain_type_err()`
46854703
/// afterwards to present additional details, particularly when it comes to lifetime-related

src/librustc_typeck/astconv.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ pub fn opt_ast_region_to_region<'tcx, AC: AstConv<'tcx>, RS: RegionScope>(
244244
fn ast_path_substs_for_ty<'tcx,AC,RS>(
245245
this: &AC,
246246
rscope: &RS,
247-
decl_def_id: ast::DefId,
248247
decl_generics: &ty::Generics<'tcx>,
249248
path: &ast::Path)
250249
-> Substs<'tcx>
@@ -280,7 +279,6 @@ fn ast_path_substs_for_ty<'tcx,AC,RS>(
280279
create_substs_for_ast_path(this,
281280
rscope,
282281
path.span,
283-
decl_def_id,
284282
decl_generics,
285283
None,
286284
types,
@@ -291,7 +289,6 @@ fn create_substs_for_ast_path<'tcx,AC,RS>(
291289
this: &AC,
292290
rscope: &RS,
293291
span: Span,
294-
_decl_def_id: ast::DefId,
295292
decl_generics: &ty::Generics<'tcx>,
296293
self_ty: Option<Ty<'tcx>>,
297294
types: Vec<Ty<'tcx>>,
@@ -621,7 +618,6 @@ fn ast_path_to_trait_ref<'a,'tcx,AC,RS>(
621618
let substs = create_substs_for_ast_path(this,
622619
&shifted_rscope,
623620
path.span,
624-
trait_def_id,
625621
&trait_def.generics,
626622
self_ty,
627623
types,
@@ -705,7 +701,6 @@ pub fn ast_path_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>(
705701

706702
let substs = ast_path_substs_for_ty(this,
707703
rscope,
708-
did,
709704
&generics,
710705
path);
711706
let ty = decl_ty.subst(tcx, &substs);
@@ -747,7 +742,7 @@ pub fn ast_path_to_ty_relaxed<'tcx,AC,RS>(
747742
Substs::new(VecPerParamSpace::params_from_type(type_params),
748743
VecPerParamSpace::params_from_type(region_params))
749744
} else {
750-
ast_path_substs_for_ty(this, rscope, did, &generics, path)
745+
ast_path_substs_for_ty(this, rscope, &generics, path)
751746
};
752747

753748
let ty = decl_ty.subst(tcx, &substs);

src/librustc_typeck/check/assoc.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,9 @@ impl<'a,'tcx> TypeFolder<'tcx> for AssociatedTypeNormalizer<'a,'tcx> {
8080
self.span,
8181
self.body_id,
8282
ObligationCauseCode::MiscObligation);
83-
let trait_ref = data.trait_ref.clone();
8483
self.fulfillment_cx
85-
.normalize_associated_type(self.infcx,
86-
trait_ref,
87-
data.item_name,
84+
.normalize_projection_type(self.infcx,
85+
data.clone(),
8886
cause)
8987
}
9088
_ => {

src/librustc_typeck/check/mod.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1758,9 +1758,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17581758
traits::ObligationCauseCode::MiscObligation);
17591759
self.inh.fulfillment_cx
17601760
.borrow_mut()
1761-
.normalize_associated_type(self.infcx(),
1762-
trait_ref,
1763-
item_name,
1761+
.normalize_projection_type(self.infcx(),
1762+
ty::ProjectionTy {
1763+
trait_ref: trait_ref,
1764+
item_name: item_name,
1765+
},
17641766
cause)
17651767
}
17661768

src/test/auxiliary/issue-18048-lib.rs renamed to src/test/auxiliary/associated-types-cc-lib.rs

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

11+
// Helper for test issue-18048, which tests associated types in a
12+
// cross-crate scenario.
13+
1114
#![crate_type="lib"]
1215
#![feature(associated_types)]
1316

src/test/run-pass/issue-18048.rs renamed to src/test/run-pass/associated-types-cc.rs

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

11-
// aux-build:issue-18048-lib.rs
11+
// aux-build:associated-types-cc-lib.rs
1212

1313
// Test that we are able to reference cross-crate traits that employ
1414
// associated types.
1515

1616
#![feature(associated_types)]
1717

18-
extern crate "issue-18048-lib" as bar;
18+
extern crate "associated-types-cc-lib" as bar;
1919

2020
use bar::Bar;
2121

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

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

11-
// ignore-pretty -- currently pretty prints as `Hash<<Self as Hasher...` which fails to parse
11+
// ignore-pretty -- FIXME(#17362) pretty prints as `Hash<<Self as Hasher...` which fails to parse
1212

1313
#![feature(associated_types)]
1414

0 commit comments

Comments
 (0)