Skip to content

Commit b372c23

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

File tree

8 files changed

+98
-15
lines changed

8 files changed

+98
-15
lines changed

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 2 additions & 2 deletions
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
);

compiler/rustc_parse/messages.ftl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ parse_expected_trait_in_trait_impl_found_type = expected a trait, found type
248248
249249
parse_expr_rarrow_call = `->` used 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

compiler/rustc_parse/src/errors.rs

Lines changed: 0 additions & 1 deletion
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

Lines changed: 23 additions & 0 deletions
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 `->` used 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 `->` used for field access or method call
20+
//~^ ERROR no field `0` on type `*mut Unnamed`
21+
}
22+
23+
fn main() {}
Lines changed: 23 additions & 0 deletions
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 `->` used 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 `->` used for field access or method call
20+
//~^ ERROR no field `0` on type `*mut Unnamed`
21+
}
22+
23+
fn main() {}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
error: `->` used 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: `->` used 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`.

tests/ui/parser/expr-rarrow-call.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ error: `->` used for field access or method call
44
LL | named->foo;
55
| ^^
66
|
7-
= help: the `.` operator will dereference the value if needed
7+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
88
help: try using `.` instead
99
|
1010
LL - named->foo;
@@ -17,7 +17,7 @@ error: `->` used for field access or method call
1717
LL | unnamed->0;
1818
| ^^
1919
|
20-
= help: the `.` operator will dereference the value if needed
20+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
2121
help: try using `.` instead
2222
|
2323
LL - unnamed->0;
@@ -30,7 +30,7 @@ error: `->` used for field access or method call
3030
LL | t->0;
3131
| ^^
3232
|
33-
= help: the `.` operator will dereference the value if needed
33+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
3434
help: try using `.` instead
3535
|
3636
LL - t->0;
@@ -43,7 +43,7 @@ error: `->` used for field access or method call
4343
LL | t->1;
4444
| ^^
4545
|
46-
= help: the `.` operator will dereference the value if needed
46+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
4747
help: try using `.` instead
4848
|
4949
LL - t->1;
@@ -56,7 +56,7 @@ error: `->` used for field access or method call
5656
LL | foo->clone();
5757
| ^^
5858
|
59-
= help: the `.` operator will dereference the value if needed
59+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
6060
help: try using `.` instead
6161
|
6262
LL - foo->clone();

tests/ui/parser/issues/issue-118530-ice.stderr

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,7 @@ error: `->` used for field access or method call
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)