Skip to content

Commit 9e1a078

Browse files
committed
clean visit_expr
1 parent 6a95d90 commit 9e1a078

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

src/librustc_typeck/check/writeback.rs

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,32 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
8585
fn tcx(&self) -> &'cx ty::ctxt<'tcx> {
8686
self.fcx.tcx()
8787
}
88+
89+
// Hacky hack: During type-checking, we treat *all* operators
90+
// as potentially overloaded. But then, during writeback, if
91+
// we observe that something like `a+b` is (known to be)
92+
// operating on scalars, we clear the overload.
93+
fn fix_scalar_binary_expr(&mut self, e: &ast::Expr) {
94+
if let ast::ExprBinary(ref op, ref lhs, ref rhs) = e.node {
95+
let lhs_ty = self.fcx.expr_ty(lhs.id);
96+
let lhs_ty = self.fcx.infcx().resolve_type_vars_if_possible(&lhs_ty);
97+
98+
let rhs_ty = self.fcx.expr_ty(rhs.id);
99+
let rhs_ty = self.fcx.infcx().resolve_type_vars_if_possible(&rhs_ty);
100+
101+
if ty::type_is_scalar(lhs_ty) && ty::type_is_scalar(rhs_ty) {
102+
self.fcx.inh.method_map.borrow_mut().remove(&MethodCall::expr(e.id));
103+
104+
// weird but true: the by-ref binops put an
105+
// adjustment on the lhs but not the rhs; the
106+
// adjustment for rhs is kind of baked into the
107+
// system.
108+
if !ast_util::is_by_value_binop(op.node) {
109+
self.fcx.inh.adjustments.borrow_mut().remove(&lhs.id);
110+
}
111+
}
112+
}
113+
}
88114
}
89115

90116
///////////////////////////////////////////////////////////////////////////
@@ -114,43 +140,16 @@ impl<'cx, 'tcx, 'v> Visitor<'v> for WritebackCx<'cx, 'tcx> {
114140
return;
115141
}
116142

117-
// Hacky hack: During type-checking, we treat *all* operators
118-
// as potentially overloaded. But then, during writeback, if
119-
// we observe that something like `a+b` is (known to be)
120-
// operating on scalars, we clear the overload.
121-
match e.node {
122-
ast::ExprBinary(ref op, ref lhs, ref rhs) => {
123-
let lhs_ty = self.fcx.expr_ty(lhs);
124-
let lhs_ty = self.fcx.infcx().resolve_type_vars_if_possible(&lhs_ty);
125-
let rhs_ty = self.fcx.expr_ty(rhs);
126-
let rhs_ty = self.fcx.infcx().resolve_type_vars_if_possible(&rhs_ty);
127-
if ty::type_is_scalar(lhs_ty) && ty::type_is_scalar(rhs_ty) {
128-
self.fcx.inh.method_map.borrow_mut().remove(&MethodCall::expr(e.id));
129-
130-
// weird but true: the by-ref binops put an
131-
// adjustment on the lhs but not the rhs; the
132-
// adjustment for rhs is kind of baked into the
133-
// system.
134-
if !ast_util::is_by_value_binop(op.node) {
135-
self.fcx.inh.adjustments.borrow_mut().remove(&lhs.id);
136-
}
137-
}
138-
}
139-
_ => { }
140-
}
143+
self.fix_scalar_binary_expr(e);
141144

142145
self.visit_node_id(ResolvingExpr(e.span), e.id);
143146
self.visit_method_map_entry(ResolvingExpr(e.span),
144147
MethodCall::expr(e.id));
145148

146-
match e.node {
147-
ast::ExprClosure(_, ref decl, _) => {
148-
for input in &decl.inputs {
149-
let _ = self.visit_node_id(ResolvingExpr(e.span),
150-
input.id);
151-
}
149+
if let ast::ExprClosure(_, ref decl, _) = e.node {
150+
for input in &decl.inputs {
151+
self.visit_node_id(ResolvingExpr(e.span), input.id);
152152
}
153-
_ => {}
154153
}
155154

156155
visit::walk_expr(self, e);

0 commit comments

Comments
 (0)