Found strange comment bug while compiling code

I am new here, hopefully this is the right place to post this...

So I was creating code for animating some LED strips and ran into a bug that took me 2 hours to find, and I have no idea what is happening here.

So basically, the current state of the code results in an error while trying to compile. I found that in line 2, where the comment contains the text "loop", if you get rid of the word "loop", the program compile w/o errors. But with the word "loop" it errors. But as you can see, there's another comment with "loop" in it (line 1), but that doesn't have any effect like the one in line 2. (The code is a new file with a few things copied from original code until it gave an error and I found it was the word "loop" in the comment on line 2)

I'm honestly so confused as to why it would fail to compile based on the word "loop" being in a comment after declaring a boolean variable? (I got my original program working again, just thought this was really strange)

// COMMENT: loop
boolean f = false;        // loop

int r = 255;
int g = 0;
int b = 0;

// Define color structure for rgb
struct color {
  int r;
  int g;
  int b;
};
typedef struct color Color;

void setup() {
  Serial.begin(9600);
}

void loop() {
  delay(100);
}


void setColor(Color *c, int r, int g, int b) {
  c->r = r;
  c->g = g;
  c->b = b;
}

This is caused by a failure of the Arduino IDE’s automated function prototype generation system. When you encounter a strange error like this, it is a good idea to check the preprocessed sketch output, which is saved to the Arduino IDE’s temporary build folder. You can do that like so:

  • File > Preferences
  • Check the box next to "Show verbose output during: compilation’
  • Click “OK”
  • Sketch > Verify/Compile
  • After compilation fails, scroll the black console window at the bottom of the Arduino IDE window all the way to the top.
  • Examine the first line of output to find the value of the “-build-path” option.

For example, if you had this:

C:\ArduinoIDE\arduino-nightly\arduino-builder -dump-prefs -logger=machine -hardware C:\ArduinoIDE\arduino-nightly\hardware -hardware C:\Users\per\AppData\Local\Arduino15\packages -hardware E:\electronics\arduino\hardware -tools C:\ArduinoIDE\arduino-nightly\tools-builder -tools C:\ArduinoIDE\arduino-nightly\hardware\tools\avr -tools C:\Users\per\AppData\Local\Arduino15\packages -built-in-libraries C:\ArduinoIDE\arduino-nightly\libraries -libraries E:\electronics\arduino\libraries -fqbn=esp32:esp32:node32s:FlashFreq=80,UploadSpeed=921600 -vid-pid=0X2341_0X0042 -ide-version=10809 -build-path C:\Users\per\AppData\Local\Temp\arduino_build_889992 -warnings=all -build-cache C:\Users\per\AppData\Local\Temp\arduino_cache_764477 -prefs=build.warn_data_percentage=75 -prefs=runtime.tools.esptool.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\esptool\2.3.1 -prefs=runtime.tools.esptool-2.3.1.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\esptool\2.3.1 -prefs=runtime.tools.mkspiffs.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\mkspiffs\0.2.3 -prefs=runtime.tools.mkspiffs-0.2.3.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\mkspiffs\0.2.3 -prefs=runtime.tools.xtensa-esp32-elf-gcc.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0 -prefs=runtime.tools.xtensa-esp32-elf-gcc-1.22.0-80-g6c4433a-5.2.0.path=C:\Users\per\AppData\Local\Arduino15\packages\esp32\tools\xtensa-esp32-elf-gcc\1.22.0-80-g6c4433a-5.2.0 -verbose C:\Users\per\AppData\Local\Temp\arduino_modified_sketch_19197\sketch_dec31a.ino

the temporary build folder is C:\Users\per\AppData\Local\Temp\arduino_build_889992.

Open the .ino.cpp file in the sketch subfolder of the temporary build folder with a text editor.

In this case, the preprocessed sketch output looks like this

#include <Arduino.h>
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_661892\\sketch_dec31a.ino"
#line 1 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_661892\\sketch_dec31a.ino"
// COMMENT: loop
#line 16 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_661892\\sketch_dec31a.ino"
void setup();
#line 20 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_661892\\sketch_dec31a.ino"
void loop();
#line 25 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_661892\\sketch_dec31a.ino"
void setColor(Color *c, int r, int g, int b);
#line 2 "C:\\Users\\per\\AppData\\Local\\Temp\\arduino_modified_sketch_661892\\sketch_dec31a.ino"
boolean f = false;        // loop

int r = 255;
int g = 0;
int b = 0;

// Define color structure for rgb
struct color {
  int r;
  int g;
  int b;
};
typedef struct color Color;

void setup() {
  Serial.begin(9600);
}

void loop() {
  delay(100);
}


void setColor(Color *c, int r, int g, int b) {
  c->r = r;
  c->g = g;
  c->b = b;
}

You can see that the setColor prototype was inserted before the definition of the Color type.

You’ll be happy to know that this bug has already been reported and fixed. If you install the hourly build of the Arduino IDE:

you’ll find that the bug no longer occurs.