-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pretty print records using field names #927
Changes from all commits
fdc4070
0196c30
39b6006
7ac4865
a70354d
bcaecc9
915e600
84bc2b9
bd735fe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use std::fmt; | ||
use std::{fmt, iter}; | ||
|
||
use expr::FieldBind; | ||
use flux_rustc_bridge::ty::region_to_string; | ||
use rustc_type_ir::DebruijnIndex; | ||
use ty::{UnevaluatedConst, ValTree}; | ||
|
@@ -235,6 +236,28 @@ impl Pretty for SubsetTy { | |
} | ||
} | ||
|
||
// This is a trick to avoid pretty printing `S [S { x: 10, y: 20}]` | ||
// and instead just print `S[{x: 10, y: 20}]` for struct-valued indices. | ||
struct IdxFmt(Expr); | ||
|
||
impl Pretty for IdxFmt { | ||
fn fmt(&self, cx: &PrettyCx, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
define_scoped!(cx, f); | ||
let e = &self.0; | ||
let e = if cx.simplify_exprs { e.simplify() } else { e.clone() }; | ||
if let ExprKind::Aggregate(AggregateKind::Adt(def_id), flds) = e.kind() | ||
&& let Some(genv) = cx.genv | ||
&& let Ok(adt_sort_def) = genv.adt_sort_def_of(def_id) | ||
{ | ||
let field_binds = iter::zip(adt_sort_def.field_names(), flds) | ||
.map(|(name, value)| FieldBind { name: *name, value: value.clone() }); | ||
w!("{{ {:?} }}", join!(", ", field_binds)) | ||
} else { | ||
w!("{:?}", &self.0) | ||
} | ||
} | ||
} | ||
|
||
impl Pretty for Ty { | ||
fn fmt(&self, cx: &PrettyCx, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
define_scoped!(cx, f); | ||
|
@@ -249,7 +272,7 @@ impl Pretty for Ty { | |
w!("[]")?; | ||
} | ||
} else { | ||
w!("[{:?}]", idx)?; | ||
w!("[{:?}]", IdxFmt(idx.clone()))?; | ||
} | ||
Ok(()) | ||
} | ||
|
@@ -277,8 +300,19 @@ impl Pretty for Ty { | |
} | ||
TyKind::Param(param_ty) => w!("{}", ^param_ty), | ||
TyKind::Downcast(adt, .., variant_idx, fields) => { | ||
w!("{:?}::{}", adt.did(), ^adt.variant(*variant_idx).name)?; | ||
if !fields.is_empty() { | ||
let is_struct = adt.is_struct(); | ||
// base-name | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ponder: maybe
we should print
But perhaps the context that it is an But also There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, a bit unclear what the right thing here is... |
||
w!("{:?}", adt.did())?; | ||
// variant-name: if it is not a struct | ||
if !is_struct { | ||
w!("::{}", ^adt.variant(*variant_idx).name)?; | ||
} | ||
// fields: use curly-braces + names for structs, otherwise use parens | ||
if is_struct { | ||
let field_binds = iter::zip(&adt.variant(*variant_idx).fields, fields) | ||
.map(|(field_def, value)| FieldBind { name: field_def.name, value }); | ||
w!(" {{ {:?} }}", join!(", ", field_binds))?; | ||
} else if !fields.is_empty() { | ||
w!("({:?})", join!(", ", fields))?; | ||
} | ||
Ok(()) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.