Skip to content

Commit

Permalink
Rollup merge of rust-lang#35611 - jonathandturner:ptr-helper, r=nikom…
Browse files Browse the repository at this point in the history
…atsakis

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
```
  • Loading branch information
eddyb committed Aug 14, 2016
2 parents 7aaa83a + 4224737 commit 80510b8
Show file tree
Hide file tree
Showing 17 changed files with 39 additions and 22 deletions.
19 changes: 18 additions & 1 deletion src/librustc/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/coercion-slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
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 @@ -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
}
4 changes: 2 additions & 2 deletions src/test/compile-fail/destructure-trait-ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
4 changes: 2 additions & 2 deletions src/test/compile-fail/dst-bad-coercions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-12997-2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-16338.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-17033.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}
6 changes: 3 additions & 3 deletions src/test/compile-fail/issue-20225.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ 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 {
type Output = ();

extern "rust-call" fn call_once(self, (_,): (T,)) {}
//~^ ERROR: has an incompatible type for trait
//~| expected &-ptr
//~| expected reference
}

fn main() {}
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-29084.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}}
Expand Down
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 @@ -52,7 +52,7 @@ fn main() {
//~^ ERROR mismatched types
//~| expected type `(bool, bool)`
//~| found type `&_`
//~| expected tuple, found &-ptr
//~| expected tuple, found reference
}


Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/issue-5500.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ fn main() {
//~^ ERROR mismatched types
//~| expected type `()`
//~| found type `&_`
//~| expected (), found &-ptr
//~| expected (), found reference
}
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 @@ -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() {}
4 changes: 2 additions & 2 deletions src/test/compile-fail/issue-7867.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
}
2 changes: 1 addition & 1 deletion src/test/compile-fail/method-self-arg-1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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}`
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/overloaded-calls-bad.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/repeat_count.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit 80510b8

Please sign in to comment.