Skip to content

Commit

Permalink
Added constness verification for impl fns.
Browse files Browse the repository at this point in the history
commit-id:9949a206
  • Loading branch information
orizi committed Jan 4, 2025
1 parent 5d85be7 commit 8ee1dc9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
12 changes: 12 additions & 0 deletions crates/cairo-lang-semantic/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 4 additions & 0 deletions crates/cairo-lang-semantic/src/items/imp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down

0 comments on commit 8ee1dc9

Please sign in to comment.