]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/rvalues.rs
Rollup merge of #31295 - steveklabnik:gh31266, r=alexcrichton
[rust.git] / src / librustc_passes / rvalues.rs
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 rustc::dep_graph::DepNode;
15 use rustc::middle::expr_use_visitor as euv;
16 use rustc::middle::infer;
17 use rustc::middle::mem_categorization as mc;
18 use rustc::middle::ty::{self, ParameterEnvironment};
19
20 use rustc_front::hir;
21 use rustc_front::intravisit;
22 use syntax::ast;
23 use syntax::codemap::Span;
24
25 pub fn check_crate(tcx: &ty::ctxt) {
26     let mut rvcx = RvalueContext { tcx: tcx };
27     tcx.visit_all_items_in_krate(DepNode::RvalueCheck, &mut rvcx);
28 }
29
30 struct RvalueContext<'a, 'tcx: 'a> {
31     tcx: &'a ty::ctxt<'tcx>,
32 }
33
34 impl<'a, 'tcx, 'v> intravisit::Visitor<'v> for RvalueContext<'a, 'tcx> {
35     fn visit_fn(&mut self,
36                 fk: intravisit::FnKind<'v>,
37                 fd: &'v hir::FnDecl,
38                 b: &'v hir::Block,
39                 s: Span,
40                 fn_id: ast::NodeId) {
41         {
42             // FIXME (@jroesch) change this to be an inference context
43             let param_env = ParameterEnvironment::for_item(self.tcx, fn_id);
44             let infcx = infer::new_infer_ctxt(self.tcx,
45                                               &self.tcx.tables,
46                                               Some(param_env.clone()));
47             let mut delegate = RvalueContextDelegate { tcx: self.tcx, param_env: &param_env };
48             let mut euv = euv::ExprUseVisitor::new(&mut delegate, &infcx);
49             euv.walk_fn(fd, b);
50         }
51         intravisit::walk_fn(self, fk, fd, b, s)
52     }
53 }
54
55 struct RvalueContextDelegate<'a, 'tcx: 'a> {
56     tcx: &'a ty::ctxt<'tcx>,
57     param_env: &'a ty::ParameterEnvironment<'a,'tcx>,
58 }
59
60 impl<'a, 'tcx> euv::Delegate<'tcx> for RvalueContextDelegate<'a, 'tcx> {
61     fn consume(&mut self,
62                _: ast::NodeId,
63                span: Span,
64                cmt: mc::cmt<'tcx>,
65                _: euv::ConsumeMode) {
66         debug!("consume; cmt: {:?}; type: {:?}", *cmt, cmt.ty);
67         if !cmt.ty.is_sized(self.param_env, span) {
68             span_err!(self.tcx.sess, span, E0161,
69                 "cannot move a value of type {0}: the size of {0} cannot be statically determined",
70                 cmt.ty);
71         }
72     }
73
74     fn matched_pat(&mut self,
75                    _matched_pat: &hir::Pat,
76                    _cmt: mc::cmt,
77                    _mode: euv::MatchMode) {}
78
79     fn consume_pat(&mut self,
80                    _consume_pat: &hir::Pat,
81                    _cmt: mc::cmt,
82                    _mode: euv::ConsumeMode) {
83     }
84
85     fn borrow(&mut self,
86               _borrow_id: ast::NodeId,
87               _borrow_span: Span,
88               _cmt: mc::cmt,
89               _loan_region: ty::Region,
90               _bk: ty::BorrowKind,
91               _loan_cause: euv::LoanCause) {
92     }
93
94     fn decl_without_init(&mut self,
95                          _id: ast::NodeId,
96                          _span: Span) {
97     }
98
99     fn mutate(&mut self,
100               _assignment_id: ast::NodeId,
101               _assignment_span: Span,
102               _assignee_cmt: mc::cmt,
103               _mode: euv::MutateMode) {
104     }
105 }