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

sem: all tuple fields now have an owning sym #816

Draft
wants to merge 5 commits into
base: devel
Choose a base branch
from

Conversation

saem
Copy link
Collaborator

@saem saem commented Jul 26, 2023

Summary

All tuple field symbols now have an owning tuple type symbol.

Details

For tuples without names create a anonymous sym as
an owner. This means all fields in a tuple will have a shared owner,
allowing for querying for reflection or code-gen.

This required modifying semAnonTuple and semTuple procedures in the
semtypes module. Now these procedures check to see if an owning symbol
already exists and if not an anonymous one is created.


Notes for Reviewers

  • first sketch of the solution, not all that pretty
  • either in this PR, or in subsequent PRs, clean things up; lots of unprincipled code here

saem added 2 commits July 26, 2023 15:12
an owner. This means all fields in a tuple will have a shared owner,
allowing for querying for reflection or code-gen.

This required modifying `semAnonTuple` and `semTuple` procedures in the
`semtypes` module. Now these procedures check to see if an owning symbol
already exists and if not an anonymous one is created.
@saem saem added the compiler/sem Related to semantic-analysis system of the compiler label Jul 26, 2023
@saem
Copy link
Collaborator Author

saem commented Jul 26, 2023

tests seem to pass locally, just doing a quick check here.

Copy link
Collaborator

@zerbina zerbina left a comment

Choose a reason for hiding this comment

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

I've noticed a potential issue with the setupOwner condition, and there might be an expectation violation with assigning the anonymous symbol to the type's owner, but I think that the direction of assigning symbols to anonymous types is good.

It's nothing that has to happen as part of this PR, but using an anonymous symbol would also make sense for anonymous procedural types.

@@ -500,6 +497,17 @@ proc semAnonTuple(c: PContext, n: PNode, prev: PType): PType =
if n.len == 0:
localReport(c.config, n, reportSem rsemTypeExpected)
result = newOrPrevType(tyTuple, prev, c)
let setupOwner = c.inGenericContext == 0 and result.sym.isNil
Copy link
Collaborator

Choose a reason for hiding this comment

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

I haven't verified it, but I think the check is an issue in the following case:

  type
    Generic[T] = object
      f: tuple[a, b: int]

When the tuple type is sem-checked, inGenericContext is greater than 0 and result.sym is nil.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Doh, good call.

I was suspicious of the owner pushing in the right hand side type section analysis for generics, and I think that's coming home to roost here.

compiler/sem/semtypes.nim Outdated Show resolved Hide resolved
compiler/sem/semtypes.nim Outdated Show resolved Hide resolved
nextSymId c.idgen, c.getCurrOwner(), n.info)
sym.flags.incl sfAnon
sym.typ = result
result.owner = sym
Copy link
Collaborator

Choose a reason for hiding this comment

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

If a symbol directly represents the type, then it is generally assigned to the types's sym field instead of owner, which for consistency would also make sense here, I think. (typesrenderer already checks for sfAnon symbols, so this should not cause problems)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It did, but more so for the silly getTypeDecl and that whole silly API in std/macros. I'll have to dig in further to see what's going wrong there.

@saem
Copy link
Collaborator Author

saem commented Jul 27, 2023

Thanks for the review, @zerbina

saem added 3 commits July 27, 2023 13:55
move `newAnonSym` from the module `semexprs` to `sem`, allowing it to be
reused in `semtypes`.
if a discriminant was too large for a variant an error was both not
logged nor did it cause compilation to fail
@saem
Copy link
Collaborator Author

saem commented Jul 29, 2023

the approach I'm taking here isn't quite right. I think more needs to be cleaned up and then a less hacky approach to symbol creation can be taken.

@saem saem marked this pull request as draft August 6, 2023 03:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler/sem Related to semantic-analysis system of the compiler
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants