Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compilation error caused by weird test::@error and mem::equal interferrence. #1997

Open
alexveden opened this issue Feb 26, 2025 · 2 comments
Milestone

Comments

@alexveden
Copy link
Contributor

alexveden commented Feb 26, 2025

Minimal code (you may need to add some imports, because I carved this snippet from another code):

module test;
import std::io;


struct MyFoo {
    uint idx;
}

struct MyFooIter {
    MyFoo* parent;
    uint idx;
}

macro MyFooIter MyFoo.iter(&self) {
    return (MyFooIter){.parent = self};
}

macro MyFoo*! MyFooIter.next(&self) {
    MyFoo foo = {.idx = 999};

    // expected to always fail
    if (!mem::equals(&foo, self.parent, MyFoo.sizeof)) {
        return IoError.GENERAL_ERROR?;
    }

    return self.parent;
}

module test @test;
fn void test_myfoo()
{
    MyFoo foo;
    MyFooIter iter = foo.iter();

    // This will compile and blow up with GENERAL_ERROR (expected)
    // iter.next()!!;
    
    // This doesn't compile with weird error
    test::@error(iter.next(),  IoError.GENERAL_ERROR);
}

Error:

 416: macro bool equals(a, b, isz len = -1, usz $align = 0)
 417: {
 418:   $if !$align:
 419:           $align = $typeof(a[0]).alignof;
                ^^^^^^
(/home/ubertrader/code/c3c/lib/std/core/mem.c3:419:3) Error: Compile time variables may only be modified in the scope they are defined in.

 70: macro MyFoo*! MyFooIter.next(&self) {
 71:     MyFoo foo = {.idx = 999};
 72:
 73:     if (!mem::equals(&foo, self.parent, MyFoo.sizeof)) {
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(/home/ubertrader/code/c3tools/test/c3tools/lsp/test_symhash.c3:73:10) Note: Inlined from here.

 88: *>
 89: macro @error(#funcresult, anyfault error_expected)
 90: {
 91:    if (catch err = #funcresult) {
                        ^^^^^^^^^^^
(/home/ubertrader/code/c3c/lib/std/core/test.c3:91:18) Note: Inlined from here.

 442:                   $Type = uint;
 443:           $case 8:
 444:           $default:
 445:                   $Type = ulong;
                        ^^^^^
(/home/ubertrader/code/c3c/lib/std/core/mem.c3:445:4) Error: Compile time variables may only be modified in the scope they are defined in.

 70: macro MyFoo*! MyFooIter.next(&self) {
 71:     MyFoo foo = {.idx = 999};
 72:
 73:     if (!mem::equals(&foo, self.parent, MyFoo.sizeof)) {
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
(/home/ubertrader/code/c3tools/test/c3tools/lsp/test_symhash.c3:73:10) Note: Inlined from here.

 88: *>
 89: macro @error(#funcresult, anyfault error_expected)
 90: {
 91:    if (catch err = #funcresult) {
                        ^^^^^^^^^^^
(/home/ubertrader/code/c3c/lib/std/core/test.c3:91:18) Note: Inlined from here.

 444:           $default:
 445:                   $Type = ulong;
 446:   $endswitch
 447:   var $step = $Type.sizeof;
                    ^^^^^
(/home/ubertrader/code/c3c/lib/std/core/mem.c3:447:14) Error: You need to assign a type to '$Type' before using it.

C3 Compiler Version:       0.6.8 (Pre-release, Feb 20 2025 11:29:04)
Installed directory:       /home/ubertrader/code/c3c/build/
Git Hash:                  e97ff1ce21bddc6dc4d406b42c983f35d16982fc
Backends:                  LLVM
LLVM version:              18.1.8
LLVM default target:       x86_64-pc-linux-gnu

@alexveden
Copy link
Contributor Author

alexveden commented Feb 26, 2025

I also found that @catch may produce the similar error

assert(@catch(iter.next()) ==  IoError.GENERAL_ERROR);

@lerno
Copy link
Collaborator

lerno commented Mar 1, 2025

This is a problem with detecting that the compile time variable isn't actually modified inside of the other macro context. I need to think a little about this.

@lerno lerno added this to the 0.7 milestone Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants