From 030987481b339616954d36b4c421e86077f00e75 Mon Sep 17 00:00:00 2001 From: varkor Date: Tue, 18 Dec 2018 23:43:00 +0000 Subject: [PATCH] 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 500bdbf6acd8..2f6de21d884f 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 bcbb51ccd6b6..0b4ccdd5a56d 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 2e122f38f385..71a2bf67f06a 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`.