Skip to content

Commit d92f339

Browse files
authored
Rollup merge of rust-lang#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 ```
2 parents cef6e24 + 4224737 commit d92f339

17 files changed

+39
-22
lines changed

src/librustc/ty/error.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,24 @@ impl<'a, 'gcx, 'lcx, 'tcx> ty::TyS<'tcx> {
222222
ty::TyArray(_, n) => format!("array of {} elements", n),
223223
ty::TySlice(_) => "slice".to_string(),
224224
ty::TyRawPtr(_) => "*-ptr".to_string(),
225-
ty::TyRef(_, _) => "&-ptr".to_string(),
225+
ty::TyRef(region, tymut) => {
226+
let tymut_string = tymut.to_string();
227+
if tymut_string == "_" || //unknown type name,
228+
tymut_string.len() > 10 || //name longer than saying "reference",
229+
region.to_string() != "" //... or a complex type
230+
{
231+
match tymut {
232+
ty::TypeAndMut{mutbl, ..} => {
233+
format!("{}reference", match mutbl {
234+
hir::Mutability::MutMutable => "mutable ",
235+
_ => ""
236+
})
237+
}
238+
}
239+
} else {
240+
format!("&{}", tymut_string)
241+
}
242+
}
226243
ty::TyFnDef(..) => format!("fn item"),
227244
ty::TyFnPtr(_) => "fn pointer".to_string(),
228245
ty::TyTrait(ref inner) => {

src/test/compile-fail/coercion-slice.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ fn main() {
1515
//~^ ERROR mismatched types
1616
//~| expected type `&[i32]`
1717
//~| found type `[{integer}; 1]`
18-
//~| expected &-ptr, found array of 1 elements
18+
//~| expected &[i32], found array of 1 elements
1919
}

src/test/compile-fail/cross-borrow-trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ pub fn main() {
2121
let _y: &Trait = x; //~ ERROR mismatched types
2222
//~| expected type `&Trait`
2323
//~| found type `Box<Trait>`
24-
//~| expected &-ptr, found box
24+
//~| expected &Trait, found box
2525
}

src/test/compile-fail/destructure-trait-ref.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ fn main() {
4242
//~^ ERROR mismatched types
4343
//~| expected type `T`
4444
//~| found type `&_`
45-
//~| expected trait T, found &-ptr
45+
//~| expected trait T, found reference
4646
let &&&x = &(&1isize as &T);
4747
//~^ ERROR mismatched types
4848
//~| expected type `T`
4949
//~| found type `&_`
50-
//~| expected trait T, found &-ptr
50+
//~| expected trait T, found reference
5151
let box box x = box 1isize as Box<T>;
5252
//~^ ERROR mismatched types
5353
//~| expected type `T`

src/test/compile-fail/dst-bad-coercions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ struct Foo<T: ?Sized> {
1919
}
2020

2121
pub fn main() {
22-
// Test that we cannot convert from *-ptr to &-ptr
22+
// Test that we cannot convert from *-ptr to &S and &T
2323
let x: *const S = &S;
2424
let y: &S = x; //~ ERROR mismatched types
2525
let y: &T = x; //~ ERROR mismatched types
2626

27-
// Test that we cannot convert from *-ptr to &-ptr (mut version)
27+
// Test that we cannot convert from *-ptr to &S and &T (mut version)
2828
let x: *mut S = &mut S;
2929
let y: &S = x; //~ ERROR mismatched types
3030
let y: &T = x; //~ ERROR mismatched types

src/test/compile-fail/issue-12997-2.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ fn bar(x: isize) { }
1717
//~^ ERROR mismatched types
1818
//~| expected type `fn(&mut __test::test::Bencher)`
1919
//~| found type `fn(isize) {bar}`
20-
//~| expected &-ptr, found isize
20+
//~| expected mutable reference, found isize

src/test/compile-fail/issue-16338.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ fn main() {
1818
//~^ ERROR mismatched types
1919
//~| expected type `&str`
2020
//~| found type `Slice<_>`
21-
//~| expected &-ptr, found struct `Slice`
21+
//~| expected &str, found struct `Slice`
2222
}

src/test/compile-fail/issue-17033.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ fn f<'r>(p: &'r mut fn(p: &mut ())) {
1212
(*p)(()) //~ ERROR mismatched types
1313
//~| expected type `&mut ()`
1414
//~| found type `()`
15-
//~| expected &-ptr, found ()
15+
//~| expected &mut (), found ()
1616
}
1717

1818
fn main() {}

src/test/compile-fail/issue-20225.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@ struct Foo;
1515
impl<'a, T> Fn<(&'a T,)> for Foo {
1616
extern "rust-call" fn call(&self, (_,): (T,)) {}
1717
//~^ ERROR: has an incompatible type for trait
18-
//~| expected &-ptr
18+
//~| expected reference
1919
}
2020

2121
impl<'a, T> FnMut<(&'a T,)> for Foo {
2222
extern "rust-call" fn call_mut(&mut self, (_,): (T,)) {}
2323
//~^ ERROR: has an incompatible type for trait
24-
//~| expected &-ptr
24+
//~| expected reference
2525
}
2626

2727
impl<'a, T> FnOnce<(&'a T,)> for Foo {
2828
type Output = ();
2929

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

3535
fn main() {}

src/test/compile-fail/issue-29084.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ macro_rules! foo {
1313
fn bar(d: u8) { }
1414
bar(&mut $d);
1515
//~^ ERROR mismatched types
16-
//~| expected u8, found &-ptr
16+
//~| expected u8, found &mut u8
1717
//~| expected type `u8`
1818
//~| found type `&mut u8`
1919
}}

src/test/compile-fail/issue-5100.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fn main() {
5252
//~^ ERROR mismatched types
5353
//~| expected type `(bool, bool)`
5454
//~| found type `&_`
55-
//~| expected tuple, found &-ptr
55+
//~| expected tuple, found reference
5656
}
5757

5858

src/test/compile-fail/issue-5500.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ fn main() {
1313
//~^ ERROR mismatched types
1414
//~| expected type `()`
1515
//~| found type `&_`
16-
//~| expected (), found &-ptr
16+
//~| expected (), found reference
1717
}

src/test/compile-fail/issue-7061.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ impl<'a> BarStruct {
1515
//~^ ERROR mismatched types
1616
//~| expected type `Box<BarStruct>`
1717
//~| found type `&'a mut BarStruct`
18-
//~| expected box, found &-ptr
18+
//~| expected box, found mutable reference
1919
}
2020

2121
fn main() {}

src/test/compile-fail/issue-7867.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ fn main() {
2727
//~^ ERROR mismatched types
2828
//~| expected type `&std::option::Option<{integer}>`
2929
//~| found type `std::option::Option<_>`
30-
//~| expected &-ptr, found enum `std::option::Option`
30+
//~| expected reference, found enum `std::option::Option`
3131
None => ()
3232
//~^ ERROR mismatched types
3333
//~| expected type `&std::option::Option<{integer}>`
3434
//~| found type `std::option::Option<_>`
35-
//~| expected &-ptr, found enum `std::option::Option`
35+
//~| expected reference, found enum `std::option::Option`
3636
}
3737
}

src/test/compile-fail/method-self-arg-1.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ fn main() {
2121
Foo::bar(x); //~ ERROR mismatched types
2222
//~| expected type `&Foo`
2323
//~| found type `Foo`
24-
//~| expected &-ptr, found struct `Foo`
24+
//~| expected &Foo, found struct `Foo`
2525
Foo::bar(&42); //~ ERROR mismatched types
2626
//~| expected type `&Foo`
2727
//~| found type `&{integer}`

src/test/compile-fail/overloaded-calls-bad.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ fn main() {
3636
y: 3,
3737
};
3838
let ans = s("what"); //~ ERROR mismatched types
39-
//~^ NOTE expected isize, found &-ptr
39+
//~^ NOTE expected isize, found reference
4040
//~| NOTE expected type
4141
//~| NOTE found type
4242
let ans = s();

src/test/compile-fail/repeat_count.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ fn main() {
3838
//~^ ERROR mismatched types
3939
//~| expected type `usize`
4040
//~| found type `&'static str`
41-
//~| expected usize, found &-ptr
41+
//~| expected usize, found reference
4242
//~| ERROR expected `usize` for repeat count, found string literal [E0306]
4343
//~| expected `usize`
4444
let f = [0; -4_isize];

0 commit comments

Comments
 (0)