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

Sum Type Field Access Fails Inside closure #23716

Closed
bbodi opened this issue Feb 13, 2025 · 1 comment · Fixed by #23731
Closed

Sum Type Field Access Fails Inside closure #23716

bbodi opened this issue Feb 13, 2025 · 1 comment · Fixed by #23731
Assignees
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: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@bbodi
Copy link

bbodi commented Feb 13, 2025

Describe the bug

When using a sum type (Type = Aa | Bb) inside a function literal (fn[t]), the compiler fails to recognize the specific type variant of t and treats it as the general Type, rather than the matched variant (Aa).

Reproduction Steps

struct Aa {a int}
struct Bb {b int}
type Type = Aa | Bb

fn main() {
	t := Type(Aa{})
	match t {
		Aa {
			println(t.a)            // works
			func := fn[t] () {
				println(typeof(t))  // works, prints "Aa"
				// println(t.a) // error: field `a` does not exist or have the same type in these sumtype `Type` variants: Bb
			}
			func()
		}
		Bb {}
	}
}

Expected Behavior

The expected behavior is that inside the function literal (fn[t]), the compiler should preserve the type information from the surrounding match branch. Since t is already matched as Aa, it should be treated as an Aa inside the function, allowing direct access to t.a without requiring additional casting.

Current Behavior

t's type inside the closure is the sumtyp (Type), and not Aa

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.9 5be2fca

Environment details (OS name and version, etc.)

V full version V 0.4.9 5be2fca
OS linux, Ubuntu 24.10
Processor 16 cpus, 64bit, little endian, AMD Ryzen 7 8840U w/ Radeon 780M Graphics
Memory 9.51GB/22.78GB
V executable /home/sharp/dev/v/v
V last modified time 2025-02-13 12:21:16
V home dir OK, value: /home/sharp/dev/v
VMODULES OK, value: /home/sharp/.vmodules
VTMP OK, value: /tmp/v_1000
Git version git version 2.45.2
V git status weekly.2025.03-35-g5be2fcab-dirty (99 commit(s) behind V master)
.git/config present true
cc version cc (Ubuntu 14.2.0-4ubuntu2) 14.2.0
gcc version gcc (Ubuntu 14.2.0-4ubuntu2) 14.2.0
clang version Ubuntu clang version 19.1.1 (1ubuntu1)
tcc version tcc version 0.9.28rc 2024-07-31 HEAD@1cee0908 (x86_64 Linux)
tcc git status thirdparty-linux-amd64 0134e9b9
emcc version emcc (Emscripten gcc/clang-like replacement + linker emulating GNU ld) 3.1.6 ()
glibc version ldd (Ubuntu GLIBC 2.40-1ubuntu3.1) 2.40

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.

@bbodi bbodi added the Bug This tag is applied to issues which reports bugs. label Feb 13, 2025
Copy link

Connected to Huly®: V_0.6-22134

@JalonSolov JalonSolov added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Feb 13, 2025
@felipensp felipensp self-assigned this Feb 15, 2025
@felipensp felipensp added Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Feb 15, 2025
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: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants