Skip to content

Commit 9ee67a5

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

File tree

5 files changed

+12
-11
lines changed

5 files changed

+12
-11
lines changed

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
);

compiler/rustc_parse/messages.ftl

+1-1
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

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

+5-5
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

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +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
42+
= help: the `.` operator will automatically dereference the value, except if the value is a raw pointer
4343
help: try using `.` instead
4444
|
4545
LL - attr::fn bar() -> String {

tests/ui/suggestions/parenthesized-deref-suggestion.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ LL | (sess as *const Session).opts;
66
|
77
help: the value is a raw pointer; try dereferencing it
88
|
9-
LL | (*(sess as *const Session)).opts;
10-
| ++ +
9+
LL - (sess as *const Session).opts;
10+
LL + (*sess as *const Session).opts;
11+
|
1112

1213
error[E0609]: no field `0` on type `[u32; 1]`
1314
--> $DIR/parenthesized-deref-suggestion.rs:10:21

0 commit comments

Comments
 (0)