Skip to content

Commit

Permalink
Fix string for array access suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
varkor committed Dec 18, 2018
1 parent d6969ac commit 0309874
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/librustc_typeck/check/mod.rs
Expand Up @@ -3372,7 +3372,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
len.assert_usize(self.tcx),
field.as_str().parse::<u64>()
) {
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 {
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/parenthesised-deref-suggestion.rs
Expand Up @@ -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]`
}
9 changes: 9 additions & 0 deletions src/test/ui/parenthesised-deref-suggestion.stderr
Expand Up @@ -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`.

0 comments on commit 0309874

Please sign in to comment.