Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong error when declaring an attribute right after an incomplete variable declaration #23396

Open
davidebeatrici opened this issue Jan 7, 2025 · 1 comment
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@davidebeatrici
Copy link
Contributor

davidebeatrici commented Jan 7, 2025

Describe the bug

error: unexpected token `:`, expecting `]`
    3 | test_var = "Test"
    4 | 
    5 | @[table: 'users']
      |        ^
    6 | struct User {
    7 |     name string

Reproduction Steps

#!/usr/bin/env v

test_var = "Test"

@[table: 'users']
struct User {
  name string
}

Expected Behavior

The compiler should warn about the variable declaration being incomplete.

Current Behavior

The compiler errors out with a wrong explanation.

Possible Solution

No response

Additional Information/Context

No response

V version

0.4.9

Environment details (OS name and version, etc.)

V full version: V 0.4.9 3953445
OS: linux, "OpenMandriva Lx 24.12"
Processor: 24 cpus, 64bit, little endian, AMD Ryzen 9 5900X 12-Core Processor

getwd: /home/user
vexe: /usr/libexec/vlang/v
vexe mtime: 2025-01-04 02:37:39

vroot: NOT writable, value: /usr/libexec/vlang
VMODULES: OK, value: /home/user/.vmodules
VTMP: OK, value: /tmp/v_1001

Git version: git version 2.47.1
Git vroot status: Error: fatal: not a git repository (or any of the parent directories): .git
.git/config present: false

CC version: OpenMandriva 19.1.6-1 clang version 19.1.6 (/builddir/build/BUILD/llvm-19.1.6-build/llvm-project-llvmorg-19.1.6/clang 1cc1c8b2641ccba94c7b90b5e4c4e500729f7556)
emcc version: N/A
thirdparty/tcc: N/A

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21827

@davidebeatrici davidebeatrici added the Bug This tag is applied to issues which reports bugs. label Jan 7, 2025
@jorgeluismireles
Copy link

Seems there is a reminiscence about syntax for script mode. Next code in the playground:

x = 'v'
struct Str { }

produces the message:

Running code...
Can't run code. The server returned an error:
code.v:1:1: notice: script mode started here
    1 | x = 'v'
      | ^
    2 | struct Str { }
code.v:2:8: error: all definitions must occur before code in script mode
    1 | x = 'v'
    2 | struct Str { }
      |        ~~~
Exited with error status 1
Please try again.

If we move the struct before the x then the compiler request to change the syntax:

undefined ident: `x` (use `:=` to declare a variable)

The REPL also enforces the syntax:

 \   \  /   /  |  Welcome to the V REPL (for help with V itself, type  exit , then run  v help ).
  \   \/   /   |  Note: the REPL is highly experimental. For best V experience, use a text editor, 
   \      /    |  save your code in a  main.v  file and execute:  v run main.v 
    \    /     |  V 0.4.9 7078a2e . Use  list  to see the accumulated program so far.
     \__/      |  Use Ctrl-C or  exit  to exit, or  help  to see other available commands.

>>> x = 'v'
error: undefined ident: `x` (use `:=` to declare a variable)
    5 | import math
    6 | 
    7 | x = 'v'
      | ^
>>> x := 'v'
>>> 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs.
Projects
None yet
Development

No branches or pull requests

2 participants