Skip to content

Commit 184d7c2

Browse files
committed
flow-control: Add diagnostics mechanism.
1 parent 1faeb69 commit 184d7c2

File tree

5 files changed

+85
-14
lines changed

5 files changed

+85
-14
lines changed

crates/cairo-lang-lowering/src/lower/flow_control/create_graph.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ mod patterns;
1717

1818
/// Creates a graph node for [semantic::ExprIf].
1919
pub fn create_graph_expr_if<'db>(
20-
ctx: &LoweringContext<'db, '_>,
20+
ctx: &mut LoweringContext<'db, '_>,
2121
expr: &semantic::ExprIf<'db>,
2222
) -> FlowControlGraph<'db> {
2323
let mut graph = FlowControlGraphBuilder::default();
@@ -102,13 +102,13 @@ pub fn create_graph_expr_if<'db>(
102102
}
103103
}
104104

105-
graph.finalize(current_node)
105+
graph.finalize(current_node, ctx)
106106
}
107107

108108
/// Creates a graph node for [semantic::ExprMatch].
109109
#[allow(dead_code)]
110110
pub fn create_graph_expr_match<'db>(
111-
ctx: &LoweringContext<'db, '_>,
111+
ctx: &mut LoweringContext<'db, '_>,
112112
expr: &semantic::ExprMatch<'db>,
113113
) -> FlowControlGraph<'db> {
114114
let mut graph = FlowControlGraphBuilder::default();
@@ -164,5 +164,5 @@ pub fn create_graph_expr_match<'db>(
164164
next: match_node_id,
165165
}));
166166

167-
graph.finalize(root)
167+
graph.finalize(root, ctx)
168168
}

crates/cairo-lang-lowering/src/lower/flow_control/graph.rs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,18 @@
2121
2222
use std::fmt::Debug;
2323

24+
use cairo_lang_diagnostics::DiagnosticAdded;
2425
use cairo_lang_semantic::{self as semantic, ConcreteVariant, PatternVariable};
2526
use cairo_lang_syntax::node::ast::ExprPtr;
27+
use cairo_lang_syntax::node::ids::SyntaxStablePtrId;
2628
use cairo_lang_utils::unordered_hash_set::UnorderedHashSet;
2729
use itertools::Itertools;
2830

31+
use crate::diagnostic::{
32+
LoweringDiagnosticKind, LoweringDiagnostics, LoweringDiagnosticsBuilder,
33+
};
2934
use crate::ids::LocationId;
35+
use crate::lower::context::LoweringContext;
3036

3137
/// Represents a variable in the flow control graph.
3238
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
@@ -260,6 +266,8 @@ impl<'db> Debug for FlowControlGraph<'db> {
260266
pub struct FlowControlGraphBuilder<'db> {
261267
graph: FlowControlGraph<'db>,
262268
used_vars: UnorderedHashSet<FlowControlVar>,
269+
/// Current emitted diagnostics.
270+
diagnostics: LoweringDiagnostics<'db>,
263271
}
264272

265273
impl<'db> FlowControlGraphBuilder<'db> {
@@ -280,8 +288,15 @@ impl<'db> FlowControlGraphBuilder<'db> {
280288
}
281289

282290
/// Finalizes the graph and returns the final [FlowControlGraph].
283-
pub fn finalize(self, root: NodeId) -> FlowControlGraph<'db> {
291+
///
292+
/// Adds the reported diagnostics to the context.
293+
pub fn finalize(
294+
self,
295+
root: NodeId,
296+
ctx: &mut LoweringContext<'db, '_>,
297+
) -> FlowControlGraph<'db> {
284298
assert_eq!(root.0, self.graph.size() - 1, "The root must be the last node.");
299+
ctx.diagnostics.extend(self.diagnostics.build());
285300
self.graph
286301
}
287302

@@ -308,6 +323,16 @@ impl<'db> FlowControlGraphBuilder<'db> {
308323
pub fn var_ty(&self, input_var: FlowControlVar) -> semantic::TypeId<'db> {
309324
self.graph.var_types[input_var.0]
310325
}
326+
327+
/// Reports a diagnostic.
328+
#[expect(dead_code)]
329+
pub fn report(
330+
&mut self,
331+
stable_ptr: impl Into<SyntaxStablePtrId<'db>>,
332+
kind: LoweringDiagnosticKind<'db>,
333+
) -> DiagnosticAdded {
334+
self.diagnostics.report(stable_ptr, kind)
335+
}
311336
}
312337

313338
impl<'db> Default for FlowControlGraphBuilder<'db> {
@@ -318,6 +343,10 @@ impl<'db> Default for FlowControlGraphBuilder<'db> {
318343
var_locations: Vec::new(),
319344
pattern_vars: Vec::new(),
320345
};
321-
Self { graph, used_vars: UnorderedHashSet::default() }
346+
Self {
347+
graph,
348+
used_vars: UnorderedHashSet::default(),
349+
diagnostics: LoweringDiagnostics::default(),
350+
}
322351
}
323352
}

crates/cairo-lang-lowering/src/lower/flow_control/graph_test.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,39 @@ fn test_create_graph(
5959
let mut encapsulating_ctx =
6060
create_encapsulating_ctx(db, test_function.function_id, &test_function.signature);
6161

62-
let ctx = create_lowering_context(
62+
let mut ctx = create_lowering_context(
6363
db,
6464
test_function.function_id,
6565
&test_function.signature,
6666
&mut encapsulating_ctx,
6767
);
6868

6969
let graph = match &expr {
70-
semantic::Expr::If(expr) => create_graph_expr_if(&ctx, expr),
71-
semantic::Expr::Match(expr) => create_graph_expr_match(&ctx, expr),
70+
semantic::Expr::If(expr) => create_graph_expr_if(&mut ctx, expr),
71+
semantic::Expr::Match(expr) => create_graph_expr_match(&mut ctx, expr),
7272
_ => {
7373
panic!("Unsupported expression: {:?}", expr.debug(&expr_formatter));
7474
}
7575
};
7676

77-
let error = verify_diagnostics_expectation(args, &semantic_diagnostics);
78-
7977
// Lower the graph.
80-
let lowered_str = if args.get("skip_lowering").unwrap_or(&"false".into()) == "true" {
81-
"".into()
78+
let (lowered_str, lowering_diagnostics) = if args.get("skip_lowering").unwrap_or(&"false".into()) == "true" {
79+
("".into(), ctx.diagnostics.build().format(db))
8280
} else {
8381
let lowered = lower_graph_as_function(ctx, expr_id, &graph);
84-
formatted_lowered(db, Some(&lowered))
82+
(formatted_lowered(db, Some(&lowered)), lowered.diagnostics.format(db))
8583
};
8684

85+
let error = verify_diagnostics_expectation(
86+
args,
87+
&format!("{semantic_diagnostics}{lowering_diagnostics}"),
88+
);
89+
8790
TestRunnerResult {
8891
outputs: OrderedHashMap::from([
8992
("graph".into(), format!("{graph:?}")),
9093
("semantic_diagnostics".into(), semantic_diagnostics),
94+
("lowering_diagnostics".into(), lowering_diagnostics),
9195
("lowered".into(), lowered_str),
9296
]),
9397
error,

crates/cairo-lang-lowering/src/lower/flow_control/test_data/if

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Root: 3
2121

2222
//! > semantic_diagnostics
2323

24+
//! > lowering_diagnostics
25+
2426
//! > lowered
2527
Parameters: v0: core::bool
2628
blk0 (root):
@@ -74,6 +76,8 @@ Root: 3
7476

7577
//! > semantic_diagnostics
7678

79+
//! > lowering_diagnostics
80+
7781
//! > lowered
7882
Parameters: v0: core::bool
7983
blk0 (root):
@@ -132,6 +136,8 @@ Root: 6
132136

133137
//! > semantic_diagnostics
134138

139+
//! > lowering_diagnostics
140+
135141
//! > lowered
136142
Parameters: v0: core::felt252, v1: core::felt252
137143
blk0 (root):
@@ -221,6 +227,8 @@ Root: 7
221227

222228
//! > semantic_diagnostics
223229

230+
//! > lowering_diagnostics
231+
224232
//! > lowered
225233
Parameters: v0: core::felt252, v1: test::MyStruct
226234
blk0 (root):
@@ -320,6 +328,8 @@ Root: 3
320328

321329
//! > semantic_diagnostics
322330

331+
//! > lowering_diagnostics
332+
323333
//! > lowered
324334
Parameters:
325335
blk0 (root):
@@ -361,6 +371,8 @@ Root: 3
361371

362372
//! > semantic_diagnostics
363373

374+
//! > lowering_diagnostics
375+
364376
//! > lowered
365377
Parameters:
366378
blk0 (root):
@@ -423,6 +435,8 @@ Root: 3
423435

424436
//! > semantic_diagnostics
425437

438+
//! > lowering_diagnostics
439+
426440
//! > lowered
427441
Parameters:
428442
blk0 (root):
@@ -481,6 +495,8 @@ Root: 4
481495

482496
//! > semantic_diagnostics
483497

498+
//! > lowering_diagnostics
499+
484500
//! > lowered
485501
Parameters: v0: core::option::Option::<core::option::Option::<core::felt252>>
486502
blk0 (root):
@@ -555,6 +571,8 @@ Root: 5
555571

556572
//! > semantic_diagnostics
557573

574+
//! > lowering_diagnostics
575+
558576
//! > lowered
559577
Parameters:
560578
blk0 (root):

crates/cairo-lang-lowering/src/lower/flow_control/test_data/match

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ Root: 4
3131

3232
//! > semantic_diagnostics
3333

34+
//! > lowering_diagnostics
35+
3436
//! > lowered
3537
Parameters: v0: test::Color
3638
blk0 (root):
@@ -124,6 +126,8 @@ Root: 7
124126

125127
//! > semantic_diagnostics
126128

129+
//! > lowering_diagnostics
130+
127131
//! > lowered
128132
Parameters: v0: test::Color
129133
blk0 (root):
@@ -204,6 +208,8 @@ Root: 6
204208

205209
//! > semantic_diagnostics
206210

211+
//! > lowering_diagnostics
212+
207213
//! > lowered
208214
Parameters: v0: (test::Color, test::Color)
209215
blk0 (root):
@@ -303,6 +309,8 @@ Root: 7
303309

304310
//! > semantic_diagnostics
305311

312+
//! > lowering_diagnostics
313+
306314
//! > lowered
307315
Parameters: v0: core::option::Option::<(test::Color, test::Color)>
308316
blk0 (root):
@@ -402,6 +410,8 @@ Root: 10
402410

403411
//! > semantic_diagnostics
404412

413+
//! > lowering_diagnostics
414+
405415
//! > lowered
406416

407417
//! > ==========================================================================
@@ -439,6 +449,8 @@ Root: 4
439449

440450
//! > semantic_diagnostics
441451

452+
//! > lowering_diagnostics
453+
442454
//! > lowered
443455

444456
//! > ==========================================================================
@@ -469,6 +481,8 @@ Root: 6
469481

470482
//! > semantic_diagnostics
471483

484+
//! > lowering_diagnostics
485+
472486
//! > lowered
473487
Parameters: v0: core::felt252
474488
blk0 (root):
@@ -574,6 +588,8 @@ Root: 18
574588

575589
//! > semantic_diagnostics
576590

591+
//! > lowering_diagnostics
592+
577593
//! > lowered
578594
Parameters: v0: core::felt252, v1: core::felt252
579595
blk0 (root):
@@ -787,6 +803,8 @@ Root: 7
787803

788804
//! > semantic_diagnostics
789805

806+
//! > lowering_diagnostics
807+
790808
//! > lowered
791809
Parameters: v0: @(test::Color, test::Color)
792810
blk0 (root):
@@ -890,6 +908,8 @@ Root: 7
890908

891909
//! > semantic_diagnostics
892910

911+
//! > lowering_diagnostics
912+
893913
//! > lowered
894914
Parameters: v0: (test::Color, test::Color)
895915
blk0 (root):

0 commit comments

Comments
 (0)