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 8 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
32 changes: 24 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,25 @@ 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} were supplied",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this POU takes 0 arguments but 1 argument were supplied

We could be even more fancy here by changing were to was for single arguments 💅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, now we are at peak fancy level 🕺

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be the party p**per but you don't need the check if you say something like
"Expected {x} arguments but got {y}."

expected = message(expected),
actual = message(actual)
))
.with_error_code("E032")
.with_location(location.into())
}

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