From 8ee1dc9f06ac99d6a98d0f42d31e3d709f44b3ea Mon Sep 17 00:00:00 2001 From: Ori Ziv Date: Thu, 26 Dec 2024 10:23:51 +0200 Subject: [PATCH] Added constness verification for impl fns. commit-id:9949a206 --- crates/cairo-lang-semantic/src/diagnostic.rs | 12 ++++++++++++ crates/cairo-lang-semantic/src/items/imp.rs | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/crates/cairo-lang-semantic/src/diagnostic.rs b/crates/cairo-lang-semantic/src/diagnostic.rs index 7723c1b8f6d..76729327c1d 100644 --- a/crates/cairo-lang-semantic/src/diagnostic.rs +++ b/crates/cairo-lang-semantic/src/diagnostic.rs @@ -696,6 +696,14 @@ impl DiagnosticEntry for SemanticDiagnostic { `{trait_name}`. The trait function is declared as nopanic." ) } + SemanticDiagnosticKind::PassConstAsNonConst { impl_function_id, trait_id } => { + let name = impl_function_id.name(db.upcast()); + let trait_name = trait_id.name(db.upcast()); + format!( + "The signature of function `{name}` is incompatible with trait \ + `{trait_name}`. The trait function is declared as const." + ) + } SemanticDiagnosticKind::PanicableFromNonPanicable => { "Function is declared as nopanic but calls a function that may panic.".into() } @@ -1283,6 +1291,10 @@ pub enum SemanticDiagnosticKind { impl_function_id: ImplFunctionId, trait_id: TraitId, }, + PassConstAsNonConst { + impl_function_id: ImplFunctionId, + trait_id: TraitId, + }, PanicableFromNonPanicable, PanicableExternFunction, PluginDiagnostic(PluginDiagnostic), diff --git a/crates/cairo-lang-semantic/src/items/imp.rs b/crates/cairo-lang-semantic/src/items/imp.rs index 99b658e3dd0..ef9b9578f37 100644 --- a/crates/cairo-lang-semantic/src/items/imp.rs +++ b/crates/cairo-lang-semantic/src/items/imp.rs @@ -3301,6 +3301,10 @@ fn validate_impl_function_signature( diagnostics.report(signature_syntax, PassPanicAsNopanic { impl_function_id, trait_id }); } + if concrete_trait_signature.is_const && !signature.is_const { + diagnostics.report(signature_syntax, PassConstAsNonConst { impl_function_id, trait_id }); + } + let expected_ty = inference.rewrite(concrete_trait_signature.return_type).no_err(); let actual_ty = inference.rewrite(signature.return_type).no_err();