Comments (4)
If I understand you correctly - you are expecting this code not to work
No, I never said your code isn't supposed to work.
More practical example of this:
...
This code will work but again compiler will output 3 warnings 240.
Yes, because variable i
gets written to up to 4 times (the actual number of writes depends on the contents of the input text), but never gets read until it's used in printf
- again, not because your code doesn't work, but because of the way how assignments work as sub-expressions (you can re-read my previous post for a more thorough explanation).
Although I do agree that having warning 240 in such situations might be undesirable, I'm not sure how to reasonably "fix" this without sacrificing potential for detecting actual mistakes. In other words, what you have here is simply a rare corner-case, and I don't think there's a way to make the compiler distinguish it from other situations when there's a real mistake made by the user.
In any case, for now you can either
- use
#pragma warning push/disable 240/pop
to temporarily disable the diagnostic, or - simply add extra code to actually read from the variable before it gets assigned the next value:
while((i = strfind(string, "!"), i != -1) || (i = strfind(string, ","), i != -1) || (i = strfind(string, "."), i != -1) || (i = strfind(string, "?")) != -1) {
but then you'll end up with a slightly slower compiled code, so I'd recommend using option 1.
from compiler.
In (i = 1) == 1
you might expect variable i
being read before executing operator ==
, but it's not.
Assignments in Pawn (and many other C-like languages) not only store the assigned value into a variable, but also yield it as a result of the assignment operation, so in (i = 1) == 1
operand 1
(the first one) also serves as a result of sub-expression (i = 1)
- the compiler doesn't generate any code for reading from variable i
at all, it just reuses the assigned value. Why read from the variable if you already have the needed value at hand?
As a result, when you assign another value to i
(i = 2
), that warning 240 message is actually correct.
TL;DR: This is not a bug, this is how assignments in C-like languages work.
from compiler.
If I understand you correctly - you are expecting this code not to work:
#include < console >
main() {
new i;
if ((i = 5) == 2 || (i = 3) == 3 || (i = 4) == 4) { // 2x warning 240
printf("OK! %d", i); // Output: OK! 3
}
}
More practical example of this:
#include < console >
#include < string >
main() {
new i, string[] = "S,o.m!e?thing";
while((i = strfind(string, "!")) != -1 || (i = strfind(string, ",")) != -1 || (i = strfind(string, ".")) != -1 || (i = strfind(string, "?")) != -1) {
printf("Char %c (idx %d) must be deleted", string[i], i);
strdel(string, i, i + 1); // Avoid infinite loop
}
}
This code will work but again compiler will output 3 warnings 240.
from compiler.
Although I do agree that having warning 240 in such situations might be undesirable, I'm not sure how to reasonably "fix" this without sacrificing potential for detecting actual mistakes. In other words, what you have here is simply a rare corner-case, and I don't think there's a way to make the compiler distinguish it from other situations when there's a real mistake made by the user.
Understandable.
Thanks.
from compiler.
Related Issues (20)
- Stop Compiler
- Samp includes error HOT 1
- Packed string access in enum arrays
- Optimization of conditional expressions
- Can't compile on linux! HOT 2
- when to wait for the next build? HOT 1
- Classes and objects in PAWN? HOT 3
- Named only parameters. HOT 3
- External natives are always in the header, even when not used. HOT 2
- Constant "Run time error 19: "File or function is not found"" error
- default include path for standard libraries? HOT 1
- The Application was unable to start correctly.(0xc000007b) HOT 1
- fatal error 100: cannot read from file: "Pawn.CMD" HOT 1
- fatal error 100: cannot read from file: "interception"
- [DIY] Build from source on Apple Chip M1 using rosetta2 HOT 6
- `initialization data exceeds declared size` when using progressive initialization with an array size 1
- `const` can be specified for a function argument more than once
- error compile
- pawnc reports wrong filenames and line numbers on warning number 203 & 213
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from compiler.