|
| 1 | +// Copyright 2014 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 | +// Checks that all rvalues in a crate have statically known size. check_crate |
| 12 | +// is the public starting point. |
| 13 | + |
| 14 | +use middle::expr_use_visitor as euv; |
| 15 | +use middle::mem_categorization as mc; |
| 16 | +use middle::ty; |
| 17 | +use util::ppaux::ty_to_string; |
| 18 | + |
| 19 | +use syntax::ast; |
| 20 | +use syntax::codemap::Span; |
| 21 | +use syntax::visit; |
| 22 | + |
| 23 | +pub fn check_crate(tcx: &ty::ctxt, |
| 24 | + krate: &ast::Crate) { |
| 25 | + let mut rvcx = RvalueContext { tcx: tcx }; |
| 26 | + visit::walk_crate(&mut rvcx, krate, ()); |
| 27 | +} |
| 28 | + |
| 29 | +struct RvalueContext<'a> { |
| 30 | + tcx: &'a ty::ctxt |
| 31 | +} |
| 32 | + |
| 33 | +impl<'a> visit::Visitor<()> for RvalueContext<'a> { |
| 34 | + fn visit_fn(&mut self, |
| 35 | + _: &visit::FnKind, |
| 36 | + fd: &ast::FnDecl, |
| 37 | + b: &ast::Block, |
| 38 | + _: Span, |
| 39 | + _: ast::NodeId, |
| 40 | + _: ()) { |
| 41 | + let mut euv = euv::ExprUseVisitor::new(self, self.tcx); |
| 42 | + euv.walk_fn(fd, b); |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +impl<'a> euv::Delegate for RvalueContext<'a> { |
| 47 | + fn consume(&mut self, |
| 48 | + _: ast::NodeId, |
| 49 | + span: Span, |
| 50 | + cmt: mc::cmt, |
| 51 | + _: euv::ConsumeMode) { |
| 52 | + debug!("consume; cmt: {:?}; type: {}", *cmt, ty_to_string(self.tcx, cmt.ty)); |
| 53 | + if !ty::type_is_sized(self.tcx, cmt.ty) { |
| 54 | + span_err!(self.tcx.sess, span, E0161, |
| 55 | + "cannot move a value of type {0}: the size of {0} cannot be statically determined", |
| 56 | + ty_to_string(self.tcx, cmt.ty)); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + fn consume_pat(&mut self, |
| 61 | + _consume_pat: &ast::Pat, |
| 62 | + _cmt: mc::cmt, |
| 63 | + _mode: euv::ConsumeMode) { |
| 64 | + } |
| 65 | + |
| 66 | + fn borrow(&mut self, |
| 67 | + _borrow_id: ast::NodeId, |
| 68 | + _borrow_span: Span, |
| 69 | + _cmt: mc::cmt, |
| 70 | + _loan_region: ty::Region, |
| 71 | + _bk: ty::BorrowKind, |
| 72 | + _loan_cause: euv::LoanCause) { |
| 73 | + } |
| 74 | + |
| 75 | + fn decl_without_init(&mut self, |
| 76 | + _id: ast::NodeId, |
| 77 | + _span: Span) { |
| 78 | + } |
| 79 | + |
| 80 | + fn mutate(&mut self, |
| 81 | + _assignment_id: ast::NodeId, |
| 82 | + _assignment_span: Span, |
| 83 | + _assignee_cmt: mc::cmt, |
| 84 | + _mode: euv::MutateMode) { |
| 85 | + } |
| 86 | +} |
0 commit comments