Skip to content

Commit e0f5980

Browse files
committed
Auto merge of #21351 - eddyb:x-coerce--a-new-hope, r=nikomatsakis
Coercions will now attempt to autoderef as needed before reborrowing. This includes overloaded `Deref`, e.g. `&Rc<T>` coerces to `&T`, and `DerefMut`, e.g. `&mut Vec<T>` coerces to `&mut [T]` (in addition to `&[T]`). Closes #21432.
2 parents 52c74e6 + ae076e1 commit e0f5980

File tree

10 files changed

+343
-293
lines changed

10 files changed

+343
-293
lines changed

src/librustc/middle/infer/mod.rs

+13-30
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@ use std::rc::Rc;
3333
use syntax::ast;
3434
use syntax::codemap;
3535
use syntax::codemap::Span;
36-
use util::common::indent;
3736
use util::nodemap::FnvHashMap;
3837
use util::ppaux::{ty_to_string};
3938
use util::ppaux::{Repr, UserString};
4039

41-
use self::coercion::Coerce;
4240
use self::combine::{Combine, Combineable, CombineFields};
4341
use self::region_inference::{RegionVarBindings, RegionSnapshot};
4442
use self::equate::Equate;
@@ -47,7 +45,6 @@ use self::lub::Lub;
4745
use self::unify::{UnificationTable, InferCtxtMethodsForSimplyUnifiableTypes};
4846
use self::error_reporting::ErrorReporting;
4947

50-
pub mod coercion;
5148
pub mod combine;
5249
pub mod doc;
5350
pub mod equate;
@@ -68,7 +65,6 @@ pub type Bound<T> = Option<T>;
6865
pub type cres<'tcx, T> = Result<T,ty::type_err<'tcx>>; // "combine result"
6966
pub type ures<'tcx> = cres<'tcx, ()>; // "unify result"
7067
pub type fres<T> = Result<T, fixup_err>; // "fixup result"
71-
pub type CoerceResult<'tcx> = cres<'tcx, Option<ty::AutoAdjustment<'tcx>>>;
7268

7369
pub struct InferCtxt<'a, 'tcx: 'a> {
7470
pub tcx: &'a ty::ctxt<'tcx>,
@@ -409,24 +405,6 @@ fn expected_found<T>(a_is_expected: bool,
409405
}
410406
}
411407

412-
pub fn mk_coercety<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
413-
a_is_expected: bool,
414-
origin: TypeOrigin,
415-
a: Ty<'tcx>,
416-
b: Ty<'tcx>)
417-
-> CoerceResult<'tcx> {
418-
debug!("mk_coercety({} -> {})", a.repr(cx.tcx), b.repr(cx.tcx));
419-
indent(|| {
420-
cx.commit_if_ok(|| {
421-
let trace = TypeTrace {
422-
origin: origin,
423-
values: Types(expected_found(a_is_expected, a, b))
424-
};
425-
Coerce(cx.combine_fields(a_is_expected, trace)).tys(a, b)
426-
})
427-
})
428-
}
429-
430408
trait then<'tcx> {
431409
fn then<T, F>(&self, f: F) -> Result<T, ty::type_err<'tcx>> where
432410
T: Clone,
@@ -689,10 +667,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
689667
{
690668
debug!("sub_types({} <: {})", a.repr(self.tcx), b.repr(self.tcx));
691669
self.commit_if_ok(|| {
692-
let trace = TypeTrace {
693-
origin: origin,
694-
values: Types(expected_found(a_is_expected, a, b))
695-
};
670+
let trace = TypeTrace::types(origin, a_is_expected, a, b);
696671
self.sub(a_is_expected, trace).tys(a, b).to_ures()
697672
})
698673
}
@@ -705,10 +680,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
705680
-> ures<'tcx>
706681
{
707682
self.commit_if_ok(|| {
708-
let trace = TypeTrace {
709-
origin: origin,
710-
values: Types(expected_found(a_is_expected, a, b))
711-
};
683+
let trace = TypeTrace::types(origin, a_is_expected, a, b);
712684
self.equate(a_is_expected, trace).tys(a, b).to_ures()
713685
})
714686
}
@@ -1118,6 +1090,17 @@ impl<'tcx> TypeTrace<'tcx> {
11181090
self.origin.span()
11191091
}
11201092

1093+
pub fn types(origin: TypeOrigin,
1094+
a_is_expected: bool,
1095+
a: Ty<'tcx>,
1096+
b: Ty<'tcx>)
1097+
-> TypeTrace<'tcx> {
1098+
TypeTrace {
1099+
origin: origin,
1100+
values: Types(expected_found(a_is_expected, a, b))
1101+
}
1102+
}
1103+
11211104
pub fn dummy(tcx: &ty::ctxt<'tcx>) -> TypeTrace<'tcx> {
11221105
TypeTrace {
11231106
origin: Misc(codemap::DUMMY_SP),

src/librustc_typeck/check/callee.rs

+2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use super::LvaluePreference;
2121
use super::method;
2222
use super::structurally_resolved_type;
2323
use super::TupleArgumentsFlag;
24+
use super::UnresolvedTypeAction;
2425
use super::write_call;
2526

2627
use middle::infer;
@@ -77,6 +78,7 @@ pub fn check_call<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
7778
callee_expr.span,
7879
original_callee_ty,
7980
Some(callee_expr),
81+
UnresolvedTypeAction::Error,
8082
LvaluePreference::NoPreference,
8183
|adj_ty, idx| {
8284
let autoderefref = ty::AutoDerefRef { autoderefs: idx, autoref: None };

0 commit comments

Comments
 (0)