Commit Graph

419 Commits

Author SHA1 Message Date
Doug Gregor
2756377367 [Type checker] Separate more functionality from the TypeChecker instance.
Use the usual bag of tricks to eliminating dependence on the
TypeChecker instance: static functions, LazyResolver callbacks, and
emitting diagnostics on decls/ASTContext.
2018-08-20 21:39:44 -07:00
Doug Gregor
7fa4f1df54 Merge pull request #18859 from DougGregor/type-resolution-stage
[Type Checker] Add TypeResolution(Stage) to describe type computations.
2018-08-20 18:44:31 -07:00
Doug Gregor
79a55895de [Type Checker] Replace GenericTypeResolver with a TypeResolution.
Replace the GenericTypeResolver type hierarchy with TypeResolution,
which more simply encapsulates the information needed to resolve
dependent types and makes explicit those places where we are
using resolution to contextual types.
2018-08-20 16:53:44 -07:00
Slava Pestov
527ff375dc AST: Rename old form of {Generic,}FunctionType::get() to getOld()
This makes it easier to grep for and eventually remove the
remaining usages.

It also allows you to write FunctionType::get({}, ...) to call the
ArrayRef overload empty parameter list, instead of picking the Type
overload and calling it with an empty Type() value.

While I"m at it, in a few places instead of renaming just clean up
usages where it was completely mechanical to do so.
2018-08-17 19:28:17 -04:00
Slava Pestov
a54251074c AST: ExtInfo just wraps an unsigned integer, no need to pass it by reference 2018-08-17 19:27:43 -04:00
Jordan Rose
537954fb93 [AST] Rename several DeclContext methods to be clearer and shorter (#18798)
- getAsDeclOrDeclExtensionContext -> getAsDecl

This is basically the same as a dyn_cast, so it should use a 'getAs'
name like TypeBase does.

- getAsNominalTypeOrNominalTypeExtensionContext -> getSelfNominalTypeDecl
- getAsClassOrClassExtensionContext -> getSelfClassDecl
- getAsEnumOrEnumExtensionContext -> getSelfEnumDecl
- getAsStructOrStructExtensionContext -> getSelfStructDecl
- getAsProtocolOrProtocolExtensionContext -> getSelfProtocolDecl
- getAsTypeOrTypeExtensionContext -> getSelfTypeDecl (private)

These do /not/ return some form of 'this'; instead, they get the
extended types when 'this' is an extension. They started off life with
'is' names, which makes sense, but changed to this at some point.  The
names I went with match up with getSelfInterfaceType and
getSelfTypeInContext, even though strictly speaking they're closer to
what getDeclaredInterfaceType does. But it didn't seem right to claim
that an extension "declares" the ClassDecl here.

- getAsProtocolExtensionContext -> getExtendedProtocolDecl

Like the above, this didn't return the ExtensionDecl; it returned its
extended type.

This entire commit is a mechanical change: find-and-replace, followed
by manual reformatted but no code changes.
2018-08-17 14:05:24 -07:00
Pavel Yaskevich
2db0e60275 [ConstraintSystem] Add kind to generic requirement locator
Having requirement kind encoded in the locator helps to identify
what kind of fix to generate (applicable to same-type, superclass)
without retrieving requirement itself.
2018-08-16 01:45:52 -07:00
Slava Pestov
31a570d53c AST: Replace TypeBase::getRValueInstanceType() with getMetatypeInstanceType()
That is, don't look through InOutType anymore, and update callers to
call getInOutObjectType() as well (or not, where it was obvious to me
that InOutType could not appear).

This surfaces more remaining uses of getInOutObjectType() directly.
2018-08-13 21:13:10 -07:00
David Zarzycki
6b6ef5af24 [Sema] NFC: Refactor most TypeResolutionFlags into a traditional enum
TypeResolutionFlags is overly complicated at the moment because the vast
majority of flag combinations are impossible and nonsensical. With this
patch, we create a new TypeResolverContext type that is a classic enum
and far easier to reason about. It also enables "exhaustive enum"
checking, unlike the "flags" based approach.
2018-08-13 08:18:40 -04:00
Doug Gregor
3018becde7 [Type checker] Eliminate type checking from early extension binding.
Use ExtensionDecl::getExtendedNominal() to wire up extensions to their
nominal types early in type checking (the bindExtensions()) operation,
rather than going through type validation to do so.
2018-08-03 11:26:49 -07:00
Mark Lacey
423d2c6139 Merge pull request #18478 from rudkx/getFixedTypeRecursive
[ConstraintSystem] Simplify getFixedTypeRecursive a little.
2018-08-02 19:27:46 -07:00
Mark Lacey
10ea2a59ce [ConstraintSystem] Simplify getFixedTypeRecursive a little.
By making it...recursive.
2018-08-02 15:14:51 -07:00
Slava Pestov
8fc7f7e1e6 Sema: Fix ConstraintSystem::getTypeOfMemberReference() for protocol typealiases 2018-08-01 23:01:56 -07:00
Pavel Yaskevich
16cbc00cbe [ConstraintSystem] Switch to useDC for getUnopenedTypeOfReference while calling from getTypeOfReference 2018-07-28 20:28:42 -07:00
Pavel Yaskevich
f9c6ed54ed [Sema] NFC: Refactor getUnopenedTypeOfReference to accept callback for type 2018-07-28 20:28:41 -07:00
Pavel Yaskevich
d62be440ce [CSSolver] Split `getType(ValueDecl*) into three - getType{OrNull, OrInterfaceType} 2018-07-28 20:28:41 -07:00
Pavel Yaskevich
398abdfb7c [CSSolver] Add closure parameter type caching
While inferring avoid associating type variables with closure
parameters, use cache instead and only set types when everything
is properly type-checked, this avoids multiple problems one of
them - leaking type variables outside of constraint system they
belong to.
2018-07-28 20:28:41 -07:00
Mark Lacey
8f9fcaa275 Merge pull request #18325 from rudkx/minor-refactoring
[ConstraintSystem] Some small clean-ups to getFixedType, getFixedTypeRecursive, and simplifyType
2018-07-28 19:15:12 -07:00
Mark Lacey
a5da413c2a [ConstraintSystem] Simplify getFixedTypeRecursive a bit. 2018-07-27 23:34:03 -07:00
Mark Lacey
4ac4d42b37 [ConstraintSystem] Another small simplification. 2018-07-27 21:08:20 -07:00
Pavel Yaskevich
624c183fe0 [ConstraintGraph] Change gatherConstraints to take SetVector
For stable iteration order, let's switch from `SmallPtrSet`
to `SetVector` which ensures insertion order iteration.
2018-07-27 15:34:15 -07:00
Pavel Yaskevich
48dd1e837b [ConstraintGraph] Add filtering to gatherConstraints per type variable
Most of the use-cases of `gatherConstraints` require filtering
at least based on the constraint kind that caller is interested in,
so instead of returning unrelated results and asking caller to
filter separately, let's add that functionality directly to
`gatherConstraints`.
2018-07-26 22:41:15 -07:00
Pavel Yaskevich
dd798accd8 [ConstraintGraph] Use set to gather constraints for type variables
Since it's possible to find the same constraint through two different
but equivalent type variables, let's use a set to store constraints
instead of a vector to avoid processing the same constraint multiple
times.
2018-07-26 22:41:07 -07:00
Slava Pestov
90cd772228 AST: Remove getParameterLists() and friends from AbstractFunctionDecl subclasses
Now, an AbstractFunctionDecl always stores a single parameter list.

Furthermore, ConstructorDecl and DestructorDecl always store a
ParamDecl for 'self'.

FuncDecl only has a 'self' if it is a member of a nominal type or
extension, so we tail-allocate the storage for it.
2018-07-22 20:56:56 -07:00
Slava Pestov
e1da265873 Sema: Remove uses of AbstractFunctionDecl::getParameterLists() 2018-07-19 21:21:17 -07:00
Slava Pestov
acc494e3ff Sema: Simplify simplifyTypeImpl() a bit 2018-07-16 16:44:27 -07:00
Mark Lacey
aa30cc5adc Merge branch 'master' into remove-swift-3-in-expr-typechecking 2018-07-11 20:55:32 -07:00
Slava Pestov
4878889c1e Sema: Better fix for <rdar://problem/36449760> 2018-07-10 17:01:12 -07:00
Mark Lacey
28f759c8b0 Remove support for -swift-version 3 from the expression type checker.
This is the obvious stuff. There will probably be more fallout.
2018-07-06 16:10:08 -07:00
Mark Lacey
1a2d7f5e99 [ConstraintSystem] Reinstate favoring more specialized of two generic overloads.
This was originally commited in
3d32e89e33, and then backed out in
c40fd3966c due to concern over
behavioral changes.

This version avoids the optimization when there are any optional types
involved in the signatures of the functions.

For these cases, compareDeclarations can return the wrong order at the
moment.

I have another PR in the works that attempts to begin unraveling some
of the issues around fixing the overload comparisons.
2018-06-19 23:42:51 -07:00
Doug Gregor
d098b3e571 [Sema] Remove the IterativeTypeChecker. 2018-06-15 22:46:55 -07:00
Huon Wilson
e4d306f499 Merge pull request #17168 from huonw/validate-conditional-type-in-expr
[Sema] More aggressive consideration of conditionally defined types in expressions
2018-06-15 08:58:24 +10:00
Huon Wilson
5f585a12b4 [Sema] Generalize checking of conditional nested typealiases to all types.
One can have arbitrary types defined in a constrained extension, not just
typealiases, e.g.

struct Foo<T> {}
extension Foo where T == Int {
    struct X {}
}

A mention of `Foo<String>.X` should be flagged, since String != Int. Previously
this worked (in expressions) only if `X` was a typealias, and with this patch
the above example is flagged too.

Part of https://bugs.swift.org/browse/SR-5440 and
https://bugs.swift.org/browse/SR-7976 and rdar://problem/41063870.
2018-06-14 17:34:16 +10:00
Mark Lacey
197593b4aa Merge pull request #17136 from rudkx/rdar40868990
[ConstraintSystem] Allow LValues for the bindings of an IUO @optional…
2018-06-12 12:20:23 -07:00
Mark Lacey
803fbee0dd [ConstraintSystem] Allow LValues for the bindings of an IUO @optional requirement.
We were failing to bind the alternatives for an IUO @optional
requirement because we forgot to set the appropriate type variable option.

Fixes: rdar://problem/40868990
2018-06-11 23:44:00 -07:00
Joe Groff
711984234d Sema: Pass the useDC down to getTypeOfReference.
This fixes a subtle issue where, during single-expression closure type inference, we would ask for the settability of local variables from the outer function's context, leading us to mistakenly consider them mutable inside single-expression closure contexts. DI would catch some but not all violations of the expected semantics here.
2018-06-11 10:53:00 -07:00
Mark Lacey
c40fd3966c [ConstraintSystem] Remove an unsound optimization hack for generic functions.
I think there is something far more narrow we could do here, but I'm
not sure if there is real code that such a hack would benefit from. We
can leave that to consider another day.

Fixes rdar://problem/40819547 (aka https://bugs.swift.org/browse/SR-7884).
2018-06-06 23:59:08 -07:00
Pavel Yaskevich
7445a96e82 [CSSolver] NFC: Refactor openGenericRequirements to fix ASAN failure 2018-06-01 14:21:31 -07:00
Pavel Yaskevich
d57330975a [CSSolver] Make sure generic requirements are checked for typealiases
If solver encounters a typealias inside of constrainted extension
make sure to add its requirements to the constraint system, otherwise
it might produce invalid solutions.
2018-06-01 00:23:36 -07:00
Robert Widmann
7e8aa9fcc6 [NFC] Rename @lvalue Locator Path Element
8271c1a removed the last hacky usage of ArrayElementType,
leaving behind just the @lvalue-to-inout conversions.  Rename
the locator path element to reflect this and do a bit of cleanup on the
unrelated-but-near-enough other hack RValueAdjustment.
2018-05-26 16:48:41 -07:00
Slava Pestov
5a93195949 Sema: Refactor applyUnboundGenericArguments() to take an array of Types not TypeLocs
Most callers were using TypeLoc::withoutLoc() to wrap a Type,
and the one place that still had TypeReprs can resolve the
TypeReprs right there instead.
2018-04-28 00:11:47 -07:00
Pavel Yaskevich
aa2b25b3dd Merge pull request #15868 from xedin/solver-get-params
[Sema] Attempt to replace usages of `getInput()` with `getParams()`
2018-04-26 23:32:15 -07:00
Pavel Yaskevich
4baeec8965 [ConstraintSystem] Switch to use AnyFunctionType::getParams() instead of getInput() 2018-04-26 17:33:08 -07:00
Slava Pestov
0cbbd9ae5d Sema: Don't set 'validation started' bit on ParamDecls from validateDecl()
Otherwise, when the horrible ExprCleanser comes along, it will set
the interface type of the ParamDecl to nullptr. However there is
no way to clear the 'validation started' bit, so the next time
validateDecl() is called on this ParamDecl we will just return,
causing crashes downstream when callers expect it to already have
an interface type set.

Fixes <rdar://problem/38189778> and possibly some instances of
<rdar://problem/36434823>.
2018-04-25 18:04:58 -07:00
Huon Wilson
18683f305d Merge pull request #15587 from huonw/minmax
[Sema] Perform name lookup in outer scopes in some cases involving conditional conformances
2018-04-20 22:33:07 +10:00
Huon Wilson
285c2a469b [CS] Add decl-ref outer alternatives as non-favored options.
Given something like `max(1, 2)` inside a type that conforms to Sequence, this
allows the compiler to consider Swift.max as well as the two methods with that
name on Sequence.
2018-04-20 00:55:11 +10:00
Huon Wilson
9b68eb8a80 [AST] Allow unqualified name lookup to return results from outer scopes.
This is necessary for disambiguating calls to functions like `min` and
`max`.
2018-04-19 11:07:48 +10:00
Mark Lacey
c2b1d0388d Swap two conditions to get dynamically looked-up optional requirements working.
This function (and getTypeOfMemberReference) deserve a deeper cleanup,
but for the moment lets unblock a compatibility test.

Fixes SR-7098 / rdar://problem/38394645
2018-04-10 15:06:36 -07:00
Doug Gregor
c8ab588fb1 Merge pull request #15776 from DougGregor/anyobject-lookup-unavailable
[Type checker] Prefer available declarations in AnyObject lookup.
2018-04-05 15:41:47 -07:00
Doug Gregor
d20bcf14c2 [Type checker] Prefer available declarations in AnyObject lookup.
When we perform AnyObject lookup, we can find multiple declarations with
the same signature. When we do so, we pick fairly arbitrarily among them.
However, make sure that we don’t pick an unavailable declaration when
an available declaration is… available.

Fixes rdar://problem/39115471.
2018-04-05 14:44:13 -07:00