Skip to content

Commit

Permalink
feat: better println for Quoted (#5896)
Browse files Browse the repository at this point in the history
# Description

## Problem

Resolves #5899

For debugging purposes, when you `println` a quoted value, some token
values aren't expanded, making it a bit harder to understand what's
going on.

## Summary

Now interned expressions are shown in `Quoted` values.

This program:

```rust
fn main() {
    comptime
    {
        let n = quote { [1, 2, 3] }.as_expr().unwrap();
        let q = quote { $n };
        println(q);
    }
}
```

Used to print:

```
quote { (expr) }
```

Now it prints: 

```
quote { [1, 2, 3] }
```

## Additional Context

None.

## Documentation

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[For Experimental Features]** Documentation to be submitted in a
separate PR.

# PR Checklist

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
asterite authored Sep 3, 2024
1 parent 4e616b3 commit 6f30e42
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions compiler/noirc_frontend/src/hir/comptime/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,11 +567,33 @@ impl<'value, 'interner> Display for ValuePrinter<'value, 'interner> {
Value::Quoted(tokens) => {
write!(f, "quote {{")?;
for token in tokens.iter() {
write!(f, " ")?;

match token {
Token::QuotedType(id) => {
write!(f, " {}", self.interner.get_quoted_type(*id))?;
write!(f, "{}", self.interner.get_quoted_type(*id))?;
}
Token::InternedExpr(id) => {
let value = Value::expression(ExpressionKind::Interned(*id));
value.display(self.interner).fmt(f)?;
}
Token::InternedStatement(id) => {
let value = Value::statement(StatementKind::Interned(*id));
value.display(self.interner).fmt(f)?;
}
Token::InternedLValue(id) => {
let value = Value::lvalue(LValue::Interned(*id, Span::default()));
value.display(self.interner).fmt(f)?;
}
other => write!(f, " {other}")?,
Token::InternedUnresolvedTypeData(id) => {
let value = Value::UnresolvedType(UnresolvedTypeData::Interned(*id));
value.display(self.interner).fmt(f)?;
}
Token::UnquoteMarker(id) => {
let value = Value::TypedExpr(TypedExpr::ExprId(*id));
value.display(self.interner).fmt(f)?;
}
other => write!(f, "{other}")?,
}
}
write!(f, " }}")
Expand Down Expand Up @@ -632,7 +654,16 @@ impl<'value, 'interner> Display for ValuePrinter<'value, 'interner> {
Value::Expr(ExprValue::LValue(lvalue)) => {
write!(f, "{}", remove_interned_in_lvalue(self.interner, lvalue.clone()))
}
Value::TypedExpr(_) => write!(f, "(typed expr)"),
Value::TypedExpr(TypedExpr::ExprId(id)) => {
let hir_expr = self.interner.expression(id);
let expr = hir_expr.to_display_ast(self.interner, Span::default());
write!(f, "{}", expr.kind)
}
Value::TypedExpr(TypedExpr::StmtId(id)) => {
let hir_statement = self.interner.statement(id);
let stmt = hir_statement.to_display_ast(self.interner, Span::default());
write!(f, "{}", stmt.kind)
}
Value::UnresolvedType(typ) => {
if let UnresolvedTypeData::Interned(id) = typ {
let typ = self.interner.get_unresolved_type_data(*id);
Expand Down

0 comments on commit 6f30e42

Please sign in to comment.