Skip to content

Commit 1884fe3

Browse files
committed
add a user_substs map into the typeck tables
1 parent 61b0072 commit 1884fe3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/librustc/ty/context.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use middle::resolve_lifetime::{self, ObjectLifetimeDefault};
3333
use middle::stability;
3434
use mir::{self, Mir, interpret};
3535
use mir::interpret::Allocation;
36-
use ty::subst::{Kind, Substs, Subst};
36+
use ty::subst::{CanonicalSubsts, Kind, Substs, Subst};
3737
use ty::ReprOptions;
3838
use traits;
3939
use traits::{Clause, Clauses, Goal, Goals};
@@ -371,6 +371,18 @@ pub struct TypeckTables<'tcx> {
371371
/// other items.
372372
node_substs: ItemLocalMap<&'tcx Substs<'tcx>>,
373373

374+
/// Stores the substitutions that the user explicit gave (if any)
375+
/// attached to `id`. These will not include any inferred
376+
/// values. The canonical form is used to capture things like `_`
377+
/// or other unspecified values.
378+
///
379+
/// Example:
380+
///
381+
/// If the user wrote `foo.collect::<Vec<_>>()`, then the
382+
/// canonical substitutions would include only `for<X> { Vec<X>
383+
/// }`.
384+
user_substs: ItemLocalMap<CanonicalSubsts<'tcx>>,
385+
374386
adjustments: ItemLocalMap<Vec<ty::adjustment::Adjustment<'tcx>>>,
375387

376388
/// Stores the actual binding mode for all instances of hir::BindingAnnotation.
@@ -444,6 +456,7 @@ impl<'tcx> TypeckTables<'tcx> {
444456
user_provided_tys: ItemLocalMap(),
445457
node_types: ItemLocalMap(),
446458
node_substs: ItemLocalMap(),
459+
user_substs: ItemLocalMap(),
447460
adjustments: ItemLocalMap(),
448461
pat_binding_modes: ItemLocalMap(),
449462
pat_adjustments: ItemLocalMap(),
@@ -561,6 +574,18 @@ impl<'tcx> TypeckTables<'tcx> {
561574
self.node_substs.get(&id.local_id).cloned()
562575
}
563576

577+
pub fn user_substs_mut(&mut self) -> LocalTableInContextMut<CanonicalSubsts<'tcx>> {
578+
LocalTableInContextMut {
579+
local_id_root: self.local_id_root,
580+
data: &mut self.user_substs
581+
}
582+
}
583+
584+
pub fn user_substs(&self, id: hir::HirId) -> Option<CanonicalSubsts<'tcx>> {
585+
validate_hir_id_for_typeck_tables(self.local_id_root, id, false);
586+
self.user_substs.get(&id.local_id).cloned()
587+
}
588+
564589
// Returns the type of a pattern as a monotype. Like @expr_ty, this function
565590
// doesn't provide type parameter substitutions.
566591
pub fn pat_ty(&self, pat: &hir::Pat) -> Ty<'tcx> {
@@ -740,6 +765,7 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
740765
ref user_provided_tys,
741766
ref node_types,
742767
ref node_substs,
768+
ref user_substs,
743769
ref adjustments,
744770
ref pat_binding_modes,
745771
ref pat_adjustments,
@@ -762,6 +788,7 @@ impl<'a, 'gcx> HashStable<StableHashingContext<'a>> for TypeckTables<'gcx> {
762788
user_provided_tys.hash_stable(hcx, hasher);
763789
node_types.hash_stable(hcx, hasher);
764790
node_substs.hash_stable(hcx, hasher);
791+
user_substs.hash_stable(hcx, hasher);
765792
adjustments.hash_stable(hcx, hasher);
766793
pat_binding_modes.hash_stable(hcx, hasher);
767794
pat_adjustments.hash_stable(hcx, hasher);

src/librustc/ty/subst.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// Type substitutions.
1212

1313
use hir::def_id::DefId;
14+
use infer::canonical::Canonical;
1415
use ty::{self, Lift, List, Ty, TyCtxt};
1516
use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1617

@@ -179,6 +180,8 @@ impl<'tcx> Decodable for Kind<'tcx> {
179180
/// A substitution mapping generic parameters to new values.
180181
pub type Substs<'tcx> = List<Kind<'tcx>>;
181182

183+
pub type CanonicalSubsts<'gcx> = Canonical<'gcx, &'gcx Substs<'gcx>>;
184+
182185
impl<'a, 'gcx, 'tcx> Substs<'tcx> {
183186
/// Creates a Substs that maps each generic parameter to itself.
184187
pub fn identity_for_item(tcx: TyCtxt<'a, 'gcx, 'tcx>, def_id: DefId)

0 commit comments

Comments
 (0)