vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.76k stars 2.16k forks source link

compile-time reflection error for enum/struct declared in other module #20948

Closed mac-hel closed 7 months ago

mac-hel commented 7 months ago

Describe the bug

compile time looping over enum/struct fields throws compiler error if enum/struct is declared in other module

Reproduction Steps

module_a/
    my_data.v
main.v

my_data.v

module module_a

pub enum MyEnumModule {
  raz
  dwa
}

pub struct MyStructModule {
    id string
    costam int
}

main.v

import net.http;
import module_a

enum MyEnumTop {
  raz
  dwa
}

struct MyStructTop {
  id string
  costam int
}

fn main() {

  // enum/struct declared in same file no issues:
  $for field in MyStructTop.fields {
    println(field.name);
  }
  $for method_val in MyEnumTop.values {
    println(method_val.name);
  }

  // enum/struct declared in other module errors:

  // ERROR:
  $for field in module_a.MyStructModule.fields {
    println(field.name);
  }

  // ERROR:
  $for method_val in module_a.MyEnumModule.values {
    println(method_val.name);
  }

  // ERROR:
  $for method_val in http.Method.values {
    println(method_val.name);
  }
}

Expected Behavior

compile, print enum/struct's fields

Current Behavior

compile time error:

>> v run main.v                                                                                                                                                                                                        
            main.v:27:26: error: unknown kind `MyStructModule`, available are: `methods`, `fields`, `values`, `variants` or `attributes`                                                                                           
               25 |                                                                                                                                                                                                                            
               26 |   // ERROR:                                                                                                                                                                                                                
               27 |   $for field in module_a.MyStructModule.fields {                                                                                                                                                                           
                  |                          ~~~~~~~~~~~~~~                                                                                                                                                                                    
               28 |     println(field.name);                                                                                                                                                                                                   
               29 |   }    

Possible Solution

No response

Additional Information/Context

Trying to work arround this with type EnumAlias = module_a.MyEnumModule and iterating over alias also gives error, but this time its builder error

V version

V 0.4.4 ac9b724

Environment details (OS name and version, etc.)

V full version: V 0.4.4 f603035.ac9b724 OS: linux, "EndeavourOS Linux" Processor: 8 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz

getwd: /H/_nobackup/Projects/v_errors/v_compiletime_for_enum vexe: /H/_nobackup/mh/.local/share/v/v vexe mtime: 2024-03-03 14:33:21

vroot: OK, value: /H/_nobackup/mh/.local/share/v VMODULES: OK, value: /home/mh/.vmodules VTMP: OK, value: /tmp/v_1000

Git version: git version 2.44.0 Git vroot status: weekly.2024.06-140-gac9b724b .git/config present: true

CC version: cc (GCC) 13.2.1 20230801 thirdparty/tcc status: thirdparty-linux-amd64 99683af0

[!NOTE] You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote. Other reactions and those to comments will not be taken into account.

mac-hel commented 7 months ago

possibly related: https://github.com/vlang/v/issues/20787 ?