Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structured suggestion for missing mut/const in raw pointer #102284

Merged
merged 1 commit into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions compiler/rustc_parse/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,10 +397,13 @@ impl<'a> Parser<'a> {
fn parse_ty_ptr(&mut self) -> PResult<'a, TyKind> {
let mutbl = self.parse_const_or_mut().unwrap_or_else(|| {
let span = self.prev_token.span;
let msg = "expected mut or const in raw pointer type";
self.struct_span_err(span, msg)
.span_label(span, msg)
.help("use `*mut T` or `*const T` as appropriate")
self.struct_span_err(span, "expected `mut` or `const` keyword in raw pointer type")
.span_suggestions(
davidtwco marked this conversation as resolved.
Show resolved Hide resolved
span.shrink_to_hi(),
"add `mut` or `const` here",
["mut ".to_string(), "const ".to_string()].into_iter(),
Applicability::HasPlaceholders,
)
.emit();
Mutability::Not
});
Expand Down
2 changes: 1 addition & 1 deletion src/test/ui/parser/bad-pointer-type.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
fn foo(_: *()) {
//~^ ERROR expected mut or const in raw pointer type
//~^ ERROR expected `mut` or `const` keyword in raw pointer type
}

fn main() {}
11 changes: 8 additions & 3 deletions src/test/ui/parser/bad-pointer-type.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
error: expected mut or const in raw pointer type
error: expected `mut` or `const` keyword in raw pointer type
--> $DIR/bad-pointer-type.rs:1:11
|
LL | fn foo(_: *()) {
| ^ expected mut or const in raw pointer type
| ^
|
= help: use `*mut T` or `*const T` as appropriate
help: add `mut` or `const` here
|
LL | fn foo(_: *const ()) {
| +++++
LL | fn foo(_: *mut ()) {
| +++

error: aborting due to previous error

7 changes: 7 additions & 0 deletions src/test/ui/parser/double-pointer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let x: i32 = 5;
let ptr: *const i32 = &x;
let dptr: **const i32 = &ptr;
//~^ ERROR expected `mut` or `const` keyword in raw pointer type
//~| HELP add `mut` or `const` here
}
15 changes: 15 additions & 0 deletions src/test/ui/parser/double-pointer.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error: expected `mut` or `const` keyword in raw pointer type
--> $DIR/double-pointer.rs:4:15
|
LL | let dptr: **const i32 = &ptr;
| ^
|
help: add `mut` or `const` here
|
LL | let dptr: *const *const i32 = &ptr;
| +++++
LL | let dptr: *mut *const i32 = &ptr;
| +++

error: aborting due to previous error