From 6aca3301491bc11bf403b9abeedcfbd63c2a43fd Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Sun, 24 Dec 2017 11:40:54 +0200 Subject: [PATCH] Handle casts to integer/float variables These can happen if prior errors disable defaulting. Fixes #43825. --- src/librustc/ty/cast.rs | 3 +++ src/test/ui/cast-errors-issue-43825.rs | 17 +++++++++++++++++ src/test/ui/cast-errors-issue-43825.stderr | 8 ++++++++ 3 files changed, 28 insertions(+) create mode 100644 src/test/ui/cast-errors-issue-43825.rs create mode 100644 src/test/ui/cast-errors-issue-43825.stderr diff --git a/src/librustc/ty/cast.rs b/src/librustc/ty/cast.rs index 0badb85e9e09..c118b7a4692e 100644 --- a/src/librustc/ty/cast.rs +++ b/src/librustc/ty/cast.rs @@ -20,6 +20,7 @@ use syntax::ast; pub enum IntTy { U(ast::UintTy), I, + Ivar, CEnum, Bool, Char @@ -63,6 +64,8 @@ impl<'tcx> CastTy<'tcx> { ty::TyBool => Some(CastTy::Int(IntTy::Bool)), ty::TyChar => Some(CastTy::Int(IntTy::Char)), ty::TyInt(_) => Some(CastTy::Int(IntTy::I)), + ty::TyInfer(ty::InferTy::IntVar(_)) => Some(CastTy::Int(IntTy::Ivar)), + ty::TyInfer(ty::InferTy::FloatVar(_)) => Some(CastTy::Float), ty::TyUint(u) => Some(CastTy::Int(IntTy::U(u))), ty::TyFloat(_) => Some(CastTy::Float), ty::TyAdt(d,_) if d.is_enum() && d.is_payloadfree() => diff --git a/src/test/ui/cast-errors-issue-43825.rs b/src/test/ui/cast-errors-issue-43825.rs new file mode 100644 index 000000000000..65b391205e38 --- /dev/null +++ b/src/test/ui/cast-errors-issue-43825.rs @@ -0,0 +1,17 @@ +// Copyright 2017 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 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let error = error; //~ ERROR cannot find value `error` + + // These used to cause errors. + 0 as f32; + 0.0 as u32; +} diff --git a/src/test/ui/cast-errors-issue-43825.stderr b/src/test/ui/cast-errors-issue-43825.stderr new file mode 100644 index 000000000000..db0a33e927fa --- /dev/null +++ b/src/test/ui/cast-errors-issue-43825.stderr @@ -0,0 +1,8 @@ +error[E0425]: cannot find value `error` in this scope + --> $DIR/cast-errors-issue-43825.rs:12:17 + | +12 | let error = error; //~ ERROR cannot find value `error` + | ^^^^^ not found in this scope + +error: aborting due to previous error +