diff --git a/src/librustc_mir/build/expr/as_place.rs b/src/librustc_mir/build/expr/as_place.rs index 29c3fb69339..820822b7f5b 100644 --- a/src/librustc_mir/build/expr/as_place.rs +++ b/src/librustc_mir/build/expr/as_place.rs @@ -139,17 +139,19 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { ExprKind::PlaceTypeAscription { source, user_ty } => { let place = unpack!(block = this.as_place(block, source)); - this.cfg.push( - block, - Statement { - source_info, - kind: StatementKind::AscribeUserType( - place.clone(), - Variance::Invariant, - user_ty, - ), - }, - ); + if let Some(user_ty) = user_ty { + this.cfg.push( + block, + Statement { + source_info, + kind: StatementKind::AscribeUserType( + place.clone(), + Variance::Invariant, + user_ty, + ), + }, + ); + } block.and(place) } ExprKind::ValueTypeAscription { source, user_ty } => { @@ -157,17 +159,19 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> { let temp = unpack!( block = this.as_temp(block, source.temp_lifetime, source, mutability) ); - this.cfg.push( - block, - Statement { - source_info, - kind: StatementKind::AscribeUserType( - Place::Local(temp.clone()), - Variance::Invariant, - user_ty, - ), - }, - ); + if let Some(user_ty) = user_ty { + this.cfg.push( + block, + Statement { + source_info, + kind: StatementKind::AscribeUserType( + Place::Local(temp.clone()), + Variance::Invariant, + user_ty, + ), + }, + ); + } block.and(Place::Local(temp)) } diff --git a/src/librustc_mir/hair/cx/expr.rs b/src/librustc_mir/hair/cx/expr.rs index 72532766d43..5adaad15e4c 100644 --- a/src/librustc_mir/hair/cx/expr.rs +++ b/src/librustc_mir/hair/cx/expr.rs @@ -749,7 +749,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, ExprKind::ValueTypeAscription { source: cast_expr.to_ref(), - user_ty: user_ty, + user_ty: Some(user_ty), } } else { cast @@ -757,15 +757,7 @@ fn make_mirror_unadjusted<'a, 'gcx, 'tcx>(cx: &mut Cx<'a, 'gcx, 'tcx>, } hir::ExprKind::Type(ref source, ref ty) => { let user_provided_tys = cx.tables.user_provided_tys(); - let user_ty = UserTypeAnnotation::Ty( - *user_provided_tys - .get(ty.hir_id) - .expect(&format!( - "{:?} not found in user_provided_tys, source: {:?}", - ty, - source, - )) - ); + let user_ty = user_provided_tys.get(ty.hir_id).map(|&c_ty| UserTypeAnnotation::Ty(c_ty)); if source.is_place_expr() { ExprKind::PlaceTypeAscription { source: source.to_ref(), diff --git a/src/librustc_mir/hair/mod.rs b/src/librustc_mir/hair/mod.rs index 781b6c92aa1..788db5c0b7e 100644 --- a/src/librustc_mir/hair/mod.rs +++ b/src/librustc_mir/hair/mod.rs @@ -276,12 +276,12 @@ pub enum ExprKind<'tcx> { PlaceTypeAscription { source: ExprRef<'tcx>, /// Type that the user gave to this expression - user_ty: UserTypeAnnotation<'tcx>, + user_ty: Option>, }, ValueTypeAscription { source: ExprRef<'tcx>, /// Type that the user gave to this expression - user_ty: UserTypeAnnotation<'tcx>, + user_ty: Option>, }, Closure { closure_id: DefId, diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index b184b77b7ed..ff92104d127 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -2359,8 +2359,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { pub fn to_ty_saving_user_provided_ty(&self, ast_ty: &hir::Ty) -> Ty<'tcx> { let ty = self.to_ty(ast_ty); - let c_ty = self.infcx.canonicalize_response(&ty); - self.tables.borrow_mut().user_provided_tys_mut().insert(ast_ty.hir_id, c_ty); + + // If the type given by the user has free regions, save it for + // later, since NLL would like to enforce those. Other sorts + // of things are already sufficiently enforced. =) + if ty.has_free_regions() { + let c_ty = self.infcx.canonicalize_response(&ty); + self.tables.borrow_mut().user_provided_tys_mut().insert(ast_ty.hir_id, c_ty); + } + ty } diff --git a/src/test/ui/consts/const-eval/conditional_array_execution.rs b/src/test/ui/consts/const-eval/conditional_array_execution.rs index 9a6ffcd2c67..8142ed0155a 100644 --- a/src/test/ui/consts/const-eval/conditional_array_execution.rs +++ b/src/test/ui/consts/const-eval/conditional_array_execution.rs @@ -14,7 +14,6 @@ const X: u32 = 5; const Y: u32 = 6; const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; //~^ WARN this constant cannot be used -//~| ERROR fn main() { println!("{}", FOO); diff --git a/src/test/ui/consts/const-eval/conditional_array_execution.stderr b/src/test/ui/consts/const-eval/conditional_array_execution.stderr index b22a8d8aead..29f5f8e2ade 100644 --- a/src/test/ui/consts/const-eval/conditional_array_execution.stderr +++ b/src/test/ui/consts/const-eval/conditional_array_execution.stderr @@ -13,7 +13,7 @@ LL | #![warn(const_err)] | ^^^^^^^^^ error[E0080]: referenced constant has errors - --> $DIR/conditional_array_execution.rs:20:20 + --> $DIR/conditional_array_execution.rs:19:20 | LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; | ----- attempt to subtract with overflow @@ -21,20 +21,12 @@ LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; LL | println!("{}", FOO); | ^^^ -error[E0080]: could not evaluate constant - --> $DIR/conditional_array_execution.rs:20:20 +error[E0080]: erroneous constant used + --> $DIR/conditional_array_execution.rs:19:20 | LL | println!("{}", FOO); | ^^^ referenced constant has errors -error[E0080]: constant evaluation error - --> $DIR/conditional_array_execution.rs:15:1 - | -LL | const FOO: u32 = [X - Y, Y - X][(X < Y) as usize]; - | ^^^^^^^^^^^^^^^^^^-----^^^^^^^^^^^^^^^^^^^^^^^^^^^ - | | - | attempt to subtract with overflow - -error: aborting due to 3 previous errors +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0080`. diff --git a/src/test/ui/consts/const-eval/promoted_errors.stderr b/src/test/ui/consts/const-eval/promoted_errors.stderr index 1c4868c2af9..85b5696be94 100644 --- a/src/test/ui/consts/const-eval/promoted_errors.stderr +++ b/src/test/ui/consts/const-eval/promoted_errors.stderr @@ -34,12 +34,6 @@ warning: attempt to divide by zero LL | println!("{}", 1/(false as u32)); | ^^^^^^^^^^^^^^^^ -warning: this expression will panic at runtime - --> $DIR/promoted_errors.rs:24:20 - | -LL | println!("{}", 1/(false as u32)); - | ^^^^^^^^^^^^^^^^ attempt to divide by zero - warning: attempt to divide by zero --> $DIR/promoted_errors.rs:26:14 |