Skip to content

Commit

Permalink
Auto merge of #42887 - GuillaumeGomez:remove-err-methods, r=nikomatsakis
Browse files Browse the repository at this point in the history
Remove err methods

To be merged after #42519.

cc @Susurrus @QuietMisdreavus
  • Loading branch information
bors committed Jun 29, 2017
2 parents 7acce37 + 5acc1de commit d0e0f53
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 32 deletions.
22 changes: 0 additions & 22 deletions src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<M>(&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<M>(&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<M>(&self,
sp: Span,
mk_diag: M,
Expand Down
10 changes: 4 additions & 6 deletions src/librustc_typeck/check/cast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
62 changes: 61 additions & 1 deletion src/librustc_typeck/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> = 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! {
Expand Down Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions src/test/compile-fail/E0619.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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
_ => {}
}
}
13 changes: 13 additions & 0 deletions src/test/compile-fail/E0620.rs
Original file line number Diff line number Diff line change
@@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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
}

0 comments on commit d0e0f53

Please sign in to comment.