Skip to content

Commit

Permalink
Rollup merge of rust-lang#56966 - varkor:raw-pointer-deref-parens, r=…
Browse files Browse the repository at this point in the history
…zackmdavis

Correct strings for raw pointer deref and array access suggestions

Fixes rust-lang#56714.
Fixes rust-lang#56963.

r? @zackmdavis
  • Loading branch information
Centril authored Dec 23, 2018
2 parents 3a5e4da + 0309874 commit d875fcd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
12 changes: 8 additions & 4 deletions src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3440,7 +3440,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 All @@ -3454,11 +3456,13 @@ 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 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,
Expand Down
8 changes: 6 additions & 2 deletions src/test/ui/issues/issue-11004.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 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

Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/parenthesised-deref-suggestion.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
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`

let x = [0u32];
(x as [u32; 1]).0; //~ ERROR no field `0` on type `[u32; 1]`
}
21 changes: 21 additions & 0 deletions src/test/ui/parenthesised-deref-suggestion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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[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`.
4 changes: 3 additions & 1 deletion src/test/ui/unsafe/unsafe-fn-autoderef.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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 native pointer; try dereferencing it: `(*p).f`
| --^
| |
| help: `p` is a raw pointer; try dereferencing it: `(*p).f`

error: aborting due to previous error

Expand Down

0 comments on commit d875fcd

Please sign in to comment.