Skip to content

Commit 8b635cb

Browse files
authored
Rollup merge of #102284 - compiler-errors:missing-type-in-raw-ptr, r=davidtwco
Structured suggestion for missing `mut`/`const` in raw pointer Fixes #102261
2 parents 8d2faa2 + 594134d commit 8b635cb

File tree

5 files changed

+38
-8
lines changed

5 files changed

+38
-8
lines changed

compiler/rustc_parse/src/parser/ty.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -397,10 +397,13 @@ impl<'a> Parser<'a> {
397397
fn parse_ty_ptr(&mut self) -> PResult<'a, TyKind> {
398398
let mutbl = self.parse_const_or_mut().unwrap_or_else(|| {
399399
let span = self.prev_token.span;
400-
let msg = "expected mut or const in raw pointer type";
401-
self.struct_span_err(span, msg)
402-
.span_label(span, msg)
403-
.help("use `*mut T` or `*const T` as appropriate")
400+
self.struct_span_err(span, "expected `mut` or `const` keyword in raw pointer type")
401+
.span_suggestions(
402+
span.shrink_to_hi(),
403+
"add `mut` or `const` here",
404+
["mut ".to_string(), "const ".to_string()].into_iter(),
405+
Applicability::HasPlaceholders,
406+
)
404407
.emit();
405408
Mutability::Not
406409
});
+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
fn foo(_: *()) {
2-
//~^ ERROR expected mut or const in raw pointer type
2+
//~^ ERROR expected `mut` or `const` keyword in raw pointer type
33
}
44

55
fn main() {}
+8-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
error: expected mut or const in raw pointer type
1+
error: expected `mut` or `const` keyword in raw pointer type
22
--> $DIR/bad-pointer-type.rs:1:11
33
|
44
LL | fn foo(_: *()) {
5-
| ^ expected mut or const in raw pointer type
5+
| ^
66
|
7-
= help: use `*mut T` or `*const T` as appropriate
7+
help: add `mut` or `const` here
8+
|
9+
LL | fn foo(_: *const ()) {
10+
| +++++
11+
LL | fn foo(_: *mut ()) {
12+
| +++
813

914
error: aborting due to previous error
1015

src/test/ui/parser/double-pointer.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
fn main() {
2+
let x: i32 = 5;
3+
let ptr: *const i32 = &x;
4+
let dptr: **const i32 = &ptr;
5+
//~^ ERROR expected `mut` or `const` keyword in raw pointer type
6+
//~| HELP add `mut` or `const` here
7+
}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error: expected `mut` or `const` keyword in raw pointer type
2+
--> $DIR/double-pointer.rs:4:15
3+
|
4+
LL | let dptr: **const i32 = &ptr;
5+
| ^
6+
|
7+
help: add `mut` or `const` here
8+
|
9+
LL | let dptr: *const *const i32 = &ptr;
10+
| +++++
11+
LL | let dptr: *mut *const i32 = &ptr;
12+
| +++
13+
14+
error: aborting due to previous error
15+

0 commit comments

Comments
 (0)