Commit Graph

1030 Commits

Author SHA1 Message Date
David Zarzycki
6dc2b7fda9 [Sema] NFC: Adopt reference storage type meta-programming macros 2018-06-30 06:44:33 -04:00
John McCall
9bee3cac5a Generalize storage implementations to support generalized accessors.
The storage kind has been replaced with three separate "impl kinds",
one for each of the basic access kinds (read, write, and read/write).
This makes it far easier to mix-and-match implementations of different
accessors, as well as subtleties like implementing both a setter
and an independent read/write operation.

AccessStrategy has become a bit more explicit about how exactly the
access should be implemented.  For example, the accessor-based kinds
now carry the exact accessor intended to be used.  Also, I've shifted
responsibilities slightly between AccessStrategy and AccessSemantics
so that AccessSemantics::Ordinary can be used except in the sorts of
semantic-bypasses that accessor synthesis wants.  This requires
knowing the correct DC of the access when computing the access strategy;
the upshot is that SILGenFunction now needs a DC.

Accessor synthesis has been reworked so that only the declarations are
built immediately; body synthesis can be safely delayed out of the main
decl-checking path.  This caused a large number of ramifications,
especially for lazy properties, and greatly inflated the size of this
patch.  That is... really regrettable.  The impetus for changing this
was necessity: I needed to rework accessor synthesis to end its reliance
on distinctions like Stored vs. StoredWithTrivialAccessors, and those
fixes were exposing serious re-entrancy problems, and fixing that... well.
Breaking the fixes apart at this point would be a serious endeavor.
2018-06-30 05:19:03 -04:00
Joe Groff
28fc7b0c80 Sema: Diagnose completely unapplied references to mutating methods.
The currying behavior of method references completely breaks in the face of `inout` semantics, even moreso with exclusivity enforcement, but we failed to diagnose these references in Swift 4 and previous versions. Raise a compatibility warning when these references are found in Swift 4 code, or error in Swift 5 and later. Simplify the partial application logic here slightly too now that standalone functions do not allow currying. Addresses rdar://problem/41361334 | SR-8074.
2018-06-29 21:04:42 -07:00
Mark Lacey
fea720f229 [MiscDiagnostics] Emit a deprecation warning for some writes through literal keypaths.
We incorrectly allowed some keypaths to be inferred as writable
keypaths in Swift 3/4 modes. This no longer happens when
-swift-version 5 is specified.

This warning is a limited attempt at providing some advanced notice of
code that will break, only in the cases where the keypath is a direct
argument to a keypath subscript write.

Fixes: rdar://problem/40068274
2018-06-25 23:03:06 -07:00
John McCall
9022b5152f Rename accessor kinds from IsGetter -> IsGet, etc.
Introduce some metaprogramming of accessors and generally prepare
for storing less-structured accessor lists.

NFC except for a change to the serialization format.
2018-06-14 17:08:55 -04:00
Slava Pestov
296ce3f312 AST: Remove hack-around for getInterfaceType() on ParamDecl returning InOutType
Most callers did not want the InOutType here, and checked
the ParamDecl's flags instead.
2018-06-13 15:38:52 -07:00
David Zarzycki
7a5d9680e1 [Sema] NFC: Reorder misc bits for better code gen
As a general rule, if one wants packs miscellaneous bits into what is
otherwise just a number, the low bits are preferable over the high bits,
at least on x86. On ppc64/ppc64le, the results are more of a wash. For
reference:

struct X {
    unsigned kind : 3;
    unsigned level : 29;
};

X example(X x, unsigned y) {
    x.level += y;
    return x;
}

bool example(X x) {
    return x.level > 1;
}

==== x86_64 with 'kind' first ====

_Z7example1Xj:
        leal    (%rdi,%rsi,8), %eax
        retq

_Z7example1X:
        cmpl    $15, %edi
        seta    %al
        retq

==== x86_64 with 'kind' last ====

_Z7example1Xj:
        leal    (%rsi,%rdi), %eax
        andl    $536870911, %eax        # imm = 0x1FFFFFFF
        andl    $-536870912, %edi       # imm = 0xE0000000
        orl     %edi, %eax
        retq

_Z7example1X:
        testl   $536870910, %edi        # imm = 0x1FFFFFFE
        setne   %al
        retq

==== PPC64 with 'kind' first ====

_Z7example1Xj:
        add 5, 5, 4
        rlwimi 5, 4, 0, 0, 2
        stw 5, 0(3)
        blr

_Z7example1X:
        rlwinm 3, 3, 0, 3, 30
        cntlzw  3, 3
        srwi 3, 3, 5
        xori 3, 3, 1
        blr

==== PPC64 with 'kind' last ====

_Z7example1Xj:
        slwi 5, 5, 3
        add 4, 5, 4
        stw 4, 0(3)
        blr

_Z7example1X:
        clrldi  3, 3, 32
        subfic 3, 3, 15
        rldicl 3, 3, 1, 63
        blr

==== PPC64LE with 'kind' first ====

_Z7example1Xj:
        slwi 4, 4, 3
        add 3, 4, 3
        blr

_Z7example1X:
        clrldi  3, 3, 32
        subfic 3, 3, 15
        rldicl 3, 3, 1, 63
        blr

==== PPC64LE with 'kind' last ====

_Z7example1Xj:
        add 4, 4, 3
        rlwimi 3, 4, 0, 3, 31
        blr

_Z7example1X:
        rlwinm 3, 3, 0, 3, 30
        cntlzw  3, 3
        srwi 3, 3, 5
        xori 3, 3, 1
        blr
2018-05-17 16:04:28 -04:00
Mark Lacey
08f80ec361 Properly handle subscripts when avoiding IUO-to-Any coercion warnings.
Ensure that we grab the decl from the subscript expression so that we
can check whether it returns an IUO.

Also add tests for subscripts and failable inits.
2018-05-07 18:10:57 -07:00
Mark Lacey
a61a6d536e Conditionalize warnings for IUO-to-Any coercion on Swift version.
These warnings are turning out to be pretty noisy for code that
declares IUOs (e.g. for @IBOutlets) and then passes them to
Objective-C APIs with parameters declared as _Nonnull id.

Since we bridge non-nil values successfully in most cases, and
previuosly written and correctly executing code is either not seeing
nil values passed in or are handling the nil (which is bridged as
NSNull), it seems like a nuisance to warn about these for existing
Swift versions.

We'll conditionalize the warning, and then users can deal with these
when moving to the new language version.

Fixes: rdar://problem/39886178
2018-05-07 15:49:56 -07:00
Doug Gregor
192234415d [AST] Store SubstitutionMaps in ConcreteDeclRef and Witness data structures.
Replace two prominent uses of SubstitutionList, in ConcreteDeclRef and
Witness, with SubstitutionMap. Deal with the myriad places where we
now have substitution maps and need substitution lists (or vice versa)
caused by this change.

Overall, removes ~50 explicit uses of SubstitutionList (of ~400).
2018-05-02 13:38:14 -07:00
Pavel Yaskevich
02b2aa7485 [MiscDiagnostics] Switch to use AnyFunctionType::getParams() instead of getInput() 2018-04-26 17:33:08 -07:00
David Zarzycki
9a8a9d44b9 Merge pull request #16058 from davezarzycki/qoi_formalize_ReferenceOwnership_diags
[Diag] QoI: Add ReferenceOwnership to DiagnosticArgumentKind
2018-04-20 11:02:48 -04:00
David Zarzycki
e326954bd0 [Diag] QoI: Add ReferenceOwnership to DiagnosticArgumentKind
By formalizing ReferenceOwnership as a diagnostic argument kind, we get
less boilerplate, better type safety, better output consistency, and
last but not least: future proofing.
2018-04-20 08:32:43 -04:00
Huon Wilson
fe00f53e48 [AST] Use flags instead of booleans for UnqualifiedLookup. 2018-04-09 10:58:49 +10:00
Hamish
4f8ad1afec [Sema] Revise #14875 in response to feedback
- Diagnose on the location of the '=' equals sign for assignments
- Use the variable decl as the location for the note
- Make AST node params 'const'
2018-04-02 23:05:33 +01:00
Hamish
92931b7434 Merge remote-tracking branch 'upstream/master' into warn-weak-insta-dealloc 2018-04-02 18:06:45 +01:00
Doug Gregor
b2b69e8abf Rename BoundNameAliasType to NameAliasType.
NameAliasType is dead! Long live NameAliasType!
2018-03-25 21:35:17 -07:00
Doug Gregor
c43f96a855 [AST] Remove now-unused NameAliasType. 2018-03-25 21:35:16 -07:00
Slava Pestov
6b899b625e Merge pull request #15435 from slavapestov/one-more-base-identifier
Sema: Change a getBaseIdentifier() to userFacingName()
2018-03-22 19:08:52 -07:00
Slava Pestov
7d9599a93d Sema: Change a getBaseIdentifier() to userFacingName() 2018-03-22 14:19:38 -07:00
Doug Gregor
253bd10bd3 Cope with BoundNameAliasType in more places that handle NameAliasType. 2018-03-21 23:49:33 -07:00
Mark Lacey
994c2d1acd Improve type join for function types.
It is still not completely general, but this moves things along a
little bit.
2018-03-21 11:51:54 -07:00
Slava Pestov
d118654fa8 Sema: Peephole for 'nil' literal of Optional type 2018-03-18 00:13:55 -07:00
Slava Pestov
615d068d63 Sema: Replace some uses of getBaseIdentifier() with userFacingName() 2018-03-14 22:26:58 -07:00
Hamish
48f08bdbc7 [Sema] Diagnose immediate deallocation of instances assigned to non-owning variables
When performing a binding/assignment to a weak or unowned variable/property from an initialiser call, emit a warning that the instance will be immediately deallocated.
2018-03-08 16:32:35 +00:00
Sho Ikeda
74ba135008 Merge pull request #15040 from ikesyo/gardening-not-empty
[gardening] Use `!empty()` over `size() > 0`
2018-03-08 18:47:12 +09:00
Huon Wilson
e307e54098 [AST] Explicitly track things marked __owned. 2018-03-08 12:36:24 +11:00
Sho Ikeda
cea6c03eb2 [gardening] Use !empty() over size() > 0 2018-03-08 09:21:09 +09:00
Slava Pestov
26e69d9667 Sema: Diagnose self.init and super.init inside closures
Fixes <rdar://problem/27420414>.
2018-03-06 18:20:07 -08:00
Mark Lacey
d63bb3fc53 Remove most uses of OptionalTypeKind.
What remains are places where we are conflating optionality with
either nullability or failability.
2018-02-10 16:24:09 -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
Hamish
416cb7f156 [Sema] Update Optional-to-Any tests for #14299
As IUOs types are no longer generated, we don't show them in diagnostics.
2018-02-02 20:00:02 +00:00
Hamish
7d3785a15e [Sema] Enable Optional-to-Any diagnostics for IUOs
For example, the implicit coercion from `Int!` to `Any` now produces a warning. Previously a warning wasn't emitted despite the optional being implicitly erased to `Any` (but warnings were emitted in some other cases, such as `Int?!` to `Any?`; this commit fixes that inconsistency).
2018-02-02 14:07:03 +00:00
Hamish
115836210f [Sema] Improve Optional-to-Any diagnostics for collections
When implicitly coercing a collection of optional elements to a collection of less-optional `Any` elements, e.g `[Any?]` to `[Any]`, emit a slightly better diagnostic:

Expression implicitly coerced from '[Any?]' to '[Any]'

and allow for silencing of the diagnostic with an explicit coercion, e.g `as [Any]`.
2018-02-02 14:07:02 +00:00
Hamish
016af6bfae [Sema] Improve Optional-to-Any diagnostics for nested optionals
When implicitly coercing a nested optional to a less optional `Any`, e.g `Any??` to `Any?`, emit a slightly better diagnostic:

Expression implicitly coerced from 'Any??' to 'Any?'

and allow for silencing of the diagnostic with an explicit coercion, e.g `as Any?`.
2018-02-02 14:07:01 +00:00
Hamish
1891c3820b [Sema] Change Optional-to-Any diagnostics to take a variable destination type
This will let us customise the diagnostic for collections and nested optionals.
2018-02-02 14:07:00 +00:00
Hamish
e0f9732eed [Sema] Refactor Optional-to-Any diagnostic logic (NFC) 2018-02-02 14:07:00 +00:00
Mark Lacey
3aa8cfd72d Fix one source of problems with allowing @noescape functions to escape.
We have other issues with Any and generics, but those will require
some additional work to get reasonable diagnostics.

Fixes part of rdar://24097075.
2018-01-23 09:26:11 -08:00
gregomni
0c3c0fd59b Support for fallthrough into cases with pattern variables. 2018-01-20 11:10:00 -08:00
John McCall
7f0f8830cd Split AccessorDecl out from FuncDecl. NFC.
This has three principal advantages:

- It gives some additional type-safety when working
  with known accessors.

- It makes it significantly easier to test whether a declaration
  is an accessor and encourages the use of a common idiom.

- It saves a small amount of memory in both FuncDecl and its
  serialized form.
2018-01-12 14:20:27 -05:00
John McCall
02d7fe36ba Extend the addObserver/removeObserver to AnyObject lookup idioms.
rdar://33850465
2017-11-29 18:04:22 -05:00
Doug Gregor
3882c2922c [Type checker] Teach optional-promotion warning to understand member operators.
Fixes a diagnostics regression for expressions such as "x != nil"
(where x is non-optional), due to moving Optional's == into Optional
itself.
2017-11-17 10:52:05 -08:00
Greg Parker
e8475cc130 Revert "Use conditional conformances to implement Equatable for Optional, Array and Dictionary" 2017-11-15 14:17:22 -08:00
Doug Gregor
c8e21e7b57 [Type checker] Teach optional-promotion warning to understand member operators.
Fixes a diagnostics regression for expressions such as "x != nil"
(where x is non-optional), due to moving Optional's == into Optional
itself.
2017-11-14 16:23:20 -08:00
Doug Gregor
a49455e3b2 [Clang importer] Move the "all properties" lists into the name importer.
Swift's ASTContext contained all of the logic to find the complete list
of properties for an Objective-C class, which is used by the Clang importer
to influence the mapping of Objective-C names into Swift. Swift's
ASTContext also included a *cache* for this information, indexed by
the Clang `ObjCInterfaceDecl *`. However, this cache was getting
populated/queried from the Clang importer's name importer, such that
the keys would be Clang declarations used to build modules and then
deallocated. If that memory eventually gets reused for a different
`ObjCInterfaceDecl *`, we would get incorrect/stale all-properties
information.

Almost Surely fixes rdar://problem/35347167, which is a
nondeterministic failure where UIView's `addGestureRecognizer(_:)` gets
occasionally imported as `add(_:)`.
2017-11-08 15:25:15 -08:00
Hamish
d734631120 [Sema] Allow non-escaping functions to be passed as subscript arguments 2017-11-02 11:04:53 +00:00
Tony Allevato
715ba632dd Merge branch 'master' into synthesize-equatable-hashable 2017-10-09 15:57:36 -07:00
Brian King
a5a6684fce Improve diagnostic language 2017-09-26 20:47:09 -04:00
Brian King
c5397ba41b Clean up implementation 2017-09-26 20:46:40 -04:00