degory / ghul

compiler for the ghūl programming language
https://ghul.dev
GNU Affero General Public License v3.0
4 stars 0 forks source link

When importing a symbol's members, only members that are safe to import should be included #1136

Open degory opened 6 months ago

degory commented 6 months ago

It's currently possible to import the members of types into a namespace scope with use, similar to importing the members of a namespace.

This works correctly more or less by accident for some kinds of symbols, but for other kinds of symbols the results are not usable.

It works correctly for:

It doesn't work for

And because it's an unintended side effect of the way use works, no checking is done to determine whether it's safe or meaningful for a given type member to be accessed without a qualifying instance. This leads to confusing error reports or even no errors and incorrect code generation if instance members are mistakenly imported.

It's not practical, and wouldn't be particularly useful, to make the broken scenarios work:

Instead we should restrict use to symbols that can be safely imported into global scope. When applied to entire types reflected from another assembly, use should only implicitly import static symbols. An error should be reported if the user attempts to explicitly import non-static member symbols from a reflected assembly. Attempting to import anything other than a union variant from a type defined in the current project source code should result in an error.

class TEST is
  test_static() static is si
  test_instance() is si
si

use TEST; // imports only test_static() into global scope

use TEST.test_static; // OK - is importing a static symbol into global scope

use TEST.test_instance; // report error - attempting to import an instance symbol into global scope