diff options
| author | Chris Denton <chris@chrisdenton.dev> | 2025-04-22 15:24:05 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-04-22 15:24:05 +0000 |
| commit | 8089e317b55841af5e9f3d08952e44ce01853c26 (patch) | |
| tree | 8821f52b305cab85509992bb62ca664dc24ab97f /tests/ui/parser/issues | |
| parent | 1586660fb270fcf32a77521b7348ac138c99c489 (diff) | |
| parent | b5e8f1f0cec8065eb9cb453ad4c5aa5bec8baf32 (diff) | |
| download | rust-8089e317b55841af5e9f3d08952e44ce01853c26.tar.gz rust-8089e317b55841af5e9f3d08952e44ce01853c26.zip | |
Rollup merge of #139921 - Kivooeo:master, r=WaffleLapkin
improve diagnostic for raw pointer field access with -> This PR enhances the error messages emitted by the Rust compiler when users attempt to use the `->` operator for field access on raw pointers or when dereferencing is needed. The changes aim to provide clearer guidance, by suggesting the correct use of the `.` operator and explicit dereferencing. **Before:** ``` help: `xs` is a raw pointer; try dereferencing it | LL | (*xs)->count += 1; | ++ + ``` **Now:** ``` help: use `.` on a dereferenced raw pointer instead | LL - xs->count += 1; LL + (*xs).count += 1; | ``` I added extra clarification in the message. Since this error occurs in the parser, we can't be certain that the type is a raw pointer. That's why the message includes only a small note in brackets. (In contrast, the message above is emitted in HIR, where we *can* check whether it's a raw pointer.) **Before:** ``` --> main.rs:11:11 | 11 | xs->count += 1; | ^^ | = help: the . operator will dereference the value if needed ``` **After:** ``` --> main.rs:11:11 | 11 | xs->count += 1; | ^^ | = help: the `.` operator will automatically dereference the value, except if the value is a raw pointer ```
Diffstat (limited to 'tests/ui/parser/issues')
| -rw-r--r-- | tests/ui/parser/issues/issue-118530-ice.rs | 2 | ||||
| -rw-r--r-- | tests/ui/parser/issues/issue-118530-ice.stderr | 4 |
2 files changed, 3 insertions, 3 deletions
diff --git a/tests/ui/parser/issues/issue-118530-ice.rs b/tests/ui/parser/issues/issue-118530-ice.rs index cf14eebec2b..8930eb86c6b 100644 --- a/tests/ui/parser/issues/issue-118530-ice.rs +++ b/tests/ui/parser/issues/issue-118530-ice.rs @@ -5,7 +5,7 @@ fn bar() -> String { attr::fn bar() -> String { //~ ERROR expected identifier, found keyword `fn` //~^ ERROR expected one of `(`, `.`, `::`, `;`, `?`, `}`, or an operator, found `{` //~| ERROR expected `;`, found `bar` - //~| ERROR `->` used for field access or method call + //~| ERROR `->` is not valid syntax for field accesses and method calls #[attr] [1, 2, 3].iter().map().collect::<String>() #[attr] diff --git a/tests/ui/parser/issues/issue-118530-ice.stderr b/tests/ui/parser/issues/issue-118530-ice.stderr index 72c0397e9c9..ef891d1dc29 100644 --- a/tests/ui/parser/issues/issue-118530-ice.stderr +++ b/tests/ui/parser/issues/issue-118530-ice.stderr @@ -33,13 +33,13 @@ LL | attr::fn bar() -> String { | | | help: add `;` here -error: `->` used for field access or method call +error: `->` is not valid syntax for field accesses and method calls --> $DIR/issue-118530-ice.rs:5:20 | LL | attr::fn bar() -> String { | ^^ | - = help: the `.` operator will dereference the value if needed + = help: the `.` operator will automatically dereference the value, except if the value is a raw pointer help: try using `.` instead | LL - attr::fn bar() -> String { |
