Skip to content

[ConstraintSystem] Introduce a new type to represent a type hole #33658

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 8 commits into from
Aug 28, 2020

Conversation

xedin
Copy link
Contributor

@xedin xedin commented Aug 27, 2020

Instead of using UnresolvedType as a placeholder for a type hole,
let's switch over to a dedicated "rich" HoleType which is capable
of storing "originator" type - type variable or dependent member
type which couldn't be resolved.

This makes it easier for the solver to determine origins of
a hole which helps to diagnose certain problems better. It also
helps code completion to locate "expected type" of the context
even when it couldn't be completely resolved.

xedin added 4 commits August 26, 2020 16:55
Instead of using `UnresolvedType` as a placeholder for a type hole,
let's switch over to a dedicated "rich" `HoleType` which is capable
of storing "originator" type - type variable or dependent member
type which couldn't be resolved.

This makes it easier for the solver to determine origins of
a hole which helps to diagnose certain problems better. It also
helps code completion to locate "expected type" of the context
even when it couldn't be completely resolved.
@xedin xedin requested a review from hborla August 27, 2020 01:47
@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b4d3237

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please test

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b4d3237

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - b4d3237

Type HoleType::get(ASTContext &ctx, OriginatorType originator) {
assert(originator);
RecursiveTypeProperties properties = RecursiveTypeProperties::HasTypeHole;
auto arena = getArena(properties);
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this is going to always allocate the type in the permanent arena, which is bad if it contains a pointer to a TypeVariableType. The recursive properties used to determine the arena should be the properties of the originator type, not the new properties of the hole type itself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn’t realize that, thank you, Slava!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I need to make sure that types containing holes as well as hole types themselves are allocated in ConstraintSolver arena...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed! Every type which contains holes or is a HoleType itself is not going to be allocated in ConstraintSolver arena. @slavapestov WDYT?

…lver` arena

Since `HoleType` directly as well as other types which could contain holes
are bound to a lifetime of constraint system that created them, we need to
make sure that such types are always allocated using `ConstraintSolver`
arena instead of a permanent one.
@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - b6c2f89

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - b6c2f89

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please test Linux platform

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 8f2e53c

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please test Linux platform

@CodaFi
Copy link
Contributor

CodaFi commented Aug 27, 2020

Outside of the solver, there are a number of places we assert that we aren't seeing types with type variables in them. Those should be extended to also assert about not seeing holes.

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

I can do that for completeness and I have added an assert to SetExprTypes which makes sure that AST doesn't get holes...

@theblixguy
Copy link
Collaborator

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@theblixguy Yes, that's one of the things which could be improved with "rich" hole types.

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@CodaFi Added more asserts to match what we have for type variables.

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please smoke test

@xedin
Copy link
Contributor Author

xedin commented Aug 27, 2020

@swift-ci please test

@swiftlang swiftlang deleted a comment from swift-ci Aug 27, 2020
@swiftlang swiftlang deleted a comment from swift-ci Aug 27, 2020
@swiftlang swiftlang deleted a comment from swift-ci Aug 28, 2020
@xedin
Copy link
Contributor Author

xedin commented Aug 28, 2020

@swift-ci please test source compatibility

@xedin xedin merged commit a6c3e6f into swiftlang:master Aug 28, 2020
Copy link
Contributor

@davezarzycki davezarzycki left a comment

Choose a reason for hiding this comment

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

Sorry for the post-commit review. I didn't see this until now.

@@ -126,7 +126,7 @@ namespace {
} // end anonymous namespace

void Expr::setType(Type T) {
assert(!T || !T->hasTypeVariable());
assert(!T || !T->hasTypeVariable() || !T->hasHole());
Copy link
Contributor

Choose a reason for hiding this comment

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

What do you think about creating a isCheckedType() method that ensures that the type is not a UNCHECKED_TYPE node? That way we don't need to update every call site that does this: !T->hasTypeVariable().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense to me but I think we can't do it at the moment unless UnresolvedType is excluded from that check because code completion is still replying on solutions with unresolved types applied to AST (we are currently working on fixing that)...

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.

8 participants