Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions crates/cairo-lang-semantic/src/expr/test_data/constant
Original file line number Diff line number Diff line change
Expand Up @@ -709,6 +709,36 @@ impl AnotherFoo<impl F: Foo> of Foo {

//! > ==========================================================================

//! > Const not with generic impl constants is unsupported.

//! > test_runner_name
test_function_diagnostics(expect_diagnostics: true)

//! > function_code
fn foo() {}

//! > function_name
foo

//! > module_code
trait FlagTrait {
const FLAG: bool;
}
impl AFlag of FlagTrait {
const FLAG: bool = true;
}
impl AnotherFlag<impl F: FlagTrait> of FlagTrait {
const FLAG: bool = !F::FLAG;
}

//! > expected_diagnostics
error[E2127]: This expression is not supported as constant.
--> lib.cairo:8:24
const FLAG: bool = !F::FLAG;
^^^^^^^^

//! > ==========================================================================

//! > User-defined extern const fn in constant expression.

//! > test_runner_name
Expand Down
12 changes: 12 additions & 0 deletions crates/cairo-lang-semantic/src/items/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,18 @@ impl<'a, 'r, 'mt> ConstantEvaluateContext<'a, 'r, 'mt> {
if condition { self.true_const } else { self.false_const }
};

let has_non_concrete_arg = args.iter().any(|arg| {
!arg.is_fully_concrete(db) && !matches!(arg.long(db), ConstValue::Missing(_))
});
if (imp.function == self.eq_fn || imp.function == self.ne_fn || imp.function == self.not_fn)
&& has_non_concrete_arg
{
return to_missing(
self.diagnostics
.report(expr.stable_ptr.untyped(), SemanticDiagnosticKind::UnsupportedConstant),
);
}

if imp.function == self.eq_fn {
return bool_value(args[0] == args[1]);
} else if imp.function == self.ne_fn {
Expand Down