arduino-builder icon indicating copy to clipboard operation
arduino-builder copied to clipboard

Incorrect placement of auto-generated prototypes gives misleading error messages

Open 6v6gt-duino opened this issue 4 years ago • 4 comments

This issue is also mentioned here: https://forum.arduino.cc/index.php?topic=711626.0 (post #7)

The expected behaviour is that this simple code example should compile:

// Arduino IDE 1.8.13

enum class StateA { one, two, three } ;
StateA stateA ;

void setStateA( StateA newState ) {
  stateA = newState ;
}

enum class StateB { one, two, three } ;
StateB stateB ;

void setStateB( StateB newState ) {
  stateB = newState ;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin( 115200 ) ;
  setStateA( StateA::one ) ;
  setStateB( StateB::one ) ;
}

void loop() {
}

The observed behaviour is this:

sketch_oct10a:11:17: error: variable or field 'setStateB' declared void
 void setStateB( StateB newState ) {
                 ^~~~~~
sketch_oct10a:11:17: error: 'StateB' was not declared in this scope
C:\Users\6V6GT\Documents\Arduino\sketch_oct10a\sketch_oct10a.ino:11:17: note: suggested alternative: 'StateA'
 void setStateB( StateB newState ) {
                 ^~~~~~
                 StateA
exit status 1
variable or field 'setStateB' declared void

The reason is clearly that one auto-generated function prototype has been wrongly placed (from the file ino.cpp ):

#include <Arduino.h>
#line 1 "C:\\Users\\6V6GT\\Documents\\Arduino\\sketch_oct10a\\sketch_oct10a.ino"
enum class StateA { one, two, three } ;
StateA stateA ;

#line 4 "C:\\Users\\6V6GT\\Documents\\Arduino\\sketch_oct10a\\sketch_oct10a.ino"
void setStateA( StateA newState );
#line 11 "C:\\Users\\6V6GT\\Documents\\Arduino\\sketch_oct10a\\sketch_oct10a.ino"
void setStateB( StateB newState );
#line 15 "C:\\Users\\6V6GT\\Documents\\Arduino\\sketch_oct10a\\sketch_oct10a.ino"
void setup();
#line 22 "C:\\Users\\6V6GT\\Documents\\Arduino\\sketch_oct10a\\sketch_oct10a.ino"
void loop();
#line 4 "C:\\Users\\6V6GT\\Documents\\Arduino\\sketch_oct10a\\sketch_oct10a.ino"
void setStateA( StateA newState ) {
  stateA = newState ;
}

enum class StateB { one, two, three } ;
StateB stateB ;

void setStateB( StateB newState ) {
  stateB = newState ;
}

void setup() {
  // put your setup code here, to run once:
  Serial.begin( 115200 ) ;
  setStateA( StateA::one ) ;
  setStateB( StateB::one ) ;
}

void loop() {
}

6v6gt-duino avatar Nov 01 '20 11:11 6v6gt-duino

Hi @6v6gt-duino , thank for reporting! The issue lies in the prototype generator as you noticed. What bothers me is that both functions are already declared before their usage so the prototype generator shouldn't create anything. We'll investigate soon and report back

facchinm avatar Nov 02 '20 09:11 facchinm

Here is another link including a discussion of this issue and including additional error/test cases: https://forum.arduino.cc/index.php?topic=718515.0

6v6gt-duino avatar Dec 17 '20 07:12 6v6gt-duino

This topic is now nearly 3 years old. The problem persists in IDE 2.1.1.

We'll investigate soon and report back

A concerned Arduino user ;)

sterretjeToo avatar Jul 19 '23 06:07 sterretjeToo

That gives me the opportunity of adding another example which has occurred in the meantime: https://forum.arduino.cc/t/is-the-location-of-a-global-function-important/996378

6v6gt-duino avatar Jul 19 '23 07:07 6v6gt-duino