Skip to content

Error compiling .ino files with functions returning certain types #1683

@cjastone

Description

@cjastone

Configuration

Operating system: Windows 10

PlatformIO Version (platformio --version): 3.6.0a2

Description of problem

When building an .ino file with functions returning reference types, the file is not correctly preprocessed. If the functions are not declared in the order in which they are called, the build fails with "function was not declared in this scope". The same code compiles without error in the Arduino IDE.

Other types of functions are not affected by this and can be declared in any order without breaking anything.

Steps to Reproduce

  1. Create a function that returns a reference type (see below for an example)
  2. Create a function that calls the function that returns the reference type. Ensure this second function is declared before the function that it calls
  3. Build project - failure will result with "function not declared in this scope" error
  4. Swap order of functions so declaration occurs before call
  5. Build project - will build successfully.

Actual Results

bug_report.ino: In function 'void normalFunction()':
bug_report.ino:5:35: error: 'jsonFunction' was not declared in this scope
JsonObject& json = jsonFunction();

Expected Results

Project should build successfully.

If problems with PlatformIO Build System:

The content of platformio.ini:

[env:esp12e]
platform = espressif8266
board = esp12e
framework = arduino

[platformio]
src_dir = bug_report
lib_dir = ~\Documents\Arduino\libraries

Source file to reproduce issue:

#include "Arduino.h"
#include "ArduinoJson.h"

void normalFunction() {
  JsonObject& json = jsonFunction();
}

JsonObject& jsonFunction() {
  return JsonObject::invalid();
}

void setup() {}
void loop() {}

Additional info

Playing around, with this a bit more, I've discovered that this also affects functions returning a float.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions