Skip to content

[AST] Fix canonicalization of the function input types #12304

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

Merged
merged 1 commit into from
Oct 6, 2017

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Oct 5, 2017

Currently canonicalization of the input type was happening standalone
which means that outer parens are regarded as suger and removed,
this is incorrect after SE-0110 and such parens can't be stripped.

@xedin xedin requested a review from DougGregor October 5, 2017 21:51
@xedin
Copy link
Contributor Author

xedin commented Oct 5, 2017

@DougGregor I'm not sure about one thing - do we need a flag for getCanonicalInputType which would distinguish between swift 3 and swift 4 modes?

@slavapestov
Copy link
Contributor

@xedin I think it would be better to represent all types in the correct way for Swift 4, and have Swift 3 mode simulated with various implicit coercions.

lib/AST/Type.cpp Outdated
@@ -1022,6 +1022,31 @@ void ProtocolType::canonicalizeProtocols(
llvm::array_pod_sort(protocols.begin(), protocols.end(), compareProtocols);
}

static Type
getCanonicalInputType(AnyFunctionType *funcType,
std::function<CanType(Type)> getCanonicalType) {
Copy link
Member

Choose a reason for hiding this comment

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

You can use llvm::function_ref instead of std::function here, because we're not going to save the function anywhere.

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!

lib/AST/Type.cpp Outdated
getCanonicalInputType(AnyFunctionType *funcType,
std::function<CanType(Type)> getCanonicalType) {
auto inputType = funcType->getInput();
if (auto *TT = dyn_cast<TupleType>(inputType.getPointer()))
Copy link
Member

Choose a reason for hiding this comment

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

You don't need this if here; the fall through will work fine.

lib/AST/Type.cpp Outdated
if (auto *TT = dyn_cast<TupleType>(inputType.getPointer()))
return getCanonicalType(TT);

if (auto *PT = dyn_cast<ParenType>(inputType.getPointer())) {
Copy link
Member

Choose a reason for hiding this comment

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

Can this logic be simpler? It seems like we could do something like:

bool isParen = isa<ParenType>(inputType.getPointer());
inputType = inputType->getCanonicalType();
if (isParen) inputType = ParenType::get(inputType);
assert(AnyFunctionType::isCanonicalFunctionInputType(inputType));

@DougGregor
Copy link
Member

Canonicalization should always do the right thing (regardless of language dialect), and we can teach the type checker about extra implicit conversions if needed.

@xedin xedin force-pushed the fix-func-input-canonicalization branch from 87fea3f to 703080f Compare October 5, 2017 23:10
Currently canonicalization of the input type was happening standalone
which means that outer parens are regarded as suger and removed,
this is incorrect after SE-0110 and such parens can't be stripped.
@xedin xedin force-pushed the fix-func-input-canonicalization branch from 703080f to 7d2e29a Compare October 5, 2017 23:12
@xedin
Copy link
Contributor Author

xedin commented Oct 5, 2017

@DougGregor How does it look now?

@xedin
Copy link
Contributor Author

xedin commented Oct 5, 2017

@swift-ci please smoke test

@xedin
Copy link
Contributor Author

xedin commented Oct 6, 2017

@swift-ci please test source compatibility

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

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

Beautiful, thank you!

@xedin xedin merged commit 0a2cf99 into swiftlang:master Oct 6, 2017
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.

3 participants