-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Sr 11159 space engine #26754
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
Sr 11159 space engine #26754
Conversation
Please let's just restrict this to the SpaceEngine changes. The engine should pick up DeclNames by projection, so the rest of this patch isn't needed. |
lib/Sema/TypeCheckSwitchStmt.cpp
Outdated
@@ -604,8 +612,9 @@ namespace { | |||
// a disjunction of its sub-spaces because nothing got reduced. | |||
// This is especially helpful when dealing with `unknown` case | |||
// in parameter positions. | |||
if (s1 == reducedSpace) | |||
if (s1 == reducedSpace){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate the bracing, but please standardize on the spacing in the rest of this file. If in doubt, run clang-format
.
@@ -696,9 +705,9 @@ namespace { | |||
buffer << (getBoolValue() ? "true" : "false"); | |||
break; | |||
case SpaceKind::Constructor: { | |||
if (!Head.empty()) { | |||
if (!Head.getBaseIdentifier().empty()) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're going to need to print the labels into the pattern as well.
lib/Sema/TypeCheckSwitchStmt.cpp
Outdated
@@ -1435,6 +1443,11 @@ namespace { | |||
if (!SP) { | |||
// If there's no sub-pattern then there's no further recursive | |||
// structure here. Yield the constructor space. | |||
if(VP->hasUnresolvedOriginalExpr()){ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The space engine does not need to concern itself with ill-formed pattern trees. They will have been structurally diagnosed by the rest of pattern type checking, and we should not project them.
@@ -333,7 +334,7 @@ namespace { | |||
return this->isSubspace(or2Space, TC, DC); | |||
} | |||
|
|||
return true; | |||
return false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious if this affects any of our tests. I suspect it may close some SRs if this turns out to be salient.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I ran the --test
with the build script, the tests still passed in those cases.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, let's leave it in and run the broader test suite to see if this catches anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is that the validation-test?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's part of it, yes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes and the general approach look pretty good. Some feedback inline.
@@ -115,7 +115,7 @@ namespace { | |||
|
|||
// In type space, we reuse HEAD to help us print meaningful name, e.g., | |||
// tuple element name in fixits. | |||
Identifier Head; | |||
DeclName Head; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't your bug, but "Head" no longer makes sense under these changes. Perhaps Name
or Spine
(definitely rooting more for the former than the latter).
Let's also try to clean up the history a little here. In general, you can run git pull --rebase upstream master where |
3a63596
to
6380295
Compare
lib/Sema/TypeCheckSwitchStmt.cpp
Outdated
other.Head.getBaseIdentifier()) | ||
return false; | ||
if (this->Head.getArgumentNames().size() != 0 && | ||
other.Head.getArgumentNames().size() != 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This sequence of checks doesn't make sense to me. To preserve the optimization, we should be checking if the baseIdentifier
s are different and the argumentNames
differ in number.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I may have misinterpreted some fo the tests. I think this was attempting to take care of the case where if the case statement had no arguments/associated values (i.e. .a
) compared to one where it does .a(x:)
then it should be considered a subspace.
Should 0 be considered special in these cases?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We pre-expand patterns via lookup to make that impossible. Otherwise we wouldn't be able to detect overlapping patterns.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah ok, makes sense
lib/Sema/TypeCheckSwitchStmt.cpp
Outdated
} | ||
|
||
if (Spaces.empty()) { | ||
if (Head.getArgumentNames().size() > 0) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the right place to do this. If Spaces
is empty then you shouldn't have any argument names to print. The interleave
call below this needs to be adjusted to print labels.
6dc15df
to
9a2b3eb
Compare
lib/Sema/TypeCheckSwitchStmt.cpp
Outdated
} | ||
|
||
if (Spaces.empty()) { | ||
return; | ||
} | ||
|
||
llvm::SmallVector<std::pair<Identifier, Space>, 4> labelSpaces; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a more llvm-style way to do this? This seemed like the best way to attach the label to the space.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also causes a few of the tests to fail since they produce a label that wasn't there before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Those tests should be updated then.
lib/Sema/TypeCheckSwitchStmt.cpp
Outdated
if (this->Head.getArgumentNames().size() != | ||
other.Head.getArgumentNames().size()) { | ||
return false; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, why isn't this still just returning false? When would we have different labels but want to treat the constructors as the same?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be from when the two pull requests were together, but in the future I think there is a situation where the constructor could just have variable patterns in place of the labels, and in that case they could possibly be a sub space even if the constructors do not match exactly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense, but I'm worried that this was a load-bearing optimization. The rest of the code just checks the subspaces, which means it would treat even .x(a: Int)
and .x(b: Int)
as equivalent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so should these be removed and other checks be put earlier? Or just removed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just going back to checking the whole DeclName is probably okay for now. Unless that uncovers problems with existing tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be fixed and passes the basic/validation tests from the build script
9a0c25a
to
455af70
Compare
labelSpaces.push_back(std::pair<Identifier, Space>(*args, param)); | ||
args++; | ||
} else | ||
labelSpaces.push_back( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you assert that you always have the same number of argument names in the base identifier as there are subspaces? If that much is true, then you can swap the interleave for the variant of std::transform
that takes two input iterators and keep track of placing the commas yourself. Honestly, if it's all the same we can just take this code and you can leave a FIXME behind to come clean this up. I'm not too concerned about printing being efficient since this will only come up when you're diagnosing invalid code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean a full assertion that stops compilation? Or just a check that runs a different path if that is the case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, just an assert
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure just having an assertion allows all cases.
For instance, if we check the number of argument names against spaces in the following example:
enum Threepeat {
case a, b, c
}
func test3(x: Threepeat, y: Threepeat) {
switch (x, y) { // expected-error {{switch must be exhaustive}}
// expected-note@-1 {{add missing case: '(.a, .c)'}}
case (.a, .a):
()
case (.b, _):
()
case (.c, _):
()
case (_, .b):
()
}
}
with the assertion, it fails where it should not.
455af70
to
84a7c13
Compare
I added a FIXME to the initial implementation of showing the constructors, I'm not sure that an assertion can cover all the cases needed. |
Looks good. @swift-ci please test |
Build failed |
Build failed |
adding full spaceengine changes using clang-format adding argument name size comparison adding space engine changes adding full spaceengine changes using clang-format adding argument printing to interleave function updating tests for new print formatting removing extra checks from constructor subspace checking adding alternate path if the argument and space lenght are the same clang format adding FIXME for code performance adding fixme for showing constructor switching direction of beginningparenthesis
84a7c13
to
d7e977b
Compare
Apologies for that bug, the parenthesis should now be in the right direction, which I believe was the cause of the failures |
@swift-ci please test |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Thanks ⛵️ |
Split off of #26741 to separate Enum changes and Space Engine changes, related to #26741
Changes Identifiers to DeclNames so that in checking elements the associated values are taken into account. Other exhaustiveness and redundancy to compare these associated values are taken to make sure the correct message is shown.
Resolves SR-11159.