From c221e93fcd967fc93323e7efe094a7471a043a7d Mon Sep 17 00:00:00 2001 From: nathanwhit Date: Sun, 14 Jul 2019 15:05:25 -0400 Subject: [PATCH] Add info about undefined behavior to as_ref suggestions --- src/librustc_typeck/check/method/suggest.rs | 3 +++ src/test/ui/issues/issue-21596.stderr | 1 + 2 files changed, 4 insertions(+) diff --git a/src/librustc_typeck/check/method/suggest.rs b/src/librustc_typeck/check/method/suggest.rs index 8dcfa184d7dad..5febc694def0c 100644 --- a/src/librustc_typeck/check/method/suggest.rs +++ b/src/librustc_typeck/check/method/suggest.rs @@ -331,6 +331,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { err.note("try using `<*const T>::as_ref()` to get a reference to the \ type behind the pointer: https://doc.rust-lang.org/std/\ primitive.pointer.html#method.as_ref"); + err.note("using `<*const T>::as_ref()` on a pointer \ + which is unaligned or points to invalid \ + or uninitialized memory is undefined behavior"); } err } diff --git a/src/test/ui/issues/issue-21596.stderr b/src/test/ui/issues/issue-21596.stderr index 07d29f30e988a..8e4e09b13a9bd 100644 --- a/src/test/ui/issues/issue-21596.stderr +++ b/src/test/ui/issues/issue-21596.stderr @@ -5,6 +5,7 @@ LL | println!("{}", z.to_string()); | ^^^^^^^^^ | = note: try using `<*const T>::as_ref()` to get a reference to the type behind the pointer: https://doc.rust-lang.org/std/primitive.pointer.html#method.as_ref + = note: using `<*const T>::as_ref()` on a pointer which is unaligned or points to invalid or uninitialized memory is undefined behavior = note: the method `to_string` exists but the following trait bounds were not satisfied: `*const u8 : std::string::ToString`