Skip to content

Commit

Permalink
rustc: remove LvaluePreference argument from Ty::builtin_deref.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Jan 28, 2018
1 parent 87990a1 commit 800166c
Show file tree
Hide file tree
Showing 15 changed files with 45 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/librustc/middle/mem_categorization.rs
Expand Up @@ -517,7 +517,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
// a bind-by-ref means that the base_ty will be the type of the ident itself,
// but what we want here is the type of the underlying value being borrowed.
// So peel off one-level, turning the &T into T.
match base_ty.builtin_deref(false, ty::NoPreference) {
match base_ty.builtin_deref(false) {
Some(t) => t.ty,
None => {
debug!("By-ref binding of non-derefable type {:?}", base_ty);
Expand Down Expand Up @@ -1019,7 +1019,7 @@ impl<'a, 'gcx, 'tcx> MemCategorizationContext<'a, 'gcx, 'tcx> {
debug!("cat_deref: base_cmt={:?}", base_cmt);

let base_cmt_ty = base_cmt.ty;
let deref_ty = match base_cmt_ty.builtin_deref(true, ty::NoPreference) {
let deref_ty = match base_cmt_ty.builtin_deref(true) {
Some(mt) => mt.ty,
None => {
debug!("Explicit deref of non-derefable type: {:?}",
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/mir/tcx.rs
Expand Up @@ -52,7 +52,7 @@ impl<'a, 'gcx, 'tcx> PlaceTy<'tcx> {
match *elem {
ProjectionElem::Deref => {
let ty = self.to_ty(tcx)
.builtin_deref(true, ty::LvaluePreference::NoPreference)
.builtin_deref(true)
.unwrap_or_else(|| {
bug!("deref projection of non-dereferencable ty {:?}", self)
})
Expand Down
16 changes: 0 additions & 16 deletions src/librustc/ty/mod.rs
Expand Up @@ -12,7 +12,6 @@ pub use self::Variance::*;
pub use self::AssociatedItemContainer::*;
pub use self::BorrowKind::*;
pub use self::IntVarValue::*;
pub use self::LvaluePreference::*;
pub use self::fold::TypeFoldable;

use hir::{map as hir_map, FreevarMap, TraitMap};
Expand Down Expand Up @@ -2099,21 +2098,6 @@ impl<'tcx> TyS<'tcx> {
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LvaluePreference {
PreferMutLvalue,
NoPreference
}

impl LvaluePreference {
pub fn from_mutbl(m: hir::Mutability) -> Self {
match m {
hir::MutMutable => PreferMutLvalue,
hir::MutImmutable => NoPreference,
}
}
}

impl BorrowKind {
pub fn from_mutbl(m: hir::Mutability) -> BorrowKind {
match m {
Expand Down
10 changes: 2 additions & 8 deletions src/librustc/ty/sty.rs
Expand Up @@ -1514,18 +1514,12 @@ impl<'a, 'gcx, 'tcx> TyS<'tcx> {
///
/// The parameter `explicit` indicates if this is an *explicit* dereference.
/// Some types---notably unsafe ptrs---can only be dereferenced explicitly.
pub fn builtin_deref(&self, explicit: bool, pref: ty::LvaluePreference)
-> Option<TypeAndMut<'tcx>>
{
pub fn builtin_deref(&self, explicit: bool) -> Option<TypeAndMut<'tcx>> {
match self.sty {
TyAdt(def, _) if def.is_box() => {
Some(TypeAndMut {
ty: self.boxed_ty(),
mutbl: if pref == ty::PreferMutLvalue {
hir::MutMutable
} else {
hir::MutImmutable
},
mutbl: hir::MutImmutable,
})
},
TyRef(_, mt) => Some(mt),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/nll/type_check/mod.rs
Expand Up @@ -397,7 +397,7 @@ impl<'a, 'b, 'gcx, 'tcx> TypeVerifier<'a, 'b, 'gcx, 'tcx> {
let base_ty = base.to_ty(tcx);
match *pi {
ProjectionElem::Deref => {
let deref_ty = base_ty.builtin_deref(true, ty::LvaluePreference::NoPreference);
let deref_ty = base_ty.builtin_deref(true);
PlaceTy::Ty {
ty: deref_ty.map(|t| t.ty).unwrap_or_else(|| {
span_mirbug_and_err!(self, place, "deref of non-pointer {:?}", base_ty)
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_trans/abi.rs
Expand Up @@ -674,7 +674,7 @@ impl<'a, 'tcx> FnType<'tcx> {
_ => bug!("FnType::new_vtable: non-pair self {:?}", self_arg)
}

let pointee = self_arg.layout.ty.builtin_deref(true, ty::NoPreference)
let pointee = self_arg.layout.ty.builtin_deref(true)
.unwrap_or_else(|| {
bug!("FnType::new_vtable: non-pointer self {:?}", self_arg)
}).ty;
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_trans/mir/constant.rs
Expand Up @@ -740,7 +740,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
operand.llval
}
mir::CastKind::Unsize => {
let pointee_ty = operand.ty.builtin_deref(true, ty::NoPreference)
let pointee_ty = operand.ty.builtin_deref(true)
.expect("consts: unsizing got non-pointer type").ty;
let (base, old_info) = if !self.cx.type_is_sized(pointee_ty) {
// Normally, the source is a thin pointer and we are
Expand All @@ -755,7 +755,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
(operand.llval, None)
};

let unsized_ty = cast_ty.builtin_deref(true, ty::NoPreference)
let unsized_ty = cast_ty.builtin_deref(true)
.expect("consts: unsizing got non-pointer target type").ty;
let ptr_ty = self.cx.layout_of(unsized_ty).llvm_type(self.cx).ptr_to();
let base = consts::ptrcast(base, ptr_ty);
Expand Down
3 changes: 1 addition & 2 deletions src/librustc_trans/mir/operand.rs
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

use llvm::ValueRef;
use rustc::ty;
use rustc::ty::layout::{self, Align, LayoutOf, TyLayout};
use rustc::mir;
use rustc_data_structures::indexed_vec::Idx;
Expand Down Expand Up @@ -100,7 +99,7 @@ impl<'a, 'tcx> OperandRef<'tcx> {
}

pub fn deref(self, cx: &CodegenCx<'a, 'tcx>) -> PlaceRef<'tcx> {
let projected_ty = self.layout.ty.builtin_deref(true, ty::NoPreference)
let projected_ty = self.layout.ty.builtin_deref(true)
.unwrap_or_else(|| bug!("deref of non-pointer {:?}", self)).ty;
let (llptr, llextra) = match self.val {
OperandValue::Immediate(llptr) => (llptr, ptr::null_mut()),
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_typeck/check/_match.rs
Expand Up @@ -14,8 +14,8 @@ use rustc::hir::pat_util::EnumerateAndAdjustIterator;
use rustc::infer;
use rustc::infer::type_variable::TypeVariableOrigin;
use rustc::traits::ObligationCauseCode;
use rustc::ty::{self, Ty, TypeFoldable, LvaluePreference};
use check::{FnCtxt, Expectation, Diverges};
use rustc::ty::{self, Ty, TypeFoldable};
use check::{FnCtxt, Expectation, Diverges, LvaluePreference};
use check::coercion::CoerceMany;
use util::nodemap::FxHashMap;

Expand Down Expand Up @@ -500,7 +500,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {

pub fn check_dereferencable(&self, span: Span, expected: Ty<'tcx>, inner: &hir::Pat) -> bool {
if let PatKind::Binding(..) = inner.node {
if let Some(mt) = self.shallow_resolve(expected).builtin_deref(true, ty::NoPreference) {
if let Some(mt) = self.shallow_resolve(expected).builtin_deref(true) {
if let ty::TyDynamic(..) = mt.ty.sty {
// This is "x = SomeTrait" being reduced from
// "let &x = &SomeTrait" or "let box x = Box<SomeTrait>", an error.
Expand Down
9 changes: 4 additions & 5 deletions src/librustc_typeck/check/autoderef.rs
Expand Up @@ -10,15 +10,14 @@

use astconv::AstConv;

use super::{FnCtxt, LvalueOp};
use super::{FnCtxt, LvalueOp, LvaluePreference};
use super::method::MethodCallee;

use rustc::infer::InferOk;
use rustc::session::DiagnosticMessageId;
use rustc::traits;
use rustc::ty::{self, Ty, TraitRef};
use rustc::ty::{ToPredicate, TypeFoldable};
use rustc::ty::{LvaluePreference, NoPreference};
use rustc::ty::adjustment::{Adjustment, Adjust, OverloadedDeref};

use syntax_pos::Span;
Expand Down Expand Up @@ -85,7 +84,7 @@ impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {

// Otherwise, deref if type is derefable:
let (kind, new_ty) =
if let Some(mt) = self.cur_ty.builtin_deref(self.include_raw_pointers, NoPreference) {
if let Some(mt) = self.cur_ty.builtin_deref(self.include_raw_pointers) {
(AutoderefKind::Builtin, mt.ty)
} else {
let ty = self.overloaded_deref_ty(self.cur_ty)?;
Expand Down Expand Up @@ -238,8 +237,8 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
pub fn try_overloaded_deref(&self,
span: Span,
base_ty: Ty<'tcx>,
pref: LvaluePreference)
needs: Needs)
-> Option<InferOk<'tcx, MethodCallee<'tcx>>> {
self.try_overloaded_lvalue_op(span, base_ty, &[], pref, LvalueOp::Deref)
self.try_overloaded_lvalue_op(span, base_ty, &[], needs, LvalueOp::Deref)
}
}
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/callee.rs
Expand Up @@ -8,14 +8,14 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use super::{Expectation, FnCtxt, TupleArgumentsFlag};
use super::{Expectation, FnCtxt, LvaluePreference, TupleArgumentsFlag};
use super::autoderef::Autoderef;
use super::method::MethodCallee;

use hir::def::Def;
use hir::def_id::{DefId, LOCAL_CRATE};
use rustc::{infer, traits};
use rustc::ty::{self, TyCtxt, TypeFoldable, LvaluePreference, Ty};
use rustc::ty::{self, TyCtxt, TypeFoldable, Ty};
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
use syntax::abi;
use syntax::symbol::Symbol;
Expand Down
5 changes: 2 additions & 3 deletions src/librustc_typeck/check/coercion.rs
Expand Up @@ -60,7 +60,7 @@
//! sort of a minor point so I've opted to leave it for later---after all
//! we may want to adjust precisely when coercions occur.

use check::{Diverges, FnCtxt};
use check::{Diverges, FnCtxt, LvaluePreference};

use rustc::hir;
use rustc::hir::def_id::DefId;
Expand All @@ -69,8 +69,7 @@ use rustc::infer::type_variable::TypeVariableOrigin;
use rustc::lint;
use rustc::traits::{self, ObligationCause, ObligationCauseCode};
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
use rustc::ty::{self, LvaluePreference, TypeAndMut,
Ty, ClosureSubsts};
use rustc::ty::{self, TypeAndMut, Ty, ClosureSubsts};
use rustc::ty::fold::TypeFoldable;
use rustc::ty::error::TypeError;
use rustc::ty::relate::RelateResult;
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_typeck/check/method/confirm.rs
Expand Up @@ -11,11 +11,11 @@
use super::{probe, MethodCallee};

use astconv::AstConv;
use check::{FnCtxt, LvalueOp, callee};
use check::{FnCtxt, LvalueOp, callee, LvaluePreference, PreferMutLvalue};
use hir::def_id::DefId;
use rustc::ty::subst::Substs;
use rustc::traits;
use rustc::ty::{self, LvaluePreference, NoPreference, PreferMutLvalue, Ty};
use rustc::ty::{self, Ty};
use rustc::ty::subst::Subst;
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow, OverloadedDeref};
use rustc::ty::fold::TypeFoldable;
Expand Down Expand Up @@ -500,7 +500,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
let base_ty = self.resolve_type_vars_if_possible(&base_ty);

// Need to deref because overloaded lvalue ops take self by-reference.
let base_ty = base_ty.builtin_deref(false, NoPreference)
let base_ty = base_ty.builtin_deref(false)
.expect("lvalue op takes something that is not a ref")
.ty;

Expand Down
23 changes: 19 additions & 4 deletions src/librustc_typeck/check/mod.rs
Expand Up @@ -77,6 +77,7 @@ type parameter).
*/

pub use self::Expectation::*;
use self::LvaluePreference::*;
use self::autoderef::Autoderef;
use self::callee::DeferredCallResolution;
use self::coercion::{CoerceMany, DynamicCoerceMany};
Expand All @@ -95,7 +96,6 @@ use rustc::infer::type_variable::{TypeVariableOrigin};
use rustc::middle::region;
use rustc::ty::subst::{Kind, Subst, Substs};
use rustc::traits::{self, FulfillmentContext, ObligationCause, ObligationCauseCode};
use rustc::ty::{ParamTy, LvaluePreference, NoPreference, PreferMutLvalue};
use rustc::ty::{self, Ty, TyCtxt, Visibility, ToPredicate};
use rustc::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
use rustc::ty::fold::TypeFoldable;
Expand Down Expand Up @@ -368,6 +368,21 @@ impl<'a, 'gcx, 'tcx> Expectation<'tcx> {
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum LvaluePreference {
PreferMutLvalue,
NoPreference
}

impl LvaluePreference {
fn from_mutbl(m: hir::Mutability) -> Self {
match m {
hir::MutMutable => PreferMutLvalue,
hir::MutImmutable => NoPreference,
}
}
}

#[derive(Copy, Clone)]
pub struct UnsafetyState {
pub def: ast::NodeId,
Expand Down Expand Up @@ -2219,7 +2234,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let ret_ty = method.sig.output();

// method returns &T, but the type as visible to user is T, so deref
ret_ty.builtin_deref(true, NoPreference).unwrap()
ret_ty.builtin_deref(true).unwrap()
}

fn lookup_indexing(&self,
Expand Down Expand Up @@ -3572,7 +3587,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
oprnd_t = self.structurally_resolved_type(expr.span, oprnd_t);
match unop {
hir::UnDeref => {
if let Some(mt) = oprnd_t.builtin_deref(true, NoPreference) {
if let Some(mt) = oprnd_t.builtin_deref(true) {
oprnd_t = mt.ty;
} else if let Some(ok) = self.try_overloaded_deref(
expr.span, oprnd_t, lvalue_pref) {
Expand Down Expand Up @@ -5023,7 +5038,7 @@ pub fn check_bounds_are_used<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let lifetime_count = generics.lifetimes().count();

for leaf_ty in ty.walk() {
if let ty::TyParam(ParamTy {idx, ..}) = leaf_ty.sty {
if let ty::TyParam(ty::ParamTy {idx, ..}) = leaf_ty.sty {
debug!("Found use of ty param num {}", idx);
tps_used[idx as usize - lifetime_count] = true;
} else if let ty::TyError = leaf_ty.sty {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/op.rs
Expand Up @@ -10,9 +10,9 @@

//! Code related to processing overloaded binary and unary operators.

use super::FnCtxt;
use super::{FnCtxt, NoPreference, PreferMutLvalue};
use super::method::MethodCallee;
use rustc::ty::{self, Ty, TypeFoldable, NoPreference, PreferMutLvalue, TypeVariants};
use rustc::ty::{self, Ty, TypeFoldable, TypeVariants};
use rustc::ty::TypeVariants::{TyStr, TyRef};
use rustc::ty::adjustment::{Adjustment, Adjust, AutoBorrow};
use rustc::infer::type_variable::TypeVariableOrigin;
Expand Down

0 comments on commit 800166c

Please sign in to comment.