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

Show where bad union code is located #22195

Closed
jorgeluismireles opened this issue Sep 10, 2024 · 2 comments · Fixed by #22196
Closed

Show where bad union code is located #22195

jorgeluismireles opened this issue Sep 10, 2024 · 2 comments · Fixed by #22196
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Nicer V Errors Bugs/feature requests, related to improving V error messages. 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

@jorgeluismireles
Copy link

jorgeluismireles commented Sep 10, 2024

Describe the bug

Next code yields correctly an union use error, but the compiler message doesn't indicate where the offending code is located. Specially when the bad code is in another imported module, it is very difficult to find it.

pub struct Lines {
pub:
	from u8
	to   u8
	dst  u8
	src  u8
}

pub union Head {
	Lines
pub:
	serial [3]u8
}

pub struct Message {
	Head
pub:
	cmd     u8
	payload []u8
}

fn new_code_using_union_correctly() {
	m := Message{
		Head: Head{ serial: [u8(1),2,3]! }
	}
	assert unsafe { m.serial[0] == 1 }
	n := Message{
		Head: Head{ 
			Lines: Lines{ dst:11, src:10 } 
		} 
	}
	assert unsafe { n.dst == 11 }
}

// ...

fn old_code_that_worked_before_changing_message_to_union() {
	m := Message{
		from: 1  // <- INDICATE HERE IS THE PROBLEM
		src:  1
	}
	println(m)
}

// ...

Error without the details to find the problem:

$ v run union.v 
cgen error: union must not have more than 1 initializer

Reproduction Steps

Run the above code and get the incomplete (to say) compiler message.

Expected Behavior

The description of the error, in this case cgen error: union must not have more than 1 initializer but add the file name and the lines where the problem was found, like in the rest of compiler indications.

Current Behavior

Simple compiler message without helping to find the bad code.

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.7 6aeef5e

Environment details (OS name and version, etc.)

V full version: V 0.4.7 2cde320.6aeef5e
OS: linux, Ubuntu 20.04.6 LTS
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-4150 CPU @ 3.50GHz

Git version: git version 2.25.1
Git vroot status: weekly.2024.29-254-g6aeef5e4
.git/config present: true

CC version: cc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
thirdparty/tcc status: thirdparty-linux-amd64 a0799a5

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.

@jorgeluismireles jorgeluismireles added the Bug This tag is applied to issues which reports bugs. label Sep 10, 2024
@Delta456 Delta456 self-assigned this Sep 10, 2024
@felipensp felipensp added the Unit: Checker Bugs/feature requests, that are related to the type checker. label Sep 10, 2024
@spytheman spytheman added Nicer V Errors Bugs/feature requests, related to improving V error messages. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. labels Sep 11, 2024
@medvednikov
Copy link
Member

I didn't even know you could embed a union like that :) This feature needs testing.

@jorgeluismireles
Copy link
Author

I did a v up: V 0.4.7 5fcfc27 and find my original solution code didn't work anymore. But the next change fixes the problem. I guess it should be the correct way, since Lines already is embedded, should avoid to embed also Head:

pub struct Lines {
pub:
	from u8
	to   u8
	dst  u8
	src  u8
}

pub union Head {
	Lines
	serial [3]u8
}

pub struct Message {
	head Head // <--- changed (was Head embedded)
pub:
	cmd     u8
	payload []u8
}

fn new_code_using_union_correctly() {
	m := Message{
		head: Head{ // <-- changed (was Head: Head {)
			serial: [u8(1),2,3]!
		}
	}
	assert unsafe { m.head.serial[0] == 1 }
	
	n := Message{
		head: Head{ // <-- changed (was Head: Head{)
			Lines: Lines{ dst:11, src:10 } 
		} 
	}
	assert unsafe { n.head.dst == 11 } // <-- changed (was h.dst==11)
}

Now the compiler tells the programmer where is the 'old' code that should be changed.

questions/union.v:39:3: error: unknown field `from` in struct literal of type `Message`.
3 possibilities: `cmd`, `head`, `payload`.
   37 | fn old_code_that_worked_before_changing_message_to_union() {
   38 |     m := Message{
   39 |         from: 1  // <- INDICATE HERE IS THE PROBLEM
      |         ~~~~~~~
   40 |         src:  1
   41 |     }

Thanks!

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. Nicer V Errors Bugs/feature requests, related to improving V error messages. 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.

5 participants