Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Validate argument count #1233

Merged
merged 20 commits into from
Jun 17, 2024
Merged
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
33 changes: 25 additions & 8 deletions compiler/plc_diagnostics/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::Display;

use serde::{Deserialize, Serialize};

use crate::diagnostics::diagnostics_registry::DIAGNOSTICS;
use plc_ast::ast::AstNode;
use plc_source::{
source_location::{SourceLocation, SourceLocationFactory},
Expand Down Expand Up @@ -88,8 +89,10 @@ impl Diagnostic {
self
}

pub fn with_error_code(mut self, error_code: &'static str) -> Self {
self.error_code = error_code;
pub fn with_error_code(mut self, code: &'static str) -> Self {
debug_assert!(DIAGNOSTICS.get(code).is_some(), "Error {code} does not exist");

self.error_code = code;
self
}

Expand Down Expand Up @@ -199,12 +202,26 @@ impl Diagnostic {
.with_error_code("E006")
}

pub fn invalid_parameter_count(expected: usize, received: usize, location: SourceLocation) -> Diagnostic {
Diagnostic::new(
format!(
"Invalid parameter count. Received {received} parameters while {expected} parameters were expected.",
)).with_error_code("E032")
.with_location(location)
pub fn invalid_argument_count<T>(expected: usize, actual: usize, location: T) -> Diagnostic
where
T: Into<SourceLocation>,
{
// Let's be extra fancy here 🕺
fn message(value: usize) -> String {
match value {
1 => format!("{value} argument"),
_ => format!("{value} arguments"),
}
}

Diagnostic::new(format!(
"this POU takes {expected} but {actual} {was_or_were} supplied",
expected = message(expected),
actual = message(actual),
was_or_were = if actual == 1 { "was" } else { "were" }
))
.with_error_code("E032")
.with_location(location.into())
}

pub fn unknown_type(type_name: &str, location: SourceLocation) -> Diagnostic {
Expand Down
Loading
Loading