language: Check For Incorrect Order For Action Parameters

Signed-off-by: Will Hawkins <hawkinsw@obs.cr>
This commit is contained in:
Will Hawkins
2026-05-22 02:34:51 -04:00
parent a1908cc077
commit 24b0f0284a
2 changed files with 90 additions and 2 deletions
+11
View File
@@ -614,6 +614,17 @@ extension Action: Compilable {
} }
current_context = updated_context current_context = updated_context
// Check whether the parameters are in the proper order.
let remaining_parameters = action_parameters.parameters.drop(while: {
$0.type.direction() != .none
})
if remaining_parameters.contains(where: { $0.type.direction() != .none }) {
return .Error(
ErrorWithLocation(
sourceLocation: current_node!.toSourceLocation(),
withError: "All parameters with direction must precede directionless parameters"))
}
walker.next() walker.next()
#MustOr( #MustOr(
result: current_node, thing: walker.getNext(), result: current_node, thing: walker.getNext(),
+79 -2
View File
@@ -18,12 +18,12 @@
import Common import Common
import Foundation import Foundation
import Macros import Macros
import P4Lang
import Runtime import Runtime
import SwiftTreeSitter import SwiftTreeSitter
import Testing import Testing
import TreeSitter import TreeSitter
import TreeSitterP4 import TreeSitterP4
import P4Lang
@testable import P4Compiler @testable import P4Compiler
@@ -319,4 +319,81 @@ import P4Lang
), ),
Program.Compile(simple_parser_declaration)) Program.Compile(simple_parser_declaration))
) )
} }
@Test func test_simple_control_declaration_with_action_with_params_right_order() async throws {
let simple_parser_declaration = """
control simple(int x, int y) {
action a(inout int ax, bool ay) {
ax = 5;
}
table t {
key = {
x: exact;
y: exact;
}
}
apply {
}
};
"""
let x = { (tipe: P4QualifiedType) -> Bool in
switch tipe.baseType() {
case let c as Control: c.name == "simple"
default: false
}
}
let program = try! #UseOkResult(Program.Compile(simple_parser_declaration))
#expect(program.InstancesWithTypes(x).count == 1)
}
@Test func test_simple_control_declaration_with_action_with_params_wrong_order() async throws {
let simple_parser_declaration = """
control simple(int x, int y) {
action a(int ax, inout bool ay) {
ay = false;
}
table t {
key = {
x: exact;
y: exact;
}
}
apply {
}
};
"""
#expect(
#RequireErrorResult(
ErrorWithLocation(
sourceLocation: SourceLocation(41, 23),
withError: "All parameters with direction must precede directionless parameters"),
Program.Compile(simple_parser_declaration))
)
}
@Test func test_simple_control_declaration_with_action_with_params_wrong_order_interspersed() async throws {
let simple_parser_declaration = """
control simple(int x, int y) {
action a(inout bool aa, int ax, inout bool ay) {
aa = false;
}
table t {
key = {
x: exact;
y: exact;
}
}
apply {
}
};
"""
#expect(
#RequireErrorResult(
ErrorWithLocation(
sourceLocation: SourceLocation(41, 38),
withError: "All parameters with direction must precede directionless parameters"),
Program.Compile(simple_parser_declaration))
)
}