Skip to content

Commit 6877688

Browse files
committed
Refer to types using the local identifier
On type errors, refer to types using the locally available name when they have been imported into scope instead of the fully qualified path. ``` error[E0308]: mismatched types --> file.rs:7:24 | 7 | let y: Option<X> = Ok(()); | ^^^^^^ expected enum `std::option::Option`, found enum `std::result::Result` | = note: expected type `Option<X>` found type `Result<(), _>` = help: here are some functions which might fulfill your needs: - .err() - .unwrap_err() ```
1 parent 2efc1f8 commit 6877688

40 files changed

+374
-103
lines changed

src/librustc/infer/error_reporting/mod.rs

+240-41
Large diffs are not rendered by default.

src/librustc/middle/const_val.rs

+4
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ impl<'tcx> ConstVal<'tcx> {
7676
_ => None
7777
}
7878
}
79+
80+
pub fn to_usize(&self) -> Option<usize> {
81+
self.to_const_int().and_then(|i| i.to_usize())
82+
}
7983
}
8084

8185
#[derive(Clone, Debug)]

src/librustc/ty/context.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11791179
self.all_crate_nums(LOCAL_CRATE)
11801180
}
11811181

1182+
pub fn cstore(&self) -> &CrateStore {
1183+
self.cstore
1184+
}
1185+
11821186
pub fn def_key(self, id: DefId) -> hir_map::DefKey {
11831187
if id.is_local() {
11841188
self.hir.def_key(id)

src/librustc/ty/sty.rs

+6
Original file line numberDiff line numberDiff line change
@@ -1469,4 +1469,10 @@ pub struct Const<'tcx> {
14691469
pub val: ConstVal<'tcx>,
14701470
}
14711471

1472+
impl<'tcx> Const<'tcx> {
1473+
pub fn usize_val(&self) -> usize {
1474+
self.val.to_usize().unwrap_or(0)
1475+
}
1476+
}
1477+
14721478
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Const<'tcx> {}

src/librustc_const_math/int.rs

+9
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,15 @@ impl ConstInt {
188188
})
189189
}
190190

191+
/// Converts the value to a `usize` if it's in the range 0...std::usize::MAX
192+
pub fn to_usize(&self) -> Option<usize> {
193+
self.to_u128().and_then(|v| if v <= usize::max_value() as u128 {
194+
Some(v as usize)
195+
} else {
196+
None
197+
})
198+
}
199+
191200
/// Converts the value to a `u128` if it's in the range 0...std::u128::MAX
192201
pub fn to_u128(&self) -> Option<u128> {
193202
match *self {

src/test/compile-fail/bad-const-type.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
static i: String = 10;
1212
//~^ ERROR mismatched types
13-
//~| expected type `std::string::String`
13+
//~| expected type `String`
1414
//~| found type `{integer}`
1515
//~| expected struct `std::string::String`, found integral variable
1616
fn main() { println!("{}", i); }

src/test/compile-fail/cross-borrow-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ pub fn main() {
1919
let x: Box<Trait> = Box::new(Foo);
2020
let _y: &Trait = x; //~ ERROR E0308
2121
//~| expected type `&Trait`
22-
//~| found type `std::boxed::Box<Trait>`
22+
//~| found type `Box<Trait>`
2323
}

src/test/compile-fail/destructure-trait-ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ fn main() {
5151
let box box x = box 1isize as Box<T>;
5252
//~^ ERROR mismatched types
5353
//~| expected type `T`
54-
//~| found type `std::boxed::Box<_>`
54+
//~| found type `Box<_>`
5555
}

src/test/compile-fail/fn-trait-formatting.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ fn main() {
1616
let _: () = (box |_: isize| {}) as Box<FnOnce(isize)>;
1717
//~^ ERROR mismatched types
1818
//~| expected type `()`
19-
//~| found type `std::boxed::Box<std::ops::FnOnce(isize)>`
19+
//~| found type `Box<std::ops::FnOnce(isize)>`
2020
let _: () = (box |_: isize, isize| {}) as Box<Fn(isize, isize)>;
2121
//~^ ERROR mismatched types
2222
//~| expected type `()`
23-
//~| found type `std::boxed::Box<std::ops::Fn(isize, isize)>`
23+
//~| found type `Box<std::ops::Fn(isize, isize)>`
2424
let _: () = (box || -> isize { unimplemented!() }) as Box<FnMut() -> isize>;
2525
//~^ ERROR mismatched types
2626
//~| expected type `()`
27-
//~| found type `std::boxed::Box<std::ops::FnMut() -> isize>`
27+
//~| found type `Box<std::ops::FnMut() -> isize>`
2828

2929
needs_fn(1);
3030
//~^ ERROR : std::ops::Fn<(isize,)>`

src/test/compile-fail/fully-qualified-type-name1.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ fn main() {
1414
let x: Option<usize>;
1515
x = 5;
1616
//~^ ERROR mismatched types
17-
//~| expected type `std::option::Option<usize>`
17+
//~| expected type `Option<usize>`
1818
//~| found type `{integer}`
1919
//~| expected enum `std::option::Option`, found integral variable
2020
}

src/test/compile-fail/fully-qualified-type-name4.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use std::option::Option;
1515
fn bar(x: usize) -> Option<usize> {
1616
return x;
1717
//~^ ERROR mismatched types
18-
//~| expected type `std::option::Option<usize>`
18+
//~| expected type `Option<usize>`
1919
//~| found type `usize`
2020
//~| expected enum `std::option::Option`, found usize
2121
}

src/test/compile-fail/generic-type-params-name-repr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ fn main() {
3636
// Including cases where the default is using previous type params.
3737
let _: HashMap<String, isize> = ();
3838
//~^ ERROR mismatched types
39-
//~| expected type `HashMap<std::string::String, isize>`
39+
//~| expected type `HashMap<String, isize>`
4040
//~| found type `()`
4141
//~| expected struct `HashMap`, found ()
4242
let _: HashMap<String, isize, Hash<String>> = ();
4343
//~^ ERROR mismatched types
44-
//~| expected type `HashMap<std::string::String, isize>`
44+
//~| expected type `HashMap<String, isize>`
4545
//~| found type `()`
4646
//~| expected struct `HashMap`, found ()
4747

src/test/compile-fail/issue-13466.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@ pub fn main() {
1717
let _x: usize = match Some(1) {
1818
Ok(u) => u,
1919
//~^ ERROR mismatched types
20-
//~| expected type `std::option::Option<{integer}>`
21-
//~| found type `std::result::Result<_, _>`
20+
//~| expected type `Option<{integer}>`
21+
//~| found type `Result<_, _>`
2222
//~| expected enum `std::option::Option`, found enum `std::result::Result`
2323

2424
Err(e) => panic!(e)
2525
//~^ ERROR mismatched types
26-
//~| expected type `std::option::Option<{integer}>`
27-
//~| found type `std::result::Result<_, _>`
26+
//~| expected type `Option<{integer}>`
27+
//~| found type `Result<_, _>`
2828
//~| expected enum `std::option::Option`, found enum `std::result::Result`
2929
};
3030
}

src/test/compile-fail/issue-15783.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ fn main() {
1717
let x = Some(&[name]);
1818
let msg = foo(x);
1919
//~^ ERROR mismatched types
20-
//~| expected type `std::option::Option<&[&str]>`
21-
//~| found type `std::option::Option<&[&str; 1]>`
20+
//~| expected type `Option<&[&str]>`
21+
//~| found type `Option<&[&str; 1]>`
2222
//~| expected slice, found array of 1 elements
2323
assert_eq!(msg, 3);
2424
}

src/test/compile-fail/issue-3680.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn main() {
1212
match None {
1313
Err(_) => ()
1414
//~^ ERROR mismatched types
15-
//~| expected type `std::option::Option<_>`
16-
//~| found type `std::result::Result<_, _>`
15+
//~| expected type `Option<_>`
16+
//~| found type `Result<_, _>`
1717
//~| expected enum `std::option::Option`, found enum `std::result::Result`
1818
}
1919
}

src/test/compile-fail/issue-40749.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ fn main() {
1212
[0; ..10];
1313
//~^ ERROR mismatched types
1414
//~| expected type `usize`
15-
//~| found type `std::ops::RangeTo<{integer}>`
15+
//~| found type `RangeTo<{integer}>`
1616
}

src/test/compile-fail/issue-5100.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ fn main() {
4343
box (true, false) => ()
4444
//~^ ERROR mismatched types
4545
//~| expected type `(bool, bool)`
46-
//~| found type `std::boxed::Box<_>`
46+
//~| found type `Box<_>`
4747
}
4848

4949
match (true, false) {

src/test/compile-fail/issue-7061.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct BarStruct;
1313
impl<'a> BarStruct {
1414
fn foo(&'a mut self) -> Box<BarStruct> { self }
1515
//~^ ERROR mismatched types
16-
//~| expected type `std::boxed::Box<BarStruct>`
16+
//~| expected type `Box<BarStruct>`
1717
//~| found type `&'a mut BarStruct`
1818
}
1919

src/test/compile-fail/issue-7092.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ fn foo(x: Whatever) {
1616
Some(field) =>
1717
//~^ ERROR mismatched types
1818
//~| expected type `Whatever`
19-
//~| found type `std::option::Option<_>`
19+
//~| found type `Option<_>`
2020
//~| expected enum `Whatever`, found enum `std::option::Option`
2121
field.access(),
2222
}

src/test/compile-fail/issue-7867.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ fn main() {
2525
match &Some(42) {
2626
Some(x) => (),
2727
//~^ ERROR mismatched types
28-
//~| expected type `&std::option::Option<{integer}>`
29-
//~| found type `std::option::Option<_>`
28+
//~| expected type `&Option<{integer}>`
29+
//~| found type `Option<_>`
3030
//~| expected reference, found enum `std::option::Option`
3131
None => ()
3232
//~^ ERROR mismatched types
33-
//~| expected type `&std::option::Option<{integer}>`
34-
//~| found type `std::option::Option<_>`
33+
//~| expected type `&Option<{integer}>`
34+
//~| found type `Option<_>`
3535
//~| expected reference, found enum `std::option::Option`
3636
}
3737
}

src/test/compile-fail/noexporttypeexe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ fn main() {
2020
let x: isize = noexporttypelib::foo();
2121
//~^ ERROR mismatched types
2222
//~| expected type `isize`
23-
//~| found type `std::option::Option<isize>`
23+
//~| found type `Option<isize>`
2424
//~| expected isize, found enum `std::option::Option`
2525
}

src/test/compile-fail/occurs-check-2.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ fn main() {
1717
f = box g;
1818
//~^ ERROR mismatched types
1919
//~| expected type `_`
20-
//~| found type `std::boxed::Box<_>`
20+
//~| found type `Box<_>`
2121
//~| cyclic type of infinite size
2222
}

src/test/compile-fail/occurs-check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ fn main() {
1515
f = box f;
1616
//~^ ERROR mismatched types
1717
//~| expected type `_`
18-
//~| found type `std::boxed::Box<_>`
18+
//~| found type `Box<_>`
1919
//~| cyclic type of infinite size
2020
}

src/test/compile-fail/regions-infer-paramd-indirect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ impl<'a> set_f<'a> for c<'a> {
3232
fn set_f_bad(&mut self, b: Box<b>) {
3333
self.f = b;
3434
//~^ ERROR mismatched types
35-
//~| expected type `std::boxed::Box<std::boxed::Box<&'a isize>>`
36-
//~| found type `std::boxed::Box<std::boxed::Box<&isize>>`
35+
//~| expected type `Box<Box<&'a isize>>`
36+
//~| found type `Box<Box<&isize>>`
3737
//~| lifetime mismatch
3838
}
3939
}

src/test/compile-fail/tag-that-dare-not-speak-its-name.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ fn main() {
2222
let x : char = last(y);
2323
//~^ ERROR mismatched types
2424
//~| expected type `char`
25-
//~| found type `std::option::Option<_>`
25+
//~| found type `Option<_>`
2626
//~| expected char, found enum `std::option::Option`
2727
}

src/test/compile-fail/terr-sorts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ fn want_foo(f: foo) {}
2020
fn have_bar(b: bar) {
2121
want_foo(b); //~ ERROR mismatched types
2222
//~| expected type `foo`
23-
//~| found type `std::boxed::Box<foo>`
23+
//~| found type `Box<foo>`
2424
}
2525

2626
fn main() {}

src/test/compile-fail/type-mismatch-same-crate-name.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn main() {
3333
//~^ ERROR mismatched types
3434
//~| Perhaps two different versions of crate `crate_a1`
3535
//~| expected trait `main::a::Bar`
36-
//~| expected type `std::boxed::Box<main::a::Bar + 'static>`
37-
//~| found type `std::boxed::Box<main::a::Bar>`
36+
//~| expected type `Box<main::a::Bar + 'static>`
37+
//~| found type `Box<main::a::Bar>`
3838
}
3939
}

src/test/ui/block-result/consider-removing-last-semi.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ error[E0308]: mismatched types
99
14 | | }
1010
| |_^ expected struct `std::string::String`, found ()
1111
|
12-
= note: expected type `std::string::String`
12+
= note: expected type `String`
1313
found type `()`
1414

1515
error[E0308]: mismatched types
@@ -23,7 +23,7 @@ error[E0308]: mismatched types
2323
19 | | }
2424
| |_^ expected struct `std::string::String`, found ()
2525
|
26-
= note: expected type `std::string::String`
26+
= note: expected type `String`
2727
found type `()`
2828

2929
error: aborting due to 2 previous errors

src/test/ui/block-result/issue-13428.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ error[E0308]: mismatched types
1212
19 | | }
1313
| |_^ expected struct `std::string::String`, found ()
1414
|
15-
= note: expected type `std::string::String`
15+
= note: expected type `String`
1616
found type `()`
1717

1818
error[E0308]: mismatched types
@@ -26,7 +26,7 @@ error[E0308]: mismatched types
2626
24 | | }
2727
| |_^ expected struct `std::string::String`, found ()
2828
|
29-
= note: expected type `std::string::String`
29+
= note: expected type `String`
3030
found type `()`
3131

3232
error: aborting due to 2 previous errors

src/test/ui/coercion-missing-tail-expected-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ error[E0308]: mismatched types
2121
19 | | }
2222
| |_^ expected enum `std::result::Result`, found ()
2323
|
24-
= note: expected type `std::result::Result<u8, u64>`
24+
= note: expected type `Result<u8, u64>`
2525
found type `()`
2626

2727
error: aborting due to 2 previous errors

src/test/ui/lifetime-errors/ex2d-push-inference-variable-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ note: but, the lifetime must be valid for the lifetime 'b as defined on the func
2727
18 | | a.push(b);
2828
19 | | }
2929
| |_^
30-
note: ...so that expression is assignable (expected &mut std::vec::Vec<Ref<'_, i32>>, found &mut std::vec::Vec<Ref<'b, i32>>)
30+
note: ...so that expression is assignable (expected &mut Vec<Ref<'_, i32>>, found &mut Vec<Ref<'b, i32>>)
3131
--> $DIR/ex2d-push-inference-variable-2.rs:16:33
3232
|
3333
16 | let a: &mut Vec<Ref<i32>> = x;

src/test/ui/lifetime-errors/ex2e-push-inference-variable-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ note: but, the lifetime must be valid for the lifetime 'b as defined on the func
2727
18 | | Vec::push(a, b);
2828
19 | | }
2929
| |_^
30-
note: ...so that expression is assignable (expected &mut std::vec::Vec<Ref<'_, i32>>, found &mut std::vec::Vec<Ref<'b, i32>>)
30+
note: ...so that expression is assignable (expected &mut Vec<Ref<'_, i32>>, found &mut Vec<Ref<'b, i32>>)
3131
--> $DIR/ex2e-push-inference-variable-3.rs:16:33
3232
|
3333
16 | let a: &mut Vec<Ref<i32>> = x;

src/test/ui/local-ident.rs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
use Mod1::S;
12+
use Mod2::*;
13+
14+
fn main() {
15+
let x: X = S;
16+
let y: Option<usize> = Ok(2);
17+
}
18+
19+
mod Mod1 {
20+
pub struct S;
21+
}
22+
23+
mod Mod2 {
24+
pub struct X;
25+
}

src/test/ui/local-ident.stderr

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/local-ident.rs:15:16
3+
|
4+
15 | let x: X = S;
5+
| ^ expected struct `Mod2::X`, found struct `Mod1::S`
6+
|
7+
= note: expected type `X`
8+
found type `S`
9+
10+
error[E0308]: mismatched types
11+
--> $DIR/local-ident.rs:16:28
12+
|
13+
16 | let y: Option<usize> = Ok(2);
14+
| ^^^^^ expected enum `std::option::Option`, found enum `std::result::Result`
15+
|
16+
= note: expected type `Option<usize>`
17+
found type `Result<{integer}, _>`
18+
= help: here are some functions which might fulfill your needs:
19+
- .err()
20+
- .ok()
21+
- .unwrap_err()
22+
23+
error: aborting due to 2 previous errors
24+

0 commit comments

Comments
 (0)