Skip to content

Commit 350b50d

Browse files
Ariel Ben-Yehudaarielb1
Ariel Ben-Yehuda
authored andcommitted
deref the argument of overloaded MIR autoderef
Fixes #31466
1 parent 76608c8 commit 350b50d

File tree

2 files changed

+73
-4
lines changed

2 files changed

+73
-4
lines changed

src/librustc_mir/hair/cx/expr.rs

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
416416
kind: kind,
417417
};
418418

419-
debug!("unadjusted-expr={:?} applying adjustments={:?}",
419+
debug!("make_mirror: unadjusted-expr={:?} applying adjustments={:?}",
420420
expr, cx.tcx.tables.borrow().adjustments.get(&self.id));
421421

422422
// Now apply adjustments, if any.
@@ -459,10 +459,38 @@ impl<'tcx> Mirror<'tcx> for &'tcx hir::Expr {
459459
self.span,
460460
i,
461461
|mc| cx.tcx.tables.borrow().method_map.get(&mc).map(|m| m.ty));
462-
let kind = if cx.tcx.is_overloaded_autoderef(self.id, i) {
463-
overloaded_lvalue(cx, self, ty::MethodCall::autoderef(self.id, i),
464-
PassArgs::ByValue, expr.to_ref(), vec![])
462+
debug!("make_mirror: autoderef #{}, adjusted_ty={:?}", i, adjusted_ty);
463+
let method_key = ty::MethodCall::autoderef(self.id, i);
464+
let meth_ty =
465+
cx.tcx.tables.borrow().method_map.get(&method_key).map(|m| m.ty);
466+
let kind = if let Some(meth_ty) = meth_ty {
467+
debug!("make_mirror: overloaded autoderef (meth_ty={:?})", meth_ty);
468+
469+
let ref_ty = cx.tcx.no_late_bound_regions(&meth_ty.fn_ret());
470+
let (region, mutbl) = match ref_ty {
471+
Some(ty::FnConverging(&ty::TyS {
472+
sty: ty::TyRef(region, mt), ..
473+
})) => (region, mt.mutbl),
474+
_ => cx.tcx.sess.span_bug(
475+
expr.span, "autoderef returned bad type")
476+
};
477+
478+
expr = Expr {
479+
temp_lifetime: temp_lifetime,
480+
ty: cx.tcx.mk_ref(
481+
region, ty::TypeAndMut { ty: expr.ty, mutbl: mutbl }),
482+
span: expr.span,
483+
kind: ExprKind::Borrow {
484+
region: *region,
485+
borrow_kind: to_borrow_kind(mutbl),
486+
arg: expr.to_ref()
487+
}
488+
};
489+
490+
overloaded_lvalue(cx, self, method_key,
491+
PassArgs::ByRef, expr.to_ref(), vec![])
465492
} else {
493+
debug!("make_mirror: built-in autoderef");
466494
ExprKind::Deref { arg: expr.to_ref() }
467495
};
468496
expr = Expr {

src/test/run-pass/mir_autoderef.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(rustc_attrs)]
12+
13+
use std::ops::{Deref, DerefMut};
14+
15+
pub struct MyRef(u32);
16+
17+
impl Deref for MyRef {
18+
type Target = u32;
19+
fn deref(&self) -> &u32 { &self.0 }
20+
}
21+
22+
impl DerefMut for MyRef {
23+
fn deref_mut(&mut self) -> &mut u32 { &mut self.0 }
24+
}
25+
26+
27+
#[rustc_mir]
28+
fn deref(x: &MyRef) -> &u32 {
29+
x
30+
}
31+
32+
#[rustc_mir]
33+
fn deref_mut(x: &mut MyRef) -> &mut u32 {
34+
x
35+
}
36+
37+
fn main() {
38+
let mut r = MyRef(2);
39+
assert_eq!(deref(&r) as *const _, &r.0 as *const _);
40+
assert_eq!(deref_mut(&mut r) as *mut _, &mut r.0 as *mut _);
41+
}

0 commit comments

Comments
 (0)