X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=src%2Flibrustc%2Fmiddle%2Fmem_categorization.rs;h=dd2f48d25b4b0c6cc00c69dbd56b508fa824e141;hb=e9174d1eb2e039cc93eb32959f1cf64a37ccec48;hp=ee7079bb47d5920876358134884e2519d7930ecd;hpb=c1a9b12dc94607f2d0cec7929de9ba4c085f47f1;p=rustc.git diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index ee7079bb47..dd2f48d25b 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -72,14 +72,16 @@ pub use self::categorization::*; use self::Aliasability::*; -use ast_map; +use middle::def_id::DefId; +use front::map as ast_map; use middle::infer; use middle::check_const; use middle::def; -use middle::region; +use middle::ty::adjustment; use middle::ty::{self, Ty}; -use syntax::ast::{MutImmutable, MutMutable}; +use rustc_front::hir::{MutImmutable, MutMutable}; +use rustc_front::hir; use syntax::ast; use syntax::codemap::Span; @@ -94,7 +96,7 @@ pub enum categorization<'tcx> { cat_local(ast::NodeId), // local variable cat_deref(cmt<'tcx>, usize, PointerKind), // deref of a ptr cat_interior(cmt<'tcx>, InteriorKind), // something interior: field, tuple, etc - cat_downcast(cmt<'tcx>, ast::DefId), // selects a particular enum variant (*1) + cat_downcast(cmt<'tcx>, DefId), // selects a particular enum variant (*1) // (*1) downcast is only required if the enum has more than one variant } @@ -116,7 +118,7 @@ pub enum PointerKind { BorrowedPtr(ty::BorrowKind, ty::Region), /// `*T` - UnsafePtr(ast::Mutability), + UnsafePtr(hir::Mutability), /// Implicit deref of the `&T` that results from an overloaded index `[]`. Implicit(ty::BorrowKind, ty::Region), @@ -244,12 +246,12 @@ pub trait ast_node { fn span(&self) -> Span; } -impl ast_node for ast::Expr { +impl ast_node for hir::Expr { fn id(&self) -> ast::NodeId { self.id } fn span(&self) -> Span { self.span } } -impl ast_node for ast::Pat { +impl ast_node for hir::Pat { fn id(&self) -> ast::NodeId { self.id } fn span(&self) -> Span { self.span } } @@ -262,7 +264,7 @@ pub struct MemCategorizationContext<'t, 'a: 't, 'tcx : 'a> { pub type McResult = Result; impl MutabilityCategory { - pub fn from_mutbl(m: ast::Mutability) -> MutabilityCategory { + pub fn from_mutbl(m: hir::Mutability) -> MutabilityCategory { let ret = match m { MutImmutable => McImmutable, MutMutable => McDeclared @@ -304,8 +306,8 @@ impl MutabilityCategory { fn from_local(tcx: &ty::ctxt, id: ast::NodeId) -> MutabilityCategory { let ret = match tcx.map.get(id) { ast_map::NodeLocal(p) | ast_map::NodeArg(p) => match p.node { - ast::PatIdent(bind_mode, _, _) => { - if bind_mode == ast::BindByValue(ast::MutMutable) { + hir::PatIdent(bind_mode, _, _) => { + if bind_mode == hir::BindByValue(hir::MutMutable) { McDeclared } else { McImmutable @@ -366,7 +368,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { self.typer.tcx } - fn expr_ty(&self, expr: &ast::Expr) -> McResult> { + fn expr_ty(&self, expr: &hir::Expr) -> McResult> { match self.typer.node_ty(expr.id) { Ok(t) => Ok(t), Err(()) => { @@ -376,7 +378,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { } } - fn expr_ty_adjusted(&self, expr: &ast::Expr) -> McResult> { + fn expr_ty_adjusted(&self, expr: &hir::Expr) -> McResult> { let unadjusted_ty = try!(self.expr_ty(expr)); Ok(unadjusted_ty.adjust( self.tcx(), expr.span, expr.id, @@ -388,18 +390,18 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { self.typer.node_ty(id) } - fn pat_ty(&self, pat: &ast::Pat) -> McResult> { + fn pat_ty(&self, pat: &hir::Pat) -> McResult> { let base_ty = try!(self.typer.node_ty(pat.id)); // FIXME (Issue #18207): This code detects whether we are // looking at a `ref x`, and if so, figures out what the type // *being borrowed* is. But ideally we would put in a more // fundamental fix to this conflated use of the node id. let ret_ty = match pat.node { - ast::PatIdent(ast::BindByRef(_), _, _) => { + hir::PatIdent(hir::BindByRef(_), _, _) => { // 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) { + match base_ty.builtin_deref(false, ty::NoPreference) { Some(t) => t.ty, None => { return Err(()); } } @@ -411,7 +413,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { Ok(ret_ty) } - pub fn cat_expr(&self, expr: &ast::Expr) -> McResult> { + pub fn cat_expr(&self, expr: &hir::Expr) -> McResult> { match self.typer.adjustments().get(&expr.id) { None => { // No adjustments. @@ -420,16 +422,16 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { Some(adjustment) => { match *adjustment { - ty::AdjustDerefRef( - ty::AutoDerefRef { + adjustment::AdjustDerefRef( + adjustment::AutoDerefRef { autoref: None, unsize: None, autoderefs, ..}) => { // Equivalent to *expr or something similar. self.cat_expr_autoderefd(expr, autoderefs) } - ty::AdjustReifyFnPointer | - ty::AdjustUnsafeFnPointer | - ty::AdjustDerefRef(_) => { + adjustment::AdjustReifyFnPointer | + adjustment::AdjustUnsafeFnPointer | + adjustment::AdjustDerefRef(_) => { debug!("cat_expr({:?}): {:?}", adjustment, expr); @@ -443,7 +445,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { } pub fn cat_expr_autoderefd(&self, - expr: &ast::Expr, + expr: &hir::Expr, autoderefs: usize) -> McResult> { let mut cmt = try!(self.cat_expr_unadjusted(expr)); @@ -456,17 +458,17 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { return Ok(cmt); } - pub fn cat_expr_unadjusted(&self, expr: &ast::Expr) -> McResult> { + pub fn cat_expr_unadjusted(&self, expr: &hir::Expr) -> McResult> { debug!("cat_expr: id={} expr={:?}", expr.id, expr); let expr_ty = try!(self.expr_ty(expr)); match expr.node { - ast::ExprUnary(ast::UnDeref, ref e_base) => { + hir::ExprUnary(hir::UnDeref, ref e_base) => { let base_cmt = try!(self.cat_expr(&**e_base)); self.cat_deref(expr, base_cmt, 0, None) } - ast::ExprField(ref base, f_name) => { + hir::ExprField(ref base, f_name) => { let base_cmt = try!(self.cat_expr(&**base)); debug!("cat_expr(cat_field): id={} expr={:?} base={:?}", expr.id, @@ -475,12 +477,12 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { Ok(self.cat_field(expr, base_cmt, f_name.node.name, expr_ty)) } - ast::ExprTupField(ref base, idx) => { + hir::ExprTupField(ref base, idx) => { let base_cmt = try!(self.cat_expr(&**base)); Ok(self.cat_tup_field(expr, base_cmt, idx.node, expr_ty)) } - ast::ExprIndex(ref base, _) => { + hir::ExprIndex(ref base, _) => { let method_call = ty::MethodCall::expr(expr.id()); let context = InteriorOffsetKind::Index; match self.typer.node_method_ty(method_call) { @@ -512,38 +514,28 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { } } - ast::ExprPath(..) => { + hir::ExprPath(..) => { let def = self.tcx().def_map.borrow().get(&expr.id).unwrap().full_def(); self.cat_def(expr.id, expr.span, expr_ty, def) } - ast::ExprParen(ref e) => { + hir::ExprParen(ref e) => { self.cat_expr(&**e) } - ast::ExprAddrOf(..) | ast::ExprCall(..) | - ast::ExprAssign(..) | ast::ExprAssignOp(..) | - ast::ExprClosure(..) | ast::ExprRet(..) | - ast::ExprUnary(..) | ast::ExprRange(..) | - ast::ExprMethodCall(..) | ast::ExprCast(..) | - ast::ExprVec(..) | ast::ExprTup(..) | ast::ExprIf(..) | - ast::ExprBinary(..) | ast::ExprWhile(..) | - ast::ExprBlock(..) | ast::ExprLoop(..) | ast::ExprMatch(..) | - ast::ExprLit(..) | ast::ExprBreak(..) | ast::ExprMac(..) | - ast::ExprAgain(..) | ast::ExprStruct(..) | ast::ExprRepeat(..) | - ast::ExprInlineAsm(..) | ast::ExprBox(..) => { + hir::ExprAddrOf(..) | hir::ExprCall(..) | + hir::ExprAssign(..) | hir::ExprAssignOp(..) | + hir::ExprClosure(..) | hir::ExprRet(..) | + hir::ExprUnary(..) | hir::ExprRange(..) | + hir::ExprMethodCall(..) | hir::ExprCast(..) | + hir::ExprVec(..) | hir::ExprTup(..) | hir::ExprIf(..) | + hir::ExprBinary(..) | hir::ExprWhile(..) | + hir::ExprBlock(..) | hir::ExprLoop(..) | hir::ExprMatch(..) | + hir::ExprLit(..) | hir::ExprBreak(..) | + hir::ExprAgain(..) | hir::ExprStruct(..) | hir::ExprRepeat(..) | + hir::ExprInlineAsm(..) | hir::ExprBox(..) => { Ok(self.cat_rvalue_node(expr.id(), expr.span(), expr_ty)) } - - ast::ExprIfLet(..) => { - self.tcx().sess.span_bug(expr.span, "non-desugared ExprIfLet"); - } - ast::ExprWhileLet(..) => { - self.tcx().sess.span_bug(expr.span, "non-desugared ExprWhileLet"); - } - ast::ExprForLoop(..) => { - self.tcx().sess.span_bug(expr.span, "non-desugared ExprForLoop"); - } } } @@ -587,7 +579,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { })) } - def::DefUpvar(var_id, fn_node_id) => { + def::DefUpvar(var_id, _, fn_node_id) => { let ty = try!(self.node_ty(fn_node_id)); match ty.sty { ty::TyClosure(closure_id, _) => { @@ -738,7 +730,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { }; match fn_expr.node { - ast::ExprClosure(_, _, ref body) => body.id, + hir::ExprClosure(_, _, ref body) => body.id, _ => unreachable!() } }; @@ -748,7 +740,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { // The environment of a closure is guaranteed to // outlive any bindings introduced in the body of the // closure itself. - scope: region::DestructionScopeData::new(fn_body_id), + scope: self.tcx().region_maps.item_extent(fn_body_id), bound_region: ty::BrEnv }); @@ -906,7 +898,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { None => base_cmt }; let base_cmt_ty = base_cmt.ty; - match base_cmt_ty.builtin_deref(true) { + match base_cmt_ty.builtin_deref(true, ty::NoPreference) { Some(mt) => { let ret = self.cat_deref_common(node, base_cmt, deref_cnt, mt.ty, @@ -1053,7 +1045,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { span:elt.span(), cat:cat_deref(base_cmt.clone(), 0, ptr), mutbl:m, - ty: match base_cmt.ty.builtin_deref(false) { + ty: match base_cmt.ty.builtin_deref(false, ty::NoPreference) { Some(mt) => mt.ty, None => self.tcx().sess.bug("Found non-derefable type") }, @@ -1078,8 +1070,8 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { /// These last two bits of info happen to be things that borrowck needs. pub fn cat_slice_pattern(&self, vec_cmt: cmt<'tcx>, - slice_pat: &ast::Pat) - -> McResult<(cmt<'tcx>, ast::Mutability, ty::Region)> { + slice_pat: &hir::Pat) + -> McResult<(cmt<'tcx>, hir::Mutability, ty::Region)> { let slice_ty = try!(self.node_ty(slice_pat.id)); let (slice_mutbl, slice_r) = vec_slice_info(self.tcx(), slice_pat, @@ -1093,9 +1085,9 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { /// ..ref c], then the type of `ref c` will be `&&[]`, so to extract the slice details we /// have to recurse through rptrs. fn vec_slice_info(tcx: &ty::ctxt, - pat: &ast::Pat, + pat: &hir::Pat, slice_ty: Ty) - -> (ast::Mutability, ty::Region) { + -> (hir::Mutability, ty::Region) { match slice_ty.sty { ty::TyRef(r, ref mt) => match mt.ty.sty { ty::TySlice(_) => (mt.mutbl, *r), @@ -1132,7 +1124,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { node: &N, base_cmt: cmt<'tcx>, downcast_ty: Ty<'tcx>, - variant_did: ast::DefId) + variant_did: DefId) -> cmt<'tcx> { let ret = Rc::new(cmt_ { id: node.id(), @@ -1146,16 +1138,16 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { ret } - pub fn cat_pattern(&self, cmt: cmt<'tcx>, pat: &ast::Pat, mut op: F) -> McResult<()> - where F: FnMut(&MemCategorizationContext<'t, 'a, 'tcx>, cmt<'tcx>, &ast::Pat), + pub fn cat_pattern(&self, cmt: cmt<'tcx>, pat: &hir::Pat, mut op: F) -> McResult<()> + where F: FnMut(&MemCategorizationContext<'t, 'a, 'tcx>, cmt<'tcx>, &hir::Pat), { self.cat_pattern_(cmt, pat, &mut op) } // FIXME(#19596) This is a workaround, but there should be a better way to do this - fn cat_pattern_(&self, cmt: cmt<'tcx>, pat: &ast::Pat, op: &mut F) + fn cat_pattern_(&self, cmt: cmt<'tcx>, pat: &hir::Pat, op: &mut F) -> McResult<()> - where F : FnMut(&MemCategorizationContext<'t, 'a, 'tcx>, cmt<'tcx>, &ast::Pat), + where F : FnMut(&MemCategorizationContext<'t, 'a, 'tcx>, cmt<'tcx>, &hir::Pat), { // Here, `cmt` is the categorization for the value being // matched and pat is the pattern it is being matched against. @@ -1216,21 +1208,21 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { let cmt = match opt_def { Some(def::DefVariant(enum_did, variant_did, _)) // univariant enums do not need downcasts - if !self.tcx().enum_is_univariant(enum_did) => { + if !self.tcx().lookup_adt_def(enum_did).is_univariant() => { self.cat_downcast(pat, cmt.clone(), cmt.ty, variant_did) } _ => cmt }; match pat.node { - ast::PatWild(_) => { + hir::PatWild(_) => { // _ } - ast::PatEnum(_, None) => { + hir::PatEnum(_, None) => { // variant(..) } - ast::PatEnum(_, Some(ref subpats)) => { + hir::PatEnum(_, Some(ref subpats)) => { match opt_def { Some(def::DefVariant(..)) => { // variant(x, y, z) @@ -1268,19 +1260,19 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { } } - ast::PatQPath(..) => { + hir::PatQPath(..) => { // Lone constant: ignore } - ast::PatIdent(_, _, Some(ref subpat)) => { + hir::PatIdent(_, _, Some(ref subpat)) => { try!(self.cat_pattern_(cmt, &**subpat, op)); } - ast::PatIdent(_, _, None) => { + hir::PatIdent(_, _, None) => { // nullary variant or identifier: ignore } - ast::PatStruct(_, ref field_pats, _) => { + hir::PatStruct(_, ref field_pats, _) => { // {f1: p1, ..., fN: pN} for fp in field_pats { let field_ty = try!(self.pat_ty(&*fp.node.pat)); // see (*2) @@ -1289,7 +1281,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { } } - ast::PatTup(ref subpats) => { + hir::PatTup(ref subpats) => { // (p1, ..., pN) for (i, subpat) in subpats.iter().enumerate() { let subpat_ty = try!(self.pat_ty(&**subpat)); // see (*2) @@ -1301,7 +1293,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { } } - ast::PatBox(ref subpat) | ast::PatRegion(ref subpat, _) => { + hir::PatBox(ref subpat) | hir::PatRegion(ref subpat, _) => { // box p1, &p1, &mut p1. we can ignore the mutability of // PatRegion since that information is already contained // in the type. @@ -1309,7 +1301,7 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { try!(self.cat_pattern_(subcmt, &**subpat, op)); } - ast::PatVec(ref before, ref slice, ref after) => { + hir::PatVec(ref before, ref slice, ref after) => { let context = InteriorOffsetKind::Pattern; let vec_cmt = try!(self.deref_vec(pat, cmt, context)); let elt_cmt = try!(self.cat_index(pat, vec_cmt, context)); @@ -1326,13 +1318,9 @@ impl<'t, 'a,'tcx> MemCategorizationContext<'t, 'a, 'tcx> { } } - ast::PatLit(_) | ast::PatRange(_, _) => { + hir::PatLit(_) | hir::PatRange(_, _) => { /*always ok*/ } - - ast::PatMac(_) => { - self.tcx().sess.span_bug(pat.span, "unexpanded macro"); - } } Ok(())