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

Lines changed: 240 additions & 41 deletions
Large diffs are not rendered by default.

src/librustc/middle/const_val.rs

Lines changed: 4 additions & 0 deletions
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

Lines changed: 4 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 9 additions & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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
}

0 commit comments

Comments
 (0)