clang icon indicating copy to clipboard operation
clang copied to clipboard

Metaclass member injection breaks semantic analysis

Open Jenny-fa opened this issue 7 years ago • 0 comments

The invocation of Sema::InjectMetaclassMembers() in Sema::ActOnFields() appears to break semantic analysis of metaclass members that are injected into the definition of a class prototype.

In the following example, two member functions in the metaclass IPoint reference member variables whose declarations appear later in the metaclass' definition. Ordinarily (that is, within the definition of a regular class), this would be perfectly valid C++.

The current iteration of Clang disagrees.

// simple-metaclass.cpp

$class IPoint {
  double getX() const { return x; }
  double getY() const { return y; }

  double x;
  double y;
}

IPoint Point {
  // ...
};

int main() {
  Point p1;
  // Do other stuff...
  return 0;
}
$ clang++ -std=c++11 -Xclang -freflection -o simple-metaclass simple-metaclass.cpp
simple-metaclass.cpp:4:32: error: 'x' is not a member of class 'const Point'
  double getX() const { return x; }
                               ^
simple-metaclass.cpp:5:32: error: 'y' is not a member of class 'const Point'
  double getY() const { return y; }
                               ^
2 errors generated.

EDIT: The same error occurs even when the member variables precede the member functions. :woman_facepalming:

// simple-metaclass.cpp

$class IPoint {
  double x;
  double y;

  double getX() const { return x; }
  double getY() const { return y; }
}

IPoint Point {
  // ...
};

int main() {
  Point p1;
  // Do other stuff...
  return 0;
}
$ clang++ -std=c++11 -Xclang -freflection -o simple-metaclass simple-metaclass.cpp 
simple-metaclass.cpp:7:32: error: 'x' is not a member of class 'const Point'
  double getX() const { return x; }
                               ^
simple-metaclass.cpp:8:32: error: 'y' is not a member of class 'const Point'
  double getY() const { return y; }
                               ^
2 errors generated.

Jenny-fa avatar Apr 05 '17 00:04 Jenny-fa