Disallow functions with more than 255 arguments

This commit is contained in:
Greg Shuflin 2021-11-02 21:03:48 -07:00
parent 45c72f97a2
commit 8336211a4b
3 changed files with 19 additions and 3 deletions

View File

@ -562,7 +562,13 @@ impl Parser {
#[recursive_descent_method]
fn formal_param_list(&mut self) -> ParseResult<Vec<FormalParam>> {
Ok(delimited!(self, LParen, formal_param, Comma, RParen))
let tok = self.token_handler.peek();
let list = delimited!(self, LParen, formal_param, Comma, RParen);
if list.len() > 255 {
ParseError::new_with_token("A function cannot have more than 255 arguments", tok.clone())
} else {
Ok(list)
}
}
#[recursive_descent_method]

View File

@ -2,7 +2,7 @@
#![allow(clippy::upper_case_acronyms)]
#![allow(clippy::vec_init_then_push)]
//use test_case::test_case;
use std::rc::Rc;
use std::{fmt::Write, rc::Rc};
use pretty_assertions::assert_eq;
@ -788,6 +788,16 @@ fn functions() {
);
}
#[test]
fn max_function_params() {
let mut buf = "fn longfunc(".to_string();
for n in 0..256 {
write!(buf, "a{}, ", n).unwrap();
}
write!(buf, ") {{ return 20 }}").unwrap();
assert_fail!(&buf, "A function cannot have more than 255 arguments");
}
#[test]
fn functions_with_different_whitespace() {
use ExpressionKind::*;

View File

@ -9,7 +9,7 @@ use crate::{
#[derive(Debug)]
enum NameType {
//TODO eventually this needs to support closures
Param(u8), //TODO handle implications of functions being limited to 255 params
Param(u8),
LocalVariable(ItemId),
LocalFunction(ItemId),
Import(Fqsn),