Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ jobs:

- name: Test
working-directory: ${{github.workspace}}/build
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure -E "codegen/pointer.aw"
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure
10 changes: 8 additions & 2 deletions lang/source/codegen/llvm/backend_llvm.c++
Original file line number Diff line number Diff line change
Expand Up @@ -368,8 +368,12 @@ auto backend_llvm::gen_local(const middle::variable& var) -> llvm::Value*

auto* alloca = builder.CreateAlloca(get_llvm_type(context, var.type), nullptr, var.name);

if (var.value)
builder.CreateStore(gen(var.value), alloca);
if (var.value) {
auto* init = gen(var.value);
if (!init)
return nullptr;
builder.CreateStore(init, alloca);
}

return alloca;
}
Expand Down Expand Up @@ -497,6 +501,8 @@ auto backend_llvm::gen(const middle::numeric_literal& expr) -> llvm::Constant*
return ConstantFP::get(context, APFloat(APFloat::IEEEsingle(), expr.value));
if (type->isDoubleTy())
return ConstantFP::get(context, APFloat(APFloat::IEEEdouble(), expr.value));
if (type->isPointerTy())
return ConstantPointerNull::get(cast<PointerType>(type));
}
return nullptr;
}
Expand Down
5 changes: 5 additions & 0 deletions lang/source/semantic/type_inference.c++
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ struct type_inference_visitor

auto propagate_type(ir::type* type, numeric_literal& expr) -> ir::type*
{
// Only a literal `0` may stand in for a null pointer; any other
// integer literal assigned to a pointer type is a real mismatch.
if (get_if<ir::pointer_type>(&type->kind) && expr.value != "0")
return error(diag, diagnostic_id::type_mismathch, location(), type->name, std::string("numeric_literal"));

return (expr.type = type);
}

Expand Down
3 changes: 3 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ set(TESTS
errors/not_allowed_here.aw
errors/not_implemented_yet.aw
errors/recovery.aw
errors/pointer_nonzero_init.aw
codegen/array.aw
codegen/array_of_structs.aw
codegen/chain.aw
codegen/function.aw
codegen/pointer_null_init.aw
modules/main.aw
)

Expand All @@ -26,6 +28,7 @@ set(NEGATIVE_TESTS
errors/not_allowed_here.aw
errors/not_implemented_yet.aw
errors/recovery.aw
errors/pointer_nonzero_init.aw
)

set(RUNTIME_TESTS
Expand Down
4 changes: 4 additions & 0 deletions test/codegen/pointer_null_init.aw
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func main()
{
var p: int* = 0;
}
4 changes: 4 additions & 0 deletions test/errors/pointer_nonzero_init.aw
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
func main()
{
var p: int* = 5;
}
Loading