Skip to content

Commit

Permalink
Rollup merge of rust-lang#102284 - compiler-errors:missing-type-in-ra…
Browse files Browse the repository at this point in the history
…w-ptr, r=davidtwco

Structured suggestion for missing `mut`/`const` in raw pointer

Fixes rust-lang#102261
  • Loading branch information
Dylan-DPC authored Sep 27, 2022
2 parents c168af8 + 594134d commit 79de0e4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 8 deletions.
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(
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

0 comments on commit 79de0e4

Please sign in to comment.