jamesjuett / lobster

Interactive Program Visualization Tools
8 stars 3 forks source link

Lobster doesn't recognize member function constness #291

Closed bkayes closed 2 years ago

bkayes commented 2 years ago
#include <iostream>

using namespace std;

class test {
  int i;
  test() : i(0) {}
  int func() const {
    i++;
    ++i;
    return i;
  }

  int func2() const {
    i *= 2;
    return i;
  }
};

int main() {
  test t;
  cout << t.func();
  cout << t.func2();
  cout << t.func();
}

The above compiles and runs on lobster, which is confusing to students

Notice pre/post increment and multiplication (These are the only things I tried, I bet others would not cause it fail compilation either).

iteemhe commented 2 years ago

AFAIK, lobster hasn't supported const member functions yet.

Screen Shot 2022-02-21 at 14 52 32

And, it also does not support private and public. Because class is private by default. But you can call whatever member functions you like in main.

jamesjuett commented 2 years ago

private and public are indeed not supported yet. const can exist on the types of member functions, via the type of the receiver. image

But, it's not getting properly respected when a member is accessed. The issue appears that when I ported the codebase to TS a few years ago I must have mixed things up and member access is resulting in a MemberObjectEntity from lookup rather than a MemberAccessEntity (which can carry along the const). Working on a fix for this one now.

jamesjuett commented 2 years ago

Hm, actually MemberAccessEntity isn't the solution, instead IdentifierExpression, DotExpression, and ArrorExpression should just take care of member access taking on constness from the receiver object.