Skip to content

Commit 8eb737b

Browse files
committed
rustc: record the target type of every adjustment.
1 parent 0f611b6 commit 8eb737b

File tree

29 files changed

+422
-582
lines changed

29 files changed

+422
-582
lines changed

src/librustc/cfg/construct.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
374374
let method_call = ty::MethodCall::expr(call_expr.id);
375375
let fn_ty = match self.tcx.tables().method_map.get(&method_call) {
376376
Some(method) => method.ty,
377-
None => self.tcx.expr_ty_adjusted(func_or_rcvr)
377+
None => self.tcx.tables().expr_ty_adjusted(func_or_rcvr)
378378
};
379379

380380
let func_or_rcvr_exit = self.expr(func_or_rcvr, pred);

src/librustc/infer/mod.rs

+3-23
Original file line numberDiff line numberDiff line change
@@ -1256,26 +1256,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12561256
self.region_vars.new_bound(debruijn)
12571257
}
12581258

1259-
/// Apply `adjustment` to the type of `expr`
1260-
pub fn adjust_expr_ty(&self,
1261-
expr: &hir::Expr,
1262-
adjustment: Option<&adjustment::AutoAdjustment<'tcx>>)
1263-
-> Ty<'tcx>
1264-
{
1265-
let raw_ty = self.expr_ty(expr);
1266-
let raw_ty = self.shallow_resolve(raw_ty);
1267-
let resolve_ty = |ty: Ty<'tcx>| self.resolve_type_vars_if_possible(&ty);
1268-
raw_ty.adjust(self.tcx,
1269-
expr.span,
1270-
expr.id,
1271-
adjustment,
1272-
|method_call| self.tables
1273-
.borrow()
1274-
.method_map
1275-
.get(&method_call)
1276-
.map(|method| resolve_ty(method.ty)))
1277-
}
1278-
12791259
/// True if errors have been reported since this infcx was
12801260
/// created. This is sometimes used as a heuristic to skip
12811261
/// reporting errors that often occur as a result of earlier
@@ -1612,7 +1592,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
16121592
}
16131593

16141594
pub fn expr_ty_adjusted(&self, expr: &hir::Expr) -> McResult<Ty<'tcx>> {
1615-
let ty = self.adjust_expr_ty(expr, self.tables.borrow().adjustments.get(&expr.id));
1595+
let ty = self.tables.borrow().expr_ty_adjusted(expr);
16161596
self.resolve_type_vars_or_error(&ty)
16171597
}
16181598

@@ -1656,9 +1636,9 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
16561636
.map(|method| method.def_id)
16571637
}
16581638

1659-
pub fn adjustments(&self) -> Ref<NodeMap<adjustment::AutoAdjustment<'tcx>>> {
1639+
pub fn adjustments(&self) -> Ref<NodeMap<adjustment::Adjustment<'tcx>>> {
16601640
fn project_adjustments<'a, 'tcx>(tables: &'a ty::Tables<'tcx>)
1661-
-> &'a NodeMap<adjustment::AutoAdjustment<'tcx>> {
1641+
-> &'a NodeMap<adjustment::Adjustment<'tcx>> {
16621642
&tables.adjustments
16631643
}
16641644

src/librustc/middle/dead.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
128128
}
129129

130130
fn handle_field_access(&mut self, lhs: &hir::Expr, name: ast::Name) {
131-
match self.tcx.expr_ty_adjusted(lhs).sty {
131+
match self.tcx.tables().expr_ty_adjusted(lhs).sty {
132132
ty::TyAdt(def, _) => {
133133
self.insert_def_id(def.struct_variant().field_named(name).did);
134134
}
@@ -137,7 +137,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
137137
}
138138

139139
fn handle_tup_field_access(&mut self, lhs: &hir::Expr, idx: usize) {
140-
match self.tcx.expr_ty_adjusted(lhs).sty {
140+
match self.tcx.tables().expr_ty_adjusted(lhs).sty {
141141
ty::TyAdt(def, _) => {
142142
self.insert_def_id(def.struct_variant().fields[idx].did);
143143
}

src/librustc/middle/effect.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,15 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
168168
}
169169
}
170170
hir::ExprCall(ref base, _) => {
171-
let base_type = self.tcx.expr_ty_adjusted(base);
171+
let base_type = self.tcx.tables().expr_ty_adjusted(base);
172172
debug!("effect: call case, base type is {:?}",
173173
base_type);
174174
if type_is_unsafe_function(base_type) {
175175
self.require_unsafe(expr.span, "call to unsafe function")
176176
}
177177
}
178178
hir::ExprUnary(hir::UnDeref, ref base) => {
179-
let base_type = self.tcx.expr_ty_adjusted(base);
179+
let base_type = self.tcx.tables().expr_ty_adjusted(base);
180180
debug!("effect: unary case, base type is {:?}",
181181
base_type);
182182
if let ty::TyRawPtr(_) = base_type.sty {
@@ -200,7 +200,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
200200
}
201201
}
202202
hir::ExprField(ref base_expr, field) => {
203-
if let ty::TyAdt(adt, ..) = self.tcx.expr_ty_adjusted(base_expr).sty {
203+
if let ty::TyAdt(adt, ..) = self.tcx.tables().expr_ty_adjusted(base_expr).sty {
204204
if adt.is_union() {
205205
self.require_unsafe(field.span, "access to union field");
206206
}

src/librustc/middle/expr_use_visitor.rs

+23-32
Original file line numberDiff line numberDiff line change
@@ -720,20 +720,33 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
720720
//NOTE(@jroesch): mixed RefCell borrow causes crash
721721
let adj = infcx.adjustments().get(&expr.id).map(|x| x.clone());
722722
if let Some(adjustment) = adj {
723-
match adjustment {
724-
adjustment::AdjustNeverToAny(..) |
725-
adjustment::AdjustReifyFnPointer |
726-
adjustment::AdjustUnsafeFnPointer |
727-
adjustment::AdjustMutToConstPointer => {
723+
match adjustment.kind {
724+
adjustment::Adjust::NeverToAny |
725+
adjustment::Adjust::ReifyFnPointer |
726+
adjustment::Adjust::UnsafeFnPointer |
727+
adjustment::Adjust::MutToConstPointer => {
728728
// Creating a closure/fn-pointer or unsizing consumes
729729
// the input and stores it into the resulting rvalue.
730730
debug!("walk_adjustment: trivial adjustment");
731731
let cmt_unadjusted =
732732
return_if_err!(self.mc.cat_expr_unadjusted(expr));
733733
self.delegate_consume(expr.id, expr.span, cmt_unadjusted);
734734
}
735-
adjustment::AdjustDerefRef(ref adj) => {
736-
self.walk_autoderefref(expr, adj);
735+
adjustment::Adjust::DerefRef { autoderefs, autoref, unsize } => {
736+
debug!("walk_adjustment expr={:?} adj={:?}", expr, adjustment);
737+
738+
self.walk_autoderefs(expr, autoderefs);
739+
740+
let cmt_derefd =
741+
return_if_err!(self.mc.cat_expr_autoderefd(expr, autoderefs));
742+
743+
let cmt_refd =
744+
self.walk_autoref(expr, cmt_derefd, autoref);
745+
746+
if unsize {
747+
// Unsizing consumes the thin pointer and produces a fat one.
748+
self.delegate_consume(expr.id, expr.span, cmt_refd);
749+
}
737750
}
738751
}
739752
}
@@ -770,28 +783,6 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
770783
}
771784
}
772785

773-
fn walk_autoderefref(&mut self,
774-
expr: &hir::Expr,
775-
adj: &adjustment::AutoDerefRef<'tcx>) {
776-
debug!("walk_autoderefref expr={:?} adj={:?}",
777-
expr,
778-
adj);
779-
780-
self.walk_autoderefs(expr, adj.autoderefs);
781-
782-
let cmt_derefd =
783-
return_if_err!(self.mc.cat_expr_autoderefd(expr, adj.autoderefs));
784-
785-
let cmt_refd =
786-
self.walk_autoref(expr, cmt_derefd, adj.autoref);
787-
788-
if adj.unsize.is_some() {
789-
// Unsizing consumes the thin pointer and produces a fat one.
790-
self.delegate_consume(expr.id, expr.span, cmt_refd);
791-
}
792-
}
793-
794-
795786
/// Walks the autoref `opt_autoref` applied to the autoderef'd
796787
/// `expr`. `cmt_derefd` is the mem-categorized form of `expr`
797788
/// after all relevant autoderefs have occurred. Because AutoRefs
@@ -803,7 +794,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
803794
fn walk_autoref(&mut self,
804795
expr: &hir::Expr,
805796
cmt_base: mc::cmt<'tcx>,
806-
opt_autoref: Option<adjustment::AutoRef<'tcx>>)
797+
opt_autoref: Option<adjustment::AutoBorrow<'tcx>>)
807798
-> mc::cmt<'tcx>
808799
{
809800
debug!("walk_autoref(expr.id={} cmt_derefd={:?} opt_autoref={:?})",
@@ -822,7 +813,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
822813
};
823814

824815
match *autoref {
825-
adjustment::AutoPtr(r, m) => {
816+
adjustment::AutoBorrow::Ref(r, m) => {
826817
self.delegate.borrow(expr.id,
827818
expr.span,
828819
cmt_base,
@@ -831,7 +822,7 @@ impl<'a, 'gcx, 'tcx> ExprUseVisitor<'a, 'gcx, 'tcx> {
831822
AutoRef);
832823
}
833824

834-
adjustment::AutoUnsafe(m) => {
825+
adjustment::AutoBorrow::RawPtr(m) => {
835826
debug!("walk_autoref: expr.id={} cmt_base={:?}",
836827
expr.id,
837828
cmt_base);

src/librustc/middle/liveness.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1114,7 +1114,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11141114
hir::ExprCall(ref f, ref args) => {
11151115
// FIXME(canndrew): This is_never should really be an is_uninhabited
11161116
let diverges = !self.ir.tcx.tables().is_method_call(expr.id) &&
1117-
self.ir.tcx.expr_ty_adjusted(&f).fn_ret().0.is_never();
1117+
self.ir.tcx.tables().expr_ty_adjusted(&f).fn_ret().0.is_never();
11181118
let succ = if diverges {
11191119
self.s.exit_ln
11201120
} else {

src/librustc/middle/mem_categorization.rs

+12-14
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
354354
}
355355

356356
fn expr_ty_adjusted(&self, expr: &hir::Expr) -> McResult<Ty<'tcx>> {
357-
let unadjusted_ty = self.expr_ty(expr)?;
358-
Ok(unadjusted_ty.adjust(
359-
self.tcx(), expr.span, expr.id,
360-
self.infcx.adjustments().get(&expr.id),
361-
|method_call| self.infcx.node_method_ty(method_call)))
357+
self.infcx.expr_ty_adjusted(expr)
362358
}
363359

364360
fn node_ty(&self, id: ast::NodeId) -> McResult<Ty<'tcx>> {
@@ -396,19 +392,21 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
396392
}
397393

398394
Some(adjustment) => {
399-
match *adjustment {
400-
adjustment::AdjustDerefRef(
401-
adjustment::AutoDerefRef {
402-
autoref: None, unsize: None, autoderefs, ..}) => {
395+
match adjustment.kind {
396+
adjustment::Adjust::DerefRef {
397+
autoderefs,
398+
autoref: None,
399+
unsize: false
400+
} => {
403401
// Equivalent to *expr or something similar.
404402
self.cat_expr_autoderefd(expr, autoderefs)
405403
}
406404

407-
adjustment::AdjustNeverToAny(..) |
408-
adjustment::AdjustReifyFnPointer |
409-
adjustment::AdjustUnsafeFnPointer |
410-
adjustment::AdjustMutToConstPointer |
411-
adjustment::AdjustDerefRef(_) => {
405+
adjustment::Adjust::NeverToAny |
406+
adjustment::Adjust::ReifyFnPointer |
407+
adjustment::Adjust::UnsafeFnPointer |
408+
adjustment::Adjust::MutToConstPointer |
409+
adjustment::Adjust::DerefRef {..} => {
412410
debug!("cat_expr({:?}): {:?}",
413411
adjustment,
414412
expr);

src/librustc/middle/stability.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr,
559559
}
560560
hir::ExprField(ref base_e, ref field) => {
561561
span = field.span;
562-
match tcx.expr_ty_adjusted(base_e).sty {
562+
match tcx.tables().expr_ty_adjusted(base_e).sty {
563563
ty::TyAdt(def, _) => {
564564
def.struct_variant().field_named(field.node).did
565565
}
@@ -569,7 +569,7 @@ pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr,
569569
}
570570
hir::ExprTupField(ref base_e, ref field) => {
571571
span = field.span;
572-
match tcx.expr_ty_adjusted(base_e).sty {
572+
match tcx.tables().expr_ty_adjusted(base_e).sty {
573573
ty::TyAdt(def, _) => {
574574
def.struct_variant().fields[field.node].did
575575
}

0 commit comments

Comments
 (0)