From 497a0f12ebfc2d2b080b7f1ef788d40c6fc73d88 Mon Sep 17 00:00:00 2001 From: LindonAliu Date: Thu, 23 Jul 2026 23:27:02 +0200 Subject: [PATCH 1/2] fix(gil): deduce struct field pointer type --- .../Aggregates/StructFieldPtrInst.hpp | 3 ++ include/GILGen/Context.hpp | 7 ++-- test/GILGen/GILGenStmt.cpp | 41 +++++++++++++++++++ 3 files changed, 47 insertions(+), 4 deletions(-) diff --git a/include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp b/include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp index 364511cc3..1d9acd908 100644 --- a/include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp +++ b/include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp @@ -2,6 +2,7 @@ #define GLU_GIL_INSTRUCTIONS_STRUCT_FIELD_PTR_INST_HPP #include "AggregateInst.hpp" +#include "AST/Types/PointerTy.hpp" namespace glu::gil { @@ -32,6 +33,8 @@ class StructFieldPtrInst : public AggregateInst { , _member(member) , _ptr(pointerType) { + auto *ptrType = llvm::cast(pointerType); + assert(ptrType->getPointee() == member.getType()); } /// @brief Gets the result type at the specified index. diff --git a/include/GILGen/Context.hpp b/include/GILGen/Context.hpp index 741f3bfa8..6c091e0bb 100644 --- a/include/GILGen/Context.hpp +++ b/include/GILGen/Context.hpp @@ -411,11 +411,10 @@ class Context { gil::StructFieldPtrInst * buildStructFieldPtr(gil::Value structPtr, gil::Member member) { - // Create a pointer type to the field type - auto *fieldPtrType = _functionDecl->getModule() - ->getContext() + auto *fieldPtrType = getASTContext() ->getTypesMemoryArena() - .create(member.getType() + .create( + member.getType() ); return insertInstruction( new gil::StructFieldPtrInst(structPtr, member, fieldPtrType) diff --git a/test/GILGen/GILGenStmt.cpp b/test/GILGen/GILGenStmt.cpp index be92a1640..acc9f4e60 100644 --- a/test/GILGen/GILGenStmt.cpp +++ b/test/GILGen/GILGenStmt.cpp @@ -1,4 +1,5 @@ #include "GILGen/GILGen.hpp" +#include "Instructions.hpp" #include "Parser.hpp" #include "Scanner.hpp" #include "Sema/Sema.hpp" @@ -43,3 +44,43 @@ TEST(GILGenStmt, Empty) bb->getInstructions().front().getKind(), InstKind::ReturnInstKind ); } + +TEST(GILGenStmt, StructFieldPtrResultTypePointsToFieldType) +{ + PREP_PARSER(R"( + struct Inner { + value: Int + } + + func copy(i: *Inner) -> Inner { + var result: Inner; + result.value = i.*.value; + return result; + } + )"); + + ASSERT_EQ(module->getDecls().size(), 2u); + auto *fn = llvm::cast(module->getDecls()[1]); + auto gilModule = std::make_unique("test_module"); + GlobalContext globalCtx(gilModule.get()); + auto *f = generateFunction(gilModule.get(), fn, globalCtx); + + auto *structFieldPtrInst = [&]() -> StructFieldPtrInst * { + for (auto &bb : f->getBasicBlocks()) { + for (auto &inst : bb.getInstructions()) { + if (auto *fieldPtr = llvm::dyn_cast(&inst)) + return fieldPtr; + } + } + return nullptr; + }(); + + ASSERT_NE(structFieldPtrInst, nullptr); + auto *resultType = llvm::dyn_cast( + structFieldPtrInst->getResultType() + ); + ASSERT_NE(resultType, nullptr); + EXPECT_EQ( + resultType->getPointee(), structFieldPtrInst->getMember().getType() + ); +} From eada7219d511501b3303016539710a4bf990ed69 Mon Sep 17 00:00:00 2001 From: LindonAliu Date: Thu, 23 Jul 2026 23:29:37 +0200 Subject: [PATCH 2/2] style: apply clang-format --- include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp | 2 +- test/GILGen/GILGenStmt.cpp | 5 ++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp b/include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp index 1d9acd908..6f50f43c9 100644 --- a/include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp +++ b/include/GIL/Instructions/Aggregates/StructFieldPtrInst.hpp @@ -1,8 +1,8 @@ #ifndef GLU_GIL_INSTRUCTIONS_STRUCT_FIELD_PTR_INST_HPP #define GLU_GIL_INSTRUCTIONS_STRUCT_FIELD_PTR_INST_HPP -#include "AggregateInst.hpp" #include "AST/Types/PointerTy.hpp" +#include "AggregateInst.hpp" namespace glu::gil { diff --git a/test/GILGen/GILGenStmt.cpp b/test/GILGen/GILGenStmt.cpp index acc9f4e60..4eda92838 100644 --- a/test/GILGen/GILGenStmt.cpp +++ b/test/GILGen/GILGenStmt.cpp @@ -76,9 +76,8 @@ TEST(GILGenStmt, StructFieldPtrResultTypePointsToFieldType) }(); ASSERT_NE(structFieldPtrInst, nullptr); - auto *resultType = llvm::dyn_cast( - structFieldPtrInst->getResultType() - ); + auto *resultType + = llvm::dyn_cast(structFieldPtrInst->getResultType()); ASSERT_NE(resultType, nullptr); EXPECT_EQ( resultType->getPointee(), structFieldPtrInst->getMember().getType()