]> git.lizzy.rs Git - rust.git/blob - src/librustc_passes/rvalues.rs
libsyntax/parse: improve associated item error reporting
[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::mem_categorization as mc;
17 use rustc::ty::{self, TyCtxt};
18 use rustc::traits::Reveal;
19
20 use rustc::hir;
21 use rustc::hir::intravisit::{Visitor, NestedVisitorMap};
22 use syntax::ast;
23 use syntax_pos::Span;
24
25 pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
26     let mut rvcx = RvalueContext { tcx: tcx };
27     tcx.visit_all_item_likes_in_krate(DepNode::RvalueCheck, &mut rvcx.as_deep_visitor());
28 }
29
30 struct RvalueContext<'a, 'tcx: 'a> {
31     tcx: TyCtxt<'a, 'tcx, 'tcx>,
32 }
33
34 impl<'a, 'tcx> Visitor<'tcx> for RvalueContext<'a, 'tcx> {
35     fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
36         NestedVisitorMap::None
37     }
38
39     fn visit_nested_body(&mut self, body_id: hir::BodyId) {
40         let body = self.tcx.hir.body(body_id);
41         self.tcx.infer_ctxt(body_id, Reveal::UserFacing).enter(|infcx| {
42             let mut delegate = RvalueContextDelegate {
43                 tcx: infcx.tcx,
44                 param_env: &infcx.parameter_environment
45             };
46             euv::ExprUseVisitor::new(&mut delegate, &infcx).consume_body(body);
47         });
48         self.visit_body(body);
49     }
50 }
51
52 struct RvalueContextDelegate<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
53     tcx: TyCtxt<'a, 'gcx, 'tcx>,
54     param_env: &'a ty::ParameterEnvironment<'gcx>,
55 }
56
57 impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for RvalueContextDelegate<'a, 'gcx, 'tcx> {
58     fn consume(&mut self,
59                _: ast::NodeId,
60                span: Span,
61                cmt: mc::cmt<'tcx>,
62                _: euv::ConsumeMode) {
63         debug!("consume; cmt: {:?}; type: {:?}", *cmt, cmt.ty);
64         let ty = self.tcx.lift_to_global(&cmt.ty).unwrap();
65         if !ty.is_sized(self.tcx.global_tcx(), self.param_env, span) {
66             span_err!(self.tcx.sess, span, E0161,
67                 "cannot move a value of type {0}: the size of {0} cannot be statically determined",
68                 ty);
69         }
70     }
71
72     fn matched_pat(&mut self,
73                    _matched_pat: &hir::Pat,
74                    _cmt: mc::cmt,
75                    _mode: euv::MatchMode) {}
76
77     fn consume_pat(&mut self,
78                    _consume_pat: &hir::Pat,
79                    _cmt: mc::cmt,
80                    _mode: euv::ConsumeMode) {
81     }
82
83     fn borrow(&mut self,
84               _borrow_id: ast::NodeId,
85               _borrow_span: Span,
86               _cmt: mc::cmt,
87               _loan_region: &'tcx ty::Region,
88               _bk: ty::BorrowKind,
89               _loan_cause: euv::LoanCause) {
90     }
91
92     fn decl_without_init(&mut self,
93                          _id: ast::NodeId,
94                          _span: Span) {
95     }
96
97     fn mutate(&mut self,
98               _assignment_id: ast::NodeId,
99               _assignment_span: Span,
100               _assignee_cmt: mc::cmt,
101               _mode: euv::MutateMode) {
102     }
103 }