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

Introduce new isEnum() and getEnumSymbol() APIs in UnionTypeSymbol to distinguish and get enum-symbol in union-type descriptors #41173

Closed

Conversation

dulajdilshan
Copy link
Contributor

@dulajdilshan dulajdilshan commented Aug 8, 2023

Purpose

$title

Fixes #38179

Approach

Describe how you are implementing the solutions along with the design details.

Samples

Provide high-level details about the samples related to this feature.

Remarks

List any other known issues, related PRs, TODO items, or any other notes related to the PR.

Check List

  • Read the Contributing Guide
  • Updated Change Log
  • Checked Tooling Support (#)
  • Added necessary tests
    • Unit Tests
    • Spec Conformance Tests
    • Integration Tests
    • Ballerina By Example Tests
  • Increased Test Coverage
  • Added necessary documentation
    • API documentation
    • Module documentation in Module.md files
    • Ballerina By Examples

@dulajdilshan dulajdilshan added this to the 2201.8.0 milestone Aug 8, 2023
@dulajdilshan dulajdilshan added Team/CompilerFETools Semantic API, Formatter, Shell Area/SemanticAPI Semantic API Related Issues #Compiler labels Aug 8, 2023
@dulajdilshan dulajdilshan changed the title Introduce new isEnum() and getEnumSymbol() APIs in UnionTypeSymbol to distinguish and get enum-symbol's form union-type descriptors Introduce new isEnum() and getEnumSymbol() APIs in UnionTypeSymbol to distinguish and get enum-symbol in union-type descriptors Aug 8, 2023
@codecov
Copy link

codecov bot commented Aug 8, 2023

Codecov Report

Patch coverage: 81.81% and project coverage change: +76.39% 🎉

Comparison is base (8a0431e) 0.00% compared to head (7a1e398) 76.39%.
Report is 17 commits behind head on master.

❗ Current head 7a1e398 differs from pull request most recent head 41b4726. Consider uploading reports for the commit 41b4726 to get more accurate results

Additional details and impacted files
@@              Coverage Diff              @@
##             master   #41173       +/-   ##
=============================================
+ Coverage      0.00%   76.39%   +76.39%     
- Complexity        0    52112    +52112     
=============================================
  Files             9     2858     +2849     
  Lines            35   195978   +195943     
  Branches          0    25403    +25403     
=============================================
+ Hits              0   149723   +149723     
- Misses           35    37953    +37918     
- Partials          0     8302     +8302     
Files Changed Coverage Δ
...ler/api/impl/symbols/BallerinaUnionTypeSymbol.java 87.50% <81.81%> (ø)

... and 2856 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

SymbolFactory symbolFactory = SymbolFactory.getInstance(this.context);
this.enumSymbol = symbolFactory.createEnumSymbol((BEnumSymbol) tsymbol, tsymbol.getName().value);;

return Optional.ofNullable(this.enumSymbol);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return Optional.ofNullable(this.enumSymbol);
return Optional.of(this.enumSymbol);

At this point can the enumSymbol be null?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for noticing!

Comment on lines +166 to +167
@Override
public Optional<EnumSymbol> getEnumSymbol() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this is ideal. isEnum kinda makes sense, but why do we need to allow retrieving the enum symbol like this? In a way, it's like having a reference to the definition from the type symbol.

Copy link
Contributor Author

@dulajdilshan dulajdilshan Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It's kinda exposing the information related to the enum symbol. Currently, we may not have such things, but having this may allow us to expose that information right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this is not a way of referencing. For referencing we have a structure as TypeRefType

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant having enumSymbol in BallerinaUnionTypeSymbol. This isn't quite right, IMO. The type doesn't need to how it was defined.

Yes. It's kinda exposing the information related to the enum symbol. Currently, we may not have such things, but having this may allow us to expose that information right?

I don't think that's the correct way. If someone wants enum symbol they can still get it from the model, right? I don't think the type symbol should expose it.

Copy link
Contributor Author

@dulajdilshan dulajdilshan Aug 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant having enumSymbol in BallerinaUnionTypeSymbol. This isn't quite right, IMO. The type doesn't need to how it was defined.

yeah. What if we have introduced some attributes only related to enums and the ballerina extension developer needs to get them? We can't expose that only-enum information via the BallerinaUnionTypeSymbol right?

I don't think that's the correct way. If someone wants enum symbol they can still get it from the model, right? I don't think the type symbol should expose it.

How can they get an enum symbol without the location?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant having enumSymbol in BallerinaUnionTypeSymbol. This isn't quite right, IMO. The type doesn't need to how it was defined.

yeah. What if we have introduced some attributes only related to enums and the ballerina extension developer needs to get them? We can't expose that only-enum information via the BallerinaUnionTypeSymbol right?

Can you give an example for this?

I don't think that's the correct way. If someone wants enum symbol they can still get it from the model, right? I don't think the type symbol should expose it.

How can they get an enum symbol without the location?

Can't they filter out from module symbols by name?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can't expose that only-enum information via the BallerinaUnionTypeSymbol right?

Yes, we can't. I can't think of such info anyway. We shouldn't mix up enums and types. An enum is just another way to define a union of string singletons. From a type symbol point of view, they are basically the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give an example for this?

what about annotation attachments? We shouldn't link them with the UnionTypeSymbol right?

Can't they filter out module symbols by name?

Yeah, we can, but we need the name to filter out right? What if we need to get the enum that is in another module, and we have referenced an enum value (e.x: OPEN) and we need to know the other sibling values of the enum (which is referenced in new type?

//  defined in another module `test0.module1`'
public enum State {
    OPEN,
    CLOSED
}

// main.bal
import test0.module1;

type ConnectionState State;

function test() {
    string x = module1.OPEN;
}

Let's say there is a ballerina extension writer who needs to get only the sibling values of OPEN.

Also, isn't it costly to use the filter when there are so many symbols?

Yes, we can't. I can't think of such info anyway. We shouldn't mix up enums and types. An enum is just another way to define a union of string singletons. From a type symbol point of view, they are basically the same.

Yes. This is true, but here we are just showing the relationship with the associated enum defined somewhere

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about annotation attachments? We shouldn't link them with the UnionTypeSymbol right?

annotation annot on type;

const RED = "RED";
const BLUE = "BLUE";

@annot
type Colour RED|BLUE;

is the same as

annotation annot on type;

@annot
enum Colour {
    RED,
    BLUE
}

so we shouldn't have to special case anything for enums.

Yeah, we can, but we need the name to filter out right?

If you have the union type you have the name?

What if we need to get the enum that is in another module, and we have referenced an enum value (e.x: OPEN) and we need to know the other sibling values of the enum (which is referenced in new type?

How does adding the enum symbol to the union type help with this? The union type corresponds to the enum, not the member?

Also, isn't it costly to use the filter when there are so many symbols?

It's just filtering based on a string? Anyway, the API being well-defined is more important IMO.

@dulajdilshan dulajdilshan removed this from the 2201.8.0 milestone Aug 16, 2023
@github-actions
Copy link

This PR has been open for more than 15 days with no activity. This will be closed in 3 days unless the stale label is removed or commented.

@github-actions
Copy link

This PR has been open for more than 15 days with no activity. This will be closed in 3 days unless the stale label is removed or commented.

@github-actions github-actions bot added the Stale label Sep 19, 2023
@dulajdilshan
Copy link
Contributor Author

Closing due to this: #38179 (comment)

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 Team/CompilerFETools Semantic API, Formatter, Shell
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Unable to distinguish enum symbol from union-type symbol in Semantic API
3 participants