Skip to content

Commit d0e0f53

Browse files
committed
Auto merge of #42887 - GuillaumeGomez:remove-err-methods, r=nikomatsakis
Remove err methods To be merged after #42519. cc @Susurrus @QuietMisdreavus
2 parents 7acce37 + 5acc1de commit d0e0f53

File tree

6 files changed

+99
-32
lines changed

6 files changed

+99
-32
lines changed

src/librustc/infer/mod.rs

-22
Original file line numberDiff line numberDiff line change
@@ -1191,28 +1191,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11911191
// types using one of these methods, and should not call span_err directly for such
11921192
// errors.
11931193

1194-
pub fn type_error_message<M>(&self,
1195-
sp: Span,
1196-
mk_msg: M,
1197-
actual_ty: Ty<'tcx>)
1198-
where M: FnOnce(String) -> String,
1199-
{
1200-
self.type_error_struct(sp, mk_msg, actual_ty).emit();
1201-
}
1202-
1203-
// FIXME: this results in errors without an error code. Deprecate?
1204-
pub fn type_error_struct<M>(&self,
1205-
sp: Span,
1206-
mk_msg: M,
1207-
actual_ty: Ty<'tcx>)
1208-
-> DiagnosticBuilder<'tcx>
1209-
where M: FnOnce(String) -> String,
1210-
{
1211-
self.type_error_struct_with_diag(sp, |actual_ty| {
1212-
self.tcx.sess.struct_span_err(sp, &mk_msg(actual_ty))
1213-
}, actual_ty)
1214-
}
1215-
12161194
pub fn type_error_struct_with_diag<M>(&self,
12171195
sp: Span,
12181196
mk_diag: M,

src/librustc_typeck/check/cast.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -239,12 +239,10 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
239239
}
240240

241241
let tstr = fcx.ty_to_string(self.cast_ty);
242-
let mut err =
243-
fcx.type_error_struct(self.span,
244-
|actual| {
245-
format!("cast to unsized type: `{}` as `{}`", actual, tstr)
246-
},
247-
self.expr_ty);
242+
let mut err = type_error_struct!(fcx.tcx.sess, self.span, self.expr_ty, E0620,
243+
"cast to unsized type: `{}` as `{}`",
244+
fcx.resolve_type_vars_if_possible(&self.expr_ty),
245+
tstr);
248246
match self.expr_ty.sty {
249247
ty::TyRef(_, ty::TypeAndMut { mutbl: mt, .. }) => {
250248
let mtstr = match mt {

src/librustc_typeck/check/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -4716,9 +4716,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
47164716
// If not, error.
47174717
if alternative.is_ty_var() || alternative.references_error() {
47184718
if !self.is_tainted_by_errors() {
4719-
self.type_error_message(sp, |_actual| {
4720-
"the type of this value must be known in this context".to_string()
4721-
}, ty);
4719+
type_error_struct!(self.tcx.sess, sp, ty, E0619,
4720+
"the type of this value must be known in this context")
4721+
.emit();
47224722
}
47234723
self.demand_suptype(sp, self.tcx.types.err, ty);
47244724
ty = self.tcx.types.err;

src/librustc_typeck/diagnostics.rs

+61-1
Original file line numberDiff line numberDiff line change
@@ -4665,6 +4665,67 @@ i_am_a_function();
46654665
```
46664666
"##,
46674667

4668+
E0619: r##"
4669+
The type-checker needed to know the type of an expression, but that type had not
4670+
yet been inferred.
4671+
4672+
Erroneous code example:
4673+
4674+
```compile_fail,E0619
4675+
let mut x = vec![];
4676+
match x.pop() {
4677+
Some(v) => {
4678+
// Here, the type of `v` is not (yet) known, so we
4679+
// cannot resolve this method call:
4680+
v.to_uppercase(); // error: the type of this value must be known in
4681+
// this context
4682+
}
4683+
None => {}
4684+
}
4685+
```
4686+
4687+
Type inference typically proceeds from the top of the function to the bottom,
4688+
figuring out types as it goes. In some cases -- notably method calls and
4689+
overloadable operators like `*` -- the type checker may not have enough
4690+
information *yet* to make progress. This can be true even if the rest of the
4691+
function provides enough context (because the type-checker hasn't looked that
4692+
far ahead yet). In this case, type annotations can be used to help it along.
4693+
4694+
To fix this error, just specify the type of the variable. Example:
4695+
4696+
```
4697+
let mut x: Vec<String> = vec![]; // We precise the type of the vec elements.
4698+
match x.pop() {
4699+
Some(v) => {
4700+
v.to_uppercase(); // Since rustc now knows the type of the vec elements,
4701+
// we can use `v`'s methods.
4702+
}
4703+
None => {}
4704+
}
4705+
```
4706+
"##,
4707+
4708+
E0620: r##"
4709+
A cast to an unsized type was attempted.
4710+
4711+
Erroneous code example:
4712+
4713+
```compile_fail,E0620
4714+
let x = &[1_usize, 2] as [usize]; // error: cast to unsized type: `&[usize; 2]`
4715+
// as `[usize]`
4716+
```
4717+
4718+
In Rust, some types don't have a known size at compile-time. For example, in a
4719+
slice type like `[u32]`, the number of elements is not known at compile-time and
4720+
hence the overall size cannot be computed. As a result, such types can only be
4721+
manipulated through a reference (e.g., `&T` or `&mut T`) or other pointer-type
4722+
(e.g., `Box` or `Rc`). Try casting to a reference instead:
4723+
4724+
```
4725+
let x = &[1_usize, 2] as &[usize]; // ok!
4726+
```
4727+
"##,
4728+
46684729
}
46694730

46704731
register_diagnostics! {
@@ -4736,5 +4797,4 @@ register_diagnostics! {
47364797
E0568, // auto-traits can not have predicates,
47374798
E0588, // packed struct cannot transitively contain a `[repr(align)]` struct
47384799
E0592, // duplicate definitions with name `{}`
4739-
E0619, // intrinsic must be a function
47404800
}

src/test/compile-fail/E0619.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x;
13+
14+
match x {
15+
(..) => {} //~ ERROR E0619
16+
_ => {}
17+
}
18+
}

src/test/compile-fail/E0620.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let _foo = &[1_usize, 2] as [usize]; //~ ERROR E0620
13+
}

0 commit comments

Comments
 (0)