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

error: invalid expression: unexpected keyword for #18838

Closed
fabiuz opened this issue Jul 11, 2023 · 5 comments · Fixed by #18840
Closed

error: invalid expression: unexpected keyword for #18838

fabiuz opened this issue Jul 11, 2023 · 5 comments · Fixed by #18840
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@fabiuz
Copy link

fabiuz commented Jul 11, 2023

Describe the bug

On code below:

const CMB_CONJUNTO_QT_DE_BOLAS = 10

fn main() {
	qt_bolas_selecionadas := 1
	
	if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {
		for id := 0; id < 100; id += 1  {      // this error occurred here, line 7.
			println("valor: ${id}")
		}
	}	
        // The 5 lines below are the same 5 lines above, but "v" indicated error only on line 7.
	if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {
		for id := 0; id < 100; id += 1  {    
			println("valor: ${id}")
		}
	}	
}

this error has occurred:

teste.v:1:7: warning: const names cannot contain uppercase letters, use snake_case instead
    1 | const CMB_CONJUNTO_QT_DE_BOLAS = 10
       |       ~~~~~~~~~~~~~~~~~~~~~~~~
    2 | 
    3 | fn main() {
teste.v:7:3: error: invalid expression: unexpected keyword `for`
    5 |     
    6 |     if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {
    7 |         for id := 0; id < 100; id += 1  {
       |         ~~~
    8 |             println("valor: ${id}")
    9 |         }

If I fix this warning of line 1, changing const name CMB_CONJUNTO_QT_DE_BOLAS for conjunto_qt_de_bolas, the error of line 7:3 ends.

If you noticed, the lines below:

if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {
		for id := 0; id < 100; id += 1  {
			println("valor: ${id}")
		}
	}

were repeated 2 times, on lines, from 6 to 10, and, from 12 a 16, but, "v", have report error only on line 7.

Expected Behavior

Line 1, really, it is a warning.
Visually, and syntactically, line 7 and line 12 have no error.
Although, in the previous line, 6, after the token "==", "v" knows that it is a constant, but in line "1", it says that the whole constant must be written in "snake_case", when line "1" is corrected, line 7 does not display any errors anymore.

Current Behavior

[fabiuz@localhost lotomania]$ v teste.v
teste.v:1:7: warning: const names cannot contain uppercase letters, use snake_case instead
    1 | const CMB_CONJUNTO_QT_DE_BOLAS = 10
       |       ~~~~~~~~~~~~~~~~~~~~~~~~
    2 | 
    3 | fn main() {
teste.v:7:3: error: invalid expression: unexpected keyword `for`
    5 |     
    6 |     if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {
    7 |         for id := 0; id < 100; id += 1  {
       |         ~~~
    8 |             println("valor: ${id}")
    9 |         }

Reproduction Steps

const CMB_CONJUNTO_QT_DE_BOLAS = 10

fn main() {
	qt_bolas_selecionadas := 1
	
	if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {		
		for id := 0; id < 100; id += 1  {
			println("valor: ${id}")
		}
	}	

	if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {
		for id := 0; id < 100; id += 1  {
			println("valor: ${id}")
		}
	}	
}

Possible Solution

No response

Additional Information/Context

No response

V version

V full version: V 0.4.0 5355c67

Environment details (OS name and version, etc.)

V full version: V 0.4.0 5355c67.6b29d62
OS: linux, "Fedora release 36 (Thirty Six)"
Processor: 8 cpus, 64bit, little endian, Intel(R) Core(TM) i7-2600 CPU @ 3.40GHz

getwd: /home/fabiuz/Documentos/vlang/loteria/lotomania
vexe: /home/fabiuz/applications/vlang/v/v
vexe mtime: 2023-07-11 00:54:34

vroot: OK, value: /home/fabiuz/applications/vlang/v
VMODULES: OK, value: /home/fabiuz/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.40.0
Git vroot status: weekly.2023.28-4-g6b29d628
.git/config present: true

CC version: cc (GCC) 12.2.1 20221121 (Red Hat 12.2.1-4)
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3
@fabiuz fabiuz added the Bug This tag is applied to issues which reports bugs. label Jul 11, 2023
@JalonSolov
Copy link
Contributor

All 3 lines should be errors, actually. V shouldn't allow all uppercase for const values, as that is against the V syntax.

The other 2 lines should be unknown symbol errors, instead of unexpected keyword errors.

@fabiuz
Copy link
Author

fabiuz commented Jul 11, 2023

On this other example:

  1. `const CMB_CONJUNTO_QT_DE_BOLAS = 10
  2. fn main() {
  3. qt_bolas_selecionadas := 1
  4. if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {
  5.  println("asdf")
    
  6. }
  7. }`

This error is:
teste2.v:9:1: error: unexpected token }, expecting {
7 | println("asdf")
8 | }
9 | }

So when, I change the constant that is in uppercase format to snake-case format, in lines 1 and 6, the error in line 9 disappears.
Looking visually, and syntactically, the code is correct, it seems that when it finds the constant in uppercase format that it considers a warning, it skips the block "{" and "}" of the sentence if, and goes to look for the token "{", which corresponds to the "if", after the block of the sentence "if".

@JalonSolov
Copy link
Contributor

JalonSolov commented Jul 11, 2023

Those are the same as your first example. The const line should have been an error, and line 6 should have been an unknown symbol error, instead of giving the "false" error on the next line.

@fabiuz
Copy link
Author

fabiuz commented Jul 11, 2023

Now, on this other example:

1. const CMB_CONJUNTO_QT_DE_BOLAS = 10
2. 
3. fn main() {
4. 	qt_bolas_selecionadas := 1
5. 	
6. 	if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {		
7. 		println("asdf")
8. 	}	
9. 
10. 	if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {		
11. 		println("asdf")
12. 	}	
13. 
14. 	if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {		
15. 		println("asdf")
16. 	}	
17. }

The error is:
este2.v:1:7: warning: const names cannot contain uppercase letters, use snake_case instead
1 | const CMB_CONJUNTO_QT_DE_BOLAS = 10
| ~~~~~~~~~~~~~~~~~~~~~~~~
2 |
3 | fn main() {
teste2.v:10:2: error: unexpected keyword if, expecting {
8 | }
9 |
10 | if qt_bolas_selecionadas == CMB_CONJUNTO_QT_DE_BOLAS {
| ~~
11 | println("asdf")
12 | }

@JalonSolov
Copy link
Contributor

It doesn't matter how many examples you give. It is all because V should not allow the uppercase const value, and is misidentifying the other errors because of that.

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

Successfully merging a pull request may close this issue.

2 participants