]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #35611 - jonathandturner:ptr-helper, r=nikomatsakis
authorEduard-Mihai Burtescu <edy.burt@gmail.com>
Sun, 14 Aug 2016 17:29:51 +0000 (20:29 +0300)
committerGitHub <noreply@github.com>
Sun, 14 Aug 2016 17:29:51 +0000 (20:29 +0300)
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
```

17 files changed:
src/librustc/ty/error.rs
src/test/compile-fail/coercion-slice.rs
src/test/compile-fail/cross-borrow-trait.rs
src/test/compile-fail/destructure-trait-ref.rs
src/test/compile-fail/dst-bad-coercions.rs
src/test/compile-fail/issue-12997-2.rs
src/test/compile-fail/issue-16338.rs
src/test/compile-fail/issue-17033.rs
src/test/compile-fail/issue-20225.rs
src/test/compile-fail/issue-29084.rs
src/test/compile-fail/issue-5100.rs
src/test/compile-fail/issue-5500.rs
src/test/compile-fail/issue-7061.rs
src/test/compile-fail/issue-7867.rs
src/test/compile-fail/method-self-arg-1.rs
src/test/compile-fail/overloaded-calls-bad.rs
src/test/compile-fail/repeat_count.rs

index 66165ec6ff7d06aa179c4c7c5279aa4daadd51ae..6b34c0a21988dd81ecf6246b1aeee854cab71888 100644 (file)
@@ -222,7 +222,24 @@ fn sort_string(&self, tcx: TyCtxt<'a, 'gcx, 'lcx>) -> String {
             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) => {
index a619f33468f4a94e068fe70d6a8aa26da03c6312..6b468ff96620d25bf0b667d9823d4ba70b5dc964 100644 (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
 }
index ea9a29c0e2ae5e9aaf1080fc10427e9e37a7dce6..672ff464718f8b2a499f8bf8b8b433e92151ff66 100644 (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
 }
index d0a31fbce91ed38475c42f7a1b1c23c20eaafac3..89fb1e105900d7deac59d2a03ff33f263b31f8ab 100644 (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`
index b7a07e487994d41cce8463c2929ce00298bf7ced..883c16b0895816076c8326de14e232ea9154ca74 100644 (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
index 436d9e91dc72f2ff4935e486780a05ab47e0feae..276d7f7c9ed339c7646ecf2d1c8e17db16e74b3b 100644 (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
index c6ce0c4c95b8bcbf97a3e0024e391ac4b9e34ccf..a4517e60d66e195bb61dd57492d5437da9b32227 100644 (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`
 }
index 0ec05b941a960dc1b7dc40bb9710272762de9e5c..1cd43cbb0f8571b250c25c31e6c7b2d0a9a75730 100644 (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() {}
index b7845f1f1168af8a9353f819903b5fe03ab59e3c..f38961c427ae9c686cb0678722bb177b157f212d 100644 (file)
 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() {}
index 00d2969a0f67d153ed10b9b1ef60995eea921019..6cb6bbf1893fc15b8638c09ab3c6f56011c3d22a 100644 (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`
     }}
index 9e78b7b947f98e9c3e6681074e8a71556dcc8eda..a1f5d74b30e3677608892c4d9457555aec1a3bbd 100644 (file)
@@ -52,7 +52,7 @@ fn main() {
 //~^ ERROR mismatched types
 //~| expected type `(bool, bool)`
 //~| found type `&_`
-//~| expected tuple, found &-ptr
+//~| expected tuple, found reference
     }
 
 
index cacbf7656def2f4bf5d27cfbd558280663889c9f..1cbb7588e17df5e5fdddb3d8f0ff54f5e2608bbf 100644 (file)
@@ -13,5 +13,5 @@ fn main() {
     //~^ ERROR mismatched types
     //~| expected type `()`
     //~| found type `&_`
-    //~| expected (), found &-ptr
+    //~| expected (), found reference
 }
index 1519d71dd3be26e92a1110bd6b183eb9e3dd200a..da6f49f3efe911b73895512be3ec9f69cf2ae590 100644 (file)
@@ -15,7 +15,7 @@ fn foo(&'a mut self) -> Box<BarStruct> { self }
     //~^ ERROR mismatched types
     //~| expected type `Box<BarStruct>`
     //~| found type `&'a mut BarStruct`
-    //~| expected box, found &-ptr
+    //~| expected box, found mutable reference
 }
 
 fn main() {}
index ed465117344d407e682d5deabf14f9c70d9999ea..7d9f8e90585219d750a0f18dc68a9af06b84750a 100644 (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`
     }
 }
index 03816362d46c34860aef664661f0898315338108..4c8800878f07df4787096e35d8d1d1429614bc9e 100644 (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}`
index 5865d93e1282ffdbe28e397fa7bf4e93f8438996..1825ec61f1ed7d84a1edf0dfdbd065416fbcdf14 100644 (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();
index 555dd0f0c3945fd52c15de1690550b973f2cb1e7..5d5113ce07c719f66c2cf622d259cb7498f3e845 100644 (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];