Skip to content

Commit 5b39fa0

Browse files
committed
fix: emit diagnostic for unsupported items in statement position instead of panicking
Fixes #9785
1 parent e87ae95 commit 5b39fa0

File tree

2 files changed

+40
-21
lines changed

2 files changed

+40
-21
lines changed

crates/cairo-lang-semantic/src/expr/compute.rs

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4753,28 +4753,24 @@ pub fn compute_and_append_statement_semantic<'db>(
47534753
}
47544754
}
47554755
}
4756-
ast::ModuleItem::Module(_) => {
4757-
unreachable!("Modules are not supported inside a function.")
4758-
}
4759-
ast::ModuleItem::FreeFunction(_) => {
4760-
unreachable!("FreeFunction type not supported.")
4761-
}
4762-
ast::ModuleItem::ExternFunction(_) => {
4763-
unreachable!("ExternFunction type not supported.")
4764-
}
4765-
ast::ModuleItem::ExternType(_) => unreachable!("ExternType type not supported."),
4766-
ast::ModuleItem::Trait(_) => unreachable!("Trait type not supported."),
4767-
ast::ModuleItem::Impl(_) => unreachable!("Impl type not supported."),
4768-
ast::ModuleItem::ImplAlias(_) => unreachable!("ImplAlias type not supported."),
4769-
ast::ModuleItem::Struct(_) => unreachable!("Struct type not supported."),
4770-
ast::ModuleItem::Enum(_) => unreachable!("Enum type not supported."),
4771-
ast::ModuleItem::TypeAlias(_) => unreachable!("TypeAlias type not supported."),
4772-
ast::ModuleItem::InlineMacro(_) => unreachable!("InlineMacro type not supported."),
4773-
ast::ModuleItem::HeaderDoc(_) => unreachable!("HeaderDoc type not supported."),
4774-
ast::ModuleItem::MacroDeclaration(_) => {
4775-
unreachable!("MacroDeclaration type not supported.")
4756+
ast::ModuleItem::Module(_)
4757+
| ast::ModuleItem::FreeFunction(_)
4758+
| ast::ModuleItem::ExternFunction(_)
4759+
| ast::ModuleItem::ExternType(_)
4760+
| ast::ModuleItem::Trait(_)
4761+
| ast::ModuleItem::Impl(_)
4762+
| ast::ModuleItem::ImplAlias(_)
4763+
| ast::ModuleItem::Struct(_)
4764+
| ast::ModuleItem::Enum(_)
4765+
| ast::ModuleItem::TypeAlias(_)
4766+
| ast::ModuleItem::InlineMacro(_)
4767+
| ast::ModuleItem::HeaderDoc(_)
4768+
| ast::ModuleItem::MacroDeclaration(_)
4769+
| ast::ModuleItem::Missing(_) => {
4770+
return Err(ctx
4771+
.diagnostics
4772+
.report(stmt_item_syntax.stable_ptr(db), UnsupportedUseItemInStatement));
47764773
}
4777-
ast::ModuleItem::Missing(_) => unreachable!("Missing type not supported."),
47784774
}
47794775
statements.push(ctx.arenas.statements.alloc(semantic::Statement::Item(
47804776
semantic::StatementItem { stable_ptr: syntax.stable_ptr(db) },

crates/cairo-lang-semantic/src/expr/test_data/statements

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,26 @@ fn unstable_function_with_note() -> felt252 {
216216
//! > function_body
217217

218218
//! > expected_diagnostics
219+
220+
//! > ==========================================================================
221+
222+
//! > Type alias as statement inside a function body.
223+
224+
//! > test_runner_name
225+
test_function_diagnostics(expect_diagnostics: true)
226+
227+
//! > function_code
228+
fn foo() {
229+
type MyAlias = felt252;
230+
}
231+
232+
//! > function_name
233+
foo
234+
235+
//! > module_code
236+
237+
//! > expected_diagnostics
238+
error[E2075]: Unsupported use item in statement.
239+
--> lib.cairo:2:5
240+
type MyAlias = felt252;
241+
^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)