Skip to content

all: add compile_error and compile_warn comptime functions#14320

Merged
spytheman merged 7 commits into
vlang:masterfrom
StunxFS:comptime_error_warning
May 9, 2022
Merged

all: add compile_error and compile_warn comptime functions#14320
spytheman merged 7 commits into
vlang:masterfrom
StunxFS:comptime_error_warning

Conversation

@StunxFS
Copy link
Copy Markdown
Contributor

@StunxFS StunxFS commented May 5, 2022

This PR adds the compile_error and compile_warn comptime functions.

Both are very useful for handling cases where, for example, a certain generic type, or an operating system, is not supported.

$ cat x.v
module main

$if linux {
    $compile_error('Linux is not supported')
}

fn main() {
}

$ ./v2 run x.v
x.v:4:5: error: Linux is not supported
    2 |
    3 | $if linux {
    4 |     $compile_error('Linux is not supported')
      |     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    5 | }
    6 |

@larpon
Copy link
Copy Markdown
Contributor

larpon commented May 5, 2022

Nice work, I like the idea of being able to give compile time errors - what are compile warnings good for, though?

It reminds me a bit of the C/C++ -Wall-type of flags needed to activate or hide warnings when using C-based compilers. Either code compiles else the programmer is doing something wrong that deserves an error - what cases or examples do we have for giving a warning at compile time?

Maybe userland compile warnings should at least result in compile errors with -prod - like they do now in the internal system?

@medvednikov
Copy link
Copy Markdown
Member

@StunxFS good job!

Can you add a test with expected output, I'll merge it then.

@spytheman
Copy link
Copy Markdown
Contributor

It can be in vlib/v/checker/tests/ - a pair of .vv and .out files, where the .vv file has the example, and the .out file is produced by v vlib/v/checker/tests/compile_error.vv > vlib/v/checker/tests/compile_error.out .

@spytheman spytheman merged commit d24dce8 into vlang:master May 9, 2022
@StunxFS StunxFS deleted the comptime_error_warning branch May 18, 2022 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants