Files
dimgigov aaeb01e518 feat(nexus): production modular HTTP/1.1 server + compiler fixes
- Rewrite apps/nexus with modular architecture:
  Config, Http, Errors, Parser, Router, Handlers, Server, Main
- Use algebraic enums for ParseResult/FileResult/HttpError
- Thread-pool server via Channel<ConnectionTask> and spawn
- Fix C backend type ordering for generic struct instances
  (Array_T, Iter_T) and algebraic enum struct payloads
- Collect Slice_T types from struct fields and enum payloads
- Fix match lowering for simple enums (direct value compare)
- Resolve match expression return type from first arm
- Infer element type for for-in over Array<UserStruct>
- Preserve generic type args in field access resolution
- Add fflush to PrintLine/Print for immediate server logs
- Add modern_features golden regression test
- Regenerate golden expected.c files
2026-06-15 00:54:03 +03:00

212 lines
4.0 KiB
Nim

import std/[unittest, strutils]
import ../bootstrap/[lexer, parser, sema]
proc checkSource(source: string): tuple[hasErrors: bool, diagnostics: seq[SemaDiagnostic]] =
let lexRes = lexer.tokenize(source, "test.bux")
if lexRes.hasErrors:
return (true, @[])
let parseRes = parser.parse(lexRes.tokens, "test.bux")
if parseRes.diagnostics.len > 0:
return (true, @[])
let (semaRes, _) = sema.analyzeFull(parseRes.module)
return (semaRes.hasErrors, semaRes.diagnostics)
suite "Borrow Checker":
test "@[Checked] function with &mut allows mutation":
let res = checkSource("""
@[Checked]
func Inc(p: &mut int) {
*p = *p + 1;
}
@[Checked]
func Main() -> int {
var x: int = 5;
Inc(&x);
return x;
}
""")
check(not res.hasErrors)
test "@[Checked] function rejects write through &T":
let res = checkSource("""
@[Checked]
func BadWrite(p: &int) {
*p = 42;
}
@[Checked]
func Main() -> int {
var x: int = 5;
BadWrite(&x);
return x;
}
""")
check(res.hasErrors)
test "unchecked function allows write through raw pointer":
let res = checkSource("""
func RawWrite(p: *int) {
*p = 42;
}
func Main() -> int {
var x: int = 5;
RawWrite(&x);
return x;
}
""")
check(not res.hasErrors)
test "&T allows reading":
let res = checkSource("""
@[Checked]
func Get(p: &int) -> int {
return *p;
}
@[Checked]
func Main() -> int {
var x: int = 5;
return Get(&x);
}
""")
check(not res.hasErrors)
test "own T type parses and resolves":
let res = checkSource("""
struct Box {
value: int;
}
func TakeOwn(b: own Box) -> int {
return b.value;
}
func Main() -> int {
var b: Box = Box { value: 42 };
return TakeOwn(b);
}
""")
check(not res.hasErrors)
test "@[Checked] rejects double mutable borrow in call":
let res = checkSource("""
@[Checked]
func Swap(a: &mut int, b: &mut int) {
let tmp = *a;
*a = *b;
*b = tmp;
}
@[Checked]
func Main() -> int {
var x: int = 10;
Swap(&x, &x);
return 0;
}
""")
check(res.hasErrors)
check(res.diagnostics[0].message.contains("mutable borrow"))
test "@[Checked] rejects use after move":
let res = checkSource("""
struct Box {
value: int;
}
@[Checked]
func Consume(b: own Box) -> int {
return b.value;
}
@[Checked]
func Main() -> int {
var b: Box = Box { value: 42 };
let x: int = Consume(b);
return b.value;
}
""")
check(res.hasErrors)
check(res.diagnostics[0].message.contains("moved"))
test "@[Checked] allows reinitialization after move":
let res = checkSource("""
struct Box {
value: int;
}
@[Checked]
func Take(b: own Box) -> int {
return b.value;
}
@[Checked]
func Main() -> int {
var b: own Box = Box { value: 1 };
Take(b);
b = Box { value: 2 };
return b.value;
}
""")
check(not res.hasErrors)
test "@[Checked] move in assignment":
let res = checkSource("""
struct Box {
value: int;
}
@[Checked]
func Main() -> int {
var a: own Box = Box { value: 1 };
var b: own Box = a;
return a.value;
}
""")
check(res.hasErrors)
check(res.diagnostics[0].message.contains("moved"))
test "@[Checked] move in return":
let res = checkSource("""
struct Box {
value: int;
}
@[Checked]
func Give() -> own Box {
var x: own Box = Box { value: 42 };
return x;
}
@[Checked]
func Main() -> int {
let b: own Box = Give();
return b.value;
}
""")
check(not res.hasErrors)
test "borrow &mut expr parses and type-checks":
let res = checkSource("""
struct Point {
x: int;
y: int;
}
func Main() -> int {
var p: Point;
p.x = 10;
let r: &mut Point = borrow &mut p;
return 0;
}
""")
check(not res.hasErrors)
test "@[Shared] attribute parses without errors":
let res = checkSource("""
@[Shared]
func SharedFunc() -> int {
return 42;
}
func Main() -> int {
return SharedFunc();
}
""")
check(not res.hasErrors)
test "borrow & expr with shared ref":
let res = checkSource("""
struct Point {
x: int;
y: int;
}
func Main() -> int {
var p: Point;
p.x = 10;
let r: &Point = borrow &p;
let val: int = (*r).x;
return val;
}
""")
check(not res.hasErrors)