Rollup merge of #35611 - jonathandturner:ptr-helper, r=nikomatsakis

Improve &-ptr printing

This PR replaces printing `&-ptr` with a more readable description.  To do so it uses a few heuristics.

If the name of the type is unknown, too long (longer than just saying "reference"), or too complex (a type with explicit lifetime annotations), it will instead opt to print either "reference" or "mutable reference", depending on the mutability of the type.

Before:

```
error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found &-ptr
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error
```

After:

```
error[E0308]: mismatched types
  --> src/test/compile-fail/issue-7061.rs:14:46
   |
14 |     fn foo(&'a mut self) -> Box<BarStruct> { self }
   |                                              ^^^^ expected box, found mutable reference
   |
   = note: expected type `Box<BarStruct>`
   = note:    found type `&'a mut BarStruct`

error: aborting due to previous error
```
This commit is contained in:
Eduard-Mihai Burtescu 2016-08-14 20:29:51 +03:00 committed by GitHub
commit 80510b800a
17 changed files with 39 additions and 22 deletions

View File

@ -222,7 +222,24 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
ty::TyArray(_, n) => format!("array of {} elements", n),
ty::TySlice(_) => "slice".to_string(),
ty::TyRawPtr(_) => "*-ptr".to_string(),
ty::TyRef(_, _) => "&-ptr".to_string(),
ty::TyRef(region, tymut) => {
let tymut_string = tymut.to_string();
if tymut_string == "_" || //unknown type name,
tymut_string.len() > 10 || //name longer than saying "reference",
region.to_string() != "" //... or a complex type
{
match tymut {
ty::TypeAndMut{mutbl, ..} => {
format!("{}reference", match mutbl {
hir::Mutability::MutMutable => "mutable ",
_ => ""
})
}
}
} else {
format!("&{}", tymut_string)
}
}
ty::TyFnDef(..) => format!("fn item"),
ty::TyFnPtr(_) => "fn pointer".to_string(),
ty::TyTrait(ref inner) => {

View File

@ -15,5 +15,5 @@ fn main() {
//~^ ERROR mismatched types
//~| expected type `&[i32]`
//~| found type `[{integer}; 1]`
//~| expected &-ptr, found array of 1 elements
//~| expected &[i32], found array of 1 elements
}

View File

@ -21,5 +21,5 @@ pub fn main() {
let _y: &Trait = x; //~ ERROR mismatched types
//~| expected type `&Trait`
//~| found type `Box<Trait>`
//~| expected &-ptr, found box
//~| expected &Trait, found box
}

View File

@ -42,12 +42,12 @@ fn main() {
//~^ ERROR mismatched types
//~| expected type `T`
//~| found type `&_`
//~| expected trait T, found &-ptr
//~| expected trait T, found reference
let &&&x = &(&1isize as &T);
//~^ ERROR mismatched types
//~| expected type `T`
//~| found type `&_`
//~| expected trait T, found &-ptr
//~| expected trait T, found reference
let box box x = box 1isize as Box<T>;
//~^ ERROR mismatched types
//~| expected type `T`

View File

@ -19,12 +19,12 @@ struct Foo<T: ?Sized> {
}
pub fn main() {
// Test that we cannot convert from *-ptr to &-ptr
// Test that we cannot convert from *-ptr to &S and &T
let x: *const S = &S;
let y: &S = x; //~ ERROR mismatched types
let y: &T = x; //~ ERROR mismatched types
// Test that we cannot convert from *-ptr to &-ptr (mut version)
// Test that we cannot convert from *-ptr to &S and &T (mut version)
let x: *mut S = &mut S;
let y: &S = x; //~ ERROR mismatched types
let y: &T = x; //~ ERROR mismatched types

View File

@ -17,4 +17,4 @@ fn bar(x: isize) { }
//~^ ERROR mismatched types
//~| expected type `fn(&mut __test::test::Bencher)`
//~| found type `fn(isize) {bar}`
//~| expected &-ptr, found isize
//~| expected mutable reference, found isize

View File

@ -18,5 +18,5 @@ fn main() {
//~^ ERROR mismatched types
//~| expected type `&str`
//~| found type `Slice<_>`
//~| expected &-ptr, found struct `Slice`
//~| expected &str, found struct `Slice`
}

View File

@ -12,7 +12,7 @@ fn f<'r>(p: &'r mut fn(p: &mut ())) {
(*p)(()) //~ ERROR mismatched types
//~| expected type `&mut ()`
//~| found type `()`
//~| expected &-ptr, found ()
//~| expected &mut (), found ()
}
fn main() {}

View File

@ -15,13 +15,13 @@ struct Foo;
impl<'a, T> Fn<(&'a T,)> for Foo {
extern "rust-call" fn call(&self, (_,): (T,)) {}
//~^ ERROR: has an incompatible type for trait
//~| expected &-ptr
//~| expected reference
}
impl<'a, T> FnMut<(&'a T,)> for Foo {
extern "rust-call" fn call_mut(&mut self, (_,): (T,)) {}
//~^ ERROR: has an incompatible type for trait
//~| expected &-ptr
//~| expected reference
}
impl<'a, T> FnOnce<(&'a T,)> for Foo {
@ -29,7 +29,7 @@ impl<'a, T> FnOnce<(&'a T,)> for Foo {
extern "rust-call" fn call_once(self, (_,): (T,)) {}
//~^ ERROR: has an incompatible type for trait
//~| expected &-ptr
//~| expected reference
}
fn main() {}

View File

@ -13,7 +13,7 @@ macro_rules! foo {
fn bar(d: u8) { }
bar(&mut $d);
//~^ ERROR mismatched types
//~| expected u8, found &-ptr
//~| expected u8, found &mut u8
//~| expected type `u8`
//~| found type `&mut u8`
}}

View File

@ -52,7 +52,7 @@ fn main() {
//~^ ERROR mismatched types
//~| expected type `(bool, bool)`
//~| found type `&_`
//~| expected tuple, found &-ptr
//~| expected tuple, found reference
}

View File

@ -13,5 +13,5 @@ fn main() {
//~^ ERROR mismatched types
//~| expected type `()`
//~| found type `&_`
//~| expected (), found &-ptr
//~| expected (), found reference
}

View File

@ -15,7 +15,7 @@ impl<'a> BarStruct {
//~^ ERROR mismatched types
//~| expected type `Box<BarStruct>`
//~| found type `&'a mut BarStruct`
//~| expected box, found &-ptr
//~| expected box, found mutable reference
}
fn main() {}

View File

@ -27,11 +27,11 @@ fn main() {
//~^ ERROR mismatched types
//~| expected type `&std::option::Option<{integer}>`
//~| found type `std::option::Option<_>`
//~| expected &-ptr, found enum `std::option::Option`
//~| expected reference, found enum `std::option::Option`
None => ()
//~^ ERROR mismatched types
//~| expected type `&std::option::Option<{integer}>`
//~| found type `std::option::Option<_>`
//~| expected &-ptr, found enum `std::option::Option`
//~| expected reference, found enum `std::option::Option`
}
}

View File

@ -21,7 +21,7 @@ fn main() {
Foo::bar(x); //~ ERROR mismatched types
//~| expected type `&Foo`
//~| found type `Foo`
//~| expected &-ptr, found struct `Foo`
//~| expected &Foo, found struct `Foo`
Foo::bar(&42); //~ ERROR mismatched types
//~| expected type `&Foo`
//~| found type `&{integer}`

View File

@ -36,7 +36,7 @@ fn main() {
y: 3,
};
let ans = s("what"); //~ ERROR mismatched types
//~^ NOTE expected isize, found &-ptr
//~^ NOTE expected isize, found reference
//~| NOTE expected type
//~| NOTE found type
let ans = s();

View File

@ -38,7 +38,7 @@ fn main() {
//~^ ERROR mismatched types
//~| expected type `usize`
//~| found type `&'static str`
//~| expected usize, found &-ptr
//~| expected usize, found reference
//~| ERROR expected `usize` for repeat count, found string literal [E0306]
//~| expected `usize`
let f = [0; -4_isize];