Commit Graph

7976 Commits

Author SHA1 Message Date
practicalswift
cc6fe4f3b2 Merge pull request #6441 from practicalswift/
[gardening] Fix accidental double and triple spaces.
2016-12-21 22:59:46 +01:00
practicalswift
b253b21014 [gardening] Make sure argument names in comments match the actual parameter names 2016-12-21 22:56:01 +01:00
Doug Gregor
dcdef4f7f5 [Type checker] Improve "downcast only unwraps optionals" diagnostics.
Specialize and improve the "downcast only unwraps optionals"
diagnostic to provide specific diagnostics + Fix-Its for the various
casts of forced cast, conditional cast, and "isa" check. Specifically:

* With a forced cast, customize the diagnostic. We still insert the
  appropriate number of !'s, but now we remove the 'as! T' (if an
  implicit conversion would suffice) or replace the 'as!' with 'as'
  (if we still need a bridge)

* With a conditional cast, only emit a diagnostic if we're removing
  just one level of optional. In such cases, we either have a no-op
  (an implicit conversion would do) or we could just use 'as' to the
  optional type, so emit a customized warning to do that. If we are
  removing more than one level of optional, don't complain:
  conditional casts can remove optionals. Add the appropriate Fix-Its
  here.

* With an 'is' expression, only emit a diagnostic if we're removing
  just one level of optional. In this case, the 'is' check is
  equivalent to '!= nil'. Add a Fix-It for that.

Across the board, reduce the error to a warning. These are
semantically-well-formed casts, it's just that they could be written
better.

Fixes rdar://problem/28856049 and rdar://problem/22275685.
2016-12-21 13:47:19 -08:00
Doug Gregor
40140d6e8d [Type checker] Drop unused closure parameter in TypeChecker::typeCheckCheckedCast.
This closure parameter isn't ever actually used and isn't needed. NFC
2016-12-21 13:46:14 -08:00
Doug Gregor
fa47d57b4e [AST] Remove CheckedCastKind::BridgeFromObjectiveC.
It's not handled any differently from ValueCast, so simplify it away.
2016-12-21 13:46:14 -08:00
Doug Gregor
d9843899c4 [Type checker] Clean up handling of checked casts (as!/as?/is).
The type checker implements logic for handling checked casts in two
places: the constraint solver (for type-checking expressions
containing "as!" or "as?") and as a top-level entrypoint for
type-checking as?/as! for diagnostics and is/as patterns. Needless to
say, the two implementations were inconsistent, and in fact both were
wrong, leading to various problems---rejecting perfectly-valid "as!"
and "as?" casts outright, bogus warnings that particular as!/as? casts
always-succeed or always-fail when they wouldn't, and so on.

Start detangling the mess in two ways. First, drastically simplify the
handling of checked casts in the constraint solver, eliminating the
unprincipled "subtype" constraint checks that (among other things)
broke the handling of checked casts that involved bridging or optional
unwrapping. The simpler code is more permissive and more correct; it
essentially accepts that the user knows what she is doing with the
cast.

Second, make the type checker's checking of casts far more thorough,
which includes:

* When we're performing a collection cast, actually check that the
  element types (and key types, for a dictionary) are castable, rather
  than assuming all collection casts are legitimate. This means we'll
  get more useful "always fails" and "always succeeds" diagnostics for
  array/set/dictionary.

* Handle casts from a bridged value type to a subclass of the
  corresponding bridged class type. Previously, these would be
  incorrectly classified as "always fails".

While I'm here, eliminate a spurious diagnostic that occurs when using
a conditional cast ("as?") that could have been a coercion/bridging
conversion ("as"). The optional injection we synthesize to get the
resulting type correct was getting diagnosed as an implicit coercion,
but shouldn't have been.
2016-12-21 13:46:14 -08:00
Doug Gregor
e97ab635ea [Constraint solver] Separate bridging conversions from other conversions.
Previously, bridging conversions were handled as a form of "explicit
conversion" that was treated along the same path as normal
conversions in matchTypes(). Historically, this made some
sense---bridging was just another form of conversion---however, Swift
now separates out bridging into a different kind of conversion that is
available only via an explicit "as". This change accomplishes a few
things:

* Improves type inference around "as" coercions. We were incorrectly
  inferring type variables of the "x" in "x as T" in cases where a
  bridging conversion was expected, which cause some type inference
  failures (e.g., the SR-3319 regression).

* Detangles checking for bridging conversions from other conversions,
  so it's easier to isolate when we're applying a bridging
  conversion.

* Explicitly handle optionals when dealing with bridging conversions,
  addressing a number of problems with incorrect diagnostics, e.g.,
  complains about "unrelated type" cast failures that would succeed at
  runtime.

Addresses rdar://problem/29496775 / SR-3319 / SR-2365.
2016-12-21 13:46:14 -08:00
Doug Gregor
642c8ed3eb [Constraint solver] Collapse getBaseTypeFor(Array|Set)Type into is(Array|Set)Type.
Follow the pattern set by isDictionaryType() of performing the query
and extracting the underlying key/element types directly. We often
need both regardless. NFC
2016-12-21 13:46:13 -08:00
practicalswift
ce7a10474f [gardening] Fix accidental double and triple spaces. 2016-12-21 22:13:56 +01:00
practicalswift
f05a338e9a [gardening] Prefer (!foo) to (foo == false). Fix typo. 2016-12-21 21:25:58 +01:00
Slava Pestov
1cb656314b Sema: Fix crash with unresolved 'is' pattern 2016-12-21 14:20:28 -05:00
Slava Pestov
dc3af8fc99 Sema: Don't re-typecheck multi-statement closures in lazy var initializer
Normally you have to declare a type if the lazy property
initializer is a multi-statement closure, but if the user
forgets we don't want to crash.
2016-12-21 14:20:28 -05:00
Slava Pestov
c0d506c82c Sema: Fix CSDiag's save/restore logic for decls with no type 2016-12-21 14:20:27 -05:00
Slava Pestov
f0f1c60a53 Sema: Fix crash with invalid @objc operators
The corner case of all corner cases.
2016-12-21 14:20:27 -05:00
Slava Pestov
09dd5fec84 Sema: Small fixes 2016-12-21 14:20:27 -05:00
Slava Pestov
978941f1d5 Sema: More consistent logic for non-requirement protocol members 2016-12-21 14:20:27 -05:00
Slava Pestov
3f7a72eb84 Sema: Clean up diagnostic logic when deriving RawRepresentable conformance on enums 2016-12-21 14:20:26 -05:00
Slava Pestov
7fed7b49ed Sema: Maintain invariant that invalid FuncDecls always have an ErrorType
Otherwise, overload resolution and CSDiag disagree on what a viable
candidate is, so we can end up in a situation where the OverloadDeclRefExpr
has a concrete type that doesn't match the argument list in an ApplyExpr,
which causes a crash in CSDiag.
2016-12-21 14:20:26 -05:00
Slava Pestov
1f0817268b Sema: Don't look through metatypes and inout types in lookupMemberType()
Metatypes do not have member types and trying to look one up on a
generic parameter would cause a crash.

InOutTypes are not materializable and we should never be performing
name lookup into them.
2016-12-21 14:20:26 -05:00
Slava Pestov
3180538642 Sema: Remove unused TypeChecker::resolveMemberType() 2016-12-21 14:20:26 -05:00
Slava Pestov
57aec601d8 Sema: Fix crash in member type lookup error path 2016-12-21 14:20:25 -05:00
Slava Pestov
f97d830861 Sema: Fix nullptr dereference in noteArchetypeSource() 2016-12-21 14:20:25 -05:00
Jordan Rose
20a023bc03 Merge pull request #6419 from jrose-apple/keep-properties-and-accessors-on-the-same-page
Don't infer 'dynamic' on accessors in extensions of ObjC classes.
2016-12-20 15:25:18 -08:00
Mark Lacey
d2f1de7570 Merge pull request #6296 from rudkx/disable-any-penalty
Do not penalize binding or equality constraints involving Any.
2016-12-20 14:27:29 -08:00
Jordan Rose
2660cfc7d4 Tidy up inference of '@objc' and 'dynamic' for accessors.
No functionality change.
2016-12-20 14:25:44 -08:00
Jordan Rose
446a824c53 Don't infer 'dynamic' on accessors in extensions of ObjC classes.
Instead, propagate the decision from the associated storage decl (var
or subscript), using the mechanisms that are already in place.

rdar://problem/29741743
2016-12-20 14:19:53 -08:00
Mark Lacey
170dc8acd7 Do not penalize binding or equality constraints involving Any.
We currently have an element in the solution score related to whether we
had a binding or equality constraint involving Any.

Doing this yields some strange results, e.g. if overload resolution
results in a property declared as Any we end up discarding that solution
in favor of solutions that involve other overloads that are not declared
as Any but are also not actually better solutions (e.g. overloads that
are declared as function types).

We really want to retain both solutions in this case and allow the
ranking step of the solver to decide on the better choice.

Fixes rdar://problem/29374163, rdar://problem/29691909.
2016-12-20 13:38:14 -08:00
Jordan Rose
c2210991ca Simulate buggy Swift 3 function-type-alias access checking. (#6418)
Like 9fba89bd, typealiases in parameter position were sometimes
canonicalized away in Swift 3.0, leading to the compiler not
diagnosing improper uses of typealiases. These only occurred in "safe"
circumstances where the typealias wouldn't be leaked to clients of a
library (that is, a client would never see a name they wouldn't be
allowed to type), but it was inconsistent with other typealiases
(which were preserved) and did not follow the intended model.

This particular issue comes from typealiases for function types
needing to be marked as non-escaping in the compiler when used in
parameter position, which requires rebuilding the function type. This
lost the original "spelling" of the parameter as using a typealias in
its type, which meant the compiler did not see it. The change in
a32e11a0d to use TypeReprs to check access meant the model was now
correctly enforced, but broke source compatibility for this
"accidental feature".

The fix is to track when a typealias for a function type is being used
in parameter position, and to not check its access in that case.

rdar://problem/29739577
2016-12-20 13:31:17 -08:00
practicalswift
8ee628fb14 Merge pull request #6408 from practicalswift/gardening-20161220
[gardening] Remove "REQUIRES: asserts". Correct Swift URL. Typos. Headers. PEP-8.
2016-12-20 18:47:42 +01:00
Rintaro Ishizaki
9f43e456f4 [TypeChecker] Add diagnostic with fix-it for specialized enum case pattern in Swift3 mode. 2016-12-21 02:22:20 +09:00
Rintaro Ishizaki
80721e9c30 [TypeChecker] Don't convert specialzed call into EnumElement pattern 2016-12-20 20:21:48 +09:00
practicalswift
1c5425fff9 [gardening] Fix recently introduced typos. 2016-12-20 10:07:36 +01:00
Slava Pestov
867a956fc2 Sema: Name lookup fix for nominal type members of protocols 2016-12-19 18:49:58 -08:00
Slava Pestov
5640339439 Sema: Map types out of context when resolving inheritance clause in ITC 2016-12-19 18:49:57 -08:00
Slava Pestov
303f4bd19e Sema: resolveTypeInContext() checks all parent contexts first 2016-12-19 18:49:57 -08:00
Slava Pestov
f29cc3041a Sema: Leave behind more breadcrumbs 2016-12-19 18:49:56 -08:00
Erik Eckstein
74d979f0ac SILGen: Don’t make class accessors transparent.
Because when they are inlined they might access hidden symbols in another module, like the field offset variable.

fixes rdar://problem/29707641
2016-12-19 17:02:09 -08:00
Slava Pestov
744e4a6aa7 Merge pull request #6043 from slavapestov/circularity
Consolidate circularity checks for recursive validateDecl() calls
2016-12-19 15:42:38 -08:00
Maxwell Swadling
bb8dd86614 Removed using namespace from header 2016-12-19 10:58:46 -08:00
Maxwell Swadling
cf6cb5f74d Ran clang format on InstrumenterSupport.h and PlaygroundTransform.cpp 2016-12-19 10:58:46 -08:00
Maxwell Swadling
77caeefe10 Adopted InitExpr->getType()->getAs<LValueType>
This means the desugared type is used instead.
2016-12-19 10:56:41 -08:00
Maxwell Swadling
8ee55b3719 Fixed array size of buffer and added comment. 2016-12-19 10:56:41 -08:00
Maxwell Swadling
c6cf59a029 Added anchor to C++ class in InstrumenterSupport.h 2016-12-19 10:56:41 -08:00
Maxwell Swadling
a85006dd18 Updated comments based on feedback
Clarified entry points comments. Added formatting detection.
2016-12-19 10:56:41 -08:00
Maxwell Swadling
214efbfc3c Added a new AST Walker that instruments the AST to provide callbacks that simulate a program counter
Based off the PlaygroundTransform, this new ASTWalker leaves calls to __builtin_pc_before and __builtin_pc_after before and after a user would expect a program counter to enter a range of source code.
2016-12-19 10:56:40 -08:00
practicalswift
7b6a7a7959 Merge pull request #6371 from practicalswift/initialize-contextualTypeNode
[gardening] Initialize contextualTypeNode to nullptr to avoid the possibility of calling dyn_cast<BinaryExpr>(…) with an unitialized argument.
2016-12-19 13:42:59 +01:00
Slava Pestov
bb839c5fef Sema: Remove a couple of workarounds 2016-12-19 01:38:23 -08:00
Slava Pestov
4ed17f0f63 AST: Add a new 'isBeingValidated' flag to replace a couple of other flags
Previously, validateDecl() would check if the declaration had an
interface type and use that as an indication not to proceed.

However for functions we can only set an interface type after
checking the generic signature, so a recursive call to validateDecl()
on a function would "steal" the outer call and complete validation.

For generic types, this meant we could have a declaration with a
valid interface type but no generic signature.

Both cases were problematic, so narrow workarounds were put in
place with additional new flags. This made the code harder to
reason about.

This patch consolidates the flags and establishes new invariants:

- If validateDecl() returns and the declaration has no interface
  type and the isBeingValidated() flag is not set, it means one
  of the parent contexts is being validated by an outer recursive
  call.

- If validateDecl() returns and the declaration has the
  isBeingValidated() flag set, it may or may not have an interface
  type. In this case, the declaration itself is being validated
  by an outer recursive call.

- If validateDecl() returns and the declaration has an interface
  type and the isBeingValidated() flag is not set, it means the
  declaration and all of its parent contexts are fully validated
  and ready for use.

In general, we still want name lookup to find things that have an
interface type but are not in a valid generic context, so for this
reason nominal types and associated types get an interface type as
early as possible.

Most other code only wants to see fully formed decls, so a new
hasValidSignature() method returns true iff the interface type is
set and the isBeingValidated() flag is not set.

For example, while resolving a type, we can resolve an unqualified
reference to a nominal type without a valid signature. However, when
applying generic parameters, the hasValidSignature() flag is used
to ensure we error out instead of crashing if the generic signature
has not yet been formed.
2016-12-19 01:38:23 -08:00
Slava Pestov
45ef0f5dfb Sema: Generic types cannot witness associated type requirements
Until recently we didn't allow nested generic types at all.
In Swift 3, generic typealiases were added, and we forgot to
guard against them in witness matching, leading to a crash if
a generic typealias witnesses an associated type requirement.

Now that nested generic nominals are allowed too, add a check.

The diagnostic is not very good, but I'll revisit this later.
2016-12-18 22:34:16 -08:00
Slava Pestov
86d3828ea0 Sema: Don't recurse into validateDecl() when adding Objective-C bridgeable conformances
This catches another case where resolveType() could cause infinite
recursion. No test case, but this prevents crashers from regressing
with a subsequent patch.
2016-12-18 22:34:15 -08:00