Commit Graph

98 Commits

Author SHA1 Message Date
Doug Gregor
2e8d9a4331 [SE-0258] Adopt @propertyWrapper everywhere. 2019-06-12 13:09:40 -07:00
Doug Gregor
c02ecf9859 [SE-0258] Rename to Property Wrappers 2019-05-29 22:17:50 -07:00
Doug Gregor
db30c7784d [SE-0258] Adjust name for CodingKeys property to drop the '$'.
When synthesizing Codable conformances, the enum case name for a
property with an attached delegate does not have the '$', i.e., it's
the name of the originally-declared property.
2019-05-29 15:47:03 -07:00
Pavel Yaskevich
688042becf [Diagnostics] Add inaccessible member diagnostic 2019-03-18 13:48:08 -07:00
Brent Royal-Gordon
f2102fccf8 Fix name confusion in Decodable synthesis
Decodable’s init(from:) synthesis sometimes mistook a static property for an identically-named instance property, which could cause it to skip a property or possibly make other mistakes. This change factors a common helper function from encode(to:) and init(from:) synthesis which implements the right logic for both.
2019-03-12 16:32:21 -07:00
Karoy Lorentey
666a22feff [test] Modernize hashing throughout the test suite 2018-11-29 17:38:29 +00:00
Greg Titus
4f8e3f3597 Merge pull request #19879 from gregomni/8757
[Sema] Omit protocol match diagnosis for constructors with differing names.
2018-10-16 09:36:16 -07:00
Greg Titus
7d0bdb112e Omit protocol match diagnosis for constructors with differing names. 2018-10-14 17:08:50 -07:00
gregomni
b7df1ca1df Re-try a failingConstraint during salvage now that attemptFixes is turned on. This enables better missing conforms-to diagnoses. 2018-10-13 20:06:19 -07:00
Erik Eckstein
39bb14b094 change mangling prefix from $S to $s
This is the final ABI mangling prefix

rdar://problem/38471478
2018-09-19 13:55:11 -07:00
Joe Groff
77a0923ca6 SILGen: Emit convenience initializers as allocating entry points.
And only dispatch designated inits by their allocating entry points. rdar://problem/29634243
2018-09-13 12:31:23 -07:00
Jordan Rose
9ee996cf82 Eagerly create init(from:) when looking up 'init' on a Decodable type (#17712)
Otherwise, the initializer won't be inherited properly onto a
subclass, resulting in the base class being allocated instead of the
subclass when using Sub.init(from:).

https://bugs.swift.org/browse/SR-8083
2018-07-03 18:09:09 -07:00
Slava Pestov
3701f745c4 Migrate various Sema tests to Swift 4 2018-06-25 01:02:20 -07:00
Slava Pestov
5d2752f7d2 Run tests with -swift-version 4 by default
Some test now fail, so add an explicit -swift-version 3.
2018-06-19 23:24:19 -07:00
Itai Ferber
fc8f6824f7 Typo fix in Codable test comment 2018-05-21 07:54:17 -07:00
Huon Wilson
19d785018c [Sema] Use the conformance decl in more diagnostics/validity checks in conformance synthesis. 2018-05-09 09:55:41 +10:00
Huon Wilson
fe7240ab8a [Sema] Disallow synthesis-in-extensions in Swift 3.
'private' properties can't be accessed in extensions in Swift 3, so synthesizing
a conformance that reads from such things is going to be incorrect in an
extension.
2018-05-09 09:55:41 +10:00
Huon Wilson
27d8f17b44 [Sema] Fix synthesis of *codable conformances in extensions of generic types.
Fixes https://bugs.swift.org/browse/SR-6803 and rdar://problem/39199726.
2018-05-07 09:44:38 +10:00
Huon Wilson
3488a3a53b [Sema] Allow synthesis of protocol conformances in extensions.
This works for all protocols except for Decodable on non-final classes, because
the init requirement has to be 'required' and thus in the type's declaration.

Fixes most of https://bugs.swift.org/browse/SR-6803.
2018-05-07 09:41:25 +10:00
Michael Gottesman
3d990e98d2 [codable] When generated encode for a property, distinguish in between static/instance properties.
Previously, we just took the first match so:

1. We would try to emit a metatype lookup of the property but had prepared an instance lookup.
2. Could get the wrong type if the static/instance property had different types.

rdar://39669212
2018-04-24 15:57:47 -07: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
Slava Pestov
03bdbed0d9 Sema: Diagnose missing class initializers late
This gives us a chance to synthesize the init(from:).

Fixes <https://bugs.swift.org/browse/SR-7315>, <rdar://problem/39102390>.
2018-04-19 18:22:34 -07:00
swift-ci
335b0299c8 Merge pull request #15859 from itaiferber/codingkey-validation-invalid-typealias 2018-04-18 18:33:37 -07:00
Jordan Rose
28a1dc7770 Use 'fileprivate' for synthesized members of 'private' decls (#15980)
Since 'private' means "limit to the enclosing scope (and extensions
thereof)", putting it on a member means that the member can't be
accessed everywhere the type might show up. That's normally a good
thing, but it's not the desired effect for synthesized members used
for derived conformances, and when it comes to class initializers this
actually violates AST invariants.

rdar://problem/39478298
2018-04-17 15:19:35 -07:00
Itai Ferber
bd02909263 Fix crash in typechecking invalid CodingKeys alias 2018-04-10 15:44:45 -07:00
John McCall
7815892a76 Add unique typo corrections to the main diagnostic with a fix-it.
Continue to emit notes for the candidates, but use different text.
Note that we can emit a typo correction fix-it even if there are
multiple candidates with the same name.

Also, disable typo correction in the migrator, since the operation
is quite expensive, the notes are never presented to the user, and
the fix-its can interfere with the migrator's own edits.

Our general guidance is that fix-its should be added on the main
diagnostic only when the fix-it is highly likely to be correct.
The exact threshold is debateable.  Typo correction is certainly
capable of making mistakes, but most of its edits are right, and
when it's wrong it's usually obviously wrong.  On balance, I think
this is the right thing to do.  For what it's worth, it's also
what we do in Clang.
2018-04-07 16:01:39 -04:00
Hamish
92931b7434 Merge remote-tracking branch 'upstream/master' into warn-weak-insta-dealloc 2018-04-02 18:06:45 +01:00
Slava Pestov
a9b276cd89 Sema: Use -verify for Codable tests instead of FileCheck 2018-03-22 17:47:57 -07:00
Slava Pestov
df9e0a76e0 AST: Fix for name lookup not finding initializers in protocols
If a generic parameter has both a class and protocol constraint,
walking up to the class's superclass would cause us to subsequently
ignore any initializer requirements in the protocol.

Fixes <https://bugs.swift.org/browse/SR-1663>, <rdar://problem/22722738>.
2018-03-16 21:49:02 -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
gregomni
ebdbe6e9fb Code to derive Codable was assuming any decl of the desired key would be a
VarDecl, now does the right thing in the presence of identically named other
declarations (like nested types).
2018-02-11 19:16:38 -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
Slava Pestov
d8c5e798d5 Sema: Fix incorrect usage of NormalProtocolConformance in Codable synthesis
Noticed by inspection. It looks like there are no tests for generic
Codable types. I'm just adding one to cover the changed code path.
2017-11-04 23:40:24 -07:00
Itai Ferber
f90827e17b Codable: excluded vars shouldn’t need explicit val
Properties of Codable types which are optional and excluded via the CodingKeys enum should not require an explicit nil assignment, since in all other cases, optional vars get a default value of nil.
2017-09-11 10:29:07 -07:00
Ewa Matejska
21b2073b9e Small update to diagnostics to avoid the word yet 2017-09-07 10:34:34 -07:00
Alex Hoppen
ebd701c4b7 Represent the name of destructors by a special DeclBaseName
No longer use the known identifier `deinit` for destructors. This allows
classes to have functions called `deinit`.
2017-07-28 19:20:02 +02:00
Itai Ferber
071caf263a Additional responses to feedback
* Make Decodable synthesis lazy again
* Remove side effects from assertions
* Assert on superclass failable initializers (and verify via a unit test)
* Fix space typo
* Remove more needless instances of single-element arrays
* Unambiguously refer to init() in unit tests
* Add failable superclass init tests and a test to ensure Decodable fails to be inherited when superclass synthesis fails
* Add tests for structs to ensure they receive no-argument initializers where appropriate
2017-07-24 08:02:19 -07:00
Itai Ferber
a30e9117e6 Responses to feedback
* Use `isSpecificProtocol` instead of `==`
* Synthesize Encodable implementation lazily
* Don’t mark conformances as Used when they don’t need to be
* Avoid creating explicit one-element arrays
* Use lookupDirect instead of walking AST manually
* Produce diagnostic for Decodable classes whose non-Decodable superclass has a failable init
* Filter lazy vars manually since getStoredProperties() does not always do so
* Amend Decodable class diagnostic text
* Check for enum RawType errors only if the type was already validated
* Update unit tests to match changes
2017-07-20 08:06:21 -07:00
Itai Ferber
f873f5ec41 Add notes to suggest overriding init(from:)
Classes which inherit from Codable classes inherit their Codable implementation (as opposed to synthesizing their own). In cases where they add their own storage, they will fail to inherit their superclass’s init(from:). In this case, we provide a diagnostic suggesting that init(from:) be overridden.
2017-07-20 07:55:09 -07:00
Itai Ferber
eb031e8c86 Don’t prevent default/memberwise init
Some synthesized initializers (specifically, init(from:)) should not prevent the synthesis of a default or memberwise initializer.
2017-07-20 07:55:09 -07:00
Itai Ferber
1f8f1fea50 Allow overriding synthesized Decodable impl.
Classes which receive a synthesized Decodable implementation should be able to subclass non-Decodable classes (if the superclass has an accessible init()) method.

Also improve diagnostics when this is not the case.
2017-07-20 07:55:09 -07:00
Itai Ferber
e86428559a Expose synthesized members in AST lookups
Some types and members are synthesized by derived protocol conformances
(e.g. the CodingKeys member type or init(from:)/encode(to:) members
from Decodable/Encodable conformance) — however, they are not visible
in AST lookup if they have not been synthesized.

Exposes a LazyResolver callback for performing member synthesis where
relevant during qualified lookups to synthesize these members on demand
when needed.
2017-07-13 13:27:08 -07:00
Itai Ferber
1c020fcae7 Add unit test to confirm ordering of Codable diags 2017-06-20 14:29:59 -07:00
Itai Ferber
238a7a281e Allow synthesis of Codable for weak/unowned vars
The type of non-strong properties is wrapped in a ReferenceStorageType,
which does not conform to Codable. This needs to be unwrapped so the
inner reference type can be considered for Codable conformance.
2017-06-16 10:42:27 -07:00
Itai Ferber
91a8aaa0f4 Prevent synthesis for non-Codable stdlib types
Optional, Array, Set, and Dictionary currently all conform to Codable
regardless of the type they are generic on. Until conditional
conformance lands and we can rely on their conditional conformance, we
want to prevent Codable derived conformance if a type contains a
property of one of these types when we know it's actually going to fail
at runtime.
2017-05-22 09:29:36 -07:00
Itai Ferber
5eb58ef8d4 Improve experience of derivation of Codable types
* Introduce diagnostics that explain why derivation of
  Encodable/Decodable fail rather than just silently failing
* Allow properties with default values to be omitted from CodingKeys
  enum and from encoding/decoding
* If `CodingKeys` is a typealias, reach through it more consistently to
  get at the final target type
* Add unit tests to confirm this new behavior for classes and structs

NOTE: Although this made the diff bigger, standalone variables named
"type" have been renamed to "target" throughout, since the word "type"
may be ambiguous and makes debugging impossible. "target" is unique.
2017-05-19 09:45:54 -07:00
Itai Ferber
22185d237f CodingKey/*Codable tests don't need ObjC interop
Since all of the core types are now in the Swift stdlib, the tests no
longer rely on objc_interop or Foundation.
2017-04-25 14:11:24 -07:00
Itai Ferber
e2318392be Add CodingKey and *codable derived conformance
* Allow CodingKey conformance to be automatically derived for enums
  which have no raw type (with no associated values) and which have
  a raw type of String or Int.
* Allow Encodable and Decodable conformance to be automatically derived
  for classes and structs with Encodable/Decodable properties
* Add initial unit tests for verifying derived conformance
2017-04-25 13:13:35 -07:00