winglang / wing

A programming language for the cloud ☁️ A unified programming model, combining infrastructure and runtime code into one language ⚡
https://winglang.io
Other
4.8k stars 189 forks source link

Identifier collision between private struct and public class #5084

Open polamoros opened 7 months ago

polamoros commented 7 months ago

I tried this:

// main.w
bring "./foo.w" as foo;
bring "./bar.w" as bar;
// foo.w
pub class Foo {}
// bar.w
struct Foo {}

pub class Bar {
  pub static inflight func(input: Json) {
    let a = Foo.fromJson(input); 
  }
}

This happened:

Failed to compile.

runtime error: Identifier 'Foo' has already been declared
  --> demo/main.w:1:1
1 | bring "./foo.w" as foo;
  | ^

I expected this:

To be able to use the same identifier in separated contexts.

and/or

The error to include paths to both files.

Is there a workaround?

No response

Anything else?

No response

Wing Version

No response

Node.js Version

No response

Platform(s)

No response

Community Notes

staycoolcall911 commented 7 months ago

Internally, this is probably related to the compiler creating an environment for user defined types with the environment name as the identifier to detect duplicates. We may be able to support that by prefixing the environment name with the type (?). That said, I'm not sure we actually want to allow a Struct and a Class having the same name, as it may lead to confusing code, so I'm not sure this is a bug. This is different from #1635 which would should probably solve. WDYT?

Chriscbr commented 7 months ago

I think it's ok to treat this as a bug. I understand it's a little weird having a struct and class with the same name, but I believe it's unrelated to the struct-ness or class-ness of the types. For example, it's valid in Wing to have two classes with the same name:

// subdir1/index.w
pub class Foo {}

// subdir2/index.w
pub class Foo {}

// main.w
bring "./subdir1" as foo1;
bring "./subdir2" as foo2;

new foo1.Foo();
new foo2.Foo();

I think the example by Pol should work since he is directly importing each file and assigning each as an alias. BUT, if he tried compiling the example as a library, it should fail since the types are in the same directory:

// foo.w
pub class Foo {}

// bar.w
pub struct Foo {}

I think the error message should be something like

$ wing pack
error: running "wing compile ./" failed
error: public type Foo is defined more than once in directory "./"

But those are just my current thoughts, it's possible there are better solutions

github-actions[bot] commented 5 months ago

Hi,

This issue hasn't seen activity in 60 days. Therefore, we are marking this issue as stale for now. It will be closed after 7 days. Feel free to re-open this issue when there's an update or relevant information to be added. Thanks!