Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions compiler/rustc_hir_pretty/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -1137,16 +1137,40 @@ 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*/"),
ConstArgKind::Infer(..) => self.word("_"),
}
}

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);
Expand Down
33 changes: 33 additions & 0 deletions tests/ui/unpretty/struct-exprs-tuple-call-pretty-printing.rs
Original file line number Diff line number Diff line change
@@ -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() {}
39 changes: 39 additions & 0 deletions tests/ui/unpretty/struct-exprs-tuple-call-pretty-printing.stdout
Original file line number Diff line number Diff line change
@@ -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() { }
Loading