Skip to content

Commit d875fcd

Browse files
authored
Rollup merge of rust-lang#56966 - varkor:raw-pointer-deref-parens, r=zackmdavis
Correct strings for raw pointer deref and array access suggestions Fixes rust-lang#56714. Fixes rust-lang#56963. r? @zackmdavis
2 parents 3a5e4da + 0309874 commit d875fcd

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

src/librustc_typeck/check/mod.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -3440,7 +3440,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34403440
len.assert_usize(self.tcx),
34413441
field.as_str().parse::<u64>()
34423442
) {
3443-
let base = self.tcx.hir().node_to_pretty_string(base.id);
3443+
let base = self.tcx.sess.source_map()
3444+
.span_to_snippet(base.span)
3445+
.unwrap_or_else(|_| self.tcx.hir().node_to_pretty_string(base.id));
34443446
let help = "instead of using tuple indexing, use array indexing";
34453447
let suggestion = format!("{}[{}]", base, field);
34463448
let applicability = if len < user_index {
@@ -3454,11 +3456,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
34543456
}
34553457
}
34563458
ty::RawPtr(..) => {
3457-
let base = self.tcx.hir().node_to_pretty_string(base.id);
3458-
let msg = format!("`{}` is a native pointer; try dereferencing it", base);
3459+
let base = self.tcx.sess.source_map()
3460+
.span_to_snippet(base.span)
3461+
.unwrap_or_else(|_| self.tcx.hir().node_to_pretty_string(base.id));
3462+
let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
34593463
let suggestion = format!("(*{}).{}", base, field);
34603464
err.span_suggestion_with_applicability(
3461-
field.span,
3465+
expr.span,
34623466
&msg,
34633467
suggestion,
34643468
Applicability::MaybeIncorrect,

src/test/ui/issues/issue-11004.stderr

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,17 @@ error[E0609]: no field `x` on type `*mut A`
22
--> $DIR/issue-11004.rs:17:21
33
|
44
LL | let x : i32 = n.x; //~ no field `x` on type `*mut A`
5-
| ^ help: `n` is a native pointer; try dereferencing it: `(*n).x`
5+
| --^
6+
| |
7+
| help: `n` is a raw pointer; try dereferencing it: `(*n).x`
68

79
error[E0609]: no field `y` on type `*mut A`
810
--> $DIR/issue-11004.rs:18:21
911
|
1012
LL | let y : f64 = n.y; //~ no field `y` on type `*mut A`
11-
| ^ help: `n` is a native pointer; try dereferencing it: `(*n).y`
13+
| --^
14+
| |
15+
| help: `n` is a raw pointer; try dereferencing it: `(*n).y`
1216

1317
error: aborting due to 2 previous errors
1418

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
struct Session {
2+
opts: u8,
3+
}
4+
5+
fn main() {
6+
let sess: &Session = &Session { opts: 0 };
7+
(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]`
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
error[E0609]: no field `opts` on type `*const Session`
2+
--> $DIR/parenthesised-deref-suggestion.rs:7:30
3+
|
4+
LL | (sess as *const Session).opts; //~ ERROR no field `opts` on type `*const Session`
5+
| ^^^^
6+
help: `(sess as *const Session)` is a raw pointer; try dereferencing it
7+
|
8+
LL | (*(sess as *const Session)).opts; //~ ERROR no field `opts` on type `*const Session`
9+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
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+
19+
error: aborting due to 2 previous errors
20+
21+
For more information about this error, try `rustc --explain E0609`.

src/test/ui/unsafe/unsafe-fn-autoderef.stderr

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ error[E0609]: no field `f` on type `*const Rec`
22
--> $DIR/unsafe-fn-autoderef.rs:29:14
33
|
44
LL | return p.f; //~ ERROR no field `f` on type `*const Rec`
5-
| ^ help: `p` is a native pointer; try dereferencing it: `(*p).f`
5+
| --^
6+
| |
7+
| help: `p` is a raw pointer; try dereferencing it: `(*p).f`
68

79
error: aborting due to previous error
810

0 commit comments

Comments
 (0)