Code Monkey home page Code Monkey logo

Comments (4)

Daniel-Cortez avatar Daniel-Cortez commented on August 29, 2024 1

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

  1. use #pragma warning push/disable 240/pop to temporarily disable the diagnostic, or
  2. 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.

Daniel-Cortez avatar Daniel-Cortez commented on August 29, 2024

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.

xunder-matth avatar xunder-matth commented on August 29, 2024

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.

xunder-matth avatar xunder-matth commented on August 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.