Skip to content

Commit d6772cb

Browse files
spastorinonikomatsakis
authored andcommitted
Check Repeat Rvalue
1 parent dcf3db4 commit d6772cb

File tree

3 files changed

+93
-3
lines changed

3 files changed

+93
-3
lines changed

src/librustc/infer/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,10 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
12011201
/// translate them into the form that the NLL solver
12021202
/// understands. See the NLL module for mode details.
12031203
pub fn take_and_reset_region_constraints(&self) -> RegionConstraintData<'tcx> {
1204+
assert!(self.region_obligations.borrow().is_empty(),
1205+
"region_obligations not empty: {:#?}",
1206+
self.region_obligations.borrow());
1207+
12041208
self.borrow_region_constraints().take_and_reset_data()
12051209
}
12061210

src/librustc_mir/transform/type_check.rs

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc::infer::region_constraints::RegionConstraintData;
1717
use rustc::traits::{self, FulfillmentContext};
1818
use rustc::ty::error::TypeError;
1919
use rustc::ty::fold::TypeFoldable;
20-
use rustc::ty::{self, Ty, TyCtxt, TypeVariants};
20+
use rustc::ty::{self, Ty, TyCtxt, TypeVariants, ToPolyTraitRef};
2121
use rustc::middle::const_val::ConstVal;
2222
use rustc::mir::*;
2323
use rustc::mir::tcx::PlaceTy;
@@ -545,6 +545,13 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
545545
span_mirbug!(self, "", "errors selecting obligation: {:?}", e);
546546
}
547547

548+
self.infcx.process_registered_region_obligations(
549+
&[],
550+
None,
551+
self.param_env,
552+
self.body_id,
553+
);
554+
548555
let data = self.infcx.take_and_reset_region_constraints();
549556
if !data.is_empty() {
550557
self.constraints
@@ -1110,13 +1117,28 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
11101117
}
11111118

11121119
fn check_rvalue(&mut self, mir: &Mir<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
1120+
let tcx = self.tcx();
1121+
11131122
match rvalue {
11141123
Rvalue::Aggregate(ak, ops) => {
11151124
self.check_aggregate_rvalue(mir, rvalue, ak, ops, location)
11161125
}
1126+
1127+
Rvalue::Repeat(operand, const_usize) => {
1128+
if const_usize.as_u64() > 1 {
1129+
let operand_ty = operand.ty(mir, tcx);
1130+
1131+
let trait_ref = ty::TraitRef {
1132+
def_id: tcx.lang_items().copy_trait().unwrap(),
1133+
substs: tcx.mk_substs_trait(operand_ty, &[]),
1134+
};
1135+
1136+
self.prove_trait_ref(trait_ref, location);
1137+
}
1138+
}
1139+
11171140
// FIXME: These other cases have to be implemented in future PRs
11181141
Rvalue::Use(..) |
1119-
Rvalue::Repeat(..) |
11201142
Rvalue::Ref(..) |
11211143
Rvalue::Len(..) |
11221144
Rvalue::Cast(..) |
@@ -1205,6 +1227,31 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
12051227
}
12061228
}
12071229

1230+
fn prove_trait_ref(&mut self, trait_ref: ty::TraitRef<'tcx>, location: Location) {
1231+
self.prove_predicates(
1232+
&[
1233+
ty::Predicate::Trait(trait_ref.to_poly_trait_ref().to_poly_trait_predicate()),
1234+
],
1235+
location,
1236+
);
1237+
}
1238+
1239+
fn prove_predicates(&mut self, predicates: &[ty::Predicate<'tcx>], location: Location) {
1240+
self.fully_perform_op(location.at_self(), |this| {
1241+
let cause = this.misc(this.last_span);
1242+
let obligations = predicates
1243+
.iter()
1244+
.map(|&p| {
1245+
traits::Obligation::new(cause.clone(), this.param_env, p)
1246+
})
1247+
.collect();
1248+
Ok(InferOk {
1249+
value: (),
1250+
obligations,
1251+
})
1252+
}).unwrap()
1253+
}
1254+
12081255
fn typeck_mir(&mut self, mir: &Mir<'tcx>) {
12091256
self.last_span = mir.span;
12101257
debug!("run_on_mir: {:?}", mir.span);
@@ -1237,7 +1284,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
12371284
{
12381285
self.fully_perform_op(location.at_self(), |this| {
12391286
let mut selcx = traits::SelectionContext::new(this.infcx);
1240-
let cause = traits::ObligationCause::misc(this.last_span, ast::CRATE_NODE_ID);
1287+
let cause = this.misc(this.last_span);
12411288
let traits::Normalized { value, obligations } =
12421289
traits::normalize(&mut selcx, this.param_env, cause, value);
12431290
Ok(InferOk { value, obligations })
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2017 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+
// compile-flags: -Z borrowck=mir -Z nll
12+
13+
#![allow(warnings)]
14+
15+
struct Foo<T> {
16+
t: T,
17+
}
18+
19+
impl<T: 'static + Copy> Copy for Foo<T> {}
20+
impl<T: 'static + Copy> Clone for Foo<T> {
21+
fn clone(&self) -> Self {
22+
*self
23+
}
24+
}
25+
26+
fn main() {
27+
let mut x = 22;
28+
29+
{
30+
let p = &x; //~ ERROR borrowed value does not live long enough
31+
let w = Foo { t: p };
32+
33+
let v = [w; 22];
34+
}
35+
36+
x += 1;
37+
//~^ ERROR cannot assign to `x` because it is borrowed [E0506]
38+
}
39+
//~^ ERROR borrowed value does not live long enough [E0597]

0 commit comments

Comments
 (0)