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

[Semantic API] Type of enums returned as UNION #27804

Closed
shehan360 opened this issue Jan 12, 2021 · 5 comments · Fixed by #29626
Closed

[Semantic API] Type of enums returned as UNION #27804

shehan360 opened this issue Jan 12, 2021 · 5 comments · Fixed by #29626
Assignees
Labels
Area/SemanticAPI Semantic API Related Issues #Compiler Planning/SwanLakeDump All issues planned for Swan Lake GA release Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Improvement

Comments

@shehan360
Copy link
Contributor

shehan360 commented Jan 12, 2021

Description:
When we have reference to a enum, the semantic api returns the type as a UNION.
We have added enums to docs, and need this functionality to recognize enums in order to create links.

Steps to reproduce:

  1. Have the following code
public enum LogLevel {
    DEBUG,
    ERROR,
    INFO,
    TRACE,
    # log level of warn
    WARN,
    ALL,
    # Turn off logging
    OFF
}
public type TestRecord record {
    boolean http2PriorKnowledge = false;
    LogLevel lvl;
};
2. Get the symbol of LogLevel in TestRecord.
3. TypeDescriptor.kind() returns `UNION`

Affected Versions:

OS, DB, other environment details and versions:

Related Issues (optional):

Suggested Labels (optional):

Suggested Assignees (optional):

@pubudu91 pubudu91 added Area/SemanticAPI Semantic API Related Issues #Compiler Planning/SwanLakeDump All issues planned for Swan Lake GA release Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Bug labels Jan 12, 2021
@pubudu91 pubudu91 self-assigned this Jan 12, 2021
@pubudu91
Copy link
Contributor

@shehan360 I had a look at this. This is actually by design. The spec defines enums as follows:

module-enum-decl :=
   metadata
   [public] enum identifier { enum-member (, enum-member)* }

The spec also says the following:

The identifier is defined as a type in the same was as if it had been defined by a module-type-defn, with the type-descriptor being the union of the constants defined by the members.

So the above enum is actually the same as

public const DEBUG = "DEBUG";
public const ERROR = "ERROR";
public const INFO = "INFO";
public const TRACE = "TRACE";
public const WARN = "WARN";
public const ALL = "ALL";
public const OFF = "OFF";

public type LogLevel DEBUG|ERROR|INFO|TRACE|WARN|ALL|OFF;

This is why when you call typeDescriptor() on an enum type ref you get a union type descriptor.

At the moment, to get the associated enum symbol, you look it up by passing the location in the type ref symbol to symbol(). I initiated a discussion on whether to add a back reference to the type def as well: #27980

@shehan360
Copy link
Contributor Author

There are several broken links in api docs due to this issue. It would be great if we can get a fix for this.

@pubudu91
Copy link
Contributor

Doesn't the above mentioned approach work? You don't need to get the typedesc of the type ref of an enum right?

@pubudu91
Copy link
Contributor

As mentioned in #27980, we decided to add a method to TypeReferenceTypeSymbol to get the definition symbol associated with the type ref. This would solve this issue as well right?

@pubudu91 pubudu91 added this to the Ballerina Swan Lake - Alpha4 milestone Mar 25, 2021
@shehan360
Copy link
Contributor Author

As mentioned in #27980, we decided to add a method to TypeReferenceTypeSymbol to get the definition symbol associated with the type ref. This would solve this issue as well right?

Yes I think it will resolve the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area/SemanticAPI Semantic API Related Issues #Compiler Planning/SwanLakeDump All issues planned for Swan Lake GA release Team/CompilerFE All issues related to Language implementation and Compiler, this exclude run times. Type/Improvement
Projects
None yet
3 participants