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::middle::mem_categorization as mc;
|
2016-03-22 10:30:57 -05:00
|
|
|
use rustc::ty::{self, TyCtxt, ParameterEnvironment};
|
2016-06-30 13:22:47 -05:00
|
|
|
use rustc::traits::Reveal;
|
2014-09-01 22:55:07 -05:00
|
|
|
|
2016-03-29 00:50:44 -05:00
|
|
|
use rustc::hir;
|
2016-11-28 13:00:26 -06:00
|
|
|
use rustc::hir::intravisit::{self, Visitor, NestedVisitorMap};
|
2015-12-22 15:35:02 -06:00
|
|
|
use syntax::ast;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::Span;
|
2014-09-01 22:55:07 -05:00
|
|
|
|
2016-05-02 21:23:22 -05:00
|
|
|
pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
2014-09-01 22:55:07 -05:00
|
|
|
let mut rvcx = RvalueContext { tcx: tcx };
|
2016-11-02 17:22:59 -05:00
|
|
|
tcx.visit_all_item_likes_in_krate(DepNode::RvalueCheck, &mut rvcx.as_deep_visitor());
|
2014-09-01 22:55:07 -05:00
|
|
|
}
|
|
|
|
|
2014-04-22 07:56:37 -05:00
|
|
|
struct RvalueContext<'a, 'tcx: 'a> {
|
2016-05-02 21:23:22 -05:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2014-09-01 22:55:07 -05:00
|
|
|
}
|
|
|
|
|
2016-10-29 08:01:11 -05:00
|
|
|
impl<'a, 'tcx> Visitor<'tcx> for RvalueContext<'a, 'tcx> {
|
2016-11-28 13:00:26 -06:00
|
|
|
fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'tcx> {
|
|
|
|
NestedVisitorMap::OnlyBodies(&self.tcx.map)
|
2016-10-29 08:01:11 -05:00
|
|
|
}
|
|
|
|
|
2014-09-01 22:55:07 -05:00
|
|
|
fn visit_fn(&mut self,
|
2016-10-29 08:01:11 -05:00
|
|
|
fk: intravisit::FnKind<'tcx>,
|
|
|
|
fd: &'tcx hir::FnDecl,
|
|
|
|
b: hir::ExprId,
|
2014-10-12 16:43:32 -05:00
|
|
|
s: Span,
|
librustc: Make `Copy` opt-in.
This change makes the compiler no longer infer whether types (structures
and enumerations) implement the `Copy` trait (and thus are implicitly
copyable). Rather, you must implement `Copy` yourself via `impl Copy for
MyType {}`.
A new warning has been added, `missing_copy_implementations`, to warn
you if a non-generic public type has been added that could have
implemented `Copy` but didn't.
For convenience, you may *temporarily* opt out of this behavior by using
`#![feature(opt_out_copy)]`. Note though that this feature gate will never be
accepted and will be removed by the time that 1.0 is released, so you should
transition your code away from using it.
This breaks code like:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
Change this code to:
#[deriving(Show)]
struct Point2D {
x: int,
y: int,
}
impl Copy for Point2D {}
fn main() {
let mypoint = Point2D {
x: 1,
y: 1,
};
let otherpoint = mypoint;
println!("{}{}", mypoint, otherpoint);
}
This is the backwards-incompatible part of #13231.
Part of RFC #3.
[breaking-change]
2014-12-05 19:01:33 -06:00
|
|
|
fn_id: ast::NodeId) {
|
2016-03-24 22:22:44 -05:00
|
|
|
// FIXME (@jroesch) change this to be an inference context
|
|
|
|
let param_env = ParameterEnvironment::for_item(self.tcx, fn_id);
|
2016-05-10 20:14:41 -05:00
|
|
|
self.tcx.infer_ctxt(None, Some(param_env.clone()),
|
2016-06-30 13:22:47 -05:00
|
|
|
Reveal::NotSpecializable).enter(|infcx| {
|
2016-03-24 22:22:44 -05:00
|
|
|
let mut delegate = RvalueContextDelegate {
|
2016-05-10 20:14:41 -05:00
|
|
|
tcx: infcx.tcx,
|
2016-03-24 22:22:44 -05:00
|
|
|
param_env: ¶m_env
|
|
|
|
};
|
2016-10-29 08:01:11 -05:00
|
|
|
let body = infcx.tcx.map.expr(b);
|
2015-06-29 01:03:47 -05:00
|
|
|
let mut euv = euv::ExprUseVisitor::new(&mut delegate, &infcx);
|
2016-10-29 08:01:11 -05:00
|
|
|
euv.walk_fn(fd, body);
|
2016-03-24 22:22:44 -05:00
|
|
|
});
|
2016-07-28 04:58:45 -05:00
|
|
|
intravisit::walk_fn(self, fk, fd, b, s, fn_id)
|
2014-09-01 22:55:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-10 20:14:41 -05:00
|
|
|
struct RvalueContextDelegate<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
|
|
|
|
tcx: TyCtxt<'a, 'gcx, 'tcx>,
|
|
|
|
param_env: &'a ty::ParameterEnvironment<'gcx>,
|
2014-12-18 08:26:10 -06:00
|
|
|
}
|
|
|
|
|
2016-05-10 20:14:41 -05:00
|
|
|
impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for RvalueContextDelegate<'a, 'gcx, 'tcx> {
|
2014-09-01 22:55:07 -05:00
|
|
|
fn consume(&mut self,
|
|
|
|
_: ast::NodeId,
|
|
|
|
span: Span,
|
2014-09-29 14:11:30 -05:00
|
|
|
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);
|
2016-05-10 20:14:41 -05:00
|
|
|
let ty = self.tcx.lift_to_global(&cmt.ty).unwrap();
|
|
|
|
if !ty.is_sized(self.tcx.global_tcx(), 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",
|
2016-05-10 20:14:41 -05:00
|
|
|
ty);
|
2014-09-01 22:55:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-16 08:24:56 -05:00
|
|
|
fn matched_pat(&mut self,
|
2015-07-31 02:04:06 -05:00
|
|
|
_matched_pat: &hir::Pat,
|
2014-09-16 08:24:56 -05:00
|
|
|
_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,
|
2016-08-25 15:58:52 -05:00
|
|
|
_loan_region: &'tcx ty::Region,
|
2014-09-01 22:55:07 -05:00
|
|
|
_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) {
|
|
|
|
}
|
|
|
|
}
|