Skip to content

Commit d2af997

Browse files
authored
BE-198: HashQL: Track local declaration and types inside of the MIR (#8027)
1 parent 2fad0ad commit d2af997

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1602
-627
lines changed

Cargo.lock

Lines changed: 9 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

libs/@local/hashql/compiletest/src/suite/mir_reify.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,12 @@ use std::{
77

88
use hashql_ast::node::expr::Expr;
99
use hashql_core::{
10-
heap::Heap, id::IdVec, module::ModuleRegistry, span::SpanId, r#type::environment::Environment,
10+
heap::Heap,
11+
id::IdVec,
12+
module::ModuleRegistry,
13+
pretty::Formatter,
14+
span::SpanId,
15+
r#type::{TypeFormatter, TypeFormatterOptions, environment::Environment},
1116
};
1217
use hashql_hir::{context::HirContext, node::NodeData};
1318
use hashql_mir::{
@@ -98,10 +103,17 @@ impl Suite for MirReifySuite {
98103

99104
let (root, bodies) = mir_reify(heap, expr, &interner, &mut environment, diagnostics)?;
100105

106+
let formatter = Formatter::new(heap);
107+
let mut formatter = TypeFormatter::new(
108+
&formatter,
109+
&environment,
110+
TypeFormatterOptions::terse().with_qualified_opaque_names(true),
111+
);
101112
let mut text_format = TextFormat {
102113
writer: Vec::new(),
103114
indent: 4,
104115
sources: bodies.as_slice(),
116+
types: &mut formatter,
105117
};
106118
text_format
107119
.format(&bodies, &[root])
@@ -128,7 +140,7 @@ impl Suite for MirReifySuite {
128140

129141
let now = Instant::now();
130142
let mut child = Command::new("d2")
131-
.args(["-l", "elk", "--stdout-format", "svg", "-"])
143+
.args(["-l", "elk", "-b=false", "--stdout-format", "svg", "-"])
132144
.stdin(Stdio::piped())
133145
.stdout(Stdio::piped())
134146
.spawn()
@@ -151,6 +163,7 @@ impl Suite for MirReifySuite {
151163
sources: bodies.as_slice(),
152164
dataflow: (),
153165
buffer: D2Buffer::default(),
166+
types: formatter,
154167
};
155168
d2_format
156169
.format(&bodies, &[root])

libs/@local/hashql/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ text-size = { workspace = true, public = true }
2323
# Private workspace dependencies
2424

2525
# Private third-party dependencies
26+
anstyle-lossy = { workspace = true }
2627
bitvec = { workspace = true, features = ["alloc"] }
2728
bumpalo = { workspace = true }
2829
derive_more = { workspace = true, features = ["debug", "from"] }

libs/@local/hashql/core/src/pretty/mod.rs

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! ```rust
1717
//! use hashql_core::{
1818
//! heap::Heap,
19-
//! pretty::{Format, Formatter, RenderOptions},
19+
//! pretty::{Formatter, RenderOptions},
2020
//! symbol::sym,
2121
//! };
2222
//!
@@ -44,19 +44,21 @@ mod write;
4444
use core::fmt::{self, Display};
4545
use std::io;
4646

47-
use self::write::{AnsiWriter, PlainWriter, RenderError};
47+
use self::write::{AnsiWriter, HtmlFragmentWriter, PlainWriter, RenderError, XmlEscapingWriter};
4848
pub use self::{
4949
formatter::{Doc, Formatter, FormatterOptions},
5050
semantic::Semantic,
5151
};
5252

5353
/// Output format for rendered documents.
5454
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
55-
pub enum Format {
55+
pub enum RenderFormat {
5656
/// Plain text without ANSI escape codes.
5757
Plain,
5858
/// ANSI-colored output for terminals.
5959
Ansi,
60+
/// Output as a series of HTML text spans.
61+
HtmlFragment,
6062
}
6163

6264
/// Configuration for rendering documents to output.
@@ -65,7 +67,7 @@ pub enum Format {
6567
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6668
pub struct RenderOptions {
6769
/// Whether to use ANSI colors or plain text.
68-
pub format: Format,
70+
pub format: RenderFormat,
6971
/// Maximum line width before wrapping (default: 80).
7072
pub max_width: usize,
7173
}
@@ -76,7 +78,7 @@ impl RenderOptions {
7678
/// Enables terminal colors and syntax highlighting in the rendered output.
7779
#[must_use]
7880
pub const fn with_ansi(mut self) -> Self {
79-
self.format = Format::Ansi;
81+
self.format = RenderFormat::Ansi;
8082
self
8183
}
8284

@@ -86,7 +88,7 @@ impl RenderOptions {
8688
/// non-terminal output.
8789
#[must_use]
8890
pub const fn with_plain(mut self) -> Self {
89-
self.format = Format::Plain;
91+
self.format = RenderFormat::Plain;
9092
self
9193
}
9294

@@ -104,7 +106,7 @@ impl RenderOptions {
104106
impl Default for RenderOptions {
105107
fn default() -> Self {
106108
Self {
107-
format: Format::Ansi,
109+
format: RenderFormat::Ansi,
108110
max_width: 80,
109111
}
110112
}
@@ -121,27 +123,35 @@ pub fn render_into<W>(doc: &Doc<'_>, options: RenderOptions, write: &mut W) -> i
121123
where
122124
W: io::Write,
123125
{
126+
let mut writer = PlainWriter::new_io(write);
127+
124128
match options.format {
125-
Format::Plain => doc
126-
.render_raw(options.max_width, &mut PlainWriter::new_io(write))
127-
.map_err(RenderError::into_io),
128-
Format::Ansi => doc
129-
.render_raw(options.max_width, &mut AnsiWriter::new_io(write))
130-
.map_err(RenderError::into_io),
129+
RenderFormat::Plain => doc.render_raw(options.max_width, &mut writer),
130+
RenderFormat::Ansi => doc.render_raw(options.max_width, &mut AnsiWriter::new(writer)),
131+
RenderFormat::HtmlFragment => doc.render_raw(
132+
options.max_width,
133+
&mut XmlEscapingWriter::new(HtmlFragmentWriter::new(writer)),
134+
),
131135
}
136+
.map_err(RenderError::into_io)
132137
}
133138

134139
/// Get a displayable representation.
135140
///
136141
/// Returns a [`Display`] implementor for using in formatting contexts.
137142
#[must_use]
138143
pub fn render(doc: Doc<'_>, options: RenderOptions) -> impl Display {
139-
fmt::from_fn(move |fmt| match options.format {
140-
Format::Plain => doc
141-
.render_raw(options.max_width, &mut PlainWriter::new_fmt(fmt))
142-
.map_err(RenderError::into_fmt),
143-
Format::Ansi => doc
144-
.render_raw(options.max_width, &mut AnsiWriter::new_fmt(fmt))
145-
.map_err(RenderError::into_fmt),
144+
fmt::from_fn(move |fmt| {
145+
let mut writer = PlainWriter::new_fmt(fmt);
146+
147+
match options.format {
148+
RenderFormat::Plain => doc.render_raw(options.max_width, &mut writer),
149+
RenderFormat::Ansi => doc.render_raw(options.max_width, &mut AnsiWriter::new(writer)),
150+
RenderFormat::HtmlFragment => doc.render_raw(
151+
options.max_width,
152+
&mut XmlEscapingWriter::new(HtmlFragmentWriter::new(writer)),
153+
),
154+
}
155+
.map_err(RenderError::into_fmt)
146156
})
147157
}

0 commit comments

Comments
 (0)