Skip to content

Commit 18a0d8e

Browse files
committed
add parser check for pointer typo
1 parent 54c5812 commit 18a0d8e

File tree

4 files changed

+556
-0
lines changed

4 files changed

+556
-0
lines changed

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,34 @@ impl<'a> Parser<'a> {
276276
) -> PResult<'a, Box<Ty>> {
277277
let allow_qpath_recovery = recover_qpath == RecoverQPath::Yes;
278278
maybe_recover_from_interpolated_ty_qpath!(self, allow_qpath_recovery);
279+
280+
if (self.token.is_keyword(kw::Const) || self.token.is_keyword(kw::Mut))
281+
&& self.look_ahead(1, |t| *t == token::Star)
282+
{
283+
let kw_span = self.token.span;
284+
self.bump();
285+
286+
if self.eat(exp!(Star)) {
287+
let star_span = self.prev_token.span;
288+
289+
let _ = self.parse_ty_no_question_mark_recover();
290+
291+
let guar = self
292+
.dcx()
293+
.struct_span_err(
294+
kw_span,
295+
"raw pointer type must have a star before the mutability keyword",
296+
)
297+
.with_multipart_suggestion(
298+
"move it to the other side",
299+
vec![(star_span, String::new()), (kw_span.shrink_to_lo(), "*".to_string())],
300+
Applicability::MachineApplicable,
301+
)
302+
.emit();
303+
304+
return Ok(self.mk_ty(kw_span.to(star_span), TyKind::Err(guar)));
305+
}
306+
}
279307
if self.token == token::Pound && self.look_ahead(1, |t| *t == token::OpenBracket) {
280308
let attrs_wrapper = self.parse_outer_attributes()?;
281309
let raw_attrs = attrs_wrapper.take_for_recovery(self.psess);
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//@ run-rustfix
2+
3+
#![allow(unused)]
4+
5+
pub const P1: *const u8 = 0 as _;
6+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
7+
//~| HELP: move it to the other side
8+
9+
pub const P2: *mut u8 = 1 as _;
10+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
11+
//~| HELP: move it to the other side
12+
13+
pub const P3: *const i32 = std::ptr::null();
14+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
15+
//~| HELP: move it to the other side
16+
17+
pub const P4: *const i32 = std::ptr::null();
18+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
19+
//~| HELP: move it to the other side
20+
21+
pub const P5: *mut i32 = std::ptr::null_mut();
22+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
23+
//~| HELP: move it to the other side
24+
25+
pub const P6: *mut i32 = std::ptr::null_mut();
26+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
27+
//~| HELP: move it to the other side
28+
29+
pub const P7: *const Vec<u8> = std::ptr::null();
30+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
31+
//~| HELP: move it to the other side
32+
33+
pub const P8: *const std::collections::HashMap<String, i32> = std::ptr::null();
34+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
35+
//~| HELP: move it to the other side
36+
37+
fn func1(p: *const u8) {}
38+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
39+
//~| HELP: move it to the other side
40+
41+
fn func2(p: *mut u8) {}
42+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
43+
//~| HELP: move it to the other side
44+
45+
fn func3() -> *const u8 { std::ptr::null() }
46+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
47+
//~| HELP: move it to the other side
48+
49+
fn func4() -> *mut u8 { std::ptr::null_mut() }
50+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
51+
//~| HELP: move it to the other side
52+
53+
struct S1 {
54+
field: *const u8,
55+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
56+
//~| HELP: move it to the other side
57+
}
58+
59+
struct S2 {
60+
field: *mut u8,
61+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
62+
//~| HELP: move it to the other side
63+
}
64+
65+
type Tuple1 = (*const u8, i32);
66+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
67+
//~| HELP: move it to the other side
68+
69+
type Tuple2 = (*mut u8, i32);
70+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
71+
//~| HELP: move it to the other side
72+
73+
type Array1 = [*const u8; 10];
74+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
75+
//~| HELP: move it to the other side
76+
77+
type Array2 = [*mut u8; 10];
78+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
79+
//~| HELP: move it to the other side
80+
81+
type Alias1 = *const u8;
82+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
83+
//~| HELP: move it to the other side
84+
85+
type Alias2 = *mut u8;
86+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
87+
//~| HELP: move it to the other side
88+
89+
pub const P9: *const u8 = std::ptr::null();
90+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
91+
//~| HELP: move it to the other side
92+
93+
pub const P10: *const u8 = std::ptr::null();
94+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
95+
//~| HELP: move it to the other side
96+
97+
impl S1 {
98+
fn method(self, size: *const u32) {}
99+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
100+
//~| HELP: move it to the other side
101+
}
102+
103+
trait Trait1 {
104+
fn method(p: *const u8);
105+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
106+
//~| HELP: move it to the other side
107+
}
108+
109+
fn generic_func<T>() -> *const T { std::ptr::null() }
110+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
111+
//~| HELP: move it to the other side
112+
113+
fn main() {}
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//@ run-rustfix
2+
3+
#![allow(unused)]
4+
5+
pub const P1: const* u8 = 0 as _;
6+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
7+
//~| HELP: move it to the other side
8+
9+
pub const P2: mut* u8 = 1 as _;
10+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
11+
//~| HELP: move it to the other side
12+
13+
pub const P3: const* i32 = std::ptr::null();
14+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
15+
//~| HELP: move it to the other side
16+
17+
pub const P4: const* i32 = std::ptr::null();
18+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
19+
//~| HELP: move it to the other side
20+
21+
pub const P5: mut* i32 = std::ptr::null_mut();
22+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
23+
//~| HELP: move it to the other side
24+
25+
pub const P6: mut* i32 = std::ptr::null_mut();
26+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
27+
//~| HELP: move it to the other side
28+
29+
pub const P7: const* Vec<u8> = std::ptr::null();
30+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
31+
//~| HELP: move it to the other side
32+
33+
pub const P8: const* std::collections::HashMap<String, i32> = std::ptr::null();
34+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
35+
//~| HELP: move it to the other side
36+
37+
fn func1(p: const* u8) {}
38+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
39+
//~| HELP: move it to the other side
40+
41+
fn func2(p: mut* u8) {}
42+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
43+
//~| HELP: move it to the other side
44+
45+
fn func3() -> const* u8 { std::ptr::null() }
46+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
47+
//~| HELP: move it to the other side
48+
49+
fn func4() -> mut* u8 { std::ptr::null_mut() }
50+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
51+
//~| HELP: move it to the other side
52+
53+
struct S1 {
54+
field: const* u8,
55+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
56+
//~| HELP: move it to the other side
57+
}
58+
59+
struct S2 {
60+
field: mut* u8,
61+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
62+
//~| HELP: move it to the other side
63+
}
64+
65+
type Tuple1 = (const* u8, i32);
66+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
67+
//~| HELP: move it to the other side
68+
69+
type Tuple2 = (mut* u8, i32);
70+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
71+
//~| HELP: move it to the other side
72+
73+
type Array1 = [const* u8; 10];
74+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
75+
//~| HELP: move it to the other side
76+
77+
type Array2 = [mut* u8; 10];
78+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
79+
//~| HELP: move it to the other side
80+
81+
type Alias1 = const* u8;
82+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
83+
//~| HELP: move it to the other side
84+
85+
type Alias2 = mut* u8;
86+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
87+
//~| HELP: move it to the other side
88+
89+
pub const P9: const *u8 = std::ptr::null();
90+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
91+
//~| HELP: move it to the other side
92+
93+
pub const P10: const * u8 = std::ptr::null();
94+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
95+
//~| HELP: move it to the other side
96+
97+
impl S1 {
98+
fn method(self, size: const* u32) {}
99+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
100+
//~| HELP: move it to the other side
101+
}
102+
103+
trait Trait1 {
104+
fn method(p: const* u8);
105+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
106+
//~| HELP: move it to the other side
107+
}
108+
109+
fn generic_func<T>() -> const* T { std::ptr::null() }
110+
//~^ ERROR: raw pointer type must have a star before the mutability keyword
111+
//~| HELP: move it to the other side
112+
113+
fn main() {}

0 commit comments

Comments
 (0)