Skip to content

Commit 6132806

Browse files
committed
Correct reasoning behind writeback ref removal
The ref was actually always necessary. This clarifies some incorrect thinking in the original code that might have led to errors in the future to do with unsizing.
1 parent 2133ace commit 6132806

File tree

1 file changed

+30
-34
lines changed

1 file changed

+30
-34
lines changed

src/librustc_typeck/check/writeback.rs

+30-34
Original file line numberDiff line numberDiff line change
@@ -169,41 +169,37 @@ impl<'cx, 'gcx, 'tcx> WritebackCx<'cx, 'gcx, 'tcx> {
169169
if let hir::ExprIndex(ref base, ref index) = e.node {
170170
let mut tables = self.fcx.tables.borrow_mut();
171171

172-
let base_ty = {
173-
let base_ty = tables.expr_ty_adjusted(&base);
174-
// When unsizing, the final type of the expression is taken
175-
// from the first argument of the indexing operator, which
176-
// is a &self, and has to be deconstructed
177-
if let ty::TyRef(_, ref ref_to) = base_ty.sty {
178-
ref_to.ty
179-
} else {
180-
base_ty
181-
}
182-
};
183-
184-
let index_ty = tables.expr_ty_adjusted(&index);
185-
let index_ty = self.fcx.resolve_type_vars_if_possible(&index_ty);
186-
187-
if base_ty.builtin_index().is_some()
188-
&& index_ty == self.fcx.tcx.types.usize {
189-
// Remove the method call record
190-
tables.type_dependent_defs_mut().remove(e.hir_id);
191-
tables.node_substs_mut().remove(e.hir_id);
192-
193-
tables.adjustments_mut().get_mut(base.hir_id).map(|a| {
194-
// Discard the need for a mutable borrow
195-
match a.pop() {
196-
// Extra adjustment made when indexing causes a drop
197-
// of size information - we need to get rid of it
198-
// Since this is "after" the other adjustment to be
199-
// discarded, we do an extra `pop()`
200-
Some(Adjustment { kind: Adjust::Unsize, .. }) => {
201-
// So the borrow discard actually happens here
202-
a.pop();
203-
},
204-
_ => {}
172+
match tables.expr_ty_adjusted(&base).sty {
173+
// All valid indexing looks like this
174+
ty::TyRef(_, ty::TypeAndMut { ty: ref base_ty, .. }) => {
175+
let index_ty = tables.expr_ty_adjusted(&index);
176+
let index_ty = self.fcx.resolve_type_vars_if_possible(&index_ty);
177+
178+
if base_ty.builtin_index().is_some()
179+
&& index_ty == self.fcx.tcx.types.usize {
180+
// Remove the method call record
181+
tables.type_dependent_defs_mut().remove(e.hir_id);
182+
tables.node_substs_mut().remove(e.hir_id);
183+
184+
tables.adjustments_mut().get_mut(base.hir_id).map(|a| {
185+
// Discard the need for a mutable borrow
186+
match a.pop() {
187+
// Extra adjustment made when indexing causes a drop
188+
// of size information - we need to get rid of it
189+
// Since this is "after" the other adjustment to be
190+
// discarded, we do an extra `pop()`
191+
Some(Adjustment { kind: Adjust::Unsize, .. }) => {
192+
// So the borrow discard actually happens here
193+
a.pop();
194+
},
195+
_ => {}
196+
}
197+
});
205198
}
206-
});
199+
},
200+
// Might encounter non-valid indexes at this point, so there
201+
// has to be a fall-through
202+
_ => {},
207203
}
208204
}
209205
}

0 commit comments

Comments
 (0)