Skip to content

Commit 611b511

Browse files
committed
improve diagnostic for raw pointer field access using ->
1 parent 299877e commit 611b511

11 files changed

+116
-92
lines changed

Diff for: compiler/rustc_hir_typeck/src/expr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3289,8 +3289,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
32893289
err.multipart_suggestion(
32903290
format!("{val} is a raw pointer; try dereferencing it"),
32913291
vec![
3292-
(base.span.shrink_to_lo(), "(*".to_string()),
3293-
(base.span.shrink_to_hi(), ")".to_string()),
3292+
(base.span.shrink_to_lo(), "(*".into()),
3293+
(base.span.between(field.span), format!(").")),
32943294
],
32953295
Applicability::MaybeIncorrect,
32963296
);

Diff for: compiler/rustc_parse/messages.ftl

+2-2
Original file line numberDiff line numberDiff line change
@@ -246,9 +246,9 @@ parse_expected_struct_field = expected one of `,`, `:`, or `{"}"}`, found `{$tok
246246
247247
parse_expected_trait_in_trait_impl_found_type = expected a trait, found type
248248
249-
parse_expr_rarrow_call = `->` used for field access or method call
249+
parse_expr_rarrow_call = `->` is not valid for field access or method call
250250
.suggestion = try using `.` instead
251-
.help = the `.` operator will dereference the value if needed
251+
.help = the `.` operator will automatically dereference the value, except if the value is a raw pointer
252252
253253
parse_extern_crate_name_with_dashes = crate name using dashes are not valid in `extern crate` statements
254254
.label = dash-separated idents are not valid

Diff for: compiler/rustc_parse/src/errors.rs

-1
Original file line numberDiff line numberDiff line change
@@ -3333,7 +3333,6 @@ pub(crate) struct AsyncImpl {
33333333
#[help]
33343334
pub(crate) struct ExprRArrowCall {
33353335
#[primary_span]
3336-
#[suggestion(style = "verbose", applicability = "machine-applicable", code = ".")]
33373336
pub span: Span,
33383337
}
33393338

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ run-rustfix
2+
#![allow(
3+
dead_code,
4+
unused_must_use
5+
)]
6+
7+
struct Named {
8+
foo: usize,
9+
}
10+
11+
struct Unnamed(usize);
12+
13+
unsafe fn named_struct_field_access(named: *mut Named) {
14+
(*named).foo += 1; //~ ERROR `->` is not valid for field access or method call
15+
//~^ ERROR no field `foo` on type `*mut Named`
16+
}
17+
18+
unsafe fn unnamed_struct_field_access(unnamed: *mut Unnamed) {
19+
(*unnamed).0 += 1; //~ ERROR `->` is not valid for field access or method call
20+
//~^ ERROR no field `0` on type `*mut Unnamed`
21+
}
22+
23+
fn main() {}

Diff for: tests/ui/parser/expr-rarrow-call-on-a-raw-pointer.rs

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//@ run-rustfix
2+
#![allow(
3+
dead_code,
4+
unused_must_use
5+
)]
6+
7+
struct Named {
8+
foo: usize,
9+
}
10+
11+
struct Unnamed(usize);
12+
13+
unsafe fn named_struct_field_access(named: *mut Named) {
14+
named->foo += 1; //~ ERROR `->` is not valid for field access or method call
15+
//~^ ERROR no field `foo` on type `*mut Named`
16+
}
17+
18+
unsafe fn unnamed_struct_field_access(unnamed: *mut Unnamed) {
19+
unnamed->0 += 1; //~ ERROR `->` is not valid for field access or method call
20+
//~^ ERROR no field `0` on type `*mut Unnamed`
21+
}
22+
23+
fn main() {}
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error: `->` is not valid for field access or method call
2+
--> $DIR/expr-rarrow-call-on-a-raw-pointer.rs:14:10
3+
|
4+
LL | named->foo += 1;
5+
| ^^
6+
|
7+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
8+
9+
error: `->` is not valid for field access or method call
10+
--> $DIR/expr-rarrow-call-on-a-raw-pointer.rs:19:12
11+
|
12+
LL | unnamed->0 += 1;
13+
| ^^
14+
|
15+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
16+
17+
error[E0609]: no field `foo` on type `*mut Named`
18+
--> $DIR/expr-rarrow-call-on-a-raw-pointer.rs:14:12
19+
|
20+
LL | named->foo += 1;
21+
| ^^^ unknown field
22+
|
23+
help: `named` is a raw pointer; try dereferencing it
24+
|
25+
LL - named->foo += 1;
26+
LL + (*named).foo += 1;
27+
|
28+
29+
error[E0609]: no field `0` on type `*mut Unnamed`
30+
--> $DIR/expr-rarrow-call-on-a-raw-pointer.rs:19:14
31+
|
32+
LL | unnamed->0 += 1;
33+
| ^ unknown field
34+
|
35+
help: `unnamed` is a raw pointer; try dereferencing it
36+
|
37+
LL - unnamed->0 += 1;
38+
LL + (*unnamed).0 += 1;
39+
|
40+
41+
error: aborting due to 4 previous errors
42+
43+
For more information about this error, try `rustc --explain E0609`.

Diff for: tests/ui/parser/expr-rarrow-call.fixed

-33
This file was deleted.

Diff for: tests/ui/parser/expr-rarrow-call.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
//@ run-rustfix
21
#![allow(
32
dead_code,
43
unused_must_use
@@ -11,23 +10,23 @@ struct Named {
1110
struct Unnamed(usize);
1211

1312
fn named_struct_field_access(named: &Named) {
14-
named->foo; //~ ERROR `->` used for field access or method call
13+
named->foo; //~ ERROR `->` is not valid for field access or method call
1514
}
1615

1716
fn unnamed_struct_field_access(unnamed: &Unnamed) {
18-
unnamed->0; //~ ERROR `->` used for field access or method call
17+
unnamed->0; //~ ERROR `->` is not valid for field access or method call
1918
}
2019

2120
fn tuple_field_access(t: &(u8, u8)) {
22-
t->0; //~ ERROR `->` used for field access or method call
23-
t->1; //~ ERROR `->` used for field access or method call
21+
t->0; //~ ERROR `->` is not valid for field access or method call
22+
t->1; //~ ERROR `->` is not valid for field access or method call
2423
}
2524

2625
#[derive(Clone)]
2726
struct Foo;
2827

2928
fn method_call(foo: &Foo) {
30-
foo->clone(); //~ ERROR `->` used for field access or method call
29+
foo->clone(); //~ ERROR `->` is not valid for field access or method call
3130
}
3231

3332
fn main() {}

Diff for: tests/ui/parser/expr-rarrow-call.stderr

+15-40
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,42 @@
1-
error: `->` used for field access or method call
2-
--> $DIR/expr-rarrow-call.rs:14:10
1+
error: `->` is not valid for field access or method call
2+
--> $DIR/expr-rarrow-call.rs:13:10
33
|
44
LL | named->foo;
55
| ^^
66
|
7-
= help: the `.` operator will dereference the value if needed
8-
help: try using `.` instead
9-
|
10-
LL - named->foo;
11-
LL + named.foo;
12-
|
7+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
138

14-
error: `->` used for field access or method call
15-
--> $DIR/expr-rarrow-call.rs:18:12
9+
error: `->` is not valid for field access or method call
10+
--> $DIR/expr-rarrow-call.rs:17:12
1611
|
1712
LL | unnamed->0;
1813
| ^^
1914
|
20-
= help: the `.` operator will dereference the value if needed
21-
help: try using `.` instead
22-
|
23-
LL - unnamed->0;
24-
LL + unnamed.0;
25-
|
15+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
2616

27-
error: `->` used for field access or method call
28-
--> $DIR/expr-rarrow-call.rs:22:6
17+
error: `->` is not valid for field access or method call
18+
--> $DIR/expr-rarrow-call.rs:21:6
2919
|
3020
LL | t->0;
3121
| ^^
3222
|
33-
= help: the `.` operator will dereference the value if needed
34-
help: try using `.` instead
35-
|
36-
LL - t->0;
37-
LL + t.0;
38-
|
23+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
3924

40-
error: `->` used for field access or method call
41-
--> $DIR/expr-rarrow-call.rs:23:6
25+
error: `->` is not valid for field access or method call
26+
--> $DIR/expr-rarrow-call.rs:22:6
4227
|
4328
LL | t->1;
4429
| ^^
4530
|
46-
= help: the `.` operator will dereference the value if needed
47-
help: try using `.` instead
48-
|
49-
LL - t->1;
50-
LL + t.1;
51-
|
31+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
5232

53-
error: `->` used for field access or method call
54-
--> $DIR/expr-rarrow-call.rs:30:8
33+
error: `->` is not valid for field access or method call
34+
--> $DIR/expr-rarrow-call.rs:29:8
5535
|
5636
LL | foo->clone();
5737
| ^^
5838
|
59-
= help: the `.` operator will dereference the value if needed
60-
help: try using `.` instead
61-
|
62-
LL - foo->clone();
63-
LL + foo.clone();
64-
|
39+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
6540

6641
error: aborting due to 5 previous errors
6742

Diff for: tests/ui/parser/issues/issue-118530-ice.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn bar() -> String {
55
attr::fn bar() -> String { //~ ERROR expected identifier, found keyword `fn`
66
//~^ ERROR expected one of `(`, `.`, `::`, `;`, `?`, `}`, or an operator, found `{`
77
//~| ERROR expected `;`, found `bar`
8-
//~| ERROR `->` used for field access or method call
8+
//~| ERROR `->` is not valid for field access or method call
99
#[attr]
1010
[1, 2, 3].iter().map().collect::<String>()
1111
#[attr]

Diff for: tests/ui/parser/issues/issue-118530-ice.stderr

+2-7
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,13 @@ LL | attr::fn bar() -> String {
3333
| |
3434
| help: add `;` here
3535

36-
error: `->` used for field access or method call
36+
error: `->` is not valid for field access or method call
3737
--> $DIR/issue-118530-ice.rs:5:20
3838
|
3939
LL | attr::fn bar() -> String {
4040
| ^^
4141
|
42-
= help: the `.` operator will dereference the value if needed
43-
help: try using `.` instead
44-
|
45-
LL - attr::fn bar() -> String {
46-
LL + attr::fn bar() . String {
47-
|
42+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
4843

4944
error: expected one of `(`, `.`, `::`, `;`, `?`, `}`, or an operator, found `{`
5045
--> $DIR/issue-118530-ice.rs:5:30

0 commit comments

Comments
 (0)