Skip to content

Commit d0640ec

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

File tree

3 files changed

+46
-21
lines changed

3 files changed

+46
-21
lines changed

crates/cairo-lang-semantic/src/diagnostic.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,6 +1199,9 @@ impl<'db> DiagnosticEntry<'db> for SemanticDiagnostic<'db> {
11991199
SemanticDiagnosticKind::OnlyTypeOrConstParamsInNegImpl => {
12001200
"Negative impls may only use type or const generic parameters.".into()
12011201
}
1202+
SemanticDiagnosticKind::UnsupportedItemInStatement => {
1203+
"Item not supported as a statement.".into()
1204+
}
12021205
}
12031206
}
12041207
fn location(&self, db: &'db dyn Database) -> SpanInFile<'db> {
@@ -1462,6 +1465,7 @@ impl<'db> DiagnosticEntry<'db> for SemanticDiagnostic<'db> {
14621465
SemanticDiagnosticKind::UserDefinedInlineMacrosDisabled => error_code!(E2194),
14631466
SemanticDiagnosticKind::NonNeverLetElseType => error_code!(E2195),
14641467
SemanticDiagnosticKind::OnlyTypeOrConstParamsInNegImpl => error_code!(E2196),
1468+
SemanticDiagnosticKind::UnsupportedItemInStatement => error_code!(E2197),
14651469
SemanticDiagnosticKind::PluginDiagnostic(diag) => {
14661470
diag.error_code.unwrap_or(error_code!(E2200))
14671471
}
@@ -1881,6 +1885,7 @@ pub enum SemanticDiagnosticKind<'db> {
18811885
UserDefinedInlineMacrosDisabled,
18821886
NonNeverLetElseType,
18831887
OnlyTypeOrConstParamsInNegImpl,
1888+
UnsupportedItemInStatement,
18841889
}
18851890

18861891
/// The kind of an expression with multiple possible return types.

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

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4753,28 +4753,25 @@ 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+
return Err(ctx
4770+
.diagnostics
4771+
.report(stmt_item_syntax.stable_ptr(db), UnsupportedItemInStatement));
47764772
}
4777-
ast::ModuleItem::Missing(_) => unreachable!("Missing type not supported."),
4773+
// Diagnostics reported on syntax level already.
4774+
ast::ModuleItem::Missing(_) => return Err(skip_diagnostic()),
47784775
}
47794776
statements.push(ctx.arenas.statements.alloc(semantic::Statement::Item(
47804777
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[E2197]: Item not supported as a statement.
239+
--> lib.cairo:2:5
240+
type MyAlias = felt252;
241+
^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)