Commit Graph

180 Commits

Author SHA1 Message Date
Hamish Knight
c225d52873 [CS] Add locator last element casting members
These members provide a convenient way of casting the last element of
a locator to a given path element type.
2019-08-19 11:58:50 +01:00
Hamish Knight
a0919f73b0 [CS] Use subclasses to expose locator element info
Instead of adding specific accessors directly to
ConstraintLocator::PathElement, add subclasses that expose these
accessors.
2019-08-19 11:58:49 +01:00
Hamish Knight
b5b41a5bbe [CS] Add specific accessors for path element info
This commit replaces the `getValue()` and `getValue2()` members on
`ConstraintLocator::PathElement` with specific accessors for each
expected path component kind. IMO this adds some clarity to the call
sites, especially for `getArgIdx()` and `getParamIdx()`.

In addition, this commit adds a private `getValue` member that can
access a value at a given index, which will make it easier to add a
third value in the future.
2019-08-19 11:58:49 +01:00
Pavel Yaskevich
795a84ae6e [CSFix] Delay missing unwrap locator simplification until diagnostic
Instead of keeping two locators in the fix let's store only the
original locator and simplify it later in process of emitting
a diagnostic. That helps to avoid some duplicate work as well
as makes sure that locators supplied to the diagnostic always
have an anchor.

Resolves: rdar://problem/53344815
2019-07-28 21:09:44 -07:00
Suyash Srijan
1a1bff46d8 [CS] Don't crash when using magic literals as default arg
Squash all commits into one
2019-07-17 01:20:25 +01:00
Slava Pestov
c365ef32c6 Sema: Handle protocol compositions containing type variables in matchTypes()
We would previously fail to match something like (C<$T1> & P) against
(C<Int> & P) when the constraint kind was <= Subtype, because we would
fall back to a type equality test in that case.

However, this was only valid for protocol compositions without superclass
constraints since the superclass could contain a type variable on one
side of the constraint.

Fix this by adding support for protocol composition types to
matchDeepEqualityTypes().
2019-06-17 18:40:31 -04:00
Pavel Yaskevich
ba6a5e10f8 [ConstraintSystem] Replace special locator for return of single expr function with a flag on contextual type locator
It's only needed in one place in the constraint solver to allow
`() -> T` to `() -> ()` and `() -> Never` to `() -> T` for expressions
representing return of a single expression functions, so to simplify
contextual type handling in diagnostic and other places it would be
better to replace dedicated locator kind with a flag on existing `ContextualType`.

Resolves: rdar://problem/51641323
2019-06-12 10:13:44 -07:00
Pavel Yaskevich
37373587cb [ConstraintLocator] NFC: Add isForContextualType accessor 2019-05-29 10:26:05 -07:00
Pavel Yaskevich
29e79c66e1 [ConstraintLocator] NFC: Add a helper to return generic parameter if locator points to one 2019-05-29 10:26:04 -07:00
Pavel Yaskevich
1cc6f774a1 [ConstraintSystem] Improve contextual mismatch diagnostics for for ... in <expr> loop 2019-05-15 14:34:24 -07:00
Slava Pestov
e39d236b29 Sema: Simplify for ... in ... type checking 2019-05-15 14:34:18 -07:00
Pavel Yaskevich
43526d031c Merge pull request #24431 from xedin/diag-noescape-param-assignment
[ConstraintSystem] Improve @escaping parameter diagnostics
2019-05-04 20:09:31 -07:00
Pavel Yaskevich
21216d8ecd [ConstraintSystem] Detect and fix invalid refs in dynamic key path member lookup
KeyPath dynamic member lookup is limited to what key path itself
could do, so let's detect and diagnose invalid references just
like we do for regular key path expressions.

Resolves: rdar://problem/50376224
2019-05-02 15:02:11 -07:00
Pavel Yaskevich
943d89136c [ConstraintLocator] NFC: Add isForGenericParameter accessor 2019-05-01 18:22:31 -07:00
nate-chandler
155a155000 Merge pull request #23251 from nate-chandler/nate/omit-return
Allow return to be omitted from single expression functions.
2019-04-25 08:36:34 -07:00
Pavel Yaskevich
448e04ee52 [ConstraintLocator] Add a special locator element to denote contextual type of single expr function 2019-04-24 10:05:38 -07:00
Pavel Yaskevich
a42bf065df [ConstraintSystem] Add a special locator for type of the key path expression 2019-04-23 15:17:24 -07:00
Brent Royal-Gordon
760d4be5e0 [ConstraintSystem] Add a KeyPathComponentResult locator path element
We’ll use this instead of the function result, except for subscript components, which are already structured similarly to subscript expressions.
2019-04-17 21:15:16 -07:00
Pavel Yaskevich
55bc16e785 [Diagnostics] Diagnose static member reference as a key path component 2019-04-15 23:42:34 -07:00
Suyash Srijan
072e84acd6 [CSSimplify] Reject key path if root type is AnyObject (#23820)
Detect situations where `AnyObject` is attempted to be used as a root type of the key path
early and diagnose via new diagnostics framework.
2019-04-14 12:18:35 -07:00
Pavel Yaskevich
93f9cb1d2a [ConstraintLocator] Identify whether locator is a result of keypath dynamic member lookup or belongs to keypath subscript component 2019-04-10 13:51:19 -07:00
Pavel Yaskevich
e3ad92fbfb [ConstraintSystem] Allow to use keypath dynamic member lookup inside keypath expressions 2019-04-03 11:01:48 -07:00
Pavel Yaskevich
84167edde9 [ConstraintSystem] Add a special locator for keypath dynamic member lookup
New locator is going to store pointer to the base keypath declaration.
This information is useful to determine whether such keypath is appropriate
to access underlying properties/subscripts e.g. `WritableKeyPath` cannot be
used to access read-only storage.
2019-04-01 12:40:39 -07:00
Pavel Yaskevich
1c54c5a08b [ConstraintLocator] Add special locator for synthesized arguments 2019-02-25 17:07:26 -08:00
Pavel Yaskevich
f2abfc547a [ConstraintSystem] Add requirement kind to conditional conformance requirement locator 2019-02-07 13:51:20 -08:00
Slava Pestov
2d4b25960d Sema: Type variables for opened generic parameters store the generic parameter type and not an archetype
There's no need to instantiate archetypes in the generic environment
of the declaration being opened.

A couple of diagnostics changed. They were already misleading, and the
new diagnostics, while different, are not any more misleading than
before.
2018-09-27 20:49:23 -07:00
Slava Pestov
858f4b9562 Sema: Remove AssociatedType constraint locator element kind 2018-09-26 23:26:07 -07:00
Slava Pestov
8928cb5b8a Sema: Stop using FunctionType::getOld() when generating constraints for SubscriptExpr
Previously we would generate the following constraint here, where
'index' and 'output' are concrete types and $input is a type
variable:

- ValueMember(base, $input -> output)
- ArgumentTupleConversion(index, $input)

The problem is that we built a function type where the entire input
was a type variable, which would then bind to an argument list.

We could instead generate this constraint:

- ValueMember(base, $member)
- ApplicableFunction(index -> output, $member)

I also had to redo how keypaths map locators back to key path
components. Previously the ArgumentTupleConversion was created
with a locator ending in KeyPathComponent.

Now the ApplicableFunction is created with this locator, which means
the argument match is performed with a locator ending in
ApplyArgument.

A consequence of this is that the SubscriptIndex and SubscriptResult
locator path elements are no longer used, so remove them.

This requires various mechanical changes in places we look at
locators to handle this change. Should be NFC.
2018-08-28 14:40:56 -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
8d4b8e31aa Sema: Remove ScalarToTuple conversion
This either became dead shortly after the removal of Swift 3
compatibility mode from the constraint solver, or even earlier.

Note that the code completion test change is actually correct
because (Any) -> () is not convertible to () -> () in the
language.
2018-08-08 10:18:52 -07:00
gregomni
775cca60b6 Constrain type checking of expressions in optional pattern bindings
so that they must result in an optional type.

Add constraint locator path for identifying constraints/variables that are part of the convert type passed into the system.
2018-07-27 18:05:21 -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
Mark Lacey
43775e817d Disallow some implicit pointer conversions in autoclosures.
Disallow implicit conversion or arguments from Array, String, and
InOut (formed by &) to pointer types if the argument is for an
@autoclosure parameter.

These conversions were really only intended to be used for C/ObjC
interop, and in some contexts like autoclosures they are not safe.

Fixes: rdar://problem/31538995
2018-05-16 12:13:25 -07:00
Mark Lacey
d9094891d1 Fix a typo in some debug output. 2018-04-11 17:22:06 -07: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
Mark Lacey
43aebc832e Revert "IUO: Add support for non-subscript dynamic lookup." 2018-01-10 22:53:56 -08:00
Mark Lacey
a47d1f87c1 IUO: Add support for non-subscript dynamic lookup.
The subscript case will require some additional refactoring to
support.
2018-01-10 16:36:24 -08:00
Mark Lacey
5613d39636 IUO: Hoist the IUO disjunction creation to a better place.
We should just create the disjunction straight-away in resolveOverload
rather than doing it deep inside type variable binding.
2018-01-09 15:45:52 -08:00
Mark Lacey
dbb293289a IUO: Replace one ConstraintLocator with two.
Replace ImplicitlyUnwrappedCoercionResult with the more general
ImplicitlyUnwrappedValue.

Add ImplicitlyUnwrappedDisjunctionChoice as a marker that indicates
that we've already created a disjunction from an
ImplicitlyUnwrappedValue, in order to avoid infinite recursion during
binding.

Also add support to buildDisjunctionForImplicitlyUnwrappedOptional for
functions returning optionals.

NFC.
2018-01-04 10:30:25 -08:00
Mark Lacey
a93cde18c2 [IUO] Add a locator for the result of a cast to an IUO type.
Not used yet, so NFC.
2018-01-03 21:48:40 -08:00
Pavel Yaskevich
df0fd6abfb [ConstraintSystem] Improve type parameter requirement locators
When opening generic types with type parameter requirements,
add information about requirement location to the locator of each
generated constraint to make it easier to extract such information
if needed.
2017-11-27 13:38:38 -08:00
Doug Gregor
33810d9a6a [Constraint solver] Add a constraint locator element for conditional requirements.
Using this constraint locator element, we can check when a failed
constraint is due to an unsatisfied conditional requirement of a
protocol conformance. Unfortunately, it's hard to turn this into
an actionable diagnostic right now.
2017-11-20 21:07:13 -08:00
Joe Groff
fc23781906 Sema: First pass at type-checking Swift key paths.
TODO:

- Select the KeyPath subclass corresponding to the write capability of the key path components
- Figure out an issue with unresolved solutions being chosen with contextually-typed keypaths
- Diagnostic QoI
2017-04-04 11:31:15 -07:00
Slava Pestov
e9c255bfca Sema: Better fix for banning the '() -> T' => '() -> ()?' implicit conversion
Previously we would check TMF_UnwrappingOptional flag, which does not
stick with the constraint, so it would not always persist. Now, add a
new OptionalPayload locator element, which is more correct.

Fixes <rdar://problem/30429709>.
2017-02-13 16:59:09 -08:00
Doug Gregor
4127f700f1 [Type checker] Remove the now-unused InterpolationArgument locator element.
NFC; the prior commit eliminated all uses of this locator path element.
2017-01-30 10:04:55 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Doug Gregor
bc158c31bf [Sema] Improve diagnostics for witness mismatches against @objc protocols.
Simplify and improve the checking of @objc names when matching a
witness to a requirement in the @objc protocol. First, don't use
@objc-ness as part of the initial screening to determine whether a
witness potentially matches an @objc requirement: we will only reject
a potential witness when the potential witness has an explicit
"@nonobjc" attribute on it. Otherwise, the presence of @objc and the
corresponding Objective-C name is checked only after selecting a
candidate. This more closely mirrors what we do for override checking,
where we match based on the Swift names (first) and validate
@objc'ness afterward. It is also a stepping stone to inferring
@objc'ness and @objc names from protocol conformances.

Second, when emitting a diagnostic about a missing or incorrect @objc
annotation, make sure the Fix-It gets the @objc name right: this might
mean adding the Objective-C name along with @objc (e.g.,
"@objc(fooWithString:bar:)"), adding the name to an
unadorned-but-explicit "@objc" attribute, or fixing the name of an
@objc attribute (e.g., "@objc(foo:bar:)" becomes
@objc(fooWithString:bar:)"). Make this diagnostic an error, rather
than a note on a generic "does not conform" diagnostic, so it's much
easier to see the diagnostic and apply the Fix-It.

Third, when emitting the warning about a non-@objc near-match for an
optional @objc requirement, provide two Fix-Its: one that adds the
appropriate @objc annotation (per the paragraph above), and one that
adds @nonobjc to silence the warning.

Part of the QoI improvements for conformances to @objc protocols,
rdar://problem/25159872.
2016-04-19 10:22:23 -07:00
Greg Parker
125a146365 Revert "[Sema] Improve diagnostics for witness mismatches against @objc protocols." and "Improve diagnostics for selector collisions with @objc optional requirements."
This reverts commits 46269299cd
and 27279866ad
and c826a408dd.

The changes broke test bots, including
https://ci.swift.org/job/oss-swift-package-osx/1348/
2016-04-19 05:52:33 -07:00
Doug Gregor
46269299cd [Sema] Improve diagnostics for witness mismatches against @objc protocols.
Simplify and improve the checking of @objc names when matching a
witness to a requirement in the @objc protocol. First, don't use
@objc-ness as part of the initial screening to determine whether a
witness potentially matches an @objc requirement: we will only reject
a potential witness when the potential witness has an explicit
"@nonobjc" attribute on it. Otherwise, the presence of @objc and the
corresponding Objective-C name is checked only after selecting a
candidate. This more closely mirrors what we do for override checking,
where we match based on the Swift names (first) and validate
@objc'ness afterward. It is also a stepping stone to inferring
@objc'ness and @objc names from protocol conformances.

Second, when emitting a diagnostic about a missing or incorrect @objc
annotation, make sure the Fix-It gets the @objc name right: this might
mean adding the Objective-C name along with @objc (e.g.,
"@objc(fooWithString:bar:)"), adding the name to an
unadorned-but-explicit "@objc" attribute, or fixing the name of an
@objc attribute (e.g., "@objc(foo:bar:)" becomes
@objc(fooWithString:bar:)"). Make this diagnostic an error, rather
than a note on a generic "does not conform" diagnostic, so it's much
easier to see the diagnostic and apply the Fix-It.

Third, when emitting the warning about a non-@objc near-match for an
optional @objc requirement, provide two Fix-Its: one that adds the
appropriate @objc annotation (per the paragraph above), and one that
adds @nonobjc to silence the warning.

Part of the QoI improvements for conformances to @objc protocols,
rdar://problem/25159872.
2016-04-18 17:08:06 -07:00