Commit Graph

649 Commits

Author SHA1 Message Date
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
Pavel Yaskevich
8688dde8b3 [CSSolver] Clarify generic disjunction choice favoring conditions
Attempt to avoid trying to optimize binary generic disjunctions
when one of the choices has `Any` or `Any?` since it's risky to
assume which one is better without solving.

Resolves: rdar://problem/38625824
2018-04-03 15:25:28 -07:00
Mark Lacey
72f4b3d3b8 NFC: Minor cleanup to make createTypeVariable options default to zero.
Many (perhaps most?) calls to createTypeVariable explicitly pass 0 for
options. This change just defaults the parameter to 0 and removes all
the explicit 0's in the code.
2018-04-03 10:19:38 -07:00
David Zarzycki
c8e17157af [Sema] Remove unneeded uses of TVO_CanBindToInOut 2018-03-28 14:25:13 -04:00
Robert Widmann
03580d2fe5 Add a parameter list to EnumElementDecl
This models, but does not plumb through, default arguments.
2018-03-28 00:05:56 -04:00
Pavel Yaskevich
9f7d91323b [Sema] Don't allow member type lookup on existential types
Existential types don't support nested types so let's not
allow the lookup to make such access, with only exception
for typealias or associated types without type parameters,
they are currently allowed.

Resolves: rdar://problem/38505436
2018-03-16 13:53:09 -07:00
Mark Lacey
001bbdae13 Fix overload set construction to not have an extra element in the disjunction.
Any time we had a favored choice for a disjunction, we attempted to
not add it twice, but that little optimization was faulty.

This should speed up some expression type checking a tiny bit in cases
where the favored choice did not result in a solution.

Noticed by observation.
2018-03-07 13:54:25 -08:00
Mark Lacey
b9cd30331d [ConstraintSystem] Do not attempt to force references of unapplied functions.
We were inserting function conversion expressions that were then
turned into forces of values in cases where we merely referenced
functions, but did not actually call them.

Fixes rdar://problem/37241550.
2018-02-20 17:49:10 -08:00
Mark Lacey
649e0d977e Merge pull request #14692 from rudkx/rdar37240984
[ConstraintSystem] Implicitly force results of @optional protocol req…
2018-02-16 18:41:17 -08:00
Mark Lacey
4ee6ead47e [ConstraintSystem] Implicitly force results of @optional protocol requirements.
We were not handling IUO results of @optional protocol methods
properly, sometimes forcing the @optional requirement rather than the
result of the call.

Fixes rdar://problem/37240984.
2018-02-16 16:58:52 -08:00
Chris Lattner
a0fa5d11b4 Implement SE-0195, which introduces "Dynamic Member Lookup" Types (#14546)
* Implement the recently accepted SE-0195 proposal, which introduces "Dynamic
Member Lookup" Types.  This is a dusted off and updated version of PR13361,
which switches from DynamicMemberLookupProtocol to @dynamicMemberLookup as
was requested by the final review decision.  This also rebases it,
updates it for other changes in the compiler, fixes a bunch of bugs, and adds support for keypaths.  

Thank you to @rudx and @DougGregor in particular for the helpful review comments and test cases!
2018-02-16 16:19:50 -08:00
Doug Gregor
9226c4ceec Merge pull request #14567 from DougGregor/conformance-check-null-generic-env
[Type checker] Tolerate missing generic environments better.
2018-02-13 11:26:10 -08:00
Doug Gregor
fd5d3afb61 [Type checker] Tolerate missing generic environments better.
Before matching witnesses, ensure that we have a valid signature, and be
more tolerant of null generic environments by using the
mapTypeIntoContext entry point meant to handle null.

We haven't managed to get a decent reproducer for this, but it's
happening often enough, and this change hardens the affected code
paths.

Should fix rdar://problem/37255982.
2018-02-12 14:31:11 -08:00
Mark Lacey
3bb7ed9fe4 Remove dead assignment. 2018-02-10 21:16:22 -08:00
Doug Gregor
3d32e89e33 [Constraint solver] Favor more-specialized overload among two generics.
When we form an overload set containing two generic functions, where
one is more specialized than the other, "favor" the more-specialized
function in the constraint system so we won't explore any paths
involving the less-specialized function when the more-specialized
version applies. This should be a strict improvement, because
previously we would have always gone down both paths.

Fixes rdar://problem/37371815, taking this exponential example linear.
2018-02-08 20:41:13 -08:00
Mark Lacey
be8defb29e Rename lookThroughAllAnyOptionalTypes to lookThroughAllOptionalTypes. 2018-02-05 23:59:01 -08:00
Mark Lacey
b4b66bc8e8 Replace getAnyOptionalObjectType with getOptionalObjectType. 2018-02-05 23:59:00 -08:00
Mark Lacey
3654dcc8fe Remove getImplicitlyUnwrappedOptionalObjectType. 2018-02-03 10:57:11 -08:00
Mark Lacey
3f9689e477 Remove lookThroughImplicitlyUnwrappedOptionalType. 2018-02-03 10:57:10 -08:00
Mark Lacey
2008674495 Make ImplicitlyUnwrappedOptional<T> an unavailable typealias.
Also remove the decl from the known decls and remove a
bunch of code referencing that decl as well as a bunch of other
random things including deserialization support.

This includes removing some specialized diagnostics code that
matched the identifier ImplicitlyUnwrappedOptional, and tweaking
diagnostics for various modes and various issues.

Fixes most of rdar://problem/37121121, among other things.
2018-02-02 08:35:53 -08:00
Mark Lacey
f08823757a IUO: Generate Optional<T> rather than ImplicitlyUnwrappedOptional<T>.
Stop creating ImplicitlyUnwrappedOptional<T> so that we can remove it
from the type system.

Enable the code that generates disjunctions for Optional<T> and
rewrites expressions based on the original declared type being 'T!'.

Most of the changes supporting this were previously merged to master,
but some things were difficult to merge to master without actually
removing IUOs from the type system:
- Dynamic member lookup and dynamic subscripting
- Changes to ensure the bridging peephole still works

Past commits have attempted to retain as much fidelity with how we
were printing things as possible. There are some cases where we still
are not printing things the same way:
- In diagnostics we will print '?' rather than '!'
- Some SourceKit and Code Completion output where we print a Type
  rather than Decl.

Things like module printing via swift-ide-test attempt to print '!'
any place that we now have Optional types that were declared as IUOs.

There are some diagnostics regressions related to the fact that we can
no longer "look through" IUOs. For the same reason some output and
functionality changes in Code Completion. I have an idea of how we can
restore these, and have opened a bug to investigate doing so.

There are some small source compatibility breaks that result from
this change:
- Results of dynamic lookup that are themselves declared IUO can in
  rare circumstances be inferred differently. This shows up in
  test/ClangImporter/objc_parse.swift, where we have
    var optStr = obj.nsstringProperty
  Rather than inferring optStr to be 'String!?', we now infer this to
  be 'String??', which is in line with the expectations of SE-0054.
  The fact that we were only inferring the outermost IUO to be an
  Optional in Swift 4 was a result of the incomplete implementation of
  SE-0054 as opposed to a particular design. This should rarely cause
  problems since in the common-case of actually using the property rather
  than just assigning it to a value with inferred type, we will behave
  the same way.
- Overloading functions with inout parameters strictly by a difference
  in optionality (i.e. Optional<T> vs. ImplicitlyUnwrappedOptional<T>)
  will result in an error rather than the diagnostic that was added
  in Swift 4.1.
- Any place where '!' was being used where it wasn't supposed to be
  allowed by SE-0054 will now treat the '!' as if it were '?'.
  Swift 4.1 generates warnings for these saying that putting '!'
  in that location is deprecated. These locations include for example
  typealiases or any place where '!' is nested in another type like
  `Int!?` or `[Int!]`.

This commit effectively means ImplicitlyUnwrappedOptional<T> is no
longer part of the type system, although I haven't actually removed
all of the code dealing with it yet.

ImplicitlyUnwrappedOptional<T> is is dead, long live implicitly
unwrapped Optional<T>!

Resolves rdar://problem/33272674.
2018-01-31 12:15:58 -08:00