Skip to content

&raw $expr in a list should not recover to &raw, $expr #157015

@Zalathar

Description

@Zalathar

Code

fn takes_raw_ptr(_raw_ptr: *const u32) {}

fn main() {
    let x = 0u32;
    takes_raw_ptr(&raw x);
}

Current output

error: expected one of `!`, `)`, `,`, `.`, `::`, `?`, `const`, `mut`, `{`, or an operator, found `x`
 --> src/main.rs:5:24
  |
5 |     takes_raw_ptr(&raw x);
  |                        ^ expected one of 10 possible tokens
  |
help: `&raw` must be followed by `const` or `mut` to be a raw reference expression
  |
5 |     takes_raw_ptr(&raw const x);
  |                        +++++
5 |     takes_raw_ptr(&raw mut x);
  |                        +++
help: missing `,`
  |
5 |     takes_raw_ptr(&raw, x);
  |                       +

error[E0425]: cannot find value `raw` in this scope
 --> src/main.rs:5:20
  |
5 |     takes_raw_ptr(&raw x);
  |                    ^^^ not found in this scope

error[E0061]: this function takes 1 argument but 2 arguments were supplied
 --> src/main.rs:5:5
  |
5 |     takes_raw_ptr(&raw x);
  |     ^^^^^^^^^^^^^      - unexpected argument #2 of type `u32`
  |
note: function defined here
 --> src/main.rs:1:4
  |
1 | fn takes_raw_ptr(_raw_ptr: *const u32) {}
  |    ^^^^^^^^^^^^^
help: remove the extra argument
  |
5 -     takes_raw_ptr(&raw x);
5 +     takes_raw_ptr(&raw);
  |

Some errors have detailed explanations: E0061, E0425.
For more information about an error, try `rustc --explain E0061`.
error: could not compile `playground` (bin "playground") due to 3 previous errors

Desired output

Rationale and extra context

In this example, the user should have written &raw const x to obtain a raw pointer, and the compiler suggests doing so.

However, the compiler then recovers &raw x to &raw, x and proceeds to complain about passing 2 arguments to a function that takes 1 argument, which is confusing and unexpected because the user intended to pass 1 argument and didn't write that intervening comma.

This can be especially confusing when trying to pass raw pointers to FFI bindings that take many arguments, because it becomes difficult to determine whether the number of arguments is actually correct or not.

Other cases

Rust Version

rustc 1.95.0

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-parserArea: The lexing & parsing of Rust source code to an ASTD-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.D-verboseDiagnostics: Too much output caused by a single piece of incorrect code.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions