From a58f980b02f526b50b2d3fff4764bd7b83514bf0 Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Mon, 4 Mar 2024 18:15:28 +0200 Subject: [PATCH] os: workaround a `-prod -cc gcc` bug, affecting os.open_file (fix #20923) (related to #20872) (#20960) --- vlib/builtin/string.v | 2 +- vlib/os/file.c.v | 1 + vlib/v/gen/c/testdata/self_printer_with_prod.out | 15 +++++++++++++++ vlib/v/gen/c/testdata/self_printer_with_prod.vv | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 vlib/v/gen/c/testdata/self_printer_with_prod.out create mode 100644 vlib/v/gen/c/testdata/self_printer_with_prod.vv diff --git a/vlib/builtin/string.v b/vlib/builtin/string.v index e0ccfa33b8..3f835ff75f 100644 --- a/vlib/builtin/string.v +++ b/vlib/builtin/string.v @@ -315,7 +315,7 @@ fn (a string) clone_static() string { // clone returns a copy of the V string `a`. pub fn (a string) clone() string { - if a.len == 0 { + if a.len <= 0 { return '' } mut b := string{ diff --git a/vlib/os/file.c.v b/vlib/os/file.c.v index cb3d1e235c..6108974fe4 100644 --- a/vlib/os/file.c.v +++ b/vlib/os/file.c.v @@ -47,6 +47,7 @@ fn fix_windows_path(path string) string { } // open_file tries to open or create a file with custom flags and permissions. +@[noinline] pub fn open_file(path string, mode string, options ...int) !File { mut flags := 0 mut seek_to_end := false diff --git a/vlib/v/gen/c/testdata/self_printer_with_prod.out b/vlib/v/gen/c/testdata/self_printer_with_prod.out new file mode 100644 index 0000000000..dfe042d4a8 --- /dev/null +++ b/vlib/v/gen/c/testdata/self_printer_with_prod.out @@ -0,0 +1,15 @@ +// vtest vflags: -prod -skip-unused +// See https://github.com/vlang/v/issues/20923 +module main + +import os +import io + +fn main() { + mut file := os.open_file(@FILE, 'r')! + mut buff_reader := io.new_buffered_reader(reader: file) + for { + a := buff_reader.read_line() or { break } + println(a) + } +} diff --git a/vlib/v/gen/c/testdata/self_printer_with_prod.vv b/vlib/v/gen/c/testdata/self_printer_with_prod.vv new file mode 100644 index 0000000000..dfe042d4a8 --- /dev/null +++ b/vlib/v/gen/c/testdata/self_printer_with_prod.vv @@ -0,0 +1,15 @@ +// vtest vflags: -prod -skip-unused +// See https://github.com/vlang/v/issues/20923 +module main + +import os +import io + +fn main() { + mut file := os.open_file(@FILE, 'r')! + mut buff_reader := io.new_buffered_reader(reader: file) + for { + a := buff_reader.read_line() or { break } + println(a) + } +}