Skip to content

Commit 0a32010

Browse files
committed
Add comments to autoderef() helper and refactor it to take
an `Option<&Expr>` like everything else.
1 parent 5364c48 commit 0a32010

File tree

4 files changed

+58
-21
lines changed

4 files changed

+58
-21
lines changed

src/librustc_typeck/check/method/confirm.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
143143
// time writing the results into the various tables.
144144
let (autoderefd_ty, n, result) =
145145
check::autoderef(
146-
self.fcx, self.span, unadjusted_self_ty, Some(self.self_expr.id), NoPreference,
146+
self.fcx, self.span, unadjusted_self_ty, Some(self.self_expr), NoPreference,
147147
|_, n| if n == auto_deref_ref.autoderefs { Some(()) } else { None });
148148
assert_eq!(n, auto_deref_ref.autoderefs);
149149
assert_eq!(result, Some(()));
@@ -492,7 +492,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
492492
exprs.repr(self.tcx()));
493493

494494
// Fix up autoderefs and derefs.
495-
for (i, expr) in exprs.iter().rev().enumerate() {
495+
for (i, &expr) in exprs.iter().rev().enumerate() {
496496
// Count autoderefs.
497497
let autoderef_count = match self.fcx
498498
.inh
@@ -512,8 +512,8 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
512512
if autoderef_count > 0 {
513513
check::autoderef(self.fcx,
514514
expr.span,
515-
self.fcx.expr_ty(*expr),
516-
Some(expr.id),
515+
self.fcx.expr_ty(expr),
516+
Some(expr),
517517
PreferMutLvalue,
518518
|_, autoderefs| {
519519
if autoderefs == autoderef_count + 1 {
@@ -567,7 +567,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
567567
let result = check::try_index_step(
568568
self.fcx,
569569
MethodCall::expr(expr.id),
570-
*expr,
570+
expr,
571571
&**base_expr,
572572
adjusted_base_ty,
573573
base_adjustment,
@@ -577,7 +577,7 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
577577
if let Some((input_ty, return_ty)) = result {
578578
demand::suptype(self.fcx, index_expr.span, input_ty, index_expr_ty);
579579

580-
let expr_ty = self.fcx.expr_ty(&**expr);
580+
let expr_ty = self.fcx.expr_ty(&*expr);
581581
demand::suptype(self.fcx, expr.span, expr_ty, return_ty);
582582
}
583583
}

src/librustc_typeck/check/method/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,9 @@ pub fn lookup<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
102102
Ok(confirm::confirm(fcx, span, self_expr, call_expr, self_ty, pick, supplied_method_types))
103103
}
104104

105-
pub fn lookup_in_trait<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>,
105+
pub fn lookup_in_trait<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
106106
span: Span,
107-
self_expr: Option<&'a ast::Expr>,
107+
self_expr: Option<&ast::Expr>,
108108
m_name: ast::Name,
109109
trait_def_id: DefId,
110110
self_ty: Ty<'tcx>,
@@ -125,9 +125,9 @@ pub fn lookup_in_trait<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>,
125125
/// method-lookup code. In particular, autoderef on index is basically identical to autoderef with
126126
/// normal probes, except that the test also looks for built-in indexing. Also, the second half of
127127
/// this method is basically the same as confirmation.
128-
pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>,
128+
pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
129129
span: Span,
130-
self_expr: Option<&'a ast::Expr>,
130+
self_expr: Option<&ast::Expr>,
131131
m_name: ast::Name,
132132
trait_def_id: DefId,
133133
autoderefref: ty::AutoDerefRef<'tcx>,

src/librustc_typeck/check/mod.rs

Lines changed: 40 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2263,17 +2263,36 @@ pub enum LvaluePreference {
22632263
/// Executes an autoderef loop for the type `t`. At each step, invokes `should_stop` to decide
22642264
/// whether to terminate the loop. Returns the final type and number of derefs that it performed.
22652265
///
2266+
<<<<<<< HEAD
22662267
/// Note: this method does not modify the adjustments table. The caller is responsible for
22672268
/// inserting an AutoAdjustment record into the `fcx` using one of the suitable methods.
22682269
pub fn autoderef<'a, 'tcx, T, F>(fcx: &FnCtxt<'a, 'tcx>,
22692270
sp: Span,
2271+
||||||| merged common ancestors
2272+
/// Note: this method does not modify the adjustments table. The caller is responsible for
2273+
/// inserting an AutoAdjustment record into the `fcx` using one of the suitable methods.
2274+
pub fn autoderef<'a, 'tcx, T, F>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
2275+
=======
2276+
/// Note: this method does not modify the adjustments table. The
2277+
/// caller is responsible for inserting an AutoAdjustment record into
2278+
/// the `fcx` using one of the suitable methods. However, if
2279+
/// `opt_expr` is not `None`, it *will* insert the appropriate method
2280+
/// entries for the overloaded deref call.
2281+
pub fn autoderef<'a, 'tcx, T, F>(fcx: &FnCtxt<'a, 'tcx>,
2282+
sp: Span,
2283+
>>>>>>> Add comments to autoderef() helper and refactor it to take
22702284
base_ty: Ty<'tcx>,
2271-
expr_id: Option<ast::NodeId>,
2285+
opt_expr: Option<&ast::Expr>,
22722286
mut lvalue_pref: LvaluePreference,
22732287
mut should_stop: F)
2274-
-> (Ty<'tcx>, uint, Option<T>) where
2275-
F: FnMut(Ty<'tcx>, uint) -> Option<T>,
2288+
-> (Ty<'tcx>, uint, Option<T>)
2289+
where F: FnMut(Ty<'tcx>, uint) -> Option<T>,
22762290
{
2291+
debug!("autoderef(base_ty={}, opt_expr={}, lvalue_pref={})",
2292+
base_ty.repr(fcx.tcx()),
2293+
opt_expr,
2294+
lvalue_pref);
2295+
22772296
let mut t = base_ty;
22782297
for autoderefs in range(0, fcx.tcx().sess.recursion_limit.get()) {
22792298
let resolved_t = structurally_resolved_type(fcx, sp, t);
@@ -2291,7 +2310,19 @@ pub fn autoderef<'a, 'tcx, T, F>(fcx: &FnCtxt<'a, 'tcx>,
22912310
let mt = match ty::deref(resolved_t, false) {
22922311
Some(mt) => Some(mt),
22932312
None => {
2294-
let method_call = expr_id.map(|id| MethodCall::autoderef(id, autoderefs));
2313+
let method_call = opt_expr.map(|expr| MethodCall::autoderef(expr.id, autoderefs));
2314+
2315+
// Super subtle: it might seem as though we should
2316+
// pass `opt_expr` to `try_overloaded_deref`, so that
2317+
// the (implicit) autoref of using an overloaded deref
2318+
// would get added to the adjustment table. However we
2319+
// do not do that, because it's kind of a
2320+
// "meta-adjustment" -- instead, we just leave it
2321+
// unrecorded and know that there "will be" an
2322+
// autoref. regionck and other bits of the code base,
2323+
// when they encounter an overloaded autoderef, have
2324+
// to do some reconstructive surgery. This is a pretty
2325+
// complex mess that is begging for a proper MIR.
22952326
try_overloaded_deref(fcx, sp, method_call, None, resolved_t, lvalue_pref)
22962327
}
22972328
};
@@ -2324,7 +2355,7 @@ fn try_overloaded_deref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
23242355
// Try DerefMut first, if preferred.
23252356
let method = match (lvalue_pref, fcx.tcx().lang_items.deref_mut_trait()) {
23262357
(PreferMutLvalue, Some(trait_did)) => {
2327-
method::lookup_in_trait(fcx, span, base_expr.map(|x| &*x),
2358+
method::lookup_in_trait(fcx, span, base_expr,
23282359
token::intern("deref_mut"), trait_did,
23292360
base_ty, None)
23302361
}
@@ -2334,7 +2365,7 @@ fn try_overloaded_deref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
23342365
// Otherwise, fall back to Deref.
23352366
let method = match (method, fcx.tcx().lang_items.deref_trait()) {
23362367
(None, Some(trait_did)) => {
2337-
method::lookup_in_trait(fcx, span, base_expr.map(|x| &*x),
2368+
method::lookup_in_trait(fcx, span, base_expr,
23382369
token::intern("deref"), trait_did,
23392370
base_ty, None)
23402371
}
@@ -2390,7 +2421,7 @@ fn autoderef_for_index<'a, 'tcx, T, F>(fcx: &FnCtxt<'a, 'tcx>,
23902421
// consolidated.
23912422

23922423
let (ty, autoderefs, final_mt) =
2393-
autoderef(fcx, base_expr.span, base_ty, Some(base_expr.id), lvalue_pref, |adj_ty, idx| {
2424+
autoderef(fcx, base_expr.span, base_ty, Some(base_expr), lvalue_pref, |adj_ty, idx| {
23942425
let autoderefref = ty::AutoDerefRef { autoderefs: idx, autoref: None };
23952426
step(adj_ty, autoderefref)
23962427
});
@@ -3360,7 +3391,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
33603391
fcx.expr_ty(base));
33613392
// FIXME(eddyb) #12808 Integrate privacy into this auto-deref loop.
33623393
let (_, autoderefs, field_ty) =
3363-
autoderef(fcx, expr.span, expr_t, Some(base.id), lvalue_pref, |base_t, _| {
3394+
autoderef(fcx, expr.span, expr_t, Some(base), lvalue_pref, |base_t, _| {
33643395
match base_t.sty {
33653396
ty::ty_struct(base_id, substs) => {
33663397
debug!("struct named {}", ppaux::ty_to_string(tcx, base_t));
@@ -3421,7 +3452,7 @@ fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
34213452
let mut tuple_like = false;
34223453
// FIXME(eddyb) #12808 Integrate privacy into this auto-deref loop.
34233454
let (_, autoderefs, field_ty) =
3424-
autoderef(fcx, expr.span, expr_t, Some(base.id), lvalue_pref, |base_t, _| {
3455+
autoderef(fcx, expr.span, expr_t, Some(base), lvalue_pref, |base_t, _| {
34253456
match base_t.sty {
34263457
ty::ty_struct(base_id, substs) => {
34273458
tuple_like = ty::is_tuple_struct(tcx, base_id);

src/librustc_typeck/check/regionck.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,13 +1049,16 @@ fn type_of_node_must_outlive<'a, 'tcx>(
10491049
/// Computes the guarantor for an expression `&base` and then ensures that the lifetime of the
10501050
/// resulting pointer is linked to the lifetime of its guarantor (if any).
10511051
fn link_addr_of(rcx: &mut Rcx, expr: &ast::Expr,
1052-
mutability: ast::Mutability, base: &ast::Expr) {
1053-
debug!("link_addr_of(base=?)");
1052+
mutability: ast::Mutability, base: &ast::Expr) {
1053+
debug!("link_addr_of(expr={}, base={})", expr.repr(rcx.tcx()), base.repr(rcx.tcx()));
10541054

10551055
let cmt = {
10561056
let mc = mc::MemCategorizationContext::new(rcx.fcx);
10571057
ignore_err!(mc.cat_expr(base))
10581058
};
1059+
1060+
debug!("link_addr_of: cmt={}", cmt.repr(rcx.tcx()));
1061+
10591062
link_region_from_node_type(rcx, expr.span, expr.id, mutability, cmt);
10601063
}
10611064

@@ -1182,6 +1185,9 @@ fn link_region_from_node_type<'a, 'tcx>(rcx: &Rcx<'a, 'tcx>,
11821185
id: ast::NodeId,
11831186
mutbl: ast::Mutability,
11841187
cmt_borrowed: mc::cmt<'tcx>) {
1188+
debug!("link_region_from_node_type(id={}, mutbl={}, cmt_borrowed={})",
1189+
id, mutbl, cmt_borrowed.repr(rcx.tcx()));
1190+
11851191
let rptr_ty = rcx.resolve_node_type(id);
11861192
if !ty::type_is_error(rptr_ty) {
11871193
let tcx = rcx.fcx.ccx.tcx;

0 commit comments

Comments
 (0)