c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Better error information for macros

Open cbuttner opened this issue 1 year ago • 1 comments

module test;
import std::io;

macro macro_2(str) {
  int a = str;
}

macro macro_1(str) {
  macro_2(str);
}

fn int main(String[] args) {
  macro_1("Test");
  return 0;
}

Emits

 2: import std::io;
 3: 
 4: macro macro_2(str) {
 5:   int a = str;
              ^^^
(/tmp/test/src/main.c3:5:11) Error: You cannot cast 'String' to 'int'.

But there is no information on where this macro was invoked from.

The error should trace all the way back to the origin. Kind of like so:

 2: import std::io;
 3: 
 4: macro macro_2(str) {
 5:   int a = str;
              ^^^
(/tmp/test/src/main.c3:5:11) Error: You cannot cast 'String' to 'int'.

 6: }
 7:
 8: macro macro_1(str) {
 9:   macro_2(str);
      ^^^^^^^
(/tmp/test/src/main.c3:9:3) Note: Invoked from here.

 10: }
 11:
 12: fn int main(String[] args) {
 13:  macro_1("Test");
      ^^^^^^^
(/tmp/test/src/main.c3:13:3) Note: Invoked from here.

cbuttner avatar May 20 '24 09:05 cbuttner

I've changed this in dev, please have a try.

lerno avatar May 21 '24 06:05 lerno

Works great, thank you.

cbuttner avatar Jun 08 '24 13:06 cbuttner

Any chance this could also be improved?

module test;
import std::io;

macro @macro_2(#x) {
  int a = #x;
}

macro @macro_1(#x) {
  @macro_2(#x);
}

fn int main(String[] args) {
  @macro_1(TEST);
  return 0;
}
 2: import std::io;
 3: 
 4: macro @macro_2(#x) {
 5:   int a = #x;
              ^^
(/tmp/test/src/main.c3:5:11) Error: 'TEST' could not be found, did you spell it right?

cbuttner avatar Jun 08 '24 19:06 cbuttner

Tracing was missing a bit, try it now.

lerno avatar Jun 08 '24 20:06 lerno

Better, but I found another case:

module test;
import std::io;

macro void @macro_2(x) {
  int a = x;
}

macro void @macro_1(#x) {
  @macro_2(#x);
}

fn int main(String[] args) {
  @macro_1(TEST);
  return 0;
}
 6: }
 7: 
 8: macro void @macro_1(#x) {
 9:   @macro_2(#x);
               ^^
(/tmp/test/src/main.c3:9:12) Error: 'TEST' could not be found, did you spell it right?

cbuttner avatar Jun 10 '24 15:06 cbuttner

Heh, fixed one side of the if, but not the other... Try it again @cbuttner

lerno avatar Jun 10 '24 16:06 lerno

That case is fixed, but here's one more:

module test;

struct Foo {
  bool a;
  int b;
}

fn int main(String[] args) {
  mem::new(Foo, {
    .test = 1,
  });
  return 0;
}
 555: 
 556: /**
 557:  * @require $vacount < 2 : "Too many arguments."
 558:  * @require $or($vacount == 0, $assignable($vaexpr(0), $Type)) : "The second argument must be an initializer for |
                                                 ^^^^^^^^^^
(/home/christian/Github/c3c/lib/std/core/mem.c3:558:44) Error: This is not a valid member of 'Foo'.

cbuttner avatar Jun 11 '24 15:06 cbuttner

Thanks! Fixed now @cbuttner

lerno avatar Jun 11 '24 17:06 lerno

Cool, I'll let you know if I stumble across another case.

cbuttner avatar Jun 13 '24 08:06 cbuttner