Commit Graph

1476 Commits

Author SHA1 Message Date
Mark Lacey
3fd22c95fd Detect/prevent type variables from leaking across constraint systems.
It's possible some of the asserts will eventually migrate to something
like test-and-return, but at least for now let's ensure that we know
when this is happening.

This is not comprehensive. There are other places we are creating fresh
constraint systems and then creating constraints using type variables
from a pre-existing constraint systems.
2016-11-02 15:13:05 -07:00
Xi Ge
911f7b1805 [CodeCompletion] Using the default constraints generator for TupleExpr (#5557)
[CodeCompletion] Using the default constraints generator for TupleExpr
when inferring the type of unresolved members. rdar://28991372

When code completing, we used to create a type variable to represent the type
of an entire tuple expression. However, recent improvements on parser make this
step unnecessary and crash-prone. Thus, we use the default constraint
generator to interpret tuple expressions.
2016-10-31 12:00:50 -07:00
Doug Gregor
e094adb50a [Constraint solver] Use custom addConstraint entrypoints consistently.
When adding constraints into the constraint system, don't immediately
allocate a Constraint and add it via the most-general
addConstraint(). Instead, go through a more specific entrypoint (e.g.,
addValueMemberConstraint, addRestrictedConstraint, etc.), so we can
start phasing out the general "add an already-formed constraint"
function. NFC
2016-10-21 14:03:56 -07:00
practicalswift
de2c9c64d0 [gardening] Remove unused argument otherArg.
Added in commit 49b833b51a.
2016-10-16 20:24:12 +02:00
Doug Gregor
11b30a22a6 [Constraint solver] Remove some dead instance variables. 2016-10-14 09:31:23 -07:00
Doug Gregor
49b833b51a [Type checker] Eliminate the 'literalConformanceProto' state on type variables.
The 'literalConformanceProto' field of
TypeVariableType::Implementation didn't take into account equivalence
classes of type variables. Eliminate it, and either look at the actual
expressions (for optimizing constraints during constraint generation)
or the actual constraints on a given type variable (for determining
whether to include optionals in the set of potential type variable
bindings).

(cherry picked from commit 6bdd9cfae5)
2016-10-13 16:22:01 -07:00
Doug Gregor
0699263b84 Eliminate use of now-removed getInnerParenType(). 2016-10-12 09:33:38 -07:00
Doug Gregor
01fa24cc9b Speculatively revert "[Type checker] Eliminate the 'literalConformanceProto' state on type variables."
This reverts commit 6bdd9cfae5. This
commit *appears* to be breaking something in Dollar involving
inference with array literals and 'nil'; pull it back for more
investigation.
2016-10-12 09:20:16 -07:00
Robert Widmann
f59d5cb577 Normalize the way paren types are stripped in Sema. 2016-10-12 00:00:39 -04:00
Doug Gregor
6bdd9cfae5 [Type checker] Eliminate the 'literalConformanceProto' state on type variables.
The 'literalConformanceProto' field of
TypeVariableType::Implementation didn't take into account equivalence
classes of type variables. Eliminate it, and either look at the actual
expressions (for optimizing constraints during constraint generation)
or the actual constraints on a given type variable (for determining
whether to include optionals in the set of potential type variable
bindings).
2016-10-11 17:09:13 -07:00
Doug Gregor
331937a129 [Type checker] Track defaulted constraints based on their locator. NFC
While, tracking defaulted constraints based on their type variable
usually works in practice, it can break if the type variable ends up
being equivalent to some other type variable that. Instead, record the
locators associated with Defaultable constraints where we used the
default, which are easier to work with during constraint application.
2016-10-11 17:09:13 -07:00
Doug Gregor
50341da32b Use "TypeBase::hasError()" rather than "is<ErrorType>()" where needed.
In most places where we were checking "is<ErrorType>()", we now mean
"any error occurred". The few exceptions are in associated type
inference, code completion, and expression diagnostics, where we might
still work with partial errors.
2016-10-07 10:58:23 -07:00
Doug Gregor
262fe95344 Drastically scale back usage of SubstFlags::IgnoreMissing.
This particular flag should only be used in rare cases where we don't
want to know about failures, but instead want to get some
partially-formed type. Only very specific parts of the type checker
need this (associated type inference), and code completion relies on
it for slightly-better results.
2016-10-06 14:44:11 -07:00
Doug Gregor
8fab641da0 [AST] Simplify DependentMemberType creation and use isTypeParameter() more. NFC 2016-10-06 14:24:55 -07:00
Pavel Yaskevich
4a60a53b19 [Diagnostics] Fix bad diagnostic for failed assignment from Any to a more specific type
When generating constraints for subscript convert InOutType into LValueType,
because base of the subscript should never be marked as inout, but rather as
@lvalue to denote mutability.

Resolves <rdar://problem/25601561>.
2016-10-04 16:23:05 -07:00
Slava Pestov
cfe9e6a3de IDE: Use GenericSignatures and interface types (mostly)
There was a ton of complicated logic here to work around
two problems:

- Same-type constraints were not represented properly in
  RequirementReprs, requiring us to store them in strong form
  and parse them out when printing type interfaces.

- The TypeBase::getAllGenericArgs() method did not do the
  right thing for members of protocols and protocol extensions,
  and so instead of simple calls to Type::subst(), we had
  an elaborate 'ArchetypeTransformer' abstraction repeated
  in two places.

Rewrite this code to use GenericSignatures and
GenericFunctionType instead of old-school GenericParamLists
and PolymorphicFunctionType.

This changes the code completion and AST printer output
slightly. A few of the changes are actually fixes for cases
where the old code didn't handle substitutions properly.
A few others are subjective, for example a generic parameter
list of the form <T : Proto> now prints as <T where T : Proto>.

We can add heuristics to make the output whatever we want
here; the important thing is that now we're using modern
abstractions.
2016-10-02 23:49:15 -04:00
Rintaro Ishizaki
339387e95b [AST] Remove DefaultValueExpr type (#4713)
The last instantiation of this type was removed in 68bcb0d2af
2016-09-12 16:50:02 +09:00
Doug Gregor
ba564eec3e Merge pull request #4057 from DougGregor/dictionary-literal-key-fix
[Type checker] Don't merge dictionary key type variables with different kinds
2016-08-05 16:21:27 -07:00
Doug Gregor
4a569f3924 [Type checker] Don't merge dictionary key type variables with different expression kinds.
The constraint generator's optimization to eagerly merge type
variables for different keys in a dictionary literal was too eager,
merging the type variables for (e.g.) a string literal and an integer
literal. This prevented us from properly inferring AnyHashable key
types in dictionary literals. Fixes the rest of rdar://problem/27661580.
2016-08-05 15:56:00 -07:00
Slava Pestov
e354ecfd45 Sema: Rip out some code for diagnosing invalid patterns
The problem here is that we would just emit 'invalid pattern'
instead of digging deeper, which meant that the fix-it for
qualified enum element access wasn't getting inserted for
more complex patterns, such as 'case X(let x)'.

Unfortunately, in the matching_patterns.swift test, we emit
too many diagnostics that are not really useful to figuring
out the problem, and the old 'invalid pattern' made more
sense. I'll work on some CSDiag tweaks to address this --
I think it makes more sense to dig there than just emit a
general 'invalid pattern' diagnostic anyway.

Fixes <rdar://problem/27684266>.
2016-08-05 14:27:17 -07:00
Doug Gregor
22287ddb58 [Type system] Infer 'Any' for array elements and dictionary values and 'AnyHashable' for dictionary keys.
The id-as-Any work regressed cases where Swift code could specify
heterogeneous collection literals, e.g.,

    var states: [String: Any] = [
      "California": [
        "population": 37_000_000,
        "cities": ["Los Angeles", "San Diego", "San Jose"],
      ],
      "Oregon": [
        "population": 4_000_000,
        "cities": ["Portland", "Salem", "Eugene"],
      ]
    ]

Prior to this, the code worked (when Foundation was imported) because
we'd end up with literals of type [NSObject : AnyObject].

The new defaulting rule says that the element type of an array literal
and the key/value types of a dictionary literal can be defaulted if no
stronger type can be inferred. The default type is:

  Any, for the element type of an array literal or the value type of a
  dictionary literal, or

  AnyHashable, for the key type of a dictionary literal.

The latter is intended to compose with implicit conversions to
AnyHashable, so the most-general inferred dictionary type is
[AnyHashable : Any] and will work for any plausible dictionary
literal.

To prevent this inference from diluting types too greatly, we don't
allow this inference in "top-level" expressions, e.g.,

  let d = ["a" : 1, "b" : "two"]

will produce an error because it's a heterogeneous dictionary literal
at the top level. One should annotate this with, e.g.,

  let d = ["a" : 1, "b" : "two"] as [String : Any]

However, we do permit heterogeneous collections in nested positions,
to support cases like the original motivating example.

Fixes rdar://problem/27661580.
2016-08-04 20:58:13 -07:00
David Farler
3c11665d56 [SILGen] Directly silgen access of #dsohandle
Synthesizing a VarDecl for #dsohandle causes some unwanted accessors to
be expected, but we really don't need them: this is a global variable
for the start of the image. There are only two uses of getDSOHandle:
getting the type and emitting the SIL for it. Rather than perform
acrobatics to turn off switches, just emit access directly where it's
needed.

rdar://problem/26565092
2016-07-29 12:09:43 -07:00
Ben Langmuir
c2edbb1da7 Merge pull request #3863 from benlangmuir/debug-constraints-ide-test
[swift-ide-test] Add -debug-constraints option; add logging to typeCh…
2016-07-29 10:10:57 -07:00
Ben Langmuir
e3c9f770f4 [swift-ide-test] Add -debug-constraints option; add logging to typeCheckUnresolvedExpr
I keep wanting to debug type-checking of unresolved members, so add the
logic so I can enable it from swift-ide-test rather than having to
modify the source.
2016-07-29 09:31:32 -07:00
Doug Gregor
202cf2e754 [SE-0111] Track function reference kinds in member references.
Extend the handling of function reference kinds to member references
(e.g., x.f), and therefore the logic for stripping argument labels. We
appear to be stripping argument labels from all of the places where it
is required.
2016-07-28 15:41:59 -07:00
Doug Gregor
a9536906ff [SE-0111] Drop argument labels on references to function values.
When referencing a function in the type checker, drop argument labels
when we don't need them to type-check an immediate call to that
function. This provides the semantic behavior of SE-0111, e.g.,
references to functions as values produce unlabeled function types,
without the representational change of actually dropping argument
labels from the type system.

At the moment, this only works for bare references to functions. It
still needs to be pushed through more of the type checker and more AST
nodes to work in the general case.

Keep this work behind the frontend flag
-suppress-argument-labels-in-types for now.
2016-07-28 15:40:11 -07:00
Doug Gregor
8c7e75afa0 [AST] Eliminate OverloadedMemberRefExpr.
This expression kind was introduced in exactly one place, and only
with one member. That place can use MemberRefExpr instead.
2016-07-28 14:44:23 -07:00
Andrew Trick
a18d490d6a Migrate from UnsafePointer<Void> to UnsafeRawPointer. (#3773)
* Migrate from `UnsafePointer<Void>` to `UnsafeRawPointer`.

As proposed in SE-0107: UnsafeRawPointer.

`void*` imports as `UnsafeMutableRawPointer`.
`const void*` imports as `UnsafeRawPointer`.

Occurrences of `UnsafePointer<Void>` are replaced with UnsafeRawPointer.

* Migrate overlays from UnsafePointer<Void> to UnsafeRawPointer.

This requires explicit memory binding in several places,
particularly in NSData and CoreAudio.

* Fix a bunch of test cases for Void->Raw migration.

* qsort takes IUO values

* Bridge `Unsafe[Mutable]RawPointer as `void [const] *`.

* Parse #dsohandle as UnsafeMutableRawPointer

* Update a bunch of test cases for Void->Raw migration.

* Trivial fix for the SceneKit test case.

* Add an UnsafeRawPointer self initializer.

This is unfortunately necessary for assignment between types imported from C.

* Tiny simplification of the initializer.
2016-07-26 14:21:15 -07:00
Doug Gregor
ee8ce80727 [Constraint generation] Simplify association of argument labels with calls. 2016-07-26 11:23:57 -07:00
Andrew Trick
0ed9ee8dee Revert "Migrate from UnsafePointer<Void> to UnsafeRawPointer. (#3724)"
This reverts commit ece0951924.

This results in lldb failues on linux that I can't readily debug.
Backing out until they can be resolved.
2016-07-26 02:50:57 -07:00
Andrew Trick
ece0951924 Migrate from UnsafePointer<Void> to UnsafeRawPointer. (#3724)
* Migrate from `UnsafePointer<Void>` to `UnsafeRawPointer`.

As proposed in SE-0107: UnsafeRawPointer.

`void*` imports as `UnsafeMutableRawPointer`.
`const void*` imports as `UnsafeRawPointer`.

Occurrences of `UnsafePointer<Void>` are replaced with UnsafeRawPointer.

* Migrate overlays from UnsafePointer<Void> to UnsafeRawPointer.

This requires explicit memory binding in several places,
particularly in NSData and CoreAudio.

* Fix a bunch of test cases for Void->Raw migration.

* qsort takes IUO values

* Bridge `Unsafe[Mutable]RawPointer as `void [const] *`.

* Parse #dsohandle as UnsafeMutableRawPointer

* Update a bunch of test cases for Void->Raw migration.

* Trivial fix for the SceneKit test case.

* Add an UnsafeRawPointer self initializer.

This is unfortunately necessary for assignment between types imported from C.

* Tiny simplification of the initializer.
2016-07-26 02:18:21 -07:00
practicalswift
b5b1c57379 Remove unused variable replacementConstraints. 2016-07-24 22:12:54 +02:00
Ben Langmuir
e4b27cdc82 [CodeCompletion] Clear TypeVariableTypes from unresolved member expressions
This prevents use-after-free when the contraint system arean goes away.

rdar://problem/26057202
2016-07-22 14:22:40 -07:00
Doug Gregor
b2618e65c8 [Type checker] Eliminate the hacks used to find synthesized global '=='.
None of this is needed no that the synthesized '==' functions are
members of types, found via protocol conformance.
2016-07-21 12:54:27 -07:00
Michael Gottesman
a047bb7564 Revert "Fix the build."
This reverts commit dc24c2bd34.

Turns out Chris fixed the build but when I was looking at the bots, his fix had
not been tested yet, so I thought the tree was still red and was trying to
revert to green.
2016-07-17 16:29:18 -07:00
Michael Gottesman
dc24c2bd34 Fix the build.
This reverts commit b4cba58330.
This reverts commit a602927c75.
This reverts commit 55fbe5a763.
2016-07-17 16:17:15 -07:00
Chris Lattner
55fbe5a763 Remove Boolean as a special, privileged type used by Sema, and instead
use the concrete Bool type.  This eliminates a bunch of complexity and
makes diagnostics more concrete.
2016-07-17 15:14:24 -07:00
Robert Widmann
f97e5dcb0e [SE-0115][1/2] Rename *LiteralConvertible protocols to ExpressibleBy*Literal. This
change includes both the necessary protocol updates and the deprecation
warnings
suitable for migration.  A future patch will remove the renamings and
make this
a hard error.
2016-07-12 15:25:24 -07:00
Mark Lacey
30158adbf1 Merge pull request #3207 from rudkx/favor-call-overloads-cleanup
Minor clean-up of favorCallOverloads().
2016-06-30 08:02:45 -07:00
Ben Langmuir
d1fd8adbc4 [CodeCompletion] Workaround a bunch of issues with ErrorType in completion
The brief explanation is that we are using the type-checker in a
questionable way where for various reasons we can type-check the same
"context" expression more than once. Until we figure out how to stop
doing that, at least avoid this obvious source of issues with ErrorType
showing up during the initial (poor) typecheck and then blocking
progress when we do a more specific check later.

rdar://problem/26462306
rdar://problem/25248190
2016-06-27 16:50:30 -07:00
Mark Lacey
014633882e Minor clean-up of favorCallOverloads().
Small code shuffling to put related code together and a break from the
main loop earlier.

NFC.
2016-06-25 11:59:36 -07:00
John McCall
ddf49af810 Handle invalid contexts when computing the super type. 2016-06-22 08:48:12 -07:00
Slava Pestov
bbefeb2fc5 Sema: Better support for nested generic functions
There was a weird corner case with nested generic functions that
would fail in the SIL verifier with some nonsense about archetypes
out of context.

Fix this the "right" way, by re-working Sema function declaration
validation to assign generic signatures in a more principled way.

Previously, nested functions did not get an interface type unless
they themselves had generic parameters.

This was inconsistent with methods nested inside generic types,
which did get an interface type even if they themselves did not
have a generic parameter list.

There's some spill-over in SILGen from this change. Mostly it
makes things more consistent and fixes some corner cases.
2016-06-13 01:22:43 -07:00
Joe Groff
e72af82f04 Remove the unused isSome Optional intrinsics.
We already have detailed knowledge of Optional's layout in SILGen, so these intrinsics were almost unused. They were only used in a few obscure places by some optional-to-bool conversions, used by 'is [A]' collection tests and the codegen for 'lazy' properties. Change these over to generate an EnumIsCaseExpr that we can directly lower to a 'select_enum' instruction in SILGen, leading to better codegen and obviating the need for these intrinsic functions.
2016-06-08 09:31:47 -07:00
Argyrios Kyrtzidis
53847121f3 [IDE/Complete] Fix a bug where 'indices' property from Collections doesn't show up for code-completion.
rdar://26642818
2016-06-06 10:17:54 -07:00
Doug Gregor
01682af23a Revert "[Type checker] Be more rigorous about extracting argument labels from calls."
This reverts commit 93dac8f759.
2016-06-03 16:29:31 -07:00
Doug Gregor
93dac8f759 [Type checker] Be more rigorous about extracting argument labels from calls.
Whenever we have a call, retrieve the argument labels from the
argument structurally and associate them with the callee. We were
previously doing this as a separate AST walk (which was unnecessary),
so fold that into constraint generation for a CallExpr.

This is a slightly-pared-back version of
3753d779bc that isn't so rigid in its
interpretation of ASTs. I'll tighten up the semantics over time.
2016-06-03 14:45:21 -07:00
Doug Gregor
ee85891d11 Revert "[Type checker] Be more rigorous about extracting argument labels from calls."
This reverts commit 3753d779bc. It's
causing some type-inference problems I need to investigate.
2016-06-03 10:21:27 -07:00
Slava Pestov
0f17b899ae Sema: Remove some debug code, NFC 2016-06-02 18:20:17 -07:00
Doug Gregor
3753d779bc [Type checker] Be more rigorous about extracting argument labels from calls.
Whenever we have a call, retrieve the argument labels from the
argument structurally and associate them with the callee. We were
previously doing this as a separate AST walk (which was unnecessary),
so fold that into constraint generation for a CallExpr. We were also
allowing weird ASTs to effectively disable this information: tighten
that up and require that CallExprs always have a ParenExpr, TupleExpr,
or (as a temporary hack) a TypeExpr whose representation is a
TupleTypeRepr as their argument prior to type checking. This gives us
a more sane AST to work with, and guarantees that we aren't losing
label information.

From the user perspective, this should be NFC, because it's mostly AST
cleanup and staging.
2016-06-02 17:15:51 -07:00