From 7649738618d9a26bb7d3c156ef723438b2e9cff8 Mon Sep 17 00:00:00 2001 From: Christoffer Lerno Date: Sat, 7 Sep 2024 03:19:17 +0200 Subject: [PATCH] Improve lvalue handling in the compiler. #1357 --- releasenotes.md | 1 + src/compiler/sema_expr.c | 2 +- test/unit/regression/lvalue_handling.c3 | 19 +++++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/unit/regression/lvalue_handling.c3 diff --git a/releasenotes.md b/releasenotes.md index fa85780b8..ece882bd5 100644 --- a/releasenotes.md +++ b/releasenotes.md @@ -13,6 +13,7 @@ - Converting a slice to a vector/array would copy too little data. - Crash when reading an empty 'manifest.json'. - "optsize" did not work correctly in project.json. +- `l[0].a = 1` now supported for overloads due to better lvalue handling #1357. ### Stdlib changes - Additional init functions for hashmap. diff --git a/src/compiler/sema_expr.c b/src/compiler/sema_expr.c index 223321d0b..a9d16032d 100644 --- a/src/compiler/sema_expr.c +++ b/src/compiler/sema_expr.c @@ -4449,7 +4449,7 @@ static inline bool sema_expr_analyse_access(SemaContext *context, Expr *expr, bo if (missing_ref) *missing_ref = false; // 1. Resolve the left hand - if (!sema_analyse_expr_check(context, parent, check)) return false; + if (!sema_analyse_expr_check(context, parent, check != CHECK_VALUE ? CHECK_ADDRESS : CHECK_VALUE)) return false; // 2. The right hand side may be a @ident or ident Expr *child = expr->access_expr.child; diff --git a/test/unit/regression/lvalue_handling.c3 b/test/unit/regression/lvalue_handling.c3 new file mode 100644 index 000000000..e54482245 --- /dev/null +++ b/test/unit/regression/lvalue_handling.c3 @@ -0,0 +1,19 @@ +module lvalue_handling; +import std; +struct Foo +{ + int a; +} +def IntList = List(); +fn void subscript_overload() @test +{ + IntList x; + x.push({ 3 }); + int* a = &x[0].a; + assert(*a == 3); + assert(x[0].a == 3); + *a = 4; + assert(x[0].a == 4); + x[0].a = 5; + assert(x[0].a == 5); +} \ No newline at end of file