Skip to content

Commit 7c18b01

Browse files
committed
rename assign to coerce, remove some bad copies
1 parent c43b72c commit 7c18b01

File tree

4 files changed

+78
-84
lines changed

4 files changed

+78
-84
lines changed

src/librustc/middle/typeck/check/demand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ fn eqtype(fcx: @fn_ctxt, sp: span, expected: ty::t, actual: ty::t) {
4949
}
5050
}
5151

52-
// Checks that the type `actual` can be assigned to `expected`.
53-
fn assign(fcx: @fn_ctxt, sp: span, expected: ty::t, expr: @ast::expr) {
52+
// Checks that the type `actual` can be coerced to `expected`.
53+
fn coerce(fcx: @fn_ctxt, sp: span, expected: ty::t, expr: @ast::expr) {
5454
let expr_ty = fcx.expr_ty(expr);
5555
match fcx.mk_assignty(expr, expr_ty, expected) {
5656
result::Ok(()) => { /* ok */ }

src/librustc/middle/typeck/check/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -812,7 +812,7 @@ impl @fn_ctxt {
812812
fn mk_assignty(expr: @ast::expr, sub: ty::t, sup: ty::t)
813813
-> Result<(), ty::type_err>
814814
{
815-
match infer::mk_assignty(self.infcx(), false, expr.span, sub, sup) {
815+
match infer::mk_coercety(self.infcx(), false, expr.span, sub, sup) {
816816
Ok(None) => result::Ok(()),
817817
Err(ref e) => result::Err((*e)),
818818
Ok(Some(adjustment)) => {
@@ -823,7 +823,7 @@ impl @fn_ctxt {
823823
}
824824

825825
fn can_mk_assignty(sub: ty::t, sup: ty::t) -> Result<(), ty::type_err> {
826-
infer::can_mk_assignty(self.infcx(), sub, sup)
826+
infer::can_mk_coercety(self.infcx(), sub, sup)
827827
}
828828

829829
fn mk_eqty(a_is_expected: bool, span: span,
@@ -986,12 +986,12 @@ fn check_expr_has_type(
986986
}
987987
}
988988

989-
fn check_expr_assignable_to_type(
989+
fn check_expr_coercable_to_type(
990990
fcx: @fn_ctxt, expr: @ast::expr,
991991
expected: ty::t) -> bool
992992
{
993993
do check_expr_with_unifier(fcx, expr, Some(expected)) {
994-
demand::assign(fcx, expr.span, expected, expr)
994+
demand::coerce(fcx, expr.span, expected, expr)
995995
}
996996
}
997997

@@ -1225,7 +1225,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
12251225
}
12261226

12271227
// mismatch error happens in here
1228-
bot |= check_expr_assignable_to_type(
1228+
bot |= check_expr_coercable_to_type(
12291229
fcx, *arg, formal_ty);
12301230

12311231
}
@@ -1243,7 +1243,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
12431243
-> bool {
12441244
let mut bot = check_expr(fcx, lhs);
12451245
let lhs_type = fcx.expr_ty(lhs);
1246-
bot |= check_expr_assignable_to_type(fcx, rhs, lhs_type);
1246+
bot |= check_expr_has_type(fcx, rhs, lhs_type);
12471247
fcx.write_ty(id, ty::mk_nil(fcx.ccx.tcx));
12481248
return bot;
12491249
}
@@ -1739,7 +1739,7 @@ fn check_expr_with_unifier(fcx: @fn_ctxt,
17391739
ty::lookup_field_type(
17401740
tcx, class_id, field_id, substitutions);
17411741
bot |=
1742-
check_expr_assignable_to_type(
1742+
check_expr_coercable_to_type(
17431743
fcx,
17441744
field.node.expr,
17451745
expected_field_type);
@@ -2552,7 +2552,7 @@ fn require_integral(fcx: @fn_ctxt, sp: span, t: ty::t) {
25522552
fn check_decl_initializer(fcx: @fn_ctxt, nid: ast::node_id,
25532553
init: @ast::expr) -> bool {
25542554
let lty = ty::mk_var(fcx.ccx.tcx, lookup_local(fcx, init.span, nid));
2555-
return check_expr_assignable_to_type(fcx, init, lty);
2555+
return check_expr_coercable_to_type(fcx, init, lty);
25562556
}
25572557

25582558
fn check_decl_local(fcx: @fn_ctxt, local: @ast::local) -> bool {

src/librustc/middle/typeck/infer/assignment.rs renamed to src/librustc/middle/typeck/infer/coercion.rs

Lines changed: 56 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -79,73 +79,69 @@ fn to_ares<T>(+c: cres<T>) -> ares {
7979
}
8080
}
8181

82-
// Note: Assign is not actually a combiner, in that it does not
82+
// Note: Coerce is not actually a combiner, in that it does not
8383
// conform to the same interface, though it performs a similar
8484
// function.
85-
enum Assign = CombineFields;
85+
pub enum Coerce = CombineFields;
8686

87-
impl Assign {
88-
fn tys(a: ty::t, b: ty::t) -> ares {
89-
debug!("Assign.tys(%s => %s)",
87+
impl Coerce {
88+
fn tys(&self, a: ty::t, b: ty::t) -> ares {
89+
debug!("Coerce.tys(%s => %s)",
9090
a.inf_str(self.infcx),
9191
b.inf_str(self.infcx));
92-
let _r = indenter();
93-
94-
debug!("Assign.tys: copying first type");
95-
let copy_a = copy ty::get(a).sty;
96-
debug!("Assign.tys: copying second type");
97-
let copy_b = copy ty::get(b).sty;
98-
debug!("Assign.tys: performing match");
99-
100-
let r = match (copy_a, copy_b) {
101-
(ty::ty_bot, _) => {
92+
let _indent = indenter();
93+
let r = match (&ty::get(a).sty, &ty::get(b).sty) {
94+
(&ty::ty_bot, _) => {
10295
Ok(None)
10396
}
10497

105-
(ty::ty_infer(TyVar(a_id)), ty::ty_infer(TyVar(b_id))) => {
98+
(&ty::ty_infer(TyVar(a_id)), &ty::ty_infer(TyVar(b_id))) => {
10699
let nde_a = self.infcx.get(a_id);
107100
let nde_b = self.infcx.get(b_id);
108101
let a_bounds = nde_a.possible_types;
109102
let b_bounds = nde_b.possible_types;
110103

111104
let a_bnd = option::or(a_bounds.ub, a_bounds.lb);
112105
let b_bnd = option::or(b_bounds.lb, b_bounds.ub);
113-
self.assign_tys_or_sub(a, b, a_bnd, b_bnd)
106+
self.coerce_tys_or_sub(a, b, a_bnd, b_bnd)
114107
}
115108

116-
(ty::ty_infer(TyVar(a_id)), _) => {
109+
(&ty::ty_infer(TyVar(a_id)), _) => {
117110
let nde_a = self.infcx.get(a_id);
118111
let a_bounds = nde_a.possible_types;
119112

120113
let a_bnd = option::or(a_bounds.ub, a_bounds.lb);
121-
self.assign_tys_or_sub(a, b, a_bnd, Some(b))
114+
self.coerce_tys_or_sub(a, b, a_bnd, Some(b))
122115
}
123116

124-
(_, ty::ty_infer(TyVar(b_id))) => {
117+
(_, &ty::ty_infer(TyVar(b_id))) => {
125118
let nde_b = self.infcx.get(b_id);
126119
let b_bounds = nde_b.possible_types;
127120

128121
let b_bnd = option::or(b_bounds.lb, b_bounds.ub);
129-
self.assign_tys_or_sub(a, b, Some(a), b_bnd)
122+
self.coerce_tys_or_sub(a, b, Some(a), b_bnd)
130123
}
131124

132125
(_, _) => {
133-
self.assign_tys_or_sub(a, b, Some(a), Some(b))
126+
self.coerce_tys_or_sub(a, b, Some(a), Some(b))
134127
}
135128
};
136129

137-
debug!("Assign.tys end");
130+
debug!("Coerce.tys end");
138131

139132
move r
140133
}
141134
}
142135

143-
priv impl Assign {
144-
fn assign_tys_or_sub(
145-
a: ty::t, b: ty::t,
146-
+a_bnd: Option<ty::t>, +b_bnd: Option<ty::t>) -> ares {
147-
148-
debug!("Assign.assign_tys_or_sub(%s => %s, %s => %s)",
136+
impl Coerce {
137+
fn coerce_tys_or_sub(
138+
&self,
139+
+a: ty::t,
140+
+b: ty::t,
141+
+a_bnd: Option<ty::t>,
142+
+b_bnd: Option<ty::t>) -> ares
143+
{
144+
debug!("Coerce.coerce_tys_or_sub(%s => %s, %s => %s)",
149145
a.inf_str(self.infcx), b.inf_str(self.infcx),
150146
a_bnd.inf_str(self.infcx), b_bnd.inf_str(self.infcx));
151147
let _r = indenter();
@@ -167,59 +163,58 @@ priv impl Assign {
167163

168164
match (a_bnd, b_bnd) {
169165
(Some(a_bnd), Some(b_bnd)) => {
170-
match (/*bad*/copy ty::get(a_bnd).sty,
171-
/*bad*/copy ty::get(b_bnd).sty) {
166+
match (&ty::get(a_bnd).sty, &ty::get(b_bnd).sty) {
172167
// check for a case where a non-region pointer (@, ~) is
173-
// being assigned to a region pointer:
174-
(ty::ty_box(_), ty::ty_rptr(r_b, mt_b)) => {
168+
// being coerceed to a region pointer:
169+
(&ty::ty_box(_), &ty::ty_rptr(r_b, mt_b)) => {
175170
let nr_b = ty::mk_box(self.infcx.tcx,
176171
ty::mt {ty: mt_b.ty,
177172
mutbl: m_const});
178-
self.try_assign(1, ty::AutoPtr,
173+
self.try_coerce(1, ty::AutoPtr,
179174
a, nr_b,
180175
mt_b.mutbl, r_b)
181176
}
182-
(ty::ty_uniq(_), ty::ty_rptr(r_b, mt_b)) => {
177+
(&ty::ty_uniq(_), &ty::ty_rptr(r_b, mt_b)) => {
183178
let nr_b = ty::mk_uniq(self.infcx.tcx,
184179
ty::mt {ty: mt_b.ty,
185180
mutbl: m_const});
186-
self.try_assign(1, ty::AutoPtr,
181+
self.try_coerce(1, ty::AutoPtr,
187182
a, nr_b,
188183
mt_b.mutbl, r_b)
189184
}
190-
(ty::ty_estr(vs_a),
191-
ty::ty_estr(ty::vstore_slice(r_b)))
185+
(&ty::ty_estr(vs_a),
186+
&ty::ty_estr(ty::vstore_slice(r_b)))
192187
if is_borrowable(vs_a) => {
193188
let nr_b = ty::mk_estr(self.infcx.tcx, vs_a);
194-
self.try_assign(0, ty::AutoBorrowVec,
189+
self.try_coerce(0, ty::AutoBorrowVec,
195190
a, nr_b,
196191
m_imm, r_b)
197192
}
198193

199-
(ty::ty_evec(_, vs_a),
200-
ty::ty_evec(mt_b, ty::vstore_slice(r_b)))
194+
(&ty::ty_evec(_, vs_a),
195+
&ty::ty_evec(mt_b, ty::vstore_slice(r_b)))
201196
if is_borrowable(vs_a) => {
202197
let nr_b = ty::mk_evec(self.infcx.tcx,
203198
ty::mt {ty: mt_b.ty,
204199
mutbl: m_const},
205200
vs_a);
206-
self.try_assign(0, ty::AutoBorrowVec,
201+
self.try_coerce(0, ty::AutoBorrowVec,
207202
a, nr_b,
208203
mt_b.mutbl, r_b)
209204
}
210205

211-
(ty::ty_fn(ref a_f), ty::ty_fn(ref b_f))
206+
(&ty::ty_fn(ref a_f), &ty::ty_fn(ref b_f))
212207
if borrowable_protos(a_f.meta.proto, b_f.meta.proto) => {
213208
let nr_b = ty::mk_fn(self.infcx.tcx, ty::FnTyBase {
214209
meta: ty::FnMeta {proto: a_f.meta.proto,
215210
..b_f.meta},
216211
sig: copy b_f.sig
217212
});
218-
self.try_assign(0, ty::AutoBorrowFn,
213+
self.try_coerce(0, ty::AutoBorrowFn,
219214
a, nr_b, m_imm, b_f.meta.region)
220215
}
221216

222-
(ty::ty_fn(ref a_f), ty::ty_fn(ref b_f))
217+
(&ty::ty_fn(ref a_f), &ty::ty_fn(ref b_f))
223218
if a_f.meta.proto == ast::ProtoBare => {
224219
let b1_f = ty::FnTyBase {
225220
meta: ty::FnMeta {proto: ast::ProtoBare,
@@ -229,49 +224,50 @@ priv impl Assign {
229224
// Eventually we will need to add some sort of
230225
// adjustment here so that trans can add an
231226
// extra NULL env pointer:
232-
to_ares(Sub(*self).fns(a_f, &b1_f))
227+
to_ares(Sub(**self).fns(a_f, &b1_f))
233228
}
234229

235-
// check for &T being assigned to *T:
236-
(ty::ty_rptr(_, ref a_t), ty::ty_ptr(ref b_t)) => {
237-
to_ares(Sub(*self).mts(*a_t, *b_t))
230+
// check for &T being coerced to *T:
231+
(&ty::ty_rptr(_, ref a_t), &ty::ty_ptr(ref b_t)) => {
232+
to_ares(Sub(**self).mts(*a_t, *b_t))
238233
}
239234

240-
// otherwise, assignment follows normal subtype rules:
235+
// otherwise, coercement follows normal subtype rules:
241236
_ => {
242-
to_ares(Sub(*self).tys(a, b))
237+
to_ares(Sub(**self).tys(a, b))
243238
}
244239
}
245240
}
246241
_ => {
247242
// if insufficient bounds were available, just follow
248243
// normal subtype rules:
249-
to_ares(Sub(*self).tys(a, b))
244+
to_ares(Sub(**self).tys(a, b))
250245
}
251246
}
252247
}
253248

254-
/// Given an assignment from a type like `@a` to `&r_b/m nr_b`,
249+
/// Given an coercement from a type like `@a` to `&r_b/m nr_b`,
255250
/// this function checks that `a <: nr_b`. In that case, the
256-
/// assignment is permitted, so it constructs a fresh region
257-
/// variable `r_a >= r_b` and returns a corresponding assignment
251+
/// coercement is permitted, so it constructs a fresh region
252+
/// variable `r_a >= r_b` and returns a corresponding coercement
258253
/// record. See the discussion at the top of this file for more
259254
/// details.
260-
fn try_assign(autoderefs: uint,
255+
fn try_coerce(&self,
256+
autoderefs: uint,
261257
kind: ty::AutoRefKind,
262258
a: ty::t,
263259
nr_b: ty::t,
264260
m: ast::mutability,
265-
r_b: ty::Region) -> ares {
266-
267-
debug!("try_assign(a=%s, nr_b=%s, m=%?, r_b=%s)",
261+
r_b: ty::Region) -> ares
262+
{
263+
debug!("try_coerce(a=%s, nr_b=%s, m=%?, r_b=%s)",
268264
a.inf_str(self.infcx),
269265
nr_b.inf_str(self.infcx),
270266
m,
271267
r_b.inf_str(self.infcx));
272268

273269
do indent {
274-
let sub = Sub(*self);
270+
let sub = Sub(**self);
275271
do sub.tys(a, nr_b).chain |_t| {
276272
let r_a = self.infcx.next_region_var_nb(self.span);
277273
do sub.contraregions(r_a, r_b).chain |_r| {

0 commit comments

Comments
 (0)