]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc/middle/effect.rs
Auto merge of #30696 - steveklabnik:gh30655, r=brson
[rust.git] / src / librustc / middle / effect.rs
index 3fe1e2f5e8369903974114a506d532c48e1dd2d1..822faae772611f82c1a3b56385e4cf78f9f6e038 100644 (file)
@@ -18,8 +18,9 @@
 
 use syntax::ast;
 use syntax::codemap::Span;
-use syntax::visit;
-use syntax::visit::Visitor;
+use rustc_front::hir;
+use rustc_front::intravisit;
+use rustc_front::intravisit::{FnKind, Visitor};
 
 #[derive(Copy, Clone)]
 struct UnsafeContext {
@@ -42,7 +43,7 @@ enum RootUnsafeContext {
 
 fn type_is_unsafe_function(ty: Ty) -> bool {
     match ty.sty {
-        ty::TyBareFn(_, ref f) => f.unsafety == ast::Unsafety::Unsafe,
+        ty::TyBareFn(_, ref f) => f.unsafety == hir::Unsafety::Unsafe,
         _ => false,
     }
 }
@@ -75,14 +76,14 @@ fn require_unsafe(&mut self, span: Span, description: &str) {
 }
 
 impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
-    fn visit_fn(&mut self, fn_kind: visit::FnKind<'v>, fn_decl: &'v ast::FnDecl,
-                block: &'v ast::Block, span: Span, _: ast::NodeId) {
+    fn visit_fn(&mut self, fn_kind: FnKind<'v>, fn_decl: &'v hir::FnDecl,
+                block: &'v hir::Block, span: Span, _: ast::NodeId) {
 
         let (is_item_fn, is_unsafe_fn) = match fn_kind {
-            visit::FkItemFn(_, _, unsafety, _, _, _) =>
-                (true, unsafety == ast::Unsafety::Unsafe),
-            visit::FkMethod(_, sig, _) =>
-                (true, sig.unsafety == ast::Unsafety::Unsafe),
+            FnKind::ItemFn(_, _, unsafety, _, _, _) =>
+                (true, unsafety == hir::Unsafety::Unsafe),
+            FnKind::Method(_, sig, _) =>
+                (true, sig.unsafety == hir::Unsafety::Unsafe),
             _ => (false, false),
         };
 
@@ -93,16 +94,15 @@ fn visit_fn(&mut self, fn_kind: visit::FnKind<'v>, fn_decl: &'v ast::FnDecl,
             self.unsafe_context = UnsafeContext::new(SafeContext)
         }
 
-        visit::walk_fn(self, fn_kind, fn_decl, block, span);
+        intravisit::walk_fn(self, fn_kind, fn_decl, block, span);
 
         self.unsafe_context = old_unsafe_context
     }
 
-    fn visit_block(&mut self, block: &ast::Block) {
+    fn visit_block(&mut self, block: &hir::Block) {
         let old_unsafe_context = self.unsafe_context;
         match block.rules {
-            ast::DefaultBlock => {}
-            ast::UnsafeBlock(source) => {
+            hir::UnsafeBlock(source) => {
                 // By default only the outermost `unsafe` block is
                 // "used" and so nested unsafe blocks are pointless
                 // (the inner ones are unnecessary and we actually
@@ -118,28 +118,29 @@ fn visit_block(&mut self, block: &ast::Block) {
                 // external blocks (e.g. `unsafe { println("") }`,
                 // expands to `unsafe { ... unsafe { ... } }` where
                 // the inner one is compiler generated).
-                if self.unsafe_context.root == SafeContext || source == ast::CompilerGenerated {
+                if self.unsafe_context.root == SafeContext || source == hir::CompilerGenerated {
                     self.unsafe_context.root = UnsafeBlock(block.id)
                 }
             }
-            ast::PushUnsafeBlock(..) => {
+            hir::PushUnsafeBlock(..) => {
                 self.unsafe_context.push_unsafe_count =
                     self.unsafe_context.push_unsafe_count.checked_add(1).unwrap();
             }
-            ast::PopUnsafeBlock(..) => {
+            hir::PopUnsafeBlock(..) => {
                 self.unsafe_context.push_unsafe_count =
                     self.unsafe_context.push_unsafe_count.checked_sub(1).unwrap();
             }
+            hir::DefaultBlock | hir::PushUnstableBlock | hir:: PopUnstableBlock => {}
         }
 
-        visit::walk_block(self, block);
+        intravisit::walk_block(self, block);
 
         self.unsafe_context = old_unsafe_context
     }
 
-    fn visit_expr(&mut self, expr: &ast::Expr) {
+    fn visit_expr(&mut self, expr: &hir::Expr) {
         match expr.node {
-            ast::ExprMethodCall(_, _, _) => {
+            hir::ExprMethodCall(_, _, _) => {
                 let method_call = MethodCall::expr(expr.id);
                 let base_type = self.tcx.tables.borrow().method_map[&method_call].ty;
                 debug!("effect: method call case, base type is {:?}",
@@ -149,26 +150,26 @@ fn visit_expr(&mut self, expr: &ast::Expr) {
                                         "invocation of unsafe method")
                 }
             }
-            ast::ExprCall(ref base, _) => {
-                let base_type = self.tcx.node_id_to_type(base.id);
+            hir::ExprCall(ref base, _) => {
+                let base_type = self.tcx.expr_ty_adjusted(base);
                 debug!("effect: call case, base type is {:?}",
                         base_type);
                 if type_is_unsafe_function(base_type) {
                     self.require_unsafe(expr.span, "call to unsafe function")
                 }
             }
-            ast::ExprUnary(ast::UnDeref, ref base) => {
-                let base_type = self.tcx.node_id_to_type(base.id);
+            hir::ExprUnary(hir::UnDeref, ref base) => {
+                let base_type = self.tcx.expr_ty_adjusted(base);
                 debug!("effect: unary case, base type is {:?}",
                         base_type);
                 if let ty::TyRawPtr(_) = base_type.sty {
                     self.require_unsafe(expr.span, "dereference of raw pointer")
                 }
             }
-            ast::ExprInlineAsm(..) => {
+            hir::ExprInlineAsm(..) => {
                 self.require_unsafe(expr.span, "use of inline assembly");
             }
-            ast::ExprPath(..) => {
+            hir::ExprPath(..) => {
                 if let def::DefStatic(_, true) = self.tcx.resolve_expr(expr) {
                     self.require_unsafe(expr.span, "use of mutable static");
                 }
@@ -176,7 +177,7 @@ fn visit_expr(&mut self, expr: &ast::Expr) {
             _ => {}
         }
 
-        visit::walk_expr(self, expr);
+        intravisit::walk_expr(self, expr);
     }
 }
 
@@ -186,5 +187,5 @@ pub fn check_crate(tcx: &ty::ctxt) {
         unsafe_context: UnsafeContext::new(SafeContext),
     };
 
-    visit::walk_crate(&mut visitor, tcx.map.krate());
+    tcx.map.krate().visit_all_items(&mut visitor);
 }