From 77ef89465665700103431bf0abcdb40deb1a37ba Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 14 Apr 2026 12:45:34 +0300 Subject: [PATCH] cgen: C code compiling error (fixes #21939) --- vlib/v/gen/c/cheaders.v | 6 ++---- vlib/v/gen/c/noreturn_attr_test.v | 34 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 vlib/v/gen/c/noreturn_attr_test.v diff --git a/vlib/v/gen/c/cheaders.v b/vlib/v/gen/c/cheaders.v index 0ce3e9902..1114e11de 100644 --- a/vlib/v/gen/c/cheaders.v +++ b/vlib/v/gen/c/cheaders.v @@ -262,10 +262,8 @@ const c_common_callconv_attr = ' const c_common_noreturn_attr = ' #if !defined(VNORETURN) #if defined(__TINYC__) - #include - #define VNORETURN noreturn - #endif - # if !defined(__TINYC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L + #define VNORETURN __attribute__((noreturn)) + # elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L # define VNORETURN _Noreturn # elif !defined(VNORETURN) && defined(__GNUC__) && __GNUC__ >= 2 # define VNORETURN __attribute__((noreturn)) diff --git a/vlib/v/gen/c/noreturn_attr_test.v b/vlib/v/gen/c/noreturn_attr_test.v new file mode 100644 index 000000000..d828221db --- /dev/null +++ b/vlib/v/gen/c/noreturn_attr_test.v @@ -0,0 +1,34 @@ +import os + +const vexe = @VEXE + +const vroot = os.real_path(@VMODROOT) + +fn test_noreturn_attr_generation_does_not_depend_on_stdnoreturn_h() { + os.chdir(vroot) or {} + tmp_dir := os.join_path(os.vtmp_dir(), 'v_noreturn_attr_test') + os.mkdir_all(tmp_dir)! + defer { + os.rmdir_all(tmp_dir) or {} + } + source_path := os.join_path(tmp_dir, 'noreturn_attr_case.v') + source := [ + '@[noreturn]', + 'fn terminate() {', + '\texit(1)', + '}', + '', + 'fn main() {', + '\tterminate()', + '}', + ].join('\n') + os.write_file(source_path, source)! + cmd := '${os.quoted_path(vexe)} -o - ${os.quoted_path(source_path)}' + compilation := os.execute(cmd) + assert compilation.exit_code == 0 + generated_c := compilation.output.replace('\r\n', '\n') + assert generated_c.contains('#if !defined(VNORETURN)') + assert generated_c.contains('#if defined(__TINYC__)') + assert generated_c.contains('#define VNORETURN __attribute__((noreturn))') + assert !generated_c.contains('#include ') +} -- 2.39.5