Skip to content

Commit 85c8e41

Browse files
committed
add pretty printing
1 parent 05afcb6 commit 85c8e41

File tree

3 files changed

+102
-6
lines changed

3 files changed

+102
-6
lines changed

‎compiler/rustc_hir_pretty/src/lib.rs‎

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ use rustc_ast_pretty::pprust::state::MacHeader;
1717
use rustc_ast_pretty::pprust::{Comments, PrintState};
1818
use rustc_hir::attrs::{AttributeKind, PrintAttribute};
1919
use rustc_hir::{
20-
BindingMode, ByRef, ConstArgKind, GenericArg, GenericBound, GenericParam, GenericParamKind,
21-
HirId, ImplicitSelfKind, LifetimeParamKind, Node, PatKind, PreciseCapturingArg, RangeEnd, Term,
22-
TyPatKind,
20+
BindingMode, ByRef, ConstArg, ConstArgExprField, ConstArgKind, GenericArg, GenericBound,
21+
GenericParam, GenericParamKind, HirId, ImplicitSelfKind, LifetimeParamKind, Node, PatKind,
22+
PreciseCapturingArg, RangeEnd, Term, TyPatKind,
2323
};
2424
use rustc_span::source_map::SourceMap;
2525
use rustc_span::{FileName, Ident, Span, Symbol, kw, sym};
@@ -1137,16 +1137,40 @@ impl<'a> State<'a> {
11371137

11381138
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
11391139
match &const_arg.kind {
1140-
// FIXME(mgca): proper printing for struct exprs
1141-
ConstArgKind::Struct(..) => self.word("/* STRUCT EXPR */"),
1142-
ConstArgKind::TupleCall(..) => self.word("/* TUPLE CALL */"),
1140+
ConstArgKind::Struct(qpath, fields) => self.print_const_struct(qpath, fields),
1141+
ConstArgKind::TupleCall(qpath, args) => self.print_const_ctor(qpath, args),
11431142
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
11441143
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
11451144
ConstArgKind::Error(_, _) => self.word("/*ERROR*/"),
11461145
ConstArgKind::Infer(..) => self.word("_"),
11471146
}
11481147
}
11491148

1149+
fn print_const_struct(&mut self, qpath: &hir::QPath<'_>, fields: &&[&ConstArgExprField<'_>]) {
1150+
self.print_qpath(qpath, true);
1151+
self.word(" ");
1152+
self.word("{");
1153+
if !fields.is_empty() {
1154+
self.nbsp();
1155+
}
1156+
self.commasep(Inconsistent, *fields, |s, field| {
1157+
s.word(field.field.as_str().to_string());
1158+
s.word(":");
1159+
s.nbsp();
1160+
s.print_const_arg(field.expr);
1161+
});
1162+
self.word("}");
1163+
}
1164+
1165+
fn print_const_ctor(&mut self, qpath: &hir::QPath<'_>, args: &&[&ConstArg<'_, ()>]) {
1166+
self.print_qpath(qpath, true);
1167+
self.word("(");
1168+
self.commasep(Inconsistent, *args, |s, arg| {
1169+
s.print_const_arg(arg);
1170+
});
1171+
self.word(")");
1172+
}
1173+
11501174
fn print_call_post(&mut self, args: &[hir::Expr<'_>]) {
11511175
self.popen();
11521176
self.commasep_exprs(Inconsistent, args);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//@ compile-flags: -Zunpretty=hir
2+
//@ check-pass
3+
4+
#![feature(min_generic_const_args, adt_const_params)]
5+
#![expect(incomplete_features)]
6+
#![allow(dead_code)]
7+
8+
use std::marker::ConstParamTy;
9+
10+
struct Point(u32, u32);
11+
12+
struct Point3();
13+
14+
struct Point1 {
15+
a: u32,
16+
b: u32,
17+
}
18+
19+
struct Point2 {}
20+
21+
fn with_point<const P: Point>() {}
22+
fn with_point1<const P: Point1>() {}
23+
fn with_point2<const P: Point2>() {}
24+
fn with_point3<const P: Point3>() {}
25+
26+
fn test<const N: u32>() {
27+
with_point::<{ Point(N, N) }>();
28+
with_point1::<{ Point1 { a: N, b: N } }>();
29+
with_point2::<{ Point2 {} }>();
30+
with_point3::<{ Point3() }>();
31+
}
32+
33+
fn main() {}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//@ compile-flags: -Zunpretty=hir
2+
//@ check-pass
3+
4+
#![feature(min_generic_const_args, adt_const_params)]
5+
#![expect(incomplete_features)]
6+
#![allow(dead_code)]
7+
#[attr = MacroUse {arguments: UseAll}]
8+
extern crate std;
9+
#[prelude_import]
10+
use ::std::prelude::rust_2015::*;
11+
12+
use std::marker::ConstParamTy;
13+
14+
struct Point(u32, u32);
15+
16+
struct Point3();
17+
18+
struct Point1 {
19+
a: u32,
20+
b: u32,
21+
}
22+
23+
struct Point2 {
24+
}
25+
26+
fn with_point<const P: Point>() { }
27+
fn with_point1<const P: Point1>() { }
28+
fn with_point2<const P: Point2>() { }
29+
fn with_point3<const P: Point3>() { }
30+
31+
fn test<const N:
32+
u32>() {
33+
with_point::<Point(N, N)>();
34+
with_point1::<Point1 { a: N, b: N}>();
35+
with_point2::<Point2 {}>();
36+
with_point3::<Point3()>();
37+
}
38+
39+
fn main() { }

0 commit comments

Comments
 (0)