Skip to content

Fix up the deep equality demangler tests. #78812

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

allevato
Copy link
Member

These tests were broken. The Demangler::demangleSymbol function operates subtly differently than what swift-demangle does under the hood (which powers the mangling.txt lit test), which calls Demangler::demangleSymbolAsNode instead. That difference caused many valid partial manglings to fail and return nullptr.

Unfortunately this was never caught, because isDeepEqualTo immediately passes this to a static method (so there was no actual null pointer dereference occurring), and then the two nullptrs were compared for equality, erroneously returning true. I only happened to catch this when running the test in an environment with more strict pointer enforcement.

I've updated the tests to:

  • Separate the valid manglings from invalid manglings so that we can test both outcomes.
  • Assert inside deepEquals that neither pointer is nullptr.
  • Assert that the symbol, when demangled and printed as a string, matches the expected demangling (which was present in the .def file but never used).

The latter point ensures that all behavior is being verified, making these tests equivalent to those in manglings.txt with the added nondeterminism check that was in the original PR.

@allevato allevato requested a review from rjmccall as a code owner January 22, 2025 16:56
@allevato
Copy link
Member Author

cc @DmT021

@allevato allevato requested a review from jckarter January 22, 2025 16:57
@allevato
Copy link
Member Author

@swift-ci please smoke test

Copy link
Contributor

@DmT021 DmT021 left a comment

Choose a reason for hiding this comment

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

Looks good to me, only a minor note about the assert. Nice catch, thanks.

@@ -251,6 +251,7 @@ class Node {
}

static bool deepEquals(const Node *lhs, const Node *rhs) {
assert(lhs && rhs && "Nodes being compared may not be null");
Copy link
Contributor

Choose a reason for hiding this comment

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

If both pointers can't be null we don't need if ((!lhs && rhs) || (lhs && !rhs)). Or we can require at least one of them to be non-null, then the assert should be lhs || rhs.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if both of them may not be null it might be a good idea to take const references instead

Copy link
Member Author

Choose a reason for hiding this comment

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

Good catch about the now-redundant comparison; removed it.

I think we want to keep the types as pointers, because NodePointer (type alias of Node *) is the "currency" type used by the demangler; there are no public APIs that consume a Node &, so requiring the caller to dereference it in just this case would be a bit unusual.

These tests were broken. The `Demangler::demangleSymbol` function
operates subtly differently than what `swift-demangle` does under
the hood (which powers the `mangling.txt` lit test), which calls
calls `Demangler::demangleSymbolAsNode` instead. That difference
caused many valid partial manglings to fail and return `nullptr`.

Unfortunately this was never caught, because `isDeepEqualTo`
immediately passes `this` to a static method (so there was no
actual null pointer dereference occurring), and then the two
`nullptr`s were compared for equality, erroneously returning true.
I only happened to catch this when running the test in an
environment with more strict pointer enforcement.

I've updated the tests to:

* Separate the valid manglings from invalid manglings so that we
  can test both outcomes.
* Assert inside `deepEquals` that neither pointer is `nullptr`.
* Assert that the symbol, when demangled and printed as a string,
  matches the expected demangling (which was present in the `.def`
  file but never used).

The latter point ensures that all behavior is being verified,
making these tests equivalent to those in `manglings.txt` with
the added nondeterminism check that was in the original PR.
@allevato allevato force-pushed the fix-demangling-tests branch from 5b851e6 to d4ee054 Compare January 22, 2025 17:48
@allevato
Copy link
Member Author

@swift-ci please smoke test

@allevato allevato enabled auto-merge February 23, 2025 15:13
@allevato
Copy link
Member Author

allevato commented Mar 6, 2025

@swift-ci please smoke test Linux platform

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.

2 participants