rust/src/librustc_passes/rvalues.rs

108 lines
3.6 KiB
Rust
Raw Normal View History

2014-09-01 22:55:07 -05:00
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
// Checks that all rvalues in a crate have statically known size. check_crate
// is the public starting point.
2016-01-21 03:52:37 -06:00
use rustc::dep_graph::DepNode;
use rustc::middle::expr_use_visitor as euv;
use rustc::infer;
2016-01-21 03:52:37 -06:00
use rustc::middle::mem_categorization as mc;
use rustc::ty::{self, TyCtxt, ParameterEnvironment};
use rustc::traits::ProjectionMode;
2014-09-01 22:55:07 -05:00
2015-07-31 02:04:06 -05:00
use rustc_front::hir;
use rustc_front::intravisit;
use syntax::ast;
use syntax::codemap::Span;
2014-09-01 22:55:07 -05:00
2016-02-29 17:36:51 -06:00
pub fn check_crate(tcx: &TyCtxt) {
2014-09-01 22:55:07 -05:00
let mut rvcx = RvalueContext { tcx: tcx };
tcx.visit_all_items_in_krate(DepNode::RvalueCheck, &mut rvcx);
2014-09-01 22:55:07 -05:00
}
struct RvalueContext<'a, 'tcx: 'a> {
2016-02-29 17:36:51 -06:00
tcx: &'a TyCtxt<'tcx>,
2014-09-01 22:55:07 -05:00
}
impl<'a, 'tcx, 'v> intravisit::Visitor<'v> for RvalueContext<'a, 'tcx> {
2014-09-01 22:55:07 -05:00
fn visit_fn(&mut self,
fk: intravisit::FnKind<'v>,
2015-07-31 02:04:06 -05:00
fd: &'v hir::FnDecl,
b: &'v hir::Block,
s: Span,
fn_id: ast::NodeId) {
{
// FIXME (@jroesch) change this to be an inference context
let param_env = ParameterEnvironment::for_item(self.tcx, fn_id);
2015-07-01 15:08:25 -05:00
let infcx = infer::new_infer_ctxt(self.tcx,
&self.tcx.tables,
Some(param_env.clone()),
ProjectionMode::AnyFinal);
let mut delegate = RvalueContextDelegate { tcx: self.tcx, param_env: &param_env };
let mut euv = euv::ExprUseVisitor::new(&mut delegate, &infcx);
euv.walk_fn(fd, b);
}
intravisit::walk_fn(self, fk, fd, b, s)
2014-09-01 22:55:07 -05:00
}
}
struct RvalueContextDelegate<'a, 'tcx: 'a> {
2016-02-29 17:36:51 -06:00
tcx: &'a TyCtxt<'tcx>,
param_env: &'a ty::ParameterEnvironment<'a,'tcx>,
}
impl<'a, 'tcx> euv::Delegate<'tcx> for RvalueContextDelegate<'a, 'tcx> {
2014-09-01 22:55:07 -05:00
fn consume(&mut self,
_: ast::NodeId,
span: Span,
cmt: mc::cmt<'tcx>,
2014-09-01 22:55:07 -05:00
_: euv::ConsumeMode) {
2015-06-18 12:25:05 -05:00
debug!("consume; cmt: {:?}; type: {:?}", *cmt, cmt.ty);
if !cmt.ty.is_sized(self.param_env, span) {
2014-09-01 22:55:07 -05:00
span_err!(self.tcx.sess, span, E0161,
"cannot move a value of type {0}: the size of {0} cannot be statically determined",
2015-06-18 12:25:05 -05:00
cmt.ty);
2014-09-01 22:55:07 -05:00
}
}
fn matched_pat(&mut self,
2015-07-31 02:04:06 -05:00
_matched_pat: &hir::Pat,
_cmt: mc::cmt,
_mode: euv::MatchMode) {}
2014-09-01 22:55:07 -05:00
fn consume_pat(&mut self,
2015-07-31 02:04:06 -05:00
_consume_pat: &hir::Pat,
2014-09-01 22:55:07 -05:00
_cmt: mc::cmt,
_mode: euv::ConsumeMode) {
}
fn borrow(&mut self,
_borrow_id: ast::NodeId,
_borrow_span: Span,
_cmt: mc::cmt,
_loan_region: ty::Region,
_bk: ty::BorrowKind,
_loan_cause: euv::LoanCause) {
}
fn decl_without_init(&mut self,
_id: ast::NodeId,
_span: Span) {
}
fn mutate(&mut self,
_assignment_id: ast::NodeId,
_assignment_span: Span,
_assignee_cmt: mc::cmt,
_mode: euv::MutateMode) {
}
}