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

Structs defined inside functions behave as global definitions #22437

Open
zeozeozeo opened this issue Oct 7, 2024 · 0 comments
Open

Structs defined inside functions behave as global definitions #22437

zeozeozeo opened this issue Oct 7, 2024 · 0 comments
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@zeozeozeo
Copy link
Contributor

zeozeozeo commented Oct 7, 2024

Describe the bug

Structs defined inside functions act as if they were defined in global scope. This allows structs to be accessed outside their scope of definition.

Reproduction Steps

fn main() {
	struct Foobar {
		foo int
		bar int
	}
	fb := Foobar{5, 6}
	println(fb.foo) // 5
	x()
}

fn x() {
	fb := Foobar{7, 8}
	println(fb.bar) // 8
}

Expected Behavior

Structs defined inside functions should have local scope and should not be accessible outside their defining function. Attempting to access such structs outside their scope should result in a compilation error.

Current Behavior

5
8

Possible Solution

It may be possible to remove the type symbol from the table after the parser ends the current scope.

Additional Information/Context

Struct definitions inside functions were added in a2d385a.

V version

V 0.4.8 d3c3c39

Environment details (OS name and version, etc.)

V full version: V 0.4.8 423f804.d3c3c39
OS: windows, Microsoft Windows 10 Pro v19045 64-bit
Processor: 10 cpus, 64bit, little endian,

getwd: C:\Users\user\Downloads
vexe: C:\Users\user\Desktop\Code\v\v.exe
vexe mtime: 2024-10-07 15:20:28

vroot: OK, value: C:\Users\user\Desktop\Code\v
VMODULES: OK, value: C:\Users\user\.vmodules
VTMP: OK, value: C:\Users\user\AppData\Local\Temp\v_0

Git version: git version 2.42.0.windows.1
Git vroot status: weekly.2024.39-98-gd3c3c391
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 b425ac82

Huly®: V_0.6-20898

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.

@zeozeozeo zeozeozeo added the Bug This tag is applied to issues which reports bugs. label Oct 7, 2024
@spytheman spytheman added Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Status: Confirmed This bug has been confirmed to be valid by a contributor. labels Oct 7, 2024
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. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

No branches or pull requests

2 participants