Odin icon indicating copy to clipboard operation
Odin copied to clipboard

Problem with shadowing ( -vet )

Open ghost opened this issue 3 years ago • 4 comments

Context

package is_a_bug

import "core:fmt"

main :: proc()
{
	my_value := 3; 

	{
		my_value := "String";
		fmt.println(my_value);
	}

	fmt.println(my_value);
}
  • Operating System & Odin Version:
	Odin: dev-2022-01:cfbc1a44
	OS:   Arch Linux, Linux 5.16.1-arch1-1
	CPU:  AMD Ryzen 7 3800X 8-Core Processor             
	RAM:  16010 MiB

Expected Behavior

Should throw an error for redeclaration of my_value.

Current Behavior

Odin compiles this with or without the -vet flag. Basically if i redeclare a variable in the same scope i get an error so I assumed i should get an error even if it is redeclared it in a inner scope but that's not the case with or without the -vet flag.

ghost avatar Jan 22 '22 09:01 ghost

I think this was already discussed somewhere else. And it doesn’t make a lot of sense to throw an error when you explicitly make a new scope. But that’s a @gingerBill topic. I would understand a warning or hint bug that at the utter most.

Platin21 avatar Jan 22 '22 10:01 Platin21

Turns out -vet only currently works in case of re-declaring variables of the same type.

ghost avatar Jan 22 '22 10:01 ghost

here is another finding: -vet variable shadowing detection will not work with generic procs

the following will trigger the shadowing error as expected (both type and name match):

foo :: proc() -> int { return {} }

  bar := foo(); fmt.println(bar)
{ bar := foo(); fmt.println(bar) }

Declaration of 'bar' shadows declaration at line [...]

however, if you make the proc generic, then it will compile fine and no longer report an error:

foo :: proc(_: $T) -> int { return {} } //generic

  bar := foo(42); fmt.println(bar)
{ bar := foo(42); fmt.println(bar) }

note that the return type of the proc stayed the same (int) both variables (bar and scoped bar) have matching name and matching type and should therefore trigger the vet shadowing error

awwdev avatar Jan 23 '22 08:01 awwdev

Hello!

I am marking this issue as stale as it has not received any engagement from the community or maintainers 120 days. That does not imply that the issue has no merit! If you feel strongly about this issue

  • open a PR referencing and resolving the issue;
  • leave a comment on it and discuss ideas how you could contribute towards resolving it;
  • leave a comment and describe in detail why this issue is critical for your use case;
  • open a new issue with updated details and a plan on resolving the issue.

The motivation for this automation is to help prioritize issues in the backlog and not ignore, reject, or belittle anyone..

github-actions[bot] avatar Jul 24 '22 22:07 github-actions[bot]