Skip to content

Commit dd08360

Browse files
Rollup merge of #150695 - Kivooeo:pretty-printing, r=BoxyUwU
MGCA: pretty printing for struct expressions and tuple calls not sure 1. if there any tests that i need to adjust 2. if i should add any test for it 3. if humanity has come up with anything better than checking if that's first iteration or not with flag when printing sequences with separator in case there is no tests for it and i dont have to add any, there is a demonstration of this ✨ pretty ✨ printing (this is output from `-Z unpretty=hir`) ``` fn test_errors<const N: usize>() { // accepts_enum::<{ None::<u32> }>(); accepts_point::<Point1 { a: N, b: N }>(); accepts_point::<Point(N, N)>(); } ``` btw it does not print const block for this ``` accepts_point::<{ Point1 { a: const {N + 1}, b: N } }>(); ``` it will print ``` accepts_point::<Point1 { a: { N + 1 }, b: N }>(); ``` not sure if we want to print const blocks or not r? BoxyUwU
2 parents ce42a61 + 85c8e41 commit dd08360

3 files changed

Lines changed: 102 additions & 6 deletions

File tree

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::{DUMMY_SP, FileName, Ident, Span, Symbol, kw, sym};
@@ -1141,16 +1141,40 @@ impl<'a> State<'a> {
11411141

11421142
fn print_const_arg(&mut self, const_arg: &hir::ConstArg<'_>) {
11431143
match &const_arg.kind {
1144-
// FIXME(mgca): proper printing for struct exprs
1145-
ConstArgKind::Struct(..) => self.word("/* STRUCT EXPR */"),
1146-
ConstArgKind::TupleCall(..) => self.word("/* TUPLE CALL */"),
1144+
ConstArgKind::Struct(qpath, fields) => self.print_const_struct(qpath, fields),
1145+
ConstArgKind::TupleCall(qpath, args) => self.print_const_ctor(qpath, args),
11471146
ConstArgKind::Path(qpath) => self.print_qpath(qpath, true),
11481147
ConstArgKind::Anon(anon) => self.print_anon_const(anon),
11491148
ConstArgKind::Error(_, _) => self.word("/*ERROR*/"),
11501149
ConstArgKind::Infer(..) => self.word("_"),
11511150
}
11521151
}
11531152

1153+
fn print_const_struct(&mut self, qpath: &hir::QPath<'_>, fields: &&[&ConstArgExprField<'_>]) {
1154+
self.print_qpath(qpath, true);
1155+
self.word(" ");
1156+
self.word("{");
1157+
if !fields.is_empty() {
1158+
self.nbsp();
1159+
}
1160+
self.commasep(Inconsistent, *fields, |s, field| {
1161+
s.word(field.field.as_str().to_string());
1162+
s.word(":");
1163+
s.nbsp();
1164+
s.print_const_arg(field.expr);
1165+
});
1166+
self.word("}");
1167+
}
1168+
1169+
fn print_const_ctor(&mut self, qpath: &hir::QPath<'_>, args: &&[&ConstArg<'_, ()>]) {
1170+
self.print_qpath(qpath, true);
1171+
self.word("(");
1172+
self.commasep(Inconsistent, *args, |s, arg| {
1173+
s.print_const_arg(arg);
1174+
});
1175+
self.word(")");
1176+
}
1177+
11541178
fn print_call_post(&mut self, args: &[hir::Expr<'_>]) {
11551179
self.popen();
11561180
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)