[PATCH] Add more information when displaying a "read-only variable is not assignable" error

Richard Trieu rtrieu at google.com
Mon Jul 14 10:48:49 PDT 2014


```
$ cat c.cc
struct foo {
  int i;
  void f() const;
};
void foo::f() cont {
  i = 3;
}
$ clang c.cc
c.cc:6:5: error: read-only variable is not assignable
  i = 3;
  ~ ^
c.cc:5:11: note: method 'f' is declared const here
void foo::f() const {
~~~~~~~~~~^~~~~~~~~
1 error generated.

```
Note will point to function definition.



```
$ cat d.cc
typedef const int X;
void test(X x) {
  x = 5;
}
$ clang d.cc
d.cc:3:5: error: read-only variable is not assignable
  x = 5;
  ~ ^
d.cc:2:13: note: variable 'x' is declared here with type 'X' (aka 'const int') which is const
void test(X x) {
          ~~^
1 error generated.

```
No problems with typedefs.

I think the note should explicitly point out the const qualifier, otherwise the user will see "note: variable 'x' has type 'y'" which isn't very helpful.  Perhaps "note: variable "x" is declared here with type 'const int'; const qualifier prevents assignment" would be better?

http://reviews.llvm.org/D4479






More information about the cfe-commits mailing list