Skip to content

Commit

Permalink
Refer to types using the local identifier
Browse files Browse the repository at this point in the history
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()
```
  • Loading branch information
estebank committed Sep 17, 2017
1 parent 2efc1f8 commit 6877688
Show file tree
Hide file tree
Showing 40 changed files with 374 additions and 103 deletions.
281 changes: 240 additions & 41 deletions src/librustc/infer/error_reporting/mod.rs

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/librustc/middle/const_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ impl<'tcx> ConstVal<'tcx> {
_ => None
}
}

pub fn to_usize(&self) -> Option<usize> {
self.to_const_int().and_then(|i| i.to_usize())
}
}

#[derive(Clone, Debug)]
Expand Down
4 changes: 4 additions & 0 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1179,6 +1179,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.all_crate_nums(LOCAL_CRATE)
}

pub fn cstore(&self) -> &CrateStore {
self.cstore
}

pub fn def_key(self, id: DefId) -> hir_map::DefKey {
if id.is_local() {
self.hir.def_key(id)
Expand Down
6 changes: 6 additions & 0 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1469,4 +1469,10 @@ pub struct Const<'tcx> {
pub val: ConstVal<'tcx>,
}

impl<'tcx> Const<'tcx> {
pub fn usize_val(&self) -> usize {
self.val.to_usize().unwrap_or(0)
}
}

impl<'tcx> serialize::UseSpecializedDecodable for &'tcx Const<'tcx> {}
9 changes: 9 additions & 0 deletions src/librustc_const_math/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ impl ConstInt {
})
}

/// Converts the value to a `usize` if it's in the range 0...std::usize::MAX
pub fn to_usize(&self) -> Option<usize> {
self.to_u128().and_then(|v| if v <= usize::max_value() as u128 {
Some(v as usize)
} else {
None
})
}

/// Converts the value to a `u128` if it's in the range 0...std::u128::MAX
pub fn to_u128(&self) -> Option<u128> {
match *self {
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/bad-const-type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

static i: String = 10;
//~^ ERROR mismatched types
//~| expected type `std::string::String`
//~| expected type `String`
//~| found type `{integer}`
//~| expected struct `std::string::String`, found integral variable
fn main() { println!("{}", i); }
2 changes: 1 addition & 1 deletion src/test/compile-fail/cross-borrow-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ pub fn main() {
let x: Box<Trait> = Box::new(Foo);
let _y: &Trait = x; //~ ERROR E0308
//~| expected type `&Trait`
//~| found type `std::boxed::Box<Trait>`
//~| found type `Box<Trait>`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/destructure-trait-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ fn main() {
let box box x = box 1isize as Box<T>;
//~^ ERROR mismatched types
//~| expected type `T`
//~| found type `std::boxed::Box<_>`
//~| found type `Box<_>`
}
6 changes: 3 additions & 3 deletions src/test/compile-fail/fn-trait-formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ fn main() {
let _: () = (box |_: isize| {}) as Box<FnOnce(isize)>;
//~^ ERROR mismatched types
//~| expected type `()`
//~| found type `std::boxed::Box<std::ops::FnOnce(isize)>`
//~| found type `Box<std::ops::FnOnce(isize)>`
let _: () = (box |_: isize, isize| {}) as Box<Fn(isize, isize)>;
//~^ ERROR mismatched types
//~| expected type `()`
//~| found type `std::boxed::Box<std::ops::Fn(isize, isize)>`
//~| found type `Box<std::ops::Fn(isize, isize)>`
let _: () = (box || -> isize { unimplemented!() }) as Box<FnMut() -> isize>;
//~^ ERROR mismatched types
//~| expected type `()`
//~| found type `std::boxed::Box<std::ops::FnMut() -> isize>`
//~| found type `Box<std::ops::FnMut() -> isize>`

needs_fn(1);
//~^ ERROR : std::ops::Fn<(isize,)>`
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/fully-qualified-type-name1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() {
let x: Option<usize>;
x = 5;
//~^ ERROR mismatched types
//~| expected type `std::option::Option<usize>`
//~| expected type `Option<usize>`
//~| found type `{integer}`
//~| expected enum `std::option::Option`, found integral variable
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/fully-qualified-type-name4.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::option::Option;
fn bar(x: usize) -> Option<usize> {
return x;
//~^ ERROR mismatched types
//~| expected type `std::option::Option<usize>`
//~| expected type `Option<usize>`
//~| found type `usize`
//~| expected enum `std::option::Option`, found usize
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/generic-type-params-name-repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ fn main() {
// Including cases where the default is using previous type params.
let _: HashMap<String, isize> = ();
//~^ ERROR mismatched types
//~| expected type `HashMap<std::string::String, isize>`
//~| expected type `HashMap<String, isize>`
//~| found type `()`
//~| expected struct `HashMap`, found ()
let _: HashMap<String, isize, Hash<String>> = ();
//~^ ERROR mismatched types
//~| expected type `HashMap<std::string::String, isize>`
//~| expected type `HashMap<String, isize>`
//~| found type `()`
//~| expected struct `HashMap`, found ()

Expand Down
8 changes: 4 additions & 4 deletions src/test/compile-fail/issue-13466.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ pub fn main() {
let _x: usize = match Some(1) {
Ok(u) => u,
//~^ ERROR mismatched types
//~| expected type `std::option::Option<{integer}>`
//~| found type `std::result::Result<_, _>`
//~| expected type `Option<{integer}>`
//~| found type `Result<_, _>`
//~| expected enum `std::option::Option`, found enum `std::result::Result`

Err(e) => panic!(e)
//~^ ERROR mismatched types
//~| expected type `std::option::Option<{integer}>`
//~| found type `std::result::Result<_, _>`
//~| expected type `Option<{integer}>`
//~| found type `Result<_, _>`
//~| expected enum `std::option::Option`, found enum `std::result::Result`
};
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-15783.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ fn main() {
let x = Some(&[name]);
let msg = foo(x);
//~^ ERROR mismatched types
//~| expected type `std::option::Option<&[&str]>`
//~| found type `std::option::Option<&[&str; 1]>`
//~| expected type `Option<&[&str]>`
//~| found type `Option<&[&str; 1]>`
//~| expected slice, found array of 1 elements
assert_eq!(msg, 3);
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-3680.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ fn main() {
match None {
Err(_) => ()
//~^ ERROR mismatched types
//~| expected type `std::option::Option<_>`
//~| found type `std::result::Result<_, _>`
//~| expected type `Option<_>`
//~| found type `Result<_, _>`
//~| expected enum `std::option::Option`, found enum `std::result::Result`
}
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-40749.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ fn main() {
[0; ..10];
//~^ ERROR mismatched types
//~| expected type `usize`
//~| found type `std::ops::RangeTo<{integer}>`
//~| found type `RangeTo<{integer}>`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-5100.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ fn main() {
box (true, false) => ()
//~^ ERROR mismatched types
//~| expected type `(bool, bool)`
//~| found type `std::boxed::Box<_>`
//~| found type `Box<_>`
}

match (true, false) {
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-7061.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ struct BarStruct;
impl<'a> BarStruct {
fn foo(&'a mut self) -> Box<BarStruct> { self }
//~^ ERROR mismatched types
//~| expected type `std::boxed::Box<BarStruct>`
//~| expected type `Box<BarStruct>`
//~| found type `&'a mut BarStruct`
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-7092.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn foo(x: Whatever) {
Some(field) =>
//~^ ERROR mismatched types
//~| expected type `Whatever`
//~| found type `std::option::Option<_>`
//~| found type `Option<_>`
//~| expected enum `Whatever`, found enum `std::option::Option`
field.access(),
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/compile-fail/issue-7867.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ fn main() {
match &Some(42) {
Some(x) => (),
//~^ ERROR mismatched types
//~| expected type `&std::option::Option<{integer}>`
//~| found type `std::option::Option<_>`
//~| expected type `&Option<{integer}>`
//~| found type `Option<_>`
//~| expected reference, found enum `std::option::Option`
None => ()
//~^ ERROR mismatched types
//~| expected type `&std::option::Option<{integer}>`
//~| found type `std::option::Option<_>`
//~| expected type `&Option<{integer}>`
//~| found type `Option<_>`
//~| expected reference, found enum `std::option::Option`
}
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/noexporttypeexe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ fn main() {
let x: isize = noexporttypelib::foo();
//~^ ERROR mismatched types
//~| expected type `isize`
//~| found type `std::option::Option<isize>`
//~| found type `Option<isize>`
//~| expected isize, found enum `std::option::Option`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/occurs-check-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ fn main() {
f = box g;
//~^ ERROR mismatched types
//~| expected type `_`
//~| found type `std::boxed::Box<_>`
//~| found type `Box<_>`
//~| cyclic type of infinite size
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/occurs-check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ fn main() {
f = box f;
//~^ ERROR mismatched types
//~| expected type `_`
//~| found type `std::boxed::Box<_>`
//~| found type `Box<_>`
//~| cyclic type of infinite size
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/regions-infer-paramd-indirect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ impl<'a> set_f<'a> for c<'a> {
fn set_f_bad(&mut self, b: Box<b>) {
self.f = b;
//~^ ERROR mismatched types
//~| expected type `std::boxed::Box<std::boxed::Box<&'a isize>>`
//~| found type `std::boxed::Box<std::boxed::Box<&isize>>`
//~| expected type `Box<Box<&'a isize>>`
//~| found type `Box<Box<&isize>>`
//~| lifetime mismatch
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/tag-that-dare-not-speak-its-name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ fn main() {
let x : char = last(y);
//~^ ERROR mismatched types
//~| expected type `char`
//~| found type `std::option::Option<_>`
//~| found type `Option<_>`
//~| expected char, found enum `std::option::Option`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/terr-sorts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn want_foo(f: foo) {}
fn have_bar(b: bar) {
want_foo(b); //~ ERROR mismatched types
//~| expected type `foo`
//~| found type `std::boxed::Box<foo>`
//~| found type `Box<foo>`
}

fn main() {}
4 changes: 2 additions & 2 deletions src/test/compile-fail/type-mismatch-same-crate-name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fn main() {
//~^ ERROR mismatched types
//~| Perhaps two different versions of crate `crate_a1`
//~| expected trait `main::a::Bar`
//~| expected type `std::boxed::Box<main::a::Bar + 'static>`
//~| found type `std::boxed::Box<main::a::Bar>`
//~| expected type `Box<main::a::Bar + 'static>`
//~| found type `Box<main::a::Bar>`
}
}
4 changes: 2 additions & 2 deletions src/test/ui/block-result/consider-removing-last-semi.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ error[E0308]: mismatched types
14 | | }
| |_^ expected struct `std::string::String`, found ()
|
= note: expected type `std::string::String`
= note: expected type `String`
found type `()`

error[E0308]: mismatched types
Expand All @@ -23,7 +23,7 @@ error[E0308]: mismatched types
19 | | }
| |_^ expected struct `std::string::String`, found ()
|
= note: expected type `std::string::String`
= note: expected type `String`
found type `()`

error: aborting due to 2 previous errors
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/block-result/issue-13428.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ error[E0308]: mismatched types
19 | | }
| |_^ expected struct `std::string::String`, found ()
|
= note: expected type `std::string::String`
= note: expected type `String`
found type `()`

error[E0308]: mismatched types
Expand All @@ -26,7 +26,7 @@ error[E0308]: mismatched types
24 | | }
| |_^ expected struct `std::string::String`, found ()
|
= note: expected type `std::string::String`
= note: expected type `String`
found type `()`

error: aborting due to 2 previous errors
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/coercion-missing-tail-expected-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ error[E0308]: mismatched types
19 | | }
| |_^ expected enum `std::result::Result`, found ()
|
= note: expected type `std::result::Result<u8, u64>`
= note: expected type `Result<u8, u64>`
found type `()`

error: aborting due to 2 previous errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ note: but, the lifetime must be valid for the lifetime 'b as defined on the func
18 | | a.push(b);
19 | | }
| |_^
note: ...so that expression is assignable (expected &mut std::vec::Vec<Ref<'_, i32>>, found &mut std::vec::Vec<Ref<'b, i32>>)
note: ...so that expression is assignable (expected &mut Vec<Ref<'_, i32>>, found &mut Vec<Ref<'b, i32>>)
--> $DIR/ex2d-push-inference-variable-2.rs:16:33
|
16 | let a: &mut Vec<Ref<i32>> = x;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ note: but, the lifetime must be valid for the lifetime 'b as defined on the func
18 | | Vec::push(a, b);
19 | | }
| |_^
note: ...so that expression is assignable (expected &mut std::vec::Vec<Ref<'_, i32>>, found &mut std::vec::Vec<Ref<'b, i32>>)
note: ...so that expression is assignable (expected &mut Vec<Ref<'_, i32>>, found &mut Vec<Ref<'b, i32>>)
--> $DIR/ex2e-push-inference-variable-3.rs:16:33
|
16 | let a: &mut Vec<Ref<i32>> = x;
Expand Down
25 changes: 25 additions & 0 deletions src/test/ui/local-ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.

use Mod1::S;
use Mod2::*;

fn main() {
let x: X = S;
let y: Option<usize> = Ok(2);
}

mod Mod1 {
pub struct S;
}

mod Mod2 {
pub struct X;
}
24 changes: 24 additions & 0 deletions src/test/ui/local-ident.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
error[E0308]: mismatched types
--> $DIR/local-ident.rs:15:16
|
15 | let x: X = S;
| ^ expected struct `Mod2::X`, found struct `Mod1::S`
|
= note: expected type `X`
found type `S`

error[E0308]: mismatched types
--> $DIR/local-ident.rs:16:28
|
16 | let y: Option<usize> = Ok(2);
| ^^^^^ expected enum `std::option::Option`, found enum `std::result::Result`
|
= note: expected type `Option<usize>`
found type `Result<{integer}, _>`
= help: here are some functions which might fulfill your needs:
- .err()
- .ok()
- .unwrap_err()

error: aborting due to 2 previous errors

Loading

0 comments on commit 6877688

Please sign in to comment.