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

Union fixes #137

Merged
merged 12 commits into from
Aug 4, 2021
Merged

Union fixes #137

merged 12 commits into from
Aug 4, 2021

Conversation

FrobtheBuilder
Copy link
Contributor

Make nested unions and references to types defined in union branches invalid. Also add mstest tests for the bebop compiler core. These tests ensure the valid schemas in the Laboratory/Schemas directory do compile, and the new invalid ones I added in ShouldFail do not. I had to modify the union test schema as evidently it included nested unions which we're no longer supporting.

The checks are done in SchemaParser, which is debatably not the right place for checking references, but having access to the _typeReferences set that's only available there makes the logic a lot cleaner. It will need to be moved once we add support for multiple errors per compiler run, however. Maybe add that set to BebopSchema?

Comment on lines 45 to 55
/// <summary>
/// List of definitions enclosing this definition, from outer to inner. Empty if this is a top level definition.
/// </summary>
public List<Definition>? Scope
{
get
{
if (Parent is null)
{
return null;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the ? from the return type, and remove the if (Parent is null) { return null; }, and just let this method return an empty list for a top-level definition like the doc-comment says (the while-loop body will run 0 times 👍).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, oversight.

Comment on lines 55 to 56
var scope = _scopes.Last();
_scopes.Remove(scope);
Copy link
Contributor

Choose a reason for hiding this comment

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

This makes me think _scopes should be a Stack<T>. Maybe that also nicely captures how it is actually used (i.e. even conceptually, it's a stack of scopes).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right, I'm using that list just like a stack lol. I've got a bad case of javascript-brain where you have to use normal arrays for every kind of linear data structure.

Comment on lines 248 to 252
// It might be better for this to go inside BebopSchema.Validate but it's much simpler if I can use the typeReferences map
if (referenceScope is not null && referenceScope.Find((parent) => parent is UnionDefinition) is UnionDefinition union)
{
if (definitionScope is null || !definitionScope.Contains(union))
{
Copy link
Contributor

Choose a reason for hiding this comment

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

You can get rid of these null checks after applying the change in my earlier comment about Scope.

Comment on lines +257 to +275
/*
foreach (var definition in _definitions.Values)
{
var scope = definition.Scope;
var dependencies = definition.Dependencies();


foreach (var dependency in dependencies)
{
var depScope = _definitions[dependency].Scope;
if (depScope is not null && depScope.Find((parent) => parent is UnionDefinition) is UnionDefinition union) {
if (scope is null || !scope.Contains(union))
{
throw new ReferenceScopeException(definition, _definitions[dependency], "union");
}
}
}

}*/
Copy link
Contributor

Choose a reason for hiding this comment

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

My classic review comment about commented-out code:

If there's a good reason to keep it around but commented-out, add a comment explaining why.

If there isn't a good reason, let's get rid of it.

@FrobtheBuilder FrobtheBuilder merged commit 182c1ba into master Aug 4, 2021
@200sc 200sc mentioned this pull request Aug 7, 2021
@andrewmd5 andrewmd5 deleted the union-fixes branch July 18, 2022 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants