Skip to content

Commit

Permalink
Auto merge of #42281 - eddyb:well-adjusted, r=nikomatsakis
Browse files Browse the repository at this point in the history
Decompose Adjustment into smaller steps and remove the method map.

The method map held method callee information for:
* actual method calls (`x.f(...)`)
* overloaded unary, binary, indexing and call operators
* *every overloaded deref adjustment* (many can exist for each expression)

That last one was a historical ~~accident~~ hack, and part of the motivation for this PR, along with:
* a desire to compose adjustments more freely
* containing the autoderef logic better to avoid mutation within an inference snapshot
* not creating `TyFnDef` types which are incompatible with the original one
  * i.e. we used to take a`TyFnDef`'s `for<'a> &'a T -> &'a U` signature and instantiate `'a` using a region inference variable, *then* package the resulting `&'b T -> &'b U` signature in another `TyFnDef`, while keeping *the same* `DefId` and `Substs`
* to fix #3548 by explicitly writing autorefs for the RHS of comparison operators

Individual commits tell their own story, of "atomic" changes avoiding breaking semantics.

Future work based on this PR could include:
* removing the signature from `TyFnDef`, now that it's always "canonical"
  * some questions of variance remain, as subtyping *still* treats the signature differently
* moving part of the typeck logic for methods, autoderef and coercion into `rustc::traits`
* allowing LUB coercions (joining multiple expressions) to "stack up" many adjustments
* transitive coercions (e.g. reify or unsize after multiple steps of autoderef)

r? @nikomatsakis
  • Loading branch information
bors committed Jun 1, 2017
2 parents afd4b81 + 5fb37be commit 4ed2eda
Show file tree
Hide file tree
Showing 49 changed files with 1,479 additions and 2,147 deletions.
12 changes: 6 additions & 6 deletions src/libcollections/tests/binary_heap.rs
Expand Up @@ -134,22 +134,22 @@ fn test_push() {
fn test_push_unique() {
let mut heap = BinaryHeap::<Box<_>>::from(vec![box 2, box 4, box 9]);
assert_eq!(heap.len(), 3);
assert!(*heap.peek().unwrap() == box 9);
assert!(**heap.peek().unwrap() == 9);
heap.push(box 11);
assert_eq!(heap.len(), 4);
assert!(*heap.peek().unwrap() == box 11);
assert!(**heap.peek().unwrap() == 11);
heap.push(box 5);
assert_eq!(heap.len(), 5);
assert!(*heap.peek().unwrap() == box 11);
assert!(**heap.peek().unwrap() == 11);
heap.push(box 27);
assert_eq!(heap.len(), 6);
assert!(*heap.peek().unwrap() == box 27);
assert!(**heap.peek().unwrap() == 27);
heap.push(box 3);
assert_eq!(heap.len(), 7);
assert!(*heap.peek().unwrap() == box 27);
assert!(**heap.peek().unwrap() == 27);
heap.push(box 103);
assert_eq!(heap.len(), 8);
assert!(*heap.peek().unwrap() == box 103);
assert!(**heap.peek().unwrap() == 103);
}

fn check_to_vec(mut data: Vec<i32>) {
Expand Down
12 changes: 3 additions & 9 deletions src/librustc/cfg/construct.rs
Expand Up @@ -355,11 +355,11 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
}

hir::ExprIndex(ref l, ref r) |
hir::ExprBinary(_, ref l, ref r) if self.tables.is_method_call(expr.id) => {
hir::ExprBinary(_, ref l, ref r) if self.tables.is_method_call(expr) => {
self.call(expr, pred, &l, Some(&**r).into_iter())
}

hir::ExprUnary(_, ref e) if self.tables.is_method_call(expr.id) => {
hir::ExprUnary(_, ref e) if self.tables.is_method_call(expr) => {
self.call(expr, pred, &e, None::<hir::Expr>.iter())
}

Expand Down Expand Up @@ -412,16 +412,10 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
pred: CFGIndex,
func_or_rcvr: &hir::Expr,
args: I) -> CFGIndex {
let method_call = ty::MethodCall::expr(call_expr.id);
let fn_ty = match self.tables.method_map.get(&method_call) {
Some(method) => method.ty,
None => self.tables.expr_ty_adjusted(func_or_rcvr),
};

let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);
let ret = self.straightline(call_expr, func_or_rcvr_exit, args);
// FIXME(canndrew): This is_never should probably be an is_uninhabited.
if fn_ty.fn_ret().0.is_never() {
if self.tables.expr_ty(call_expr).is_never() {
self.add_unreachable_node()
} else {
ret
Expand Down
35 changes: 11 additions & 24 deletions src/librustc/ich/impls_ty.rs
Expand Up @@ -19,8 +19,6 @@ use std::mem;
use syntax_pos::symbol::InternedString;
use ty;

impl_stable_hash_for!(struct ty::ItemSubsts<'tcx> { substs });

impl<'a, 'tcx, T> HashStable<StableHashingContext<'a, 'tcx>> for &'tcx ty::Slice<T>
where T: HashStable<StableHashingContext<'a, 'tcx>> {
fn hash_stable<W: StableHasherResult>(&self,
Expand Down Expand Up @@ -101,19 +99,20 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::adjustment::Ad
ty::adjustment::Adjust::ReifyFnPointer |
ty::adjustment::Adjust::UnsafeFnPointer |
ty::adjustment::Adjust::ClosureFnPointer |
ty::adjustment::Adjust::MutToConstPointer => {}
ty::adjustment::Adjust::DerefRef { autoderefs, ref autoref, unsize } => {
autoderefs.hash_stable(hcx, hasher);
ty::adjustment::Adjust::MutToConstPointer |
ty::adjustment::Adjust::Unsize => {}
ty::adjustment::Adjust::Deref(ref overloaded) => {
overloaded.hash_stable(hcx, hasher);
}
ty::adjustment::Adjust::Borrow(ref autoref) => {
autoref.hash_stable(hcx, hasher);
unsize.hash_stable(hcx, hasher);
}
}
}
}

impl_stable_hash_for!(struct ty::adjustment::Adjustment<'tcx> { kind, target });
impl_stable_hash_for!(struct ty::MethodCall { expr_id, autoderef });
impl_stable_hash_for!(struct ty::MethodCallee<'tcx> { def_id, ty, substs });
impl_stable_hash_for!(struct ty::adjustment::OverloadedDeref<'tcx> { region, mutbl });
impl_stable_hash_for!(struct ty::UpvarId { var_id, closure_expr_id });
impl_stable_hash_for!(struct ty::UpvarBorrow<'tcx> { kind, region });

Expand Down Expand Up @@ -601,11 +600,10 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::TypeckTables<'
hcx: &mut StableHashingContext<'a, 'tcx>,
hasher: &mut StableHasher<W>) {
let ty::TypeckTables {
ref type_relative_path_defs,
ref type_dependent_defs,
ref node_types,
ref item_substs,
ref node_substs,
ref adjustments,
ref method_map,
ref upvar_capture_map,
ref closure_tys,
ref closure_kinds,
Expand All @@ -622,21 +620,10 @@ impl<'a, 'tcx> HashStable<StableHashingContext<'a, 'tcx>> for ty::TypeckTables<'
} = *self;

hcx.with_node_id_hashing_mode(NodeIdHashingMode::HashDefPath, |hcx| {
ich::hash_stable_nodemap(hcx, hasher, type_relative_path_defs);
ich::hash_stable_nodemap(hcx, hasher, type_dependent_defs);
ich::hash_stable_nodemap(hcx, hasher, node_types);
ich::hash_stable_nodemap(hcx, hasher, item_substs);
ich::hash_stable_nodemap(hcx, hasher, node_substs);
ich::hash_stable_nodemap(hcx, hasher, adjustments);

ich::hash_stable_hashmap(hcx, hasher, method_map, |hcx, method_call| {
let ty::MethodCall {
expr_id,
autoderef
} = *method_call;

let def_id = hcx.tcx().hir.local_def_id(expr_id);
(hcx.def_path_hash(def_id), autoderef)
});

ich::hash_stable_hashmap(hcx, hasher, upvar_capture_map, |hcx, up_var_id| {
let ty::UpvarId {
var_id,
Expand Down
40 changes: 13 additions & 27 deletions src/librustc/infer/mod.rs
Expand Up @@ -564,13 +564,14 @@ impl<'tcx, T> InferOk<'tcx, T> {
}

#[must_use = "once you start a snapshot, you should always consume it"]
pub struct CombinedSnapshot {
pub struct CombinedSnapshot<'a, 'tcx:'a> {
projection_cache_snapshot: traits::ProjectionCacheSnapshot,
type_snapshot: type_variable::Snapshot,
int_snapshot: unify::Snapshot<ty::IntVid>,
float_snapshot: unify::Snapshot<ty::FloatVid>,
region_vars_snapshot: RegionSnapshot,
was_in_snapshot: bool,
_in_progress_tables: Option<Ref<'a, ty::TypeckTables<'tcx>>>,
}

/// Helper trait for shortening the lifetimes inside a
Expand Down Expand Up @@ -888,7 +889,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
result
}

fn start_snapshot(&self) -> CombinedSnapshot {
fn start_snapshot<'b>(&'b self) -> CombinedSnapshot<'b, 'tcx> {
debug!("start_snapshot()");

let in_snapshot = self.in_snapshot.get();
Expand All @@ -901,6 +902,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
float_snapshot: self.float_unification_table.borrow_mut().snapshot(),
region_vars_snapshot: self.region_vars.start_snapshot(),
was_in_snapshot: in_snapshot,
// Borrow tables "in progress" (i.e. during typeck)
// to ban writes from within a snapshot to them.
_in_progress_tables: match self.tables {
InferTables::InProgress(ref tables) => tables.try_borrow().ok(),
_ => None
}
}
}

Expand All @@ -911,7 +918,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
int_snapshot,
float_snapshot,
region_vars_snapshot,
was_in_snapshot } = snapshot;
was_in_snapshot,
_in_progress_tables } = snapshot;

self.in_snapshot.set(was_in_snapshot);

Expand All @@ -938,7 +946,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
int_snapshot,
float_snapshot,
region_vars_snapshot,
was_in_snapshot } = snapshot;
was_in_snapshot,
_in_progress_tables } = snapshot;

self.in_snapshot.set(was_in_snapshot);

Expand Down Expand Up @@ -1645,29 +1654,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
!traits::type_known_to_meet_bound(self, ty, copy_def_id, span)
}

pub fn node_method_ty(&self, method_call: ty::MethodCall)
-> Option<Ty<'tcx>> {
self.tables
.borrow()
.method_map
.get(&method_call)
.map(|method| method.ty)
.map(|ty| self.resolve_type_vars_if_possible(&ty))
}

pub fn node_method_id(&self, method_call: ty::MethodCall)
-> Option<DefId> {
self.tables
.borrow()
.method_map
.get(&method_call)
.map(|method| method.def_id)
}

pub fn is_method_call(&self, id: ast::NodeId) -> bool {
self.tables.borrow().method_map.contains_key(&ty::MethodCall::expr(id))
}

pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture<'tcx>> {
self.tables.borrow().upvar_capture_map.get(&upvar_id).cloned()
}
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/middle/dead.rs
Expand Up @@ -95,9 +95,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
}

fn lookup_and_handle_method(&mut self, id: ast::NodeId) {
let method_call = ty::MethodCall::expr(id);
let method = self.tables.method_map[&method_call];
self.check_def_id(method.def_id);
self.check_def_id(self.tables.type_dependent_defs[&id].def_id());
}

fn handle_field_access(&mut self, lhs: &hir::Expr, name: ast::Name) {
Expand Down
5 changes: 2 additions & 3 deletions src/librustc/middle/effect.rs
Expand Up @@ -13,7 +13,6 @@
use self::RootUnsafeContext::*;

use ty::{self, Ty, TyCtxt};
use ty::MethodCall;
use lint;

use syntax::ast;
Expand Down Expand Up @@ -174,8 +173,8 @@ impl<'a, 'tcx> Visitor<'tcx> for EffectCheckVisitor<'a, 'tcx> {
fn visit_expr(&mut self, expr: &'tcx hir::Expr) {
match expr.node {
hir::ExprMethodCall(..) => {
let method_call = MethodCall::expr(expr.id);
let base_type = self.tables.method_map[&method_call].ty;
let def_id = self.tables.type_dependent_defs[&expr.id].def_id();
let base_type = self.tcx.type_of(def_id);
debug!("effect: method call case, base type is {:?}",
base_type);
if type_is_unsafe_function(base_type) {
Expand Down

0 comments on commit 4ed2eda

Please sign in to comment.