Skip to content

Commit f8edc83

Browse files
committed
Pretty-print PatKind::Missing as _.
Printing "no pattern" as `_` isn't ideal, but better than crashing, and HIR pretty-printing already has plenty of imperfections. The added `f2` and `f6` examples are ones that triggered the crash. Note that some of the added examples are printed badly, e.g. `fn(, ...)`. The next commit will fix those. Fixes #139633.
1 parent 092a284 commit f8edc83

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

Diff for: compiler/rustc_hir_pretty/src/lib.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1871,10 +1871,11 @@ impl<'a> State<'a> {
18711871
fn print_pat(&mut self, pat: &hir::Pat<'_>) {
18721872
self.maybe_print_comment(pat.span.lo());
18731873
self.ann.pre(self, AnnNode::Pat(pat));
1874-
// Pat isn't normalized, but the beauty of it
1875-
// is that it doesn't matter
1874+
// Pat isn't normalized, but the beauty of it is that it doesn't matter.
18761875
match pat.kind {
1877-
PatKind::Missing => unreachable!(),
1876+
// Printing `_` isn't ideal for a missing pattern, but it's easy and good enough.
1877+
// E.g. `fn(u32)` gets printed as `fn(_: u32)`.
1878+
PatKind::Missing => self.word("_"),
18781879
PatKind::Wild => self.word("_"),
18791880
PatKind::Never => self.word("!"),
18801881
PatKind::Binding(BindingMode(by_ref, mutbl), _, ident, sub) => {

Diff for: tests/pretty/hir-fn-variadic.pp

+36
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,39 @@
1313
}
1414

1515
unsafe extern "C" fn bar(_: i32, mut va2: ...) -> usize { va2.arg::<usize>() }
16+
17+
fn main() {
18+
fn g1(_: extern "C" fn(_: u8, va: ...)) { }
19+
fn g2(_: extern "C" fn(_: u8, ...)) { }
20+
fn g3(_: extern "C" fn(u8, va: ...)) { }
21+
fn g4(_: extern "C" fn(u8, ...)) { }
22+
23+
fn g5(_: extern "C" fn(, va: ...)) { }
24+
fn g6(_: extern "C" fn(, ...)) { }
25+
26+
{
27+
let _ =
28+
{
29+
unsafe extern "C" fn f1(_: u8, va: ...) { }
30+
};
31+
};
32+
{
33+
let _ =
34+
{
35+
unsafe extern "C" fn f2(_: u8, _: ...) { }
36+
};
37+
};
38+
39+
{
40+
let _ =
41+
{
42+
unsafe extern "C" fn f5(, va: ...) { }
43+
};
44+
};
45+
{
46+
let _ =
47+
{
48+
unsafe extern "C" fn f6(, _: ...) { }
49+
};
50+
};
51+
}

Diff for: tests/pretty/hir-fn-variadic.rs

+16
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,19 @@ extern "C" {
1111
pub unsafe extern "C" fn bar(_: i32, mut va2: ...) -> usize {
1212
va2.arg::<usize>()
1313
}
14+
15+
fn main() {
16+
fn g1(_: extern "C" fn(_: u8, va: ...)) {}
17+
fn g2(_: extern "C" fn(_: u8, ...)) {}
18+
fn g3(_: extern "C" fn(u8, va: ...)) {}
19+
fn g4(_: extern "C" fn(u8, ...)) {}
20+
21+
fn g5(_: extern "C" fn(va: ...)) {}
22+
fn g6(_: extern "C" fn(...)) {}
23+
24+
_ = { unsafe extern "C" fn f1(_: u8, va: ...) {} };
25+
_ = { unsafe extern "C" fn f2(_: u8, ...) {} };
26+
27+
_ = { unsafe extern "C" fn f5(va: ...) {} };
28+
_ = { unsafe extern "C" fn f6(...) {} };
29+
}

0 commit comments

Comments
 (0)