diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index d5020b12ee0..9500e4a3604 100644 --- a/src/librustc/infer/mod.rs +++ b/src/librustc/infer/mod.rs @@ -1191,28 +1191,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { // types using one of these methods, and should not call span_err directly for such // errors. - pub fn type_error_message(&self, - sp: Span, - mk_msg: M, - actual_ty: Ty<'tcx>) - where M: FnOnce(String) -> String, - { - self.type_error_struct(sp, mk_msg, actual_ty).emit(); - } - - // FIXME: this results in errors without an error code. Deprecate? - pub fn type_error_struct(&self, - sp: Span, - mk_msg: M, - actual_ty: Ty<'tcx>) - -> DiagnosticBuilder<'tcx> - where M: FnOnce(String) -> String, - { - self.type_error_struct_with_diag(sp, |actual_ty| { - self.tcx.sess.struct_span_err(sp, &mk_msg(actual_ty)) - }, actual_ty) - } - pub fn type_error_struct_with_diag(&self, sp: Span, mk_diag: M, diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs index 46d304976dc..7bd24c939ca 100644 --- a/src/librustc_typeck/check/cast.rs +++ b/src/librustc_typeck/check/cast.rs @@ -239,12 +239,10 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> { } let tstr = fcx.ty_to_string(self.cast_ty); - let mut err = - fcx.type_error_struct(self.span, - |actual| { - format!("cast to unsized type: `{}` as `{}`", actual, tstr) - }, - self.expr_ty); + let mut err = type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0620, + "cast to unsized type: `{}` as `{}`", + fcx.resolve_type_vars_if_possible(&self.expr_ty), + tstr); match self.expr_ty.sty { ty::TyRef(_, ty::TypeAndMut { mutbl: mt, .. }) => { let mtstr = match mt { diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 34cf1d7f96b..a8d0be2bd04 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -4716,9 +4716,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { // If not, error. if alternative.is_ty_var() || alternative.references_error() { if !self.is_tainted_by_errors() { - self.type_error_message(sp, |_actual| { - "the type of this value must be known in this context".to_string() - }, ty); + type_error_struct!(self.tcx.sess, sp, ty, E0619, + "the type of this value must be known in this context") + .emit(); } self.demand_suptype(sp, self.tcx.types.err, ty); ty = self.tcx.types.err; diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index bf5adc8644d..1b17faccc87 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4665,6 +4665,67 @@ i_am_a_function(); ``` "##, +E0619: r##" +The type-checker needed to know the type of an expression, but that type had not +yet been inferred. + +Erroneous code example: + +```compile_fail,E0619 +let mut x = vec![]; +match x.pop() { + Some(v) => { + // Here, the type of `v` is not (yet) known, so we + // cannot resolve this method call: + v.to_uppercase(); // error: the type of this value must be known in + // this context + } + None => {} +} +``` + +Type inference typically proceeds from the top of the function to the bottom, +figuring out types as it goes. In some cases -- notably method calls and +overloadable operators like `*` -- the type checker may not have enough +information *yet* to make progress. This can be true even if the rest of the +function provides enough context (because the type-checker hasn't looked that +far ahead yet). In this case, type annotations can be used to help it along. + +To fix this error, just specify the type of the variable. Example: + +``` +let mut x: Vec = vec![]; // We precise the type of the vec elements. +match x.pop() { + Some(v) => { + v.to_uppercase(); // Since rustc now knows the type of the vec elements, + // we can use `v`'s methods. + } + None => {} +} +``` +"##, + +E0620: r##" +A cast to an unsized type was attempted. + +Erroneous code example: + +```compile_fail,E0620 +let x = &[1_usize, 2] as [usize]; // error: cast to unsized type: `&[usize; 2]` + // as `[usize]` +``` + +In Rust, some types don't have a known size at compile-time. For example, in a +slice type like `[u32]`, the number of elements is not known at compile-time and +hence the overall size cannot be computed. As a result, such types can only be +manipulated through a reference (e.g., `&T` or `&mut T`) or other pointer-type +(e.g., `Box` or `Rc`). Try casting to a reference instead: + +``` +let x = &[1_usize, 2] as &[usize]; // ok! +``` +"##, + } register_diagnostics! { @@ -4736,5 +4797,4 @@ register_diagnostics! { E0568, // auto-traits can not have predicates, E0588, // packed struct cannot transitively contain a `[repr(align)]` struct E0592, // duplicate definitions with name `{}` - E0619, // intrinsic must be a function } diff --git a/src/test/compile-fail/E0619.rs b/src/test/compile-fail/E0619.rs new file mode 100644 index 00000000000..8ef90d89931 --- /dev/null +++ b/src/test/compile-fail/E0619.rs @@ -0,0 +1,18 @@ +// 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 x; + + match x { + (..) => {} //~ ERROR E0619 + _ => {} + } +} diff --git a/src/test/compile-fail/E0620.rs b/src/test/compile-fail/E0620.rs new file mode 100644 index 00000000000..5e945dfa5c8 --- /dev/null +++ b/src/test/compile-fail/E0620.rs @@ -0,0 +1,13 @@ +// 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 _foo = &[1_usize, 2] as [usize]; //~ ERROR E0620 +}