Commit Graph

101 Commits

Author SHA1 Message Date
Doug Gregor
cabdf84179 Suggest @objc for overrides of declarations from/in extensions.
The Swift class model does not support overriding declarations where either
the overridden declaration or the overriding declaration are in an extension.
However, the Objective-C class model does, so marking the declaration as
@objc (when possible) will work around the limitation.

Customize the "cannot override declaration in extension" diagnostic to
suggest adding @objc to the overridden declaration in cases where
@objc is permitted. Fixes SR-6512 / rdar://problem/35787914.
2017-12-13 14:54:32 -08:00
Jordan Rose
ca9d5a9df9 In Swift 3/4 mode, continue treating 'lazy override' as an override (#13335)
Follow-up for 7c707ce97c. Without this, the declaration would be
accepted, but any uses of the overridden property would be treated as
ambiguous because the property wouldn't really be marked as an
override.

rdar://problem/35900345
2017-12-11 15:13:55 -08:00
Slava Pestov
8a31bc7609 Add regression test for https://bugs.swift.org/browse/SR-6509 2017-12-08 12:54:03 -08:00
Jordan Rose
7c707ce97c For 'lazy', make "cannot override with a stored property" a warning (#13304)
Previous versions of Swift accidentally treated lazy properties as
computed properties because of how they were implemented. Now that we
check this correctly, we've broken source compatibility. Downgrade the
error to a warning in this case.

(Arguably we could /allow/ overriding with a stored property. The
original concerns were that you could accidentally end up with extra
storage you didn't need, and that observing accessors would behave
differently based on whether or not the property was overriding. But
there's at least no ambiguity for 'lazy', which can't have observing
accessors today.)

rdar://problem/35870371
2017-12-06 13:41:07 -08:00
Pavel Yaskevich
05d524d6e0 [CSApply] Fix source breakage related to deep-equality types and SE-0110
Fix problem related to Swift 3 mode (with assertions),
since Swift 3 mode allows passing arguments with extra parens
to parameters which don't expect them, it should be supported
by "deep equality" types e.g. Optional<T>:

```swift
func foo(_: (() -> Void)?) {}
func bar() -> ((()) -> Void)? { return nil }
foo(bar) // This expression should compile in Swift 3 mode
```

Resolves: rdar://problem/35198459
2017-11-16 21:56:24 -08:00
Jordan Rose
9888697544 Correctly check access control for a class's generic superclass (#12629)
The previous code was too clever in trying to avoid work and missed
the fact that ClassDecl::getSuperclass produces an interface type but
the types in the inheritance clause are contextual types.

This actually successfully built:

- in non-WMO builds with a public subclass and an internal base class,
  because the internal class symbol wouldn't get stripped out.

- in WMO builds with an internal subclass and a private base class,
  because 'private' has no distinction at the linkage level for a WMO
  build.

However, it's highly likely that trying to import a library containing
such types would result in instability (read: compiler and debugger
crashes), and it's clearly a mistake to allow this. (If you can't show
your superclass to a user in a library's generated interface,
something's definitely gone wrong.)

https://bugs.swift.org/browse/SR-6206
2017-11-09 13:29:24 -08:00
Slava Pestov
e805100f96 Sema: Fix source compatibility break with default initialization of optional properties
In Swift 4, properties declared with a sugared Optional type,
like Int?, have a default value of nil. This can be observed
in two ways:

- Classes and structs get an implicit no-argument initializer
- Designated initializers don't have to initialize this property

Note that this did not apply in general to properties where
the type was spelled explicitly as Optional<Int>, etc, mostly
because of implementation restrictions -- when we check if a
type has implicit initializers, we have not realized types for
all stored property members yet, and doing so is not really
possible without the iterative decl checker.

However, in some cases, we *did* perform default initialization
for Optional<Int>, because of some code duplication and
divergent code paths.

A recent refactoring cleaned up some of the mess in this area,
but accidentally broke source compatibility with code that
relied on the broken Optional<Int> case.

Fix this by simulating the old behavior in -swift-version 4,
and preserving the more correct behavior in -swift-version 5.

Fixes <rdar://problem/35319847>.
2017-11-04 23:40:24 -07:00
Doug Gregor
c94d1cd31a [Type checker] The term "layout constraint" is confusing; don't use it.
Within the compiler, we use the term "layout constraint" for any
constraint that affects the layout of a type parameter that has that
constraint. However, the only user-visible constraint is "AnyObject",
and calling that a layout constraint is confusing. Drop the term
"layout" from diagnostics.

Fixes rdar://problem/35295372.
2017-11-02 12:03:09 -07:00
Slava Pestov
93c80da77c Parse: __shared and __owned should be contextual keywords
This was a source compatibility regression, someone actually had
an identifier named __shared.
2017-10-24 20:45:50 -07:00
gregomni
7073eb15a4 Disallow override of dynamic Self return type with non-dynamic in a subclass
unless that subclass is final.
2017-10-18 13:07:04 -07:00
Greg Spiers
c5d6e17d85 Do not allow ‘weak’ and ‘unowned’ keywords for property declarations in protocols. 2017-10-01 12:43:48 +01:00
Doug Gregor
6d9e921983 Restrict the ability to specify "protocol P : class, AnyObject" to Swift >= 5. 2017-09-18 13:28:31 -07:00
Doug Gregor
d0aa22c2c7 Allow "protocol P : class, AnyObject..." for Swift 4 compatibility.
Because of the way we modeled the 'class' constraint in Swift <= 4, we
allowed both 'class' and 'AnyObject' to be specified on a protocol,
even in Swift 4 when they became equivalent. Recent refactoring
started rejecting such code; allow it now.

Fixes rdar://problem/34496151.
2017-09-18 13:28:31 -07:00
Slava Pestov
a102341c9c Merge pull request #11640 from gibachan/fixit-nested-static-value
[Qol]Fixit nested static value
2017-09-14 22:56:19 -07:00
John McCall
d76263f919 Propagate accessor types directly from the storage declaration.
This eliminates the need for an ugly and incomplete hack to suppress
noescape inference for setter arguments.  It also means we stop emitting
redundant diagnostics for problems in storage types.
2017-09-10 04:56:02 -04:00
gibachan
559ad7161e Add test for compatibility 2017-09-07 19:18:36 +09:00
Slava Pestov
4cb7edde3a Add a test for the old Swift 4 behavior of 'self' in convenience inits 2017-08-26 01:21:42 -07:00
Doug Gregor
a6516a90c6 [Type checker] Bump warning about unavailable witnesses to an error in Swift 4
Swift 3 allowed a requirement to be satisfied by an unavailable
witness, which doesn't make sense. We've been warning about it in
Swift 3 for a while; make it an error in Swift 4.
2017-07-10 11:40:35 -07:00
Pavel Yaskevich
1ffac2e651 [QoI] Improve diagnostics related to member calls
When trying to diagnose problems related to calls where
function is represented by a member of nominal type let's
attempt to be more cautious while type-checking function
expression without it's arguments, because it could produce
unrelated diagnostics.

Resolves: rdar://problem/32551313, rdar://problem/28456467, rdar://problem/31671195
2017-06-27 15:49:11 -07:00
Pavel Yaskevich
3a77295835 [QoI] Use information known about call parameters via failed constraint
This makes it a lot easier to diagnose contextual mismatch related
to arguments used by the call without relying on the type of the
function expression which is not always available.
2017-06-21 11:27:12 -07:00
Doug Gregor
8b80681394 Escalate the warning about the "== Self" type soundness hole to an error.
Swift 3 had a type soundness hole in protocol conformance checking
where the requirement contained an "== Self" constraint and the
witness was a member of a non-final class. We previously closed the
type soundness hole in PR #9830, but left it as a warning in Swift 3
compatibility mode.

Escalate that warning to an error. The optimizers break due to this
type soundness hole, and of course it can lead to other runtime
breakage because it violates the type system.

Fixes rdar://problem/28601761.
2017-06-16 13:23:25 -07:00
Slava Pestov
d91d811df7 AST: Restore old behavior for name lookup inside 'lazy' in Swift 3 mode
Restore the old Swift 3 behavior for source compatibility reasons:

- Unqualified lookup finds static properties (but not static methods)
  first, then global members.

- Qualified lookup into 'self' is still supported.

There's no change in Swift 4 mode, where the newer more correct
behavior is enabled.

Fixes <rdar://problem/32570766>.
2017-06-05 23:14:33 -07:00
Robert Widmann
71bf312a25 Migrate the rest of the tests to %empty-directory 2017-06-04 11:08:39 -07:00
Pavel Yaskevich
5523ca7b9f [ConstraintSystem] Strip labels from enum elements when passed as values
Enum elements have to be treated the same way as regular functions
when passed as values, which means labels have to be stripped from
their argument types.

Resolves: rdar://problem/32300339.
2017-05-26 15:32:48 -07:00
Pavel Yaskevich
817798151a [QoI] Add a fix-it with missing parameters for closures
If there are parameters missing in the closure declaration and
all of the present ones are anonymous let's emit a fix it suggesting
missing parameters.

Resolves: rdar://problem/32301091
2017-05-22 15:10:41 -07:00
Doug Gregor
f0abcac42c Diagnose unsound uses of same-type-to-Self requirements with non-final classes.
Protocol requirements involving same-type-to-Self constraints cannot
be witnessed by declarations in non-final classes that have the same
form of same-type requirement to the corresponding class type, because
it creates a soundness hole with subclasses:

    protocol Q {
      func foo<T: P>(_: T, _: T.T) where T.T == Self
    }

    class C: Q {
      func foo<T: P>(_: T, _: C) where T.T == C {}
    }

    class D: C {
      // in D, T.T == D does not hold
    }

Warn about this in Swift 3 compatibility mode, error on it in Swift 4
mode. When possible, provide a note + Fix-It suggesting that the
same-type constraint might be weakened to a superclass constraint
(which works with subclassing).

Fixes rdar://problem/30398503.
2017-05-22 09:31:27 -07:00
Slava Pestov
ec12e974ee Sema: More consistent enforcement of '.self' on metatype values in Swift 4 mode
In Swift 3, we required that '.self' be specified on TypeExprs
and DeclRefExprs that reference types.

However, types referenced as member lookups, such as 'Foo.Bar',
did not get this treatment, and '.self' was not required.

Fix this by emitting warnings in the cases that Swift 3 did not
diagnose, and producing errors in Swift 4 mode where we want
strict enforcement.
2017-05-21 18:14:55 -07:00
ematejska
6d8511218f Merge pull request #9636 from ematejska/update_where_clause_warning_to_error
Update where clause warning to error for Swift 4
2017-05-15 21:55:55 -07:00
Pavel Yaskevich
9a07f28ac4 Merge pull request #9587 from xedin/rdar-31973368
[QoI] Extend single tuple parameter diagnostics to function/subscript calls
2017-05-15 21:29:42 -07:00
Rintaro Ishizaki
04f31a76c0 Merge pull request #7955 from rintaro/parse-ifconfig-validate
[Parse] Separate compilation condition validation and evaluation
2017-05-16 12:06:31 +09:00
Pavel Yaskevich
ead1447aa1 [QoI] Extend single tuple parameter diagnostics to function/subscript calls
Properly diagnose cases of function/subscript argument tuple
structuring/destructuring related by not limited to SE-0110.

Resolves: rdar://problem/31973368
2017-05-15 18:41:41 -07:00
Ewa Matejska
24a9bea5d6 Adding compatiblity test, updating warning. 2017-05-15 16:26:32 -07:00
swift-ci
44a86cb374 Merge pull request #9507 from DougGregor/redundant-conformance-warnings-imported 2017-05-11 12:25:06 -07:00
Doug Gregor
a43fbedd1c [Type checker] Extend redundant-conformance warning to imported types.
Fix a silly gap in my fix for rdar://problem/31104415, where imported
types that gain conformances through their overlays were still getting
errors. Fixes SR-4820 / rdar://problem/32134724.
2017-05-11 10:53:51 -07:00
Jordan Rose
e0c248c932 Turn on deserialization recovery by default. (#9486)
I'm still leaving a -disable-deserialization-recovery flag in there
just in case.
2017-05-11 08:47:59 -07:00
swift-ci
e305259ef0 Merge pull request #9384 from xedin/rdar-31724211 2017-05-11 00:37:15 -07:00
Joe Groff
e3e0f440a1 Serialization: Recovery for protocol conformances with changed witness or requirement signatures.
Deserializing a witness record in a conformance may fail if either of the requirement or witness changed name or type, most likely due to SDK modernization changes across Swift versions. When this happens, leave an opaque placeholder in the conformance to indicate that the witness exists but we don't get to see it. For expedience, right now this just witnesses the requirement to itself, so that code in the type checker or elsewhere that tries to ad-hoc devirtualize references to the requirement just gets the requirement back. Arguably, we shouldn't include the witness at all in imported conformances, since they should be an implementation detail, but that's a bigger, riskier change. This patch as is should be enough to address rdar://problem/31185053.
2017-05-09 09:15:04 -07:00
Pavel Yaskevich
c72b402afa [QoI] Improve diagnostics for generic subscripts
While diagnosing index expression associated with subscript call
`validateContextualType` didn't look through TupleType to identify
potential nullability of the contextual type related to generic
parameters.

Resolves: rdar://problem/31724211
2017-05-08 02:41:02 -07:00
Philippe Hausler
0540022473 Merge pull request #7433 from phausler/nsrange_improvements
[Foundation] add additional conformances and functionality to NSRange
2017-05-05 16:31:31 -07:00
Joe Groff
cf6cc484c0 Switch on key paths.
Enabling the global subscript degrades some diagnostics (rdar://problem/31724211).
2017-05-05 08:32:41 -07:00
Doug Gregor
ef7b8039ec [Type checker] Warn about classes conforming to AnyObject in Swift 3 mode.
Swift 3 allowed a class to explicitly conform to AnyObject, although
it was meaningless. Recent AnyObject-related changes started rejecting
such conformances as ill-formed; allow them with a warning + Fix-It in
Swift 3 compatibility mode.
2017-05-04 10:28:27 -07:00
ematejska
75a1dcaf28 Merge pull request #9098 from KingOfBrian/feature/SE-0169
SE-0169: Add support for shared private
2017-05-02 16:12:17 -07:00
Philippe Hausler
0fd0be88e8 [Foundation] add additional conformances and functionality to NSRange 2017-05-02 10:37:04 -07:00
Doug Gregor
5bd609c98f Merge pull request #9156 from DougGregor/redundant-conformance-warnings
[Type checker] Downgrade some "redundant conformance" errors to warnings.
2017-05-01 15:00:14 -07:00
Doug Gregor
f7bccb0e68 [Type checker] Downgrade some "redundant conformance" errors to warnings.
When an extension introduces a conformance that already exists, the
type checker will reject the conformance and then ignore it. In cases
where the original conformance is in the same module as the type or
protocol (but the new, redundant conformance is in some other module),
downgrade this error to a warning. This helps with library-evolution
cases where a library omitted a particular conformance for one of its
own types/protocols in a previous version, then introduces it in a new
version.

The specific driver for this is the conformance of String to
Collection, which affects source compatibility. Fixes
rdar://problem/31104415.
2017-05-01 13:22:15 -07:00
Brian King
5447443c5b Add support for sharing the private scope between type declaration and extensions in Swift 4 2017-04-28 10:02:49 -04:00
Jordan Rose
27b4070f5f When ranking overloads, always match up final closure parameters.
(in Swift 4 mode)

There are a lot of other things that overload ranking does *not* take
into account, and I intend to file a more general bug about that, but
this should resolve some of the most egregious ambiguities with Swift
3's import rules (SE-0005, particularly "omit needless words" and
adding default arguments).

Finishes rdar://problem/25607552.
2017-04-26 19:33:07 -07:00
Jordan Rose
4b87bd93f9 Tweak DiagnosticEngine's 'aka' logic to only kick in for typealiases. (#9010)
Previously we had more ad hoc logic that tried to decide if it was
worth desugaring a type based on its structure. Now we instead look
for a typealias that might actually benefit from desugaring, and if
we don't find one we won't show the 'aka' note.
2017-04-25 19:37:22 -07:00
Slava Pestov
f3236ed019 Merge pull request #8890 from KingOfBrian/bugfix/SR-1762-error-in-swift-4
Emit error in Swift 4 mode for final protocol extensions
2017-04-21 02:19:55 -07:00
Brian King
37266d85e1 Verify behavior of Swift 3 and Swift 4 final protocol extension behavior 2017-04-20 16:06:02 -04:00