Skip to content

Commit 6ef9148

Browse files
committed
fix: emit diagnostic for unsupported extern fn in const evaluation instead of panicking
Fixes #9799
1 parent fca4f70 commit 6ef9148

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,3 +706,26 @@ impl AnotherFoo<impl F: Foo> of Foo {
706706
}
707707

708708
//! > expected_diagnostics
709+
710+
//! > ==========================================================================
711+
712+
//! > User-defined extern const fn in constant expression.
713+
714+
//! > test_runner_name
715+
test_function_diagnostics(expect_diagnostics: true)
716+
717+
//! > function_code
718+
fn foo() {}
719+
720+
//! > function_name
721+
foo
722+
723+
//! > module_code
724+
extern const fn f() -> felt252 nopanic;
725+
const X: felt252 = f();
726+
727+
//! > expected_diagnostics
728+
error[E2127]: This expression is not supported as constant.
729+
--> lib.cairo:2:20
730+
const X: felt252 = f();
731+
^^^

crates/cairo-lang-semantic/src/items/constant.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cairo_lang_debug::DebugWithDb;
55
use cairo_lang_defs::db::DefsGroup;
66
use cairo_lang_defs::ids::{
77
ConstantId, ExternFunctionId, GenericParamId, LanguageElementId, LookupItemId, ModuleItemId,
8-
NamedLanguageElementId, TopLevelLanguageElementId, TraitConstantId, TraitId, VarId,
8+
NamedLanguageElementId, TraitConstantId, TraitId, VarId,
99
};
1010
use cairo_lang_diagnostics::{
1111
DiagnosticAdded, DiagnosticEntry, DiagnosticNote, Diagnostics, Maybe, MaybeAsRef,
@@ -1085,9 +1085,12 @@ impl<'a, 'r, 'mt> ConstantEvaluateContext<'a, 'r, 'mt> {
10851085
.intern(db),
10861086
);
10871087
} else {
1088-
unreachable!(
1089-
"Unexpected extern function in constant lowering: `{}`",
1090-
extern_fn.full_path(db)
1088+
return Some(
1089+
ConstValue::Missing(self.diagnostics.report(
1090+
expr.stable_ptr.untyped(),
1091+
SemanticDiagnosticKind::UnsupportedConstant,
1092+
))
1093+
.intern(db),
10911094
);
10921095
}
10931096
}

0 commit comments

Comments
 (0)