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

Fix code snippet to discard a pointer in "naughty pointer" #281

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

haleemur
Copy link

@haleemur haleemur commented Nov 18, 2024

Description

Fix guide for versions 0.14 & 0.13

Fix typo in code snippet. Discarding the variable should be _ = &y; instead of _ = y;.

Replace var x: u16 = 0; with var x: u16 = 5; x -= 5;. Else the compiler complains with error: local variable is never mutated. The updated example also highlights that the compiler is able to detect the value of x is not 0 after a mutation.

Note that const x: u16 = 0 would have resulted in a different error message instead of panic: cast causes pointer to be null.

resolves: #275

fix typo in code snippet. Discarding the variable should be `_ = &y;` instead of `_ = y;`.

replace `var x: u16 = 0;` with `var x: u16 = 5; x -= 5;`. Else the compiler complains with `error: local variable is never mutated`. The updated example also highlights that the compiler is able to detect the value of x is not 0 through a mutation
apply fix to guide for version 0.14
@haleemur haleemur changed the title Fix snippet pointer discard var Fix code snippet to discard a pointer in "naughty pointer" Nov 18, 2024
@mlugg
Copy link

mlugg commented Nov 18, 2024

Please don't use this discard strategy; that it works is unfortunate and might change in the future. Instead, do something more obviously silly like y = y;. This is because while unused local constants sometimes have legitimate use cases, so _ = foo discards for those are okay, hiding this "local variable is never mutated" error is never correct in real Zig code, so teaching what looks like a supported way to hide it in counterproductive.

@haleemur
Copy link
Author

Thank you for the suggestion @mlugg. I replaced _ = &y; with y = y as you suggest.

@haleemur
Copy link
Author

Hi @Sobeston, Would you be able to take a look at this please. I have addressed @mlugg's comment above.

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.

Test "naughty pointer" does not compile
2 participants