From 7b8c9fb71577274276f4e35d5615f5e34315a7e8 Mon Sep 17 00:00:00 2001 From: kbkpbot Date: Thu, 29 May 2025 15:34:57 +0800 Subject: [PATCH] sync.stdatomic: turn panic() in new_atomic[T]() into a $compile_error() (#24573) --- vlib/sync/stdatomic/atomic.c.v | 10 ++++++++-- vlib/v/checker/tests/sync_stdatomic_compile_err.out | 7 +++++++ vlib/v/checker/tests/sync_stdatomic_compile_err.vv | 3 +++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/sync_stdatomic_compile_err.out create mode 100644 vlib/v/checker/tests/sync_stdatomic_compile_err.vv diff --git a/vlib/sync/stdatomic/atomic.c.v b/vlib/sync/stdatomic/atomic.c.v index eac35738f..d50ea07ed 100644 --- a/vlib/sync/stdatomic/atomic.c.v +++ b/vlib/sync/stdatomic/atomic.c.v @@ -69,12 +69,18 @@ pub struct AtomicVal[T] { // new_atomic creates a new atomic value of `T` type @[inline] pub fn new_atomic[T](val T) &AtomicVal[T] { - $if T is $int || T is bool { + // can't use `$if T is $int || T is bool` with $compile_error() now + // see issue #24562 + $if T is $int { + return &AtomicVal[T]{ + val: val + } + } $else $if T is bool { return &AtomicVal[T]{ val: val } } $else { - panic('atomic: only support number and bool types') + $compile_error('atomic: only support number and bool types') } return unsafe { nil } } diff --git a/vlib/v/checker/tests/sync_stdatomic_compile_err.out b/vlib/v/checker/tests/sync_stdatomic_compile_err.out new file mode 100644 index 000000000..c384a752a --- /dev/null +++ b/vlib/v/checker/tests/sync_stdatomic_compile_err.out @@ -0,0 +1,7 @@ +vlib/sync/stdatomic/atomic.c.v:83:3: error: atomic: only support number and bool types + 81 | } + 82 | } $else { + 83 | $compile_error('atomic: only support number and bool types') + | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + 84 | } + 85 | return unsafe { nil } diff --git a/vlib/v/checker/tests/sync_stdatomic_compile_err.vv b/vlib/v/checker/tests/sync_stdatomic_compile_err.vv new file mode 100644 index 000000000..cadffc1ae --- /dev/null +++ b/vlib/v/checker/tests/sync_stdatomic_compile_err.vv @@ -0,0 +1,3 @@ +import sync.stdatomic {new_atomic} + +_ := new_atomic(`1`) -- 2.39.5