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

breaking,checker: disallow initializing private struct fields outside structs module #21183

Merged
merged 17 commits into from
Apr 12, 2024

Conversation

ttytm
Copy link
Member

@ttytm ttytm commented Apr 4, 2024

Currently, it's possible to init a struct and assign values to it's private fields. Only accessing fields directly will trigger an error.

Currently following is possible

import amod

b := amod.Bcg{ // public struct
	x: 3 // private field
}

After the changes an error will be visible on initialization (ref. to added test:struct_field_private_err.out). Eventually the change should allow for better accessibility control and improved security.

@ttytm ttytm force-pushed the checker/disallow-init-struct-priv-fields branch from 7596007 to 40766dd Compare April 4, 2024 07:21
@ttytm
Copy link
Member Author

ttytm commented Apr 4, 2024

Some of the remaining errors might require discussion as not all fields of them might be meant for a pub mut. Gonna make a list of the ones in question.

@ttytm ttytm force-pushed the checker/disallow-init-struct-priv-fields branch 3 times, most recently from 52c43a8 to 47ca871 Compare April 4, 2024 09:56
@ttytm ttytm marked this pull request as draft April 4, 2024 09:57
@ttytm ttytm added the Breaking Change This PR introduces changes that break backward compatibility. Requires manual review. label Apr 5, 2024
@ttytm ttytm force-pushed the checker/disallow-init-struct-priv-fields branch 6 times, most recently from c6899c0 to 1ee4450 Compare April 5, 2024 15:56
vlib/cli/flag_test.v Show resolved Hide resolved
Comment on lines 11 to 13
pub mut:
sock TcpSocket
mut:
sock TcpSocket
handle int
Copy link
Member Author

@ttytm ttytm Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imho the handle should eventually not be publicly mutable. But to not introduce a big breaking change for how TcpConn structs are currently created, having it pub mut is likely the only way.

@ttytm ttytm marked this pull request as ready for review April 5, 2024 15:59
@ttytm ttytm force-pushed the checker/disallow-init-struct-priv-fields branch from 1ee4450 to 08c02a9 Compare April 5, 2024 16:14
@ttytm
Copy link
Member Author

ttytm commented Apr 5, 2024

@spytheman you have push access to https://github.com/elliotchance/vsql? If not maybe it's okay to comment it's CI step out for now as the developer is not active for the last months.

The only left failing in the V codebase is the parser.Parser fields related to cmd/tools/measure/fmt_speed.v. I'm not sure about accessibility changes to those fields.
Update: I've set the interfering to public for now.

@ttytm ttytm force-pushed the checker/disallow-init-struct-priv-fields branch 3 times, most recently from a0ca98a to a18ddd6 Compare April 5, 2024 17:14
vlib/v/checker/struct.v Outdated Show resolved Hide resolved
Copy link
Member

@spytheman spytheman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman
Copy link
Member

spytheman commented Apr 6, 2024

@spytheman you have push access to https://github.com/elliotchance/vsql? If not maybe it's okay to comment it's CI step out for now as the developer is not active for the last months.

I do not, but @elliotchance is on Discord, so I think we can coordinate a change with a PR.

I do not want to disable the CI for it on the main repo, if we can avoid it, since it is a very substantial code base, and it has caught V bugs/regressions before.

@JalonSolov
Copy link
Contributor

I have a question about this PR... how am I supposed to initialize a private field in a struct from another module, if I can't do it at instantiation time?

With this change in place, every module that has a struct with a private field will now have to supply a new or create or whatever method for that struct, in order to initialize those fields.

Either that or some sort of setter method just for those fields.

@ttytm
Copy link
Member Author

ttytm commented Apr 6, 2024

Private struct fields are usually not not meant to be initialised from other modules.
For the cases where a private struct field should be initialised a factory function like it is with @[noinit] will be required.

*check with tests above:
the tests don't test any other/new functionality just include private fields.
@ttytm ttytm force-pushed the checker/disallow-init-struct-priv-fields branch from 81cc83c to ff45e89 Compare April 8, 2024 16:38
@ttytm
Copy link
Member Author

ttytm commented Apr 8, 2024

I temporary added a fork of external V apps to the CI.

The only one remaining (with more restrictive merge access) is the PR at gitly.

@ttytm ttytm force-pushed the checker/disallow-init-struct-priv-fields branch from ff45e89 to c1f8f83 Compare April 8, 2024 19:45
@ttytm ttytm force-pushed the checker/disallow-init-struct-priv-fields branch from c1f8f83 to fbc69a4 Compare April 8, 2024 22:39
@spytheman
Copy link
Member

The gitly and the ui failures are now fixed too.

I think it is good to go.

@spytheman spytheman changed the title checker: disallow initializing private struct fields outside structs module breakin,checker: disallow initializing private struct fields outside structs module Apr 9, 2024
@spytheman spytheman changed the title breakin,checker: disallow initializing private struct fields outside structs module breaking,checker: disallow initializing private struct fields outside structs module Apr 9, 2024
vlib/v/checker/struct.v Outdated Show resolved Hide resolved
@ttytm ttytm force-pushed the checker/disallow-init-struct-priv-fields branch from 246cc89 to 06b6b9a Compare April 9, 2024 21:47
@spytheman spytheman merged commit 1a35a78 into vlang:master Apr 12, 2024
56 checks passed
@ttytm ttytm deleted the checker/disallow-init-struct-priv-fields branch April 23, 2024 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Breaking Change This PR introduces changes that break backward compatibility. Requires manual review.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants