Skip to content

Commit d6969ac

Browse files
committed
Fix string for raw pointer deref suggestion
1 parent c2402dc commit d6969ac

File tree

5 files changed

+33
-5
lines changed

5 files changed

+33
-5
lines changed

src/librustc_typeck/check/mod.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3386,11 +3386,13 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33863386
}
33873387
}
33883388
ty::RawPtr(..) => {
3389-
let base = self.tcx.hir().node_to_pretty_string(base.id);
3389+
let base = self.tcx.sess.source_map()
3390+
.span_to_snippet(base.span)
3391+
.unwrap_or_else(|_| self.tcx.hir().node_to_pretty_string(base.id));
33903392
let msg = format!("`{}` is a raw pointer; try dereferencing it", base);
33913393
let suggestion = format!("(*{}).{}", base, field);
33923394
err.span_suggestion_with_applicability(
3393-
field.span,
3395+
expr.span,
33943396
&msg,
33953397
suggestion,
33963398
Applicability::MaybeIncorrect,

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

Lines changed: 6 additions & 2 deletions
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 raw 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 raw 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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
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+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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+
error: aborting due to 2 previous errors
11+
12+
For more information about this error, try `rustc --explain E0609`.

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

Lines changed: 3 additions & 1 deletion
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 raw 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)