Skip to content

checker: fix immutable to mutable reference#22663

Merged
spytheman merged 38 commits intovlang:masterfrom
felipensp:fix_immutable_to_mutable
Dec 10, 2024
Merged

checker: fix immutable to mutable reference#22663
spytheman merged 38 commits intovlang:masterfrom
felipensp:fix_immutable_to_mutable

Conversation

@felipensp
Copy link
Copy Markdown
Member

@felipensp felipensp commented Oct 26, 2024

Fix #22653

Fixes cases like this:

fn main() {
	a := ['foo', 'bar']
	mut b := []string{}
	b = if true { a } else { a } 	
	b[0] = 'bar'
	dump(a) // ['bar', 'bar']
}

Huly®: V_0.6-21112

@felipensp felipensp marked this pull request as ready for review October 28, 2024 19:41
@felipensp
Copy link
Copy Markdown
Member Author

What do you think @medvednikov @spytheman ?

@felipensp
Copy link
Copy Markdown
Member Author

The failing CI here is really about breakage related to more restricter compiler checks.

@spytheman spytheman force-pushed the fix_immutable_to_mutable branch from b75f10a to 59273c3 Compare November 20, 2024 09:20
@spytheman
Copy link
Copy Markdown
Contributor

(rebased over current master)

@spytheman
Copy link
Copy Markdown
Contributor

spytheman commented Nov 20, 2024

I think that is a very good change, but it should be a notice for a few weeks/months, to avoid sudden breaking changes to existing projects, and allow them to fix the new notices more gradually. It will also solve the CI issues here, and allow for running regression tests with .github/workflows/compare_pr_to_master.v (which needs to compile master and the branch with different versions of the compiler, which is prevented if one of the versions has stricter errors for the older codebase).

@felipensp
Copy link
Copy Markdown
Member Author

I think that is a very good change, but it should be a notice for a few weeks/months, to avoid sudden breaking changes to existing projects, and allow them to fix the new notices more gradually. It will also solve the CI issues here, and allow for running regression tests with .github/workflows/compare_pr_to_master.v (which needs to compile master and the branch with different versions of the compiler, which is prevented if one of the versions has stricter errors for the older codebase).

I've changed to notice. 👍🏻

@spytheman
Copy link
Copy Markdown
Contributor

Thank you @felipensp .

Comment thread vlib/v/checker/checker.v
}
if !right.obj.is_mut
&& c.table.final_sym(right_type).kind in [.array, .array_fixed, .map] {
c.note('left-side of assignment expects a mutable reference, but variable `${right.name}` is immutable, declare it with `mut` to make it mutable or clone it',
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Perhaps we can mention outside unsafe{} too, but that can wait till later.
The CI is almost finished, and it is working well as it is.

@spytheman spytheman merged commit 7d47219 into vlang:master Dec 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

checker: mutability of if-expression branch returns is not enforced

2 participants