add pretty printing

This commit is contained in:
Kivooeo 2026-01-05 09:01:23 +00:00
parent 05afcb6d26
commit 85c8e41f62
3 changed files with 102 additions and 6 deletions

View file

@ -17,9 +17,9 @@ use rustc_ast_pretty::pprust::state::MacHeader;
use rustc_ast_pretty::pprust::{Comments, PrintState};
use rustc_hir::attrs::{AttributeKind, PrintAttribute};
use rustc_hir::{
BindingMode, ByRef, ConstArgKind, GenericArg, GenericBound, GenericParam, GenericParamKind,
HirId, ImplicitSelfKind, LifetimeParamKind, Node, PatKind, PreciseCapturingArg, RangeEnd, Term,
TyPatKind,
BindingMode, ByRef, ConstArg, ConstArgExprField, ConstArgKind, GenericArg, GenericBound,
GenericParam, GenericParamKind, HirId, ImplicitSelfKind, LifetimeParamKind, Node, PatKind,
PreciseCapturingArg, RangeEnd, Term, TyPatKind,
};
use rustc_span::source_map::SourceMap;
use rustc_span::{FileName, Ident, Span, Symbol, kw, sym};
@ -1137,9 +1137,8 @@ impl<'a> State<'a> {
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
match &const_arg.kind {
// FIXME(mgca): proper printing for struct exprs
ConstArgKind::Struct(..) => self.word("/* STRUCT EXPR */"),
ConstArgKind::TupleCall(..) => self.word("/* TUPLE CALL */"),
ConstArgKind::Struct(qpath, fields) => self.print_const_struct(qpath, fields),
ConstArgKind::TupleCall(qpath, args) => self.print_const_ctor(qpath, args),
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
ConstArgKind::Error(_, _) => self.word("/*ERROR*/"),
@ -1147,6 +1146,31 @@ impl<'a> State<'a> {
}
}
fn print_const_struct(&mut self, qpath: &hir::QPath<'_>, fields: &&[&ConstArgExprField<'_>]) {
self.print_qpath(qpath, true);
self.word(" ");
self.word("{");
if !fields.is_empty() {
self.nbsp();
}
self.commasep(Inconsistent, *fields, |s, field| {
s.word(field.field.as_str().to_string());
s.word(":");
s.nbsp();
s.print_const_arg(field.expr);
});
self.word("}");
}
fn print_const_ctor(&mut self, qpath: &hir::QPath<'_>, args: &&[&ConstArg<'_, ()>]) {
self.print_qpath(qpath, true);
self.word("(");
self.commasep(Inconsistent, *args, |s, arg| {
s.print_const_arg(arg);
});
self.word(")");
}
fn print_call_post(&mut self, args: &[hir::Expr<'_>]) {
self.popen();
self.commasep_exprs(Inconsistent, args);

View file

@ -0,0 +1,33 @@
//@ compile-flags: -Zunpretty=hir
//@ check-pass
#![feature(min_generic_const_args, adt_const_params)]
#![expect(incomplete_features)]
#![allow(dead_code)]
use std::marker::ConstParamTy;
struct Point(u32, u32);
struct Point3();
struct Point1 {
a: u32,
b: u32,
}
struct Point2 {}
fn with_point<const P: Point>() {}
fn with_point1<const P: Point1>() {}
fn with_point2<const P: Point2>() {}
fn with_point3<const P: Point3>() {}
fn test<const N: u32>() {
with_point::<{ Point(N, N) }>();
with_point1::<{ Point1 { a: N, b: N } }>();
with_point2::<{ Point2 {} }>();
with_point3::<{ Point3() }>();
}
fn main() {}

View file

@ -0,0 +1,39 @@
//@ compile-flags: -Zunpretty=hir
//@ check-pass
#![feature(min_generic_const_args, adt_const_params)]
#![expect(incomplete_features)]
#![allow(dead_code)]
#[attr = MacroUse {arguments: UseAll}]
extern crate std;
#[prelude_import]
use ::std::prelude::rust_2015::*;
use std::marker::ConstParamTy;
struct Point(u32, u32);
struct Point3();
struct Point1 {
a: u32,
b: u32,
}
struct Point2 {
}
fn with_point<const P: Point>() { }
fn with_point1<const P: Point1>() { }
fn with_point2<const P: Point2>() { }
fn with_point3<const P: Point3>() { }
fn test<const N:
u32>() {
with_point::<Point(N, N)>();
with_point1::<Point1 { a: N, b: N}>();
with_point2::<Point2 {}>();
with_point3::<Point3()>();
}
fn main() { }