From 5021dcd7998ff721a247369bca601bd001a1a3e0 Mon Sep 17 00:00:00 2001 From: nils <48135649+Nilstrieb@users.noreply.github.com> Date: Wed, 20 Jul 2022 18:32:58 +0200 Subject: [PATCH] Display raw pointer as `*{mut,const} T` instead of `*-ptr` in errors The `*-ptr` is rather confusing, and we have the full information for properly displaying the information. --- compiler/rustc_middle/src/ty/error.rs | 15 ++++++++++++++- src/test/ui/asm/type-check-1.stderr | 4 ++-- src/test/ui/dst/dst-bad-coercions.stderr | 8 ++++---- src/test/ui/mismatched_types/issue-19109.stderr | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/compiler/rustc_middle/src/ty/error.rs b/compiler/rustc_middle/src/ty/error.rs index ac89bec702efd..da564c66a70e1 100644 --- a/compiler/rustc_middle/src/ty/error.rs +++ b/compiler/rustc_middle/src/ty/error.rs @@ -276,10 +276,23 @@ impl<'tcx> Ty<'tcx> { } ty::Slice(ty) if ty.is_simple_ty() => format!("slice `{}`", self).into(), ty::Slice(_) => "slice".into(), - ty::RawPtr(_) => "*-ptr".into(), + ty::RawPtr(tymut) => { + let tymut_string = match tymut.mutbl { + hir::Mutability::Mut => tymut.to_string(), + hir::Mutability::Not => format!("const {}", tymut.ty), + }; + + if tymut_string != "_" && (tymut.ty.is_simple_text() || tymut_string.len() < "const raw pointer".len()) { + format!("`*{}`", tymut_string).into() + } else { + // Unknown type name, it's long or has type arguments + "raw pointer".into() + } + }, ty::Ref(_, ty, mutbl) => { let tymut = ty::TypeAndMut { ty, mutbl }; let tymut_string = tymut.to_string(); + if tymut_string != "_" && (ty.is_simple_text() || tymut_string.len() < "mutable reference".len()) { diff --git a/src/test/ui/asm/type-check-1.stderr b/src/test/ui/asm/type-check-1.stderr index 162ff1d32bc44..1845139659db5 100644 --- a/src/test/ui/asm/type-check-1.stderr +++ b/src/test/ui/asm/type-check-1.stderr @@ -106,7 +106,7 @@ error[E0308]: mismatched types --> $DIR/type-check-1.rs:60:26 | LL | asm!("{}", const 0 as *mut u8); - | ^^^^^^^^^^^^ expected integer, found *-ptr + | ^^^^^^^^^^^^ expected integer, found `*mut u8` | = note: expected type `{integer}` found raw pointer `*mut u8` @@ -133,7 +133,7 @@ error[E0308]: mismatched types --> $DIR/type-check-1.rs:78:25 | LL | global_asm!("{}", const 0 as *mut u8); - | ^^^^^^^^^^^^ expected integer, found *-ptr + | ^^^^^^^^^^^^ expected integer, found `*mut u8` | = note: expected type `{integer}` found raw pointer `*mut u8` diff --git a/src/test/ui/dst/dst-bad-coercions.stderr b/src/test/ui/dst/dst-bad-coercions.stderr index 01f862ed516e9..0d6f4d0209f71 100644 --- a/src/test/ui/dst/dst-bad-coercions.stderr +++ b/src/test/ui/dst/dst-bad-coercions.stderr @@ -2,7 +2,7 @@ error[E0308]: mismatched types --> $DIR/dst-bad-coercions.rs:14:17 | LL | let y: &S = x; - | -- ^ expected `&S`, found *-ptr + | -- ^ expected `&S`, found `*const S` | | | expected due to this | @@ -13,7 +13,7 @@ error[E0308]: mismatched types --> $DIR/dst-bad-coercions.rs:15:21 | LL | let y: &dyn T = x; - | ------ ^ expected `&dyn T`, found *-ptr + | ------ ^ expected `&dyn T`, found `*const S` | | | expected due to this | @@ -24,7 +24,7 @@ error[E0308]: mismatched types --> $DIR/dst-bad-coercions.rs:19:17 | LL | let y: &S = x; - | -- ^ expected `&S`, found *-ptr + | -- ^ expected `&S`, found `*mut S` | | | expected due to this | @@ -35,7 +35,7 @@ error[E0308]: mismatched types --> $DIR/dst-bad-coercions.rs:20:21 | LL | let y: &dyn T = x; - | ------ ^ expected `&dyn T`, found *-ptr + | ------ ^ expected `&dyn T`, found `*mut S` | | | expected due to this | diff --git a/src/test/ui/mismatched_types/issue-19109.stderr b/src/test/ui/mismatched_types/issue-19109.stderr index c25e2687b752a..5cef64bb16926 100644 --- a/src/test/ui/mismatched_types/issue-19109.stderr +++ b/src/test/ui/mismatched_types/issue-19109.stderr @@ -4,7 +4,7 @@ error[E0308]: mismatched types LL | fn function(t: &mut dyn Trait) { | - help: try adding a return type: `-> *mut dyn Trait` LL | t as *mut dyn Trait - | ^^^^^^^^^^^^^^^^^^^ expected `()`, found *-ptr + | ^^^^^^^^^^^^^^^^^^^ expected `()`, found `*mut dyn Trait` | = note: expected unit type `()` found raw pointer `*mut dyn Trait`