Skip to content

Commit f472cd9

Browse files
nikomatsakisAlexander Regueiro
authored andcommitted
Addressed points raised in review.
1 parent 5bf5994 commit f472cd9

File tree

10 files changed

+172
-69
lines changed

10 files changed

+172
-69
lines changed

src/librustc/hir/intravisit.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -626,9 +626,6 @@ pub fn walk_ty<'v, V: Visitor<'v>>(visitor: &mut V, typ: &'v Ty) {
626626
TyKind::CVarArgs(ref lt) => {
627627
visitor.visit_lifetime(lt)
628628
}
629-
TyKind::AssocTyExistential(ref bounds) => {
630-
walk_list!(visitor, visit_param_bound, bounds);
631-
}
632629
TyKind::Infer | TyKind::Err => {}
633630
}
634631
}
@@ -677,7 +674,14 @@ pub fn walk_assoc_type_binding<'v, V: Visitor<'v>>(visitor: &mut V,
677674
type_binding: &'v TypeBinding) {
678675
visitor.visit_id(type_binding.hir_id);
679676
visitor.visit_ident(type_binding.ident);
680-
visitor.visit_ty(&type_binding.ty);
677+
match type_binding.kind {
678+
TypeBindingKind::Equality { ref ty } => {
679+
visitor.visit_ty(ty);
680+
}
681+
TypeBindingKind::Constraint { ref bounds } => {
682+
walk_list!(visitor, visit_param_bound, bounds);
683+
}
684+
}
681685
}
682686

683687
pub fn walk_pat<'v, V: Visitor<'v>>(visitor: &mut V, pattern: &'v Pat) {

src/librustc/hir/lowering.rs

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,9 +1377,10 @@ impl<'a> LoweringContext<'a> {
13771377
-> hir::TypeBinding {
13781378
debug!("lower_assoc_ty_constraint(constraint={:?}, itctx={:?})", c, itctx);
13791379

1380-
// Convert to a type representing the `T::Item` value.
1381-
let ty = match c.kind {
1382-
AssocTyConstraintKind::Equality { ref ty } => self.lower_ty(ty, itctx),
1380+
let kind = match c.kind {
1381+
AssocTyConstraintKind::Equality { ref ty } => hir::TypeBindingKind::Equality {
1382+
ty: self.lower_ty(ty, itctx)
1383+
},
13831384
AssocTyConstraintKind::Bound { ref bounds } => {
13841385
// Piggy-back on the `impl Trait` context to figure out the correct behavior.
13851386
let (desugar_to_impl_trait, itctx) = match itctx {
@@ -1422,7 +1423,7 @@ impl<'a> LoweringContext<'a> {
14221423

14231424
if desugar_to_impl_trait {
14241425
// Desugar `AssocTy: Bounds` into `AssocTy = impl Bounds`. We do this by
1425-
// constructing the HIR for "impl bounds" and then lowering that.
1426+
// constructing the HIR for `impl bounds...` and then lowering that.
14261427

14271428
let impl_trait_node_id = self.sess.next_node_id();
14281429
let parent_def_index = self.current_hir_id_owner.last().unwrap().0;
@@ -1436,35 +1437,35 @@ impl<'a> LoweringContext<'a> {
14361437
);
14371438

14381439
self.with_dyn_type_scope(false, |this| {
1439-
this.lower_ty(
1440+
let ty = this.lower_ty(
14401441
&Ty {
14411442
id: this.sess.next_node_id(),
14421443
node: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
14431444
span: DUMMY_SP,
14441445
},
14451446
itctx,
1446-
)
1447+
);
1448+
1449+
hir::TypeBindingKind::Equality {
1450+
ty
1451+
}
14471452
})
14481453
} else {
1449-
// Desugar `AssocTy: Bounds` into `AssocTy = ∃ T (T: Bounds)`, where the
1450-
// "false existential" later desugars into a trait predicate.
1451-
1454+
// Desugar `AssocTy: Bounds` into a type binding where the
1455+
// later desugars into a trait predicate.
14521456
let bounds = self.lower_param_bounds(bounds, itctx);
14531457

1454-
let id = self.sess.next_node_id();
1455-
P(hir::Ty {
1456-
hir_id: self.lower_node_id(id),
1457-
node: hir::TyKind::AssocTyExistential(bounds),
1458-
span: DUMMY_SP,
1459-
})
1458+
hir::TypeBindingKind::Constraint {
1459+
bounds
1460+
}
14601461
}
14611462
}
14621463
};
14631464

14641465
hir::TypeBinding {
14651466
hir_id: self.lower_node_id(c.id),
14661467
ident: c.ident,
1467-
ty,
1468+
kind,
14681469
span: c.span,
14691470
}
14701471
}
@@ -2359,10 +2360,17 @@ impl<'a> LoweringContext<'a> {
23592360
hir::TypeBinding {
23602361
hir_id: this.next_id(),
23612362
ident: Ident::with_empty_ctxt(FN_OUTPUT_NAME),
2362-
ty: output
2363-
.as_ref()
2364-
.map(|ty| this.lower_ty(&ty, ImplTraitContext::disallowed()))
2365-
.unwrap_or_else(|| P(mk_tup(this, hir::HirVec::new(), span))),
2363+
kind: hir::TypeBindingKind::Equality {
2364+
ty: output
2365+
.as_ref()
2366+
.map(|ty| this.lower_ty(
2367+
&ty,
2368+
ImplTraitContext::disallowed()
2369+
))
2370+
.unwrap_or_else(||
2371+
P(mk_tup(this, hir::HirVec::new(), span))
2372+
),
2373+
},
23662374
span: output.as_ref().map_or(span, |ty| ty.span),
23672375
}
23682376
],
@@ -2666,7 +2674,9 @@ impl<'a> LoweringContext<'a> {
26662674
args: hir_vec![],
26672675
bindings: hir_vec![hir::TypeBinding {
26682676
ident: Ident::with_empty_ctxt(FN_OUTPUT_NAME),
2669-
ty: output_ty,
2677+
kind: hir::TypeBindingKind::Equality {
2678+
ty: output_ty,
2679+
},
26702680
hir_id: self.next_id(),
26712681
span,
26722682
}],

src/librustc/hir/mod.rs

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1780,7 +1780,7 @@ pub struct ImplItem {
17801780
pub span: Span,
17811781
}
17821782

1783-
/// Represents different contents within `impl`s.
1783+
/// Represents various kinds of content within an `impl`.
17841784
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
17851785
pub enum ImplItemKind {
17861786
/// An associated constant of the given type, set to the constant result
@@ -1794,16 +1794,51 @@ pub enum ImplItemKind {
17941794
Existential(GenericBounds),
17951795
}
17961796

1797-
// Bind a type to an associated type (`A = Foo`).
1797+
/// Bind a type to an associated type (i.e., `A = Foo`).
1798+
///
1799+
/// Bindings like `A: Debug` are represented as a special type `A =
1800+
/// $::Debug` that is understood by the astconv code.
1801+
///
1802+
/// FIXME(alexreg) -- why have a separate type for the binding case,
1803+
/// wouldn't it be better to make the `ty` field an enum like:
1804+
///
1805+
/// ```
1806+
/// enum TypeBindingKind {
1807+
/// Equals(...),
1808+
/// Binding(...),
1809+
/// }
1810+
/// ```
17981811
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
17991812
pub struct TypeBinding {
18001813
pub hir_id: HirId,
18011814
#[stable_hasher(project(name))]
18021815
pub ident: Ident,
1803-
pub ty: P<Ty>,
1816+
pub kind: TypeBindingKind,
18041817
pub span: Span,
18051818
}
18061819

1820+
// Represents the two kinds of type bindings.
1821+
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
1822+
pub enum TypeBindingKind {
1823+
/// E.g., `Foo<Bar: Send>`.
1824+
Constraint {
1825+
bounds: HirVec<GenericBound>,
1826+
},
1827+
/// E.g., `Foo<Bar = ()>`.
1828+
Equality {
1829+
ty: P<Ty>,
1830+
},
1831+
}
1832+
1833+
impl TypeBinding {
1834+
pub fn ty(&self) -> &Ty {
1835+
match self.kind {
1836+
TypeBindingKind::Equality { ref ty } => ty,
1837+
_ => bug!("expected equality type binding for parenthesized generic args"),
1838+
}
1839+
}
1840+
}
1841+
18071842
#[derive(Clone, RustcEncodable, RustcDecodable)]
18081843
pub struct Ty {
18091844
pub hir_id: HirId,
@@ -1898,8 +1933,6 @@ pub enum TyKind {
18981933
/// Placeholder for C-variadic arguments. We "spoof" the `VaList` created
18991934
/// from the variadic arguments. This type is only valid up to typeck.
19001935
CVarArgs(Lifetime),
1901-
/// The existential type (i.e., `impl Trait`) that constrains an associated type.
1902-
AssocTyExistential(HirVec<GenericBound>),
19031936
}
19041937

19051938
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
@@ -2236,18 +2269,18 @@ impl StructField {
22362269
}
22372270
}
22382271

2239-
/// Fields and constructor ids of enum variants and structs
2272+
/// Fields and constructor IDs of enum variants and structs.
22402273
#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable)]
22412274
pub enum VariantData {
2242-
/// Struct variant.
2275+
/// A struct variant.
22432276
///
2244-
/// e.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
2277+
/// E.g., `Bar { .. }` as in `enum Foo { Bar { .. } }`.
22452278
Struct(HirVec<StructField>, /* recovered */ bool),
2246-
/// Tuple variant.
2279+
/// A tuple variant.
22472280
///
22482281
/// E.g., `Bar(..)` as in `enum Foo { Bar(..) }`.
22492282
Tuple(HirVec<StructField>, HirId),
2250-
/// Unit variant.
2283+
/// A unit variant.
22512284
///
22522285
/// E.g., `Bar = ..` as in `enum Foo { Bar = .. }`.
22532286
Unit(HirId),

src/librustc/hir/print.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,6 @@ impl<'a> State<'a> {
409409
hir::TyKind::CVarArgs(_) => {
410410
self.s.word("...")?;
411411
}
412-
hir::TyKind::AssocTyExistential(ref bounds) => {
413-
self.print_bounds(":", bounds)?;
414-
}
415412
}
416413
self.end()
417414
}
@@ -1648,7 +1645,7 @@ impl<'a> State<'a> {
16481645

16491646
self.space_if_not_bol()?;
16501647
self.word_space("->")?;
1651-
self.print_type(&generic_args.bindings[0].ty)?;
1648+
self.print_type(generic_args.bindings[0].ty())?;
16521649
} else {
16531650
let start = if colons_before_params { "::<" } else { "<" };
16541651
let empty = Cell::new(true);
@@ -1693,8 +1690,15 @@ impl<'a> State<'a> {
16931690
start_or_comma(self)?;
16941691
self.print_ident(binding.ident)?;
16951692
self.s.space()?;
1696-
self.word_space("=")?;
1697-
self.print_type(&binding.ty)?;
1693+
match generic_args.bindings[0].kind {
1694+
hir::TypeBindingKind::Equality { ref ty } => {
1695+
self.word_space("=")?;
1696+
self.print_type(ty)?;
1697+
}
1698+
hir::TypeBindingKind::Constraint { ref bounds } => {
1699+
self.print_bounds(":", bounds)?;
1700+
}
1701+
}
16981702
}
16991703

17001704
if !empty.get() {

src/librustc/middle/resolve_lifetime.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -923,7 +923,7 @@ impl<'a, 'tcx> Visitor<'tcx> for LifetimeContext<'a, 'tcx> {
923923
fn visit_fn_decl(&mut self, fd: &'tcx hir::FnDecl) {
924924
let output = match fd.output {
925925
hir::DefaultReturn(_) => None,
926-
hir::Return(ref ty) => Some(ty),
926+
hir::Return(ref ty) => Some(&**ty),
927927
};
928928
self.visit_fn_like_elision(&fd.inputs, output);
929929
}
@@ -1884,7 +1884,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
18841884
if generic_args.parenthesized {
18851885
let was_in_fn_syntax = self.is_in_fn_syntax;
18861886
self.is_in_fn_syntax = true;
1887-
self.visit_fn_like_elision(generic_args.inputs(), Some(&generic_args.bindings[0].ty));
1887+
self.visit_fn_like_elision(generic_args.inputs(), Some(generic_args.bindings[0].ty()));
18881888
self.is_in_fn_syntax = was_in_fn_syntax;
18891889
return;
18901890
}
@@ -2020,7 +2020,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
20202020
}
20212021
}
20222022

2023-
fn visit_fn_like_elision(&mut self, inputs: &'tcx [hir::Ty], output: Option<&'tcx P<hir::Ty>>) {
2023+
fn visit_fn_like_elision(&mut self, inputs: &'tcx [hir::Ty], output: Option<&'tcx hir::Ty>) {
20242024
debug!("visit_fn_like_elision: enter");
20252025
let mut arg_elide = Elide::FreshLateAnon(Cell::new(0));
20262026
let arg_scope = Scope::Elision {

src/librustc_typeck/astconv.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,11 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
710710
// back separately.
711711
let assoc_bindings = generic_args.bindings.iter()
712712
.map(|binding| {
713-
let kind = if let hir::TyKind::AssocTyExistential(ref bounds) = binding.ty.node {
714-
ConvertedBindingKind::Constraint(bounds.clone())
715-
} else {
716-
ConvertedBindingKind::Equality(self.ast_ty_to_ty(&binding.ty))
713+
let kind = match binding.kind {
714+
hir::TypeBindingKind::Equality { ref ty } =>
715+
ConvertedBindingKind::Equality(self.ast_ty_to_ty(ty)),
716+
hir::TypeBindingKind::Constraint { ref bounds } =>
717+
ConvertedBindingKind::Constraint(bounds.clone()),
717718
};
718719
ConvertedBinding {
719720
item_name: binding.ident,
@@ -2060,10 +2061,6 @@ impl<'o, 'gcx: 'tcx, 'tcx> dyn AstConv<'gcx, 'tcx> + 'o {
20602061
let region = self.ast_region_to_region(&lt, None);
20612062
tcx.type_of(va_list_did).subst(tcx, &[region.into()])
20622063
}
2063-
hir::TyKind::AssocTyExistential(..) => {
2064-
// Type is never actually used.
2065-
tcx.types.err
2066-
}
20672064
hir::TyKind::Err => {
20682065
tcx.types.err
20692066
}

src/librustdoc/clean/auto_trait.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,9 @@ impl<'a, 'tcx> AutoTraitFinder<'a, 'tcx> {
626626
} => {
627627
bindings.push(TypeBinding {
628628
name: left_name.clone(),
629-
ty: rhs,
629+
kind: TypeBindingKind::Equality {
630+
ty: rhs,
631+
},
630632
});
631633
}
632634
&mut GenericArgs::Parenthesized { .. } => {

0 commit comments

Comments
 (0)