From c2402dca85716aa4b452352bec11ff949c5a1cbb Mon Sep 17 00:00:00 2001 From: varkor Date: Tue, 18 Dec 2018 22:58:49 +0000 Subject: [PATCH 1/3] Replace "native pointer" in error message with "raw pointer" --- src/librustc_typeck/check/mod.rs | 2 +- src/test/ui/issues/issue-11004.stderr | 4 ++-- src/test/ui/unsafe/unsafe-fn-autoderef.stderr | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 957c8d9f19f0e..cb14078fb93ca 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3387,7 +3387,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } ty::RawPtr(..) => { let base = self.tcx.hir().node_to_pretty_string(base.id); - let msg = format!("`{}` is a native pointer; try dereferencing it", base); + let msg = format!("`{}` is a raw pointer; try dereferencing it", base); let suggestion = format!("(*{}).{}", base, field); err.span_suggestion_with_applicability( field.span, diff --git a/src/test/ui/issues/issue-11004.stderr b/src/test/ui/issues/issue-11004.stderr index 215120c9c25ea..46b4c4bdba201 100644 --- a/src/test/ui/issues/issue-11004.stderr +++ b/src/test/ui/issues/issue-11004.stderr @@ -2,13 +2,13 @@ error[E0609]: no field `x` on type `*mut A` --> $DIR/issue-11004.rs:17:21 | LL | let x : i32 = n.x; //~ no field `x` on type `*mut A` - | ^ help: `n` is a native pointer; try dereferencing it: `(*n).x` + | ^ help: `n` is a raw pointer; try dereferencing it: `(*n).x` error[E0609]: no field `y` on type `*mut A` --> $DIR/issue-11004.rs:18:21 | LL | let y : f64 = n.y; //~ no field `y` on type `*mut A` - | ^ help: `n` is a native pointer; try dereferencing it: `(*n).y` + | ^ help: `n` is a raw pointer; try dereferencing it: `(*n).y` error: aborting due to 2 previous errors diff --git a/src/test/ui/unsafe/unsafe-fn-autoderef.stderr b/src/test/ui/unsafe/unsafe-fn-autoderef.stderr index 13fcbb347c94b..81f15b2931df3 100644 --- a/src/test/ui/unsafe/unsafe-fn-autoderef.stderr +++ b/src/test/ui/unsafe/unsafe-fn-autoderef.stderr @@ -2,7 +2,7 @@ error[E0609]: no field `f` on type `*const Rec` --> $DIR/unsafe-fn-autoderef.rs:29:14 | LL | return p.f; //~ ERROR no field `f` on type `*const Rec` - | ^ help: `p` is a native pointer; try dereferencing it: `(*p).f` + | ^ help: `p` is a raw pointer; try dereferencing it: `(*p).f` error: aborting due to previous error From d6969ac2fbc07d646a6c46631c9c96165a1774fe Mon Sep 17 00:00:00 2001 From: varkor Date: Tue, 18 Dec 2018 23:42:42 +0000 Subject: [PATCH 2/3] Fix string for raw pointer deref suggestion --- src/librustc_typeck/check/mod.rs | 6 ++++-- src/test/ui/issues/issue-11004.stderr | 8 ++++++-- src/test/ui/parenthesised-deref-suggestion.rs | 8 ++++++++ src/test/ui/parenthesised-deref-suggestion.stderr | 12 ++++++++++++ src/test/ui/unsafe/unsafe-fn-autoderef.stderr | 4 +++- 5 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 src/test/ui/parenthesised-deref-suggestion.rs create mode 100644 src/test/ui/parenthesised-deref-suggestion.stderr diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index cb14078fb93ca..500bdbf6acd85 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3386,11 +3386,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { } } ty::RawPtr(..) => { - let base = self.tcx.hir().node_to_pretty_string(base.id); + let base = self.tcx.sess.source_map() + .span_to_snippet(base.span) + .unwrap_or_else(|_| self.tcx.hir().node_to_pretty_string(base.id)); let msg = format!("`{}` is a raw pointer; try dereferencing it", base); let suggestion = format!("(*{}).{}", base, field); err.span_suggestion_with_applicability( - field.span, + expr.span, &msg, suggestion, Applicability::MaybeIncorrect, diff --git a/src/test/ui/issues/issue-11004.stderr b/src/test/ui/issues/issue-11004.stderr index 46b4c4bdba201..eb5b568b34738 100644 --- a/src/test/ui/issues/issue-11004.stderr +++ b/src/test/ui/issues/issue-11004.stderr @@ -2,13 +2,17 @@ error[E0609]: no field `x` on type `*mut A` --> $DIR/issue-11004.rs:17:21 | LL | let x : i32 = n.x; //~ no field `x` on type `*mut A` - | ^ help: `n` is a raw pointer; try dereferencing it: `(*n).x` + | --^ + | | + | help: `n` is a raw pointer; try dereferencing it: `(*n).x` error[E0609]: no field `y` on type `*mut A` --> $DIR/issue-11004.rs:18:21 | LL | let y : f64 = n.y; //~ no field `y` on type `*mut A` - | ^ help: `n` is a raw pointer; try dereferencing it: `(*n).y` + | --^ + | | + | help: `n` is a raw pointer; try dereferencing it: `(*n).y` error: aborting due to 2 previous errors diff --git a/src/test/ui/parenthesised-deref-suggestion.rs b/src/test/ui/parenthesised-deref-suggestion.rs new file mode 100644 index 0000000000000..bcbb51ccd6b65 --- /dev/null +++ b/src/test/ui/parenthesised-deref-suggestion.rs @@ -0,0 +1,8 @@ +struct Session { + opts: u8, +} + +fn main() { + let sess: &Session = &Session { opts: 0 }; + (sess as *const Session).opts; //~ ERROR no field `opts` on type `*const Session` +} diff --git a/src/test/ui/parenthesised-deref-suggestion.stderr b/src/test/ui/parenthesised-deref-suggestion.stderr new file mode 100644 index 0000000000000..2e122f38f3855 --- /dev/null +++ b/src/test/ui/parenthesised-deref-suggestion.stderr @@ -0,0 +1,12 @@ +error[E0609]: no field `opts` on type `*const Session` + --> $DIR/parenthesised-deref-suggestion.rs:7:30 + | +LL | (sess as *const Session).opts; //~ ERROR no field `opts` on type `*const Session` + | ^^^^ +help: `(sess as *const Session)` is a raw pointer; try dereferencing it + | +LL | (*(sess as *const Session)).opts; //~ ERROR no field `opts` on type `*const Session` + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +error: aborting due to 2 previous errors + +For more information about this error, try `rustc --explain E0609`. diff --git a/src/test/ui/unsafe/unsafe-fn-autoderef.stderr b/src/test/ui/unsafe/unsafe-fn-autoderef.stderr index 81f15b2931df3..7525f67051567 100644 --- a/src/test/ui/unsafe/unsafe-fn-autoderef.stderr +++ b/src/test/ui/unsafe/unsafe-fn-autoderef.stderr @@ -2,7 +2,9 @@ error[E0609]: no field `f` on type `*const Rec` --> $DIR/unsafe-fn-autoderef.rs:29:14 | LL | return p.f; //~ ERROR no field `f` on type `*const Rec` - | ^ help: `p` is a raw pointer; try dereferencing it: `(*p).f` + | --^ + | | + | help: `p` is a raw pointer; try dereferencing it: `(*p).f` error: aborting due to previous error From 030987481b339616954d36b4c421e86077f00e75 Mon Sep 17 00:00:00 2001 From: varkor Date: Tue, 18 Dec 2018 23:43:00 +0000 Subject: [PATCH 3/3] Fix string for array access suggestion --- src/librustc_typeck/check/mod.rs | 4 +++- src/test/ui/parenthesised-deref-suggestion.rs | 3 +++ src/test/ui/parenthesised-deref-suggestion.stderr | 9 +++++++++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 500bdbf6acd85..2f6de21d884f5 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3372,7 +3372,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> { len.assert_usize(self.tcx), field.as_str().parse::() ) { - let base = self.tcx.hir().node_to_pretty_string(base.id); + let base = self.tcx.sess.source_map() + .span_to_snippet(base.span) + .unwrap_or_else(|_| self.tcx.hir().node_to_pretty_string(base.id)); let help = "instead of using tuple indexing, use array indexing"; let suggestion = format!("{}[{}]", base, field); let applicability = if len < user_index { diff --git a/src/test/ui/parenthesised-deref-suggestion.rs b/src/test/ui/parenthesised-deref-suggestion.rs index bcbb51ccd6b65..0b4ccdd5a56d4 100644 --- a/src/test/ui/parenthesised-deref-suggestion.rs +++ b/src/test/ui/parenthesised-deref-suggestion.rs @@ -5,4 +5,7 @@ struct Session { fn main() { let sess: &Session = &Session { opts: 0 }; (sess as *const Session).opts; //~ ERROR no field `opts` on type `*const Session` + + let x = [0u32]; + (x as [u32; 1]).0; //~ ERROR no field `0` on type `[u32; 1]` } diff --git a/src/test/ui/parenthesised-deref-suggestion.stderr b/src/test/ui/parenthesised-deref-suggestion.stderr index 2e122f38f3855..71a2bf67f06ae 100644 --- a/src/test/ui/parenthesised-deref-suggestion.stderr +++ b/src/test/ui/parenthesised-deref-suggestion.stderr @@ -7,6 +7,15 @@ help: `(sess as *const Session)` is a raw pointer; try dereferencing it | LL | (*(sess as *const Session)).opts; //~ ERROR no field `opts` on type `*const Session` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +error[E0609]: no field `0` on type `[u32; 1]` + --> $DIR/parenthesised-deref-suggestion.rs:10:21 + | +LL | (x as [u32; 1]).0; //~ ERROR no field `0` on type `[u32; 1]` + | ----------------^ + | | + | help: instead of using tuple indexing, use array indexing: `(x as [u32; 1])[0]` + error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0609`.