Skip to content

Commit c221e93

Browse files
committed
Add info about undefined behavior to as_ref suggestions
1 parent d82fd9e commit c221e93

File tree

2 files changed

+4
-0
lines changed

2 files changed

+4
-0
lines changed

src/librustc_typeck/check/method/suggest.rs

+3
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
331331
err.note("try using `<*const T>::as_ref()` to get a reference to the \
332332
type behind the pointer: https://doc.rust-lang.org/std/\
333333
primitive.pointer.html#method.as_ref");
334+
err.note("using `<*const T>::as_ref()` on a pointer \
335+
which is unaligned or points to invalid \
336+
or uninitialized memory is undefined behavior");
334337
}
335338
err
336339
}

src/test/ui/issues/issue-21596.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ LL | println!("{}", z.to_string());
55
| ^^^^^^^^^
66
|
77
= 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
8+
= note: using `<*const T>::as_ref()` on a pointer which is unaligned or points to invalid or uninitialized memory is undefined behavior
89
= note: the method `to_string` exists but the following trait bounds were not satisfied:
910
`*const u8 : std::string::ToString`
1011

0 commit comments

Comments
 (0)