From 466d3bc1b6f45f62e0ca47f013306a901b6d63e8 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 13 Dec 2025 03:51:32 +0100 Subject: [PATCH] - Hard limit of 127 characters for identifiers. --- releasenotes.md | 1 + src/compiler/compiler_internal.h | 1 + src/compiler/lexer.c | 4 ++++ test/test_suite/lexing/too_long_ident.c3 | 8 ++++++++ 4 files changed, 14 insertions(+) create mode 100644 test/test_suite/lexing/too_long_ident.c3 diff --git a/releasenotes.md b/releasenotes.md index 0693545a4..a0a3d3c42 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -19,6 +19,7 @@ - Typedefs and structs with inline types supporting lengthof would not work with lengthof #2641. - `$defined(foo())` now correctly errors if `foo()` would require a path. - `@if($defined((char*){}.foo()))` does not error if `foo` is missing. +- Hard limit of 127 characters for identifiers. ### Stdlib changes - Add `ThreadPool` join function to wait for all threads to finish in the pool without destroying the threads. diff --git a/src/compiler/compiler_internal.h b/src/compiler/compiler_internal.h index d75e33324..27a069145 100644 --- a/src/compiler/compiler_internal.h +++ b/src/compiler/compiler_internal.h @@ -52,6 +52,7 @@ typedef uint16_t FileId; #define MAX_GLOBAL_DECL_STACK (65536) #define MAX_MODULE_NAME 31 #define MAX_MODULE_PATH 63 +#define MAX_IDENTIFIER_LENGTH 127 #define MAX_MACRO_RECURSION_DEPTH 128 #define MEMCMP_INLINE_REGS 8 #define UINT128_MAX ((Int128) { UINT64_MAX, UINT64_MAX }) diff --git a/src/compiler/lexer.c b/src/compiler/lexer.c index c9afc1af9..0240a6805 100644 --- a/src/compiler/lexer.c +++ b/src/compiler/lexer.c @@ -349,6 +349,10 @@ EXIT:; } return add_error_token(lexer, "An identifier may not consist of only '_' characters."); } + if (len > MAX_IDENTIFIER_LENGTH) + { + return add_error_token(lexer, "An identifier cannot be longer than %d characters, but this one was %u characters long.", MAX_IDENTIFIER_LENGTH, len); + } const char* interned_string = symtab_add(lexer->lexing_start, len, hash, &type); switch (type) { diff --git a/test/test_suite/lexing/too_long_ident.c3 b/test/test_suite/lexing/too_long_ident.c3 new file mode 100644 index 000000000..e159939a7 --- /dev/null +++ b/test/test_suite/lexing/too_long_ident.c3 @@ -0,0 +1,8 @@ +module test; +import std; +fn int main() +{ + int iodeofjoiewjfojfoiejfewjfijewfijewifjoiewjfioewjfioewjfioewjfiojewfiojewfiojeofjeiowfjioewjfioewjfioewjfijweifefefeffefefeeefef = 0; + int iodeofjoiewjfojfoiejfewjfijewfijewifjoiewjfioewjfioewjfioewjfiojewfiojewfiojeofjeiowfjioewjfioewjfioewjfijweifefefeffefefeeefef1 = 0; // #error: okfeokfe + return 0; +} \ No newline at end of file