From 09f42fb9ccb4dfd728e323cbe5d25ed04a8262ad Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 24 Jun 2017 22:26:45 +0200 Subject: [PATCH 1/4] Add E0619 --- src/librustc_typeck/check/mod.rs | 6 +++--- src/librustc_typeck/diagnostics.rs | 27 +++++++++++++++++++++++++++ src/test/compile-fail/E0619.rs | 18 ++++++++++++++++++ 3 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 src/test/compile-fail/E0619.rs diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 34cf1d7f96ba0..a8d0be2bd04d9 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 bf5adc8644d51..09c3445672a31 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4665,6 +4665,33 @@ i_am_a_function(); ``` "##, +E0619: r##" +A not (yet) known type was used. + +Erroneous code example: + +```compile_fail,E0619 +let x; + +match x { + (..) => {} // error: the type of this value must be known in this context + _ => {} +} +``` + +To fix this error, just specify the type of the variable. Example: + +``` +let x: i32 = 0; // Here, we say that `x` is an `i32` (and give it a value to + // avoid another compiler error). + +match x { + 0 => {} // ok! + _ => {} +} +``` +"##, + } register_diagnostics! { diff --git a/src/test/compile-fail/E0619.rs b/src/test/compile-fail/E0619.rs new file mode 100644 index 0000000000000..8ef90d8993192 --- /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 + _ => {} + } +} From 05ac25affa2f9dda6c6e9e8c5005736e98391c44 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 24 Jun 2017 23:23:47 +0200 Subject: [PATCH 2/4] Add E0620 --- src/librustc_typeck/check/cast.rs | 10 ++++------ src/librustc_typeck/diagnostics.rs | 19 +++++++++++++++++++ src/test/compile-fail/E0620.rs | 13 +++++++++++++ 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 src/test/compile-fail/E0620.rs diff --git a/src/librustc_typeck/check/cast.rs b/src/librustc_typeck/check/cast.rs index 46d304976dc63..7bd24c939caf0 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/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 09c3445672a31..112249463f67b 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4692,6 +4692,25 @@ match x { ``` "##, +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 size at compile-time (like slices and traits +for example). Therefore, you can't cast into them directly. Try casting to a +reference instead: + +``` +let x = &[1_usize, 2] as &[usize]; // ok! +``` +"##, + } register_diagnostics! { diff --git a/src/test/compile-fail/E0620.rs b/src/test/compile-fail/E0620.rs new file mode 100644 index 0000000000000..5e945dfa5c820 --- /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 +} From ff0fb9d906444aa054c6203506350cf752e5bea3 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 24 Jun 2017 23:24:07 +0200 Subject: [PATCH 3/4] Remove unused methods --- src/librustc/infer/mod.rs | 22 ---------------------- 1 file changed, 22 deletions(-) diff --git a/src/librustc/infer/mod.rs b/src/librustc/infer/mod.rs index d5020b12ee00e..9500e4a36048d 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, From 5acc1deaf8ddcf540a7d58a50046f02a1a728a8f Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Tue, 27 Jun 2017 23:07:56 +0200 Subject: [PATCH 4/4] Improve long error explanations for E0620 and E0621 --- src/librustc_typeck/diagnostics.rs | 46 +++++++++++++++++++----------- 1 file changed, 30 insertions(+), 16 deletions(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 112249463f67b..1b17faccc87f8 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -4666,28 +4666,41 @@ i_am_a_function(); "##, E0619: r##" -A not (yet) known type was used. +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 x; - -match x { - (..) => {} // error: the type of this value must be known in this context - _ => {} +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 x: i32 = 0; // Here, we say that `x` is an `i32` (and give it a value to - // avoid another compiler error). - -match x { - 0 => {} // ok! - _ => {} +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 => {} } ``` "##, @@ -4702,9 +4715,11 @@ let x = &[1_usize, 2] as [usize]; // error: cast to unsized type: `&[usize; 2]` // as `[usize]` ``` -In Rust, some types don't have a size at compile-time (like slices and traits -for example). Therefore, you can't cast into them directly. Try casting to a -reference instead: +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! @@ -4782,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 }