1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: disallow top level decl builtin fn (#17857)

This commit is contained in:
Swastik Baranwal 2023-04-03 20:00:05 +05:30 committed by GitHub
parent 22afdb5cbf
commit 1dcec62c19
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 57 additions and 36 deletions

View File

@ -1,36 +0,0 @@
name: vlang benchmarks
on:
push:
paths-ignore:
- "**.md"
pull_request:
paths-ignore:
- "**.md"
jobs:
run:
name: Run
runs-on: ubuntu-latest
if: github.event_name != 'push' || github.event.ref == 'refs/heads/master' || github.event.repository.full_name != 'vlang/v'
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install google benchmark
run: |
git clone https://github.com/google/benchmark.git
cd benchmark
cmake -E make_directory "build"
cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../
sudo cmake --build "build" --config Release --target install
- name: Run V benchmark
run: |
make
sudo ./v symlink
git clone https://github.com/vincenzopalazzo/benchmarks.git
cd benchmarks
make vdep && make v
- uses: actions/upload-artifact@v3
with:
name: vlang-benchmark
path: benchmarks/vlang/*.json

View File

@ -275,6 +275,11 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) {
c.error('fn `init` cannot have a return type', node.pos) c.error('fn `init` cannot have a return type', node.pos)
} }
} }
if node.language == .v && node.mod == 'main' && node.name.after_char(`.`) in reserved_type_names
&& !node.is_method && !c.is_builtin_mod {
c.error('top level declaration cannot shadow builtin type', node.pos)
}
if node.return_type != ast.Type(0) { if node.return_type != ast.Type(0) {
c.ensure_type_exists(node.return_type, node.return_type_pos) or { return } c.ensure_type_exists(node.return_type, node.return_type_pos) or { return }
if node.language == .v && node.is_method && node.name == 'str' { if node.language == .v && node.is_method && node.name == 'str' {

View File

@ -0,0 +1,28 @@
vlib/v/checker/tests/top_level_fn_builtin_decl_err.vv:3:1: error: top level declaration cannot shadow builtin type
1 |
2 | [inline]
3 | fn char(ch byte) fn (string) !(byte, string) {
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 | return fn [ch] (input string) !(byte, string) {
5 | return if input[0] == ch {
vlib/v/checker/tests/top_level_fn_builtin_decl_err.vv:19:18: error: unknown function: a_char
17 |
18 | for i, input in inputs {
19 | got, remain := a_char(input)!
| ~~~~~~~~~~~~~
20 |
21 | assert got == 'a'[0]
vlib/v/checker/tests/top_level_fn_builtin_decl_err.vv:21:10: error: invalid variable `got`
19 | got, remain := a_char(input)!
20 |
21 | assert got == 'a'[0]
| ~~~
22 | assert remain == remains[i]
23 | }
vlib/v/checker/tests/top_level_fn_builtin_decl_err.vv:22:10: error: invalid variable `remain`
20 |
21 | assert got == 'a'[0]
22 | assert remain == remains[i]
| ~~~~~~
23 | }
24 | }

View File

@ -0,0 +1,24 @@
[inline]
fn char(ch byte) fn (string) !(byte, string) {
return fn [ch] (input string) !(byte, string) {
return if input[0] == ch {
ch, input[1..]
} else {
error('Some error')
}
}
}
fn main() {
a_char := int('a'[0])
inputs := ['a', 'abc']
remains := ['', 'bc']
for i, input in inputs {
got, remain := a_char(input)!
assert got == 'a'[0]
assert remain == remains[i]
}
}