Skip to content

Commit 0309874

Browse files
committed
Fix string for array access suggestion
1 parent d6969ac commit 0309874

File tree

3 files changed

+15
-1
lines changed

3 files changed

+15
-1
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3372,7 +3372,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33723372
len.assert_usize(self.tcx),
33733373
field.as_str().parse::<u64>()
33743374
) {
3375-
let base = self.tcx.hir().node_to_pretty_string(base.id);
3375+
let base = self.tcx.sess.source_map()
3376+
.span_to_snippet(base.span)
3377+
.unwrap_or_else(|_| self.tcx.hir().node_to_pretty_string(base.id));
33763378
let help = "instead of using tuple indexing, use array indexing";
33773379
let suggestion = format!("{}[{}]", base, field);
33783380
let applicability = if len < user_index {

src/test/ui/parenthesised-deref-suggestion.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,7 @@ struct Session {
55
fn main() {
66
let sess: &Session = &Session { opts: 0 };
77
(sess as *const Session).opts; //~ ERROR no field `opts` on type `*const Session`
8+
9+
let x = [0u32];
10+
(x as [u32; 1]).0; //~ ERROR no field `0` on type `[u32; 1]`
811
}

src/test/ui/parenthesised-deref-suggestion.stderr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ help: `(sess as *const Session)` is a raw pointer; try dereferencing it
77
|
88
LL | (*(sess as *const Session)).opts; //~ ERROR no field `opts` on type `*const Session`
99
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
11+
error[E0609]: no field `0` on type `[u32; 1]`
12+
--> $DIR/parenthesised-deref-suggestion.rs:10:21
13+
|
14+
LL | (x as [u32; 1]).0; //~ ERROR no field `0` on type `[u32; 1]`
15+
| ----------------^
16+
| |
17+
| help: instead of using tuple indexing, use array indexing: `(x as [u32; 1])[0]`
18+
1019
error: aborting due to 2 previous errors
1120

1221
For more information about this error, try `rustc --explain E0609`.

0 commit comments

Comments
 (0)