1
Fork 0

Auto merge of #53575 - matthewjasper:unsized-is-an-error, r=estebank

Don't reduce E0161 to a warning in NLL migrate mode

This error has been on stable for a while, and allowing such code cause the compile to later ICE (since we can't codegen it). Errors `box UNSIZED EXPR` with unsized locals because it's not compatible with the current evaluation order (create the box before evaluating the expressions).

cc #53469 (fixes the ICE in this case)
cc @qnighy
This commit is contained in:
bors 2018-09-06 21:02:35 +00:00
commit a8c11d216b
13 changed files with 129 additions and 47 deletions

View file

@ -120,7 +120,6 @@ pub(in borrow_check) fn compute_regions<'cx, 'gcx, 'tcx>(
flow_inits, flow_inits,
move_data, move_data,
elements, elements,
errors_buffer,
); );
if let Some(all_facts) = &mut all_facts { if let Some(all_facts) = &mut all_facts {

View file

@ -36,7 +36,6 @@ use rustc::traits::query::type_op;
use rustc::traits::query::{Fallible, NoSolution}; use rustc::traits::query::{Fallible, NoSolution};
use rustc::ty::fold::TypeFoldable; use rustc::ty::fold::TypeFoldable;
use rustc::ty::{self, CanonicalTy, RegionVid, ToPolyTraitRef, Ty, TyCtxt, TyKind}; use rustc::ty::{self, CanonicalTy, RegionVid, ToPolyTraitRef, Ty, TyCtxt, TyKind};
use rustc_errors::Diagnostic;
use std::fmt; use std::fmt;
use std::rc::Rc; use std::rc::Rc;
use syntax_pos::{Span, DUMMY_SP}; use syntax_pos::{Span, DUMMY_SP};
@ -103,8 +102,7 @@ mod relate_tys;
/// - `liveness` -- results of a liveness computation on the MIR; used to create liveness /// - `liveness` -- results of a liveness computation on the MIR; used to create liveness
/// constraints for the regions in the types of variables /// constraints for the regions in the types of variables
/// - `flow_inits` -- results of a maybe-init dataflow analysis /// - `flow_inits` -- results of a maybe-init dataflow analysis
/// - `move_data` -- move-data constructed when performing the maybe-init dataflow analysis /// - `move_data` -- move-data constructed when performing the maybe-init dataflow analysiss
/// - `errors_buffer` -- errors are sent here for future reporting
pub(crate) fn type_check<'gcx, 'tcx>( pub(crate) fn type_check<'gcx, 'tcx>(
infcx: &InferCtxt<'_, 'gcx, 'tcx>, infcx: &InferCtxt<'_, 'gcx, 'tcx>,
param_env: ty::ParamEnv<'gcx>, param_env: ty::ParamEnv<'gcx>,
@ -117,7 +115,6 @@ pub(crate) fn type_check<'gcx, 'tcx>(
flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'_, 'gcx, 'tcx>>, flow_inits: &mut FlowAtLocation<MaybeInitializedPlaces<'_, 'gcx, 'tcx>>,
move_data: &MoveData<'tcx>, move_data: &MoveData<'tcx>,
elements: &Rc<RegionValueElements>, elements: &Rc<RegionValueElements>,
errors_buffer: &mut Vec<Diagnostic>,
) -> MirTypeckResults<'tcx> { ) -> MirTypeckResults<'tcx> {
let implicit_region_bound = infcx.tcx.mk_region(ty::ReVar(universal_regions.fr_fn_body)); let implicit_region_bound = infcx.tcx.mk_region(ty::ReVar(universal_regions.fr_fn_body));
let mut constraints = MirTypeckRegionConstraints { let mut constraints = MirTypeckRegionConstraints {
@ -157,7 +154,6 @@ pub(crate) fn type_check<'gcx, 'tcx>(
&region_bound_pairs, &region_bound_pairs,
Some(implicit_region_bound), Some(implicit_region_bound),
Some(&mut borrowck_context), Some(&mut borrowck_context),
Some(errors_buffer),
|cx| { |cx| {
cx.equate_inputs_and_outputs( cx.equate_inputs_and_outputs(
mir, mir,
@ -185,7 +181,6 @@ fn type_check_internal<'a, 'gcx, 'tcx, R>(
region_bound_pairs: &'a [(ty::Region<'tcx>, GenericKind<'tcx>)], region_bound_pairs: &'a [(ty::Region<'tcx>, GenericKind<'tcx>)],
implicit_region_bound: Option<ty::Region<'tcx>>, implicit_region_bound: Option<ty::Region<'tcx>>,
borrowck_context: Option<&'a mut BorrowCheckContext<'a, 'tcx>>, borrowck_context: Option<&'a mut BorrowCheckContext<'a, 'tcx>>,
errors_buffer: Option<&mut Vec<Diagnostic>>,
mut extra: impl FnMut(&mut TypeChecker<'a, 'gcx, 'tcx>) -> R, mut extra: impl FnMut(&mut TypeChecker<'a, 'gcx, 'tcx>) -> R,
) -> R where { ) -> R where {
let mut checker = TypeChecker::new( let mut checker = TypeChecker::new(
@ -205,7 +200,7 @@ fn type_check_internal<'a, 'gcx, 'tcx, R>(
if !errors_reported { if !errors_reported {
// if verifier failed, don't do further checks to avoid ICEs // if verifier failed, don't do further checks to avoid ICEs
checker.typeck_mir(mir, errors_buffer); checker.typeck_mir(mir);
} }
extra(&mut checker) extra(&mut checker)
@ -989,7 +984,6 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
mir: &Mir<'tcx>, mir: &Mir<'tcx>,
term: &Terminator<'tcx>, term: &Terminator<'tcx>,
term_location: Location, term_location: Location,
errors_buffer: &mut Option<&mut Vec<Diagnostic>>,
) { ) {
debug!("check_terminator: {:?}", term); debug!("check_terminator: {:?}", term);
let tcx = self.tcx(); let tcx = self.tcx();
@ -1069,7 +1063,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
&sig, &sig,
); );
let sig = self.normalize(sig, term_location); let sig = self.normalize(sig, term_location);
self.check_call_dest(mir, term, &sig, destination, term_location, errors_buffer); self.check_call_dest(mir, term, &sig, destination, term_location);
self.prove_predicates( self.prove_predicates(
sig.inputs().iter().map(|ty| ty::Predicate::WellFormed(ty)), sig.inputs().iter().map(|ty| ty::Predicate::WellFormed(ty)),
@ -1143,7 +1137,6 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
sig: &ty::FnSig<'tcx>, sig: &ty::FnSig<'tcx>,
destination: &Option<(Place<'tcx>, BasicBlock)>, destination: &Option<(Place<'tcx>, BasicBlock)>,
term_location: Location, term_location: Location,
errors_buffer: &mut Option<&mut Vec<Diagnostic>>,
) { ) {
let tcx = self.tcx(); let tcx = self.tcx();
match *destination { match *destination {
@ -1177,7 +1170,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
// this check is done at `check_local`. // this check is done at `check_local`.
if self.tcx().features().unsized_locals { if self.tcx().features().unsized_locals {
let span = term.source_info.span; let span = term.source_info.span;
self.ensure_place_sized(dest_ty, span, errors_buffer); self.ensure_place_sized(dest_ty, span);
} }
} }
None => { None => {
@ -1330,7 +1323,6 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
mir: &Mir<'tcx>, mir: &Mir<'tcx>,
local: Local, local: Local,
local_decl: &LocalDecl<'tcx>, local_decl: &LocalDecl<'tcx>,
errors_buffer: &mut Option<&mut Vec<Diagnostic>>,
) { ) {
match mir.local_kind(local) { match mir.local_kind(local) {
LocalKind::ReturnPointer | LocalKind::Arg => { LocalKind::ReturnPointer | LocalKind::Arg => {
@ -1346,18 +1338,15 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
} }
// When `#![feature(unsized_locals)]` is enabled, only function calls // When `#![feature(unsized_locals)]` is enabled, only function calls
// are checked in `check_call_dest`. // and nullary ops are checked in `check_call_dest`.
if !self.tcx().features().unsized_locals { if !self.tcx().features().unsized_locals {
let span = local_decl.source_info.span; let span = local_decl.source_info.span;
let ty = local_decl.ty; let ty = local_decl.ty;
self.ensure_place_sized(ty, span, errors_buffer); self.ensure_place_sized(ty, span);
} }
} }
fn ensure_place_sized(&mut self, fn ensure_place_sized(&mut self, ty: Ty<'tcx>, span: Span) {
ty: Ty<'tcx>,
span: Span,
errors_buffer: &mut Option<&mut Vec<Diagnostic>>) {
let tcx = self.tcx(); let tcx = self.tcx();
// Erase the regions from `ty` to get a global type. The // Erase the regions from `ty` to get a global type. The
@ -1379,15 +1368,13 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
cannot be statically determined", cannot be statically determined",
ty ty
); );
if let Some(ref mut errors_buffer) = *errors_buffer {
diag.buffer(errors_buffer); // While this is located in `nll::typeck` this error is not
} else { // an NLL error, it's a required check to prevent creation
// we're allowed to use emit() here because the // of unsized rvalues in certain cases:
// NLL migration will be turned on (and thus // * operand of a box expression
// errors will need to be buffered) *only if* // * callee in a call expression
// errors_buffer is Some. diag.emit();
diag.emit();
}
} }
} }
} }
@ -1462,6 +1449,12 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}, },
Rvalue::NullaryOp(_, ty) => { Rvalue::NullaryOp(_, ty) => {
// Even with unsized locals cannot box an unsized value.
if self.tcx().features().unsized_locals {
let span = mir.source_info(location).span;
self.ensure_place_sized(ty, span);
}
let trait_ref = ty::TraitRef { let trait_ref = ty::TraitRef {
def_id: tcx.lang_items().sized_trait().unwrap(), def_id: tcx.lang_items().sized_trait().unwrap(),
substs: tcx.mk_substs_trait(ty, &[]), substs: tcx.mk_substs_trait(ty, &[]),
@ -1895,12 +1888,12 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
}) })
} }
fn typeck_mir(&mut self, mir: &Mir<'tcx>, mut errors_buffer: Option<&mut Vec<Diagnostic>>) { fn typeck_mir(&mut self, mir: &Mir<'tcx>) {
self.last_span = mir.span; self.last_span = mir.span;
debug!("run_on_mir: {:?}", mir.span); debug!("run_on_mir: {:?}", mir.span);
for (local, local_decl) in mir.local_decls.iter_enumerated() { for (local, local_decl) in mir.local_decls.iter_enumerated() {
self.check_local(mir, local, local_decl, &mut errors_buffer); self.check_local(mir, local, local_decl);
} }
for (block, block_data) in mir.basic_blocks().iter_enumerated() { for (block, block_data) in mir.basic_blocks().iter_enumerated() {
@ -1916,7 +1909,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
location.statement_index += 1; location.statement_index += 1;
} }
self.check_terminator(mir, block_data.terminator(), location, &mut errors_buffer); self.check_terminator(mir, block_data.terminator(), location);
self.check_iscleanup(mir, block_data); self.check_iscleanup(mir, block_data);
} }
} }
@ -1973,7 +1966,6 @@ impl MirPass for TypeckMir {
&[], &[],
None, None,
None, None,
None,
|_| (), |_| (),
); );

View file

@ -4,18 +4,18 @@ error[E0161]: cannot move a value of type str: the size of str cannot be statica
LL | S[0]; LL | S[0];
| ^^^^ | ^^^^
error[E0161]: cannot move a value of type dyn std::fmt::Debug: the size of dyn std::fmt::Debug cannot be statically determined
--> $DIR/dst-index.rs:44:5
|
LL | T[0];
| ^^^^
error[E0507]: cannot move out of borrowed content error[E0507]: cannot move out of borrowed content
--> $DIR/dst-index.rs:41:5 --> $DIR/dst-index.rs:41:5
| |
LL | S[0]; LL | S[0];
| ^^^^ cannot move out of borrowed content | ^^^^ cannot move out of borrowed content
error[E0161]: cannot move a value of type dyn std::fmt::Debug: the size of dyn std::fmt::Debug cannot be statically determined
--> $DIR/dst-index.rs:44:5
|
LL | T[0];
| ^^^^
error[E0507]: cannot move out of borrowed content error[E0507]: cannot move out of borrowed content
--> $DIR/dst-index.rs:44:5 --> $DIR/dst-index.rs:44:5
| |

View file

@ -4,18 +4,18 @@ error[E0161]: cannot move a value of type str: the size of str cannot be statica
LL | let _x: Box<str> = box *"hello world"; LL | let _x: Box<str> = box *"hello world";
| ^^^^^^^^^^^^^^ | ^^^^^^^^^^^^^^
error[E0507]: cannot move out of borrowed content
--> $DIR/dst-rvalue.rs:16:28
|
LL | let _x: Box<str> = box *"hello world";
| ^^^^^^^^^^^^^^ cannot move out of borrowed content
error[E0161]: cannot move a value of type [isize]: the size of [isize] cannot be statically determined error[E0161]: cannot move a value of type [isize]: the size of [isize] cannot be statically determined
--> $DIR/dst-rvalue.rs:21:32 --> $DIR/dst-rvalue.rs:21:32
| |
LL | let _x: Box<[isize]> = box *array; LL | let _x: Box<[isize]> = box *array;
| ^^^^^^ | ^^^^^^
error[E0507]: cannot move out of borrowed content
--> $DIR/dst-rvalue.rs:16:28
|
LL | let _x: Box<str> = box *"hello world";
| ^^^^^^^^^^^^^^ cannot move out of borrowed content
error[E0508]: cannot move out of type `[isize]`, a non-copy slice error[E0508]: cannot move out of type `[isize]`, a non-copy slice
--> $DIR/dst-rvalue.rs:21:32 --> $DIR/dst-rvalue.rs:21:32
| |

View file

@ -0,0 +1,9 @@
error[E0161]: cannot move a value of type [i32]: the size of [i32] cannot be statically determined
--> $DIR/E0161.rs:32:9
|
LL | box *x; //~ ERROR E0161
| ^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0161`.

View file

@ -0,0 +1,9 @@
error[E0161]: cannot move a value of type [i32]: the size of [i32] cannot be statically determined
--> $DIR/E0161.rs:32:5
|
LL | box *x; //~ ERROR E0161
| ^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0161`.

View file

@ -0,0 +1,9 @@
error[E0161]: cannot move a value of type [i32]: the size of [i32] cannot be statically determined
--> $DIR/E0161.rs:32:9
|
LL | box *x; //~ ERROR E0161
| ^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0161`.

View file

@ -0,0 +1,9 @@
error[E0161]: cannot move a value of type [i32]: the size of [i32] cannot be statically determined
--> $DIR/E0161.rs:32:5
|
LL | box *x; //~ ERROR E0161
| ^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0161`.

View file

@ -0,0 +1,9 @@
error[E0161]: cannot move a value of type [i32]: the size of [i32] cannot be statically determined
--> $DIR/E0161.rs:32:9
|
LL | box *x; //~ ERROR E0161
| ^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0161`.

View file

@ -0,0 +1,9 @@
error[E0161]: cannot move a value of type [i32]: the size of [i32] cannot be statically determined
--> $DIR/E0161.rs:32:5
|
LL | box *x; //~ ERROR E0161
| ^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0161`.

View file

@ -8,9 +8,28 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
// ignore-compare-mode-nll
// Check that E0161 is a hard error in all possible configurations that might
// affect it.
// revisions: ast nll zflags edition astul nllul zflagsul editionul
//[zflags]compile-flags: -Z borrowck=migrate -Z two-phase-borrows
//[edition]edition:2018
//[zflagsul]compile-flags: -Z borrowck=migrate -Z two-phase-borrows
//[editionul]edition:2018
#![cfg_attr(nll, feature(nll))]
#![cfg_attr(nllul, feature(nll))]
#![cfg_attr(astul, feature(unsized_locals))]
#![cfg_attr(zflagsul, feature(unsized_locals))]
#![cfg_attr(nllul, feature(unsized_locals))]
#![cfg_attr(editionul, feature(unsized_locals))]
#![feature(box_syntax)] #![feature(box_syntax)]
fn main() { fn foo(x: Box<[i32]>) {
let _x: Box<str> = box *"hello"; //~ ERROR E0161 box *x; //~ ERROR E0161
//~^ ERROR E0507
} }
fn main() {}

View file

@ -0,0 +1,9 @@
error[E0161]: cannot move a value of type [i32]: the size of [i32] cannot be statically determined
--> $DIR/E0161.rs:32:9
|
LL | box *x; //~ ERROR E0161
| ^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0161`.

View file

@ -0,0 +1,9 @@
error[E0161]: cannot move a value of type [i32]: the size of [i32] cannot be statically determined
--> $DIR/E0161.rs:32:5
|
LL | box *x; //~ ERROR E0161
| ^^^^^^
error: aborting due to previous error
For more information about this error, try `rustc --explain E0161`.