Commit Graph

202 Commits

Author SHA1 Message Date
Slava Pestov
8ecb83e29a Sema: Diagnose unsupported '@objc' on classes and members of extensions of classes with resilient ancestry
Unless -enable-resilient-objc-class-stubs is passed in, these cases
are not supported, so now we diagnose them instead of asserting or
failing to link.

Note the behavior change here; classes with resilient ancestry were
previously isObjC(). However this is wrong since isObjC() means
"statically visible to Objective-C via the generated header".

After this patch, isObjC() only returns true for a class with resilient
ancestry if -enable-resilient-objc-class-stubs is passed in.
2019-03-26 18:58:12 -04:00
Doug Gregor
b88a875608 [Type checker] Allow extensions of typealiases naming generic specializations.
When a (non-generic) typealias refers to a specialization of a generic
type, e.g.

```swift
  typealias simd_float3 = SIMD3<Float>
```

treat an extension of the typealias as an extension of the underlying
type with same-type constraints between the generic parameters and the
specific arguments, e.g.,

```swift
  extension simd_float3 { }
```

is treated as

```swift
  extension SIMD where Scalar == Float { }
```

This addresses a source-compatibility problem with SE-0229, where
existing types such as simd3_float (which were separate structs)
became specializations of a generic SIMD type.

Fixes rdar://problem/46604664 and rdar://problem/46604370.
2018-12-14 13:31:15 -08:00
Slava Pestov
0652d08d45 Sema: Source compatibility fix for extensions of type aliases
Referring to a generic type without arguments inside the definition
of the type itself or an extension thereof is a shorthand for
forwarding the arguments from context:

struct Generic<T> {}

extension Generic {
  func makeOne() -> Generic  // same as -> Generic<T>
}

However this didn't work if the type was replaced by a typealias:

struct OldGeneric<T> {}
typealias Generic<T> = OldGeneric<T>

extension Generic {
  func makeOne() -> OldGeneric  // OK
  func makeOne() -> Generic  // error
}

Add a hack for making this work so that we better cope with the
renaming of DictionaryLiteral to KeyValuePairs in Swift 5.0.

Fixes <rdar://problem/43955962>.
2018-12-12 23:59:49 -05:00
Slava Pestov
585733476b Sema: validateExtension() builds a generic signature even if the extension is invalid
Otherwise, we crash later in code that assumes the presence of a
generic parameter list implies the presence of a generic signature
(and vice versa).

Fixes <rdar://problem/46604393>.
2018-12-11 17:48:52 -05:00
Doug Gregor
58e855ec50 [Type checker] Make sure we wire up generic parameters of a context.
The attempt to short-circuit wiring up generic parameters of a context
didn’t work when one of the inner types didn’t itself have generic
parameters. Make sure we still wire up generic parameters in this case.

Fixes a crasher encountered while investigating rdar://problem/43406595.
2018-12-04 21:12:45 -08:00
Suyash Srijan
0d9f63474a [Sema] Emit a diagnostic when extending a protocol with a redundant requirement (#20503)
* Emit a warning diagnostic if an extension contains a redundant requirement

* Updates diagnostic message and checks if the extension type is a protocol

* Updates indentation and extracts self type

* [ast] Updates diagnostic message

* [ast] fix indentation

* [ast] Change ':' to 'to' in 'protocol_extension_redundant_requirement'

* [sema] Adds protocol extension redundant requirement check

Moved from TypeCheckRequests to TypeCheckGeneric

* [ast] fix some crashes related to null ptrs, check self type before emitting a diagnostic, update tests

* [ast] renames 'owner' to 'ext'

* [sema] fix style

* [test] Add another test case for redundant requirement

Co-Authored-By: theblixguy <suyashsrijan@outlook.com>

* [test] fix failing test

The test was failing because A has already been declared as a typealias.
2018-11-15 19:18:51 -08:00
Doug Gregor
49f2330be9 [Type checker] Make sure we wire up all generic parameters for extensions.
Fixes a type checker crash found while working on detangling to metadata.
2018-11-12 23:44:41 -08:00
Slava Pestov
6afb81d3eb Sema: Fix another crash with an invalid extension
Perhaps we should refactor everything so that extensions
that are not at the top-level are still bound and type
checked normally. However that requires a bit of work, so
keep playing wack-a-mole for now to handle these invalid
states when they come up.

However while we're at it, make the code a little better
by removing a bogus diagnostic path that was not used.

Fixes <rdar://problem/45290211>, <https://bugs.swift.org/browse/SR-9009>.
2018-10-28 23:11:15 -04:00
Greg Titus
d20fdf5f82 Merge pull request #19920 from gregomni/8757
[Sema][QoI] Call out missing conformances in protocol witness candidates.
2018-10-22 16:39:51 -07:00
gregomni
782f620bf9 Of course "tiny little change, I don't have to retest" remains a dumb thing to tell yourself. 2018-10-16 19:06:24 -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
Pavel Yaskevich
63b802ca88 [AST/Printing] Don't omit empty labels in special names
This makes diagnostics more verbose and accurate, because
it's possible to distinguish how many parameters there are
based on the message itself.

Also there are multiple diagnostic messages in a format of
`<descriptive-kind> <decl-name> ...` that get printed as
e.g. `subscript 'subscript'` if empty labels are omitted.
2018-09-24 18:36:53 -07:00
Jordan Rose
5dace224a0 Merge pull request #18623 from dingobye/sr8453
[Sema] Warn for redundant access-level modifiers on setters or used in an extension.
2018-09-05 17:44:26 -07:00
Pavel Yaskevich
16fc3117cc [ConstraintSystem] Diagnose ambiguities related to solutions with fixes
If all of the solutions in the set have a single fix, which points
to the common anchor, attempt to diagnose the failure as an
ambiguity with a list of candidates and their related problems as notes.

Having richer message like that helps to understand why something is
ambiguous e.g. if there are two overloads, one requires conformance
to some protocol and another has a same-type requirement on some type,
but neither matched exactly, having both candidates in the diagnostic
message with associated errors, instead of simplify pointing to related
declarations, helps tremendously.
2018-08-24 11:20:49 -07:00
Slava Pestov
d245831fa2 Merge pull request #18694 from slavapestov/in-place-class-metadata-init
In-place class metadata initialization
2018-08-20 19:17:45 -07:00
Slava Pestov
4069fa68db Sema: Lift restriction on @objc categories of concrete subclasses of generic classes
In-place initialization means the class has a symbol we can reference
from the category, so there's nothing to do on the IRGen side.

For JIT mode, we just need to realize the class metadata by calling an
accessor instead of directly referencing the symbol though.
2018-08-20 16:23:07 -07:00
Pavel Yaskevich
3cc613497c [ConstraintSystem] Add same-type requirement fix/diagnostic
Extend new requirement failure diagnostics by adding "same-type"
generic requirement failures.
2018-08-18 13:05:32 -07:00
Ding Ye
f34020bfd4 [Sema] Warn when redundant access-level modifier is added in an extension.
This patch adds warning for redundant access-level modifiers
used in an extension. It also refines the diagnostics of
access_control_ext_member_more issues, in case the fixit
could suggest redundant modifiers.

Resolves: SR-8453.
2018-08-10 14:33:16 +10:00
Pavel Yaskevich
ba085e5bdc [Diagnostics] Improve missing conformance diagnostics for sub-types and members
If generic parameter associated with missing conformance comes
from different context diagnose the problem as "referencing" a
specific declaration from affected type.
2018-08-07 18:55:43 -07:00
Pavel Yaskevich
ad171e05cc [Diagnostics] Improve missing conformance diagnostics by using affected declaration
Instead of simply pointing out which type had conformance failures,
let's use affected declaration instead, which makes diagnostics much
richer e.g.

```
'List<[S], S.Id>' requires that 'S.Id' conform to 'Hashable'
```

versus

```
initializer 'init(_🆔)' requires that 'E' conform to 'Hashable' [with 'E' = 'S.Id']
```

Since latter message uses information about declaration, it can also
point to it in the source. That makes is much easier to understand when
problem is related to overloaded (function) declarations.
2018-08-07 12:59:53 -07:00
Doug Gregor
199bf6eabf Merge pull request #18425 from DougGregor/extended-nominal
[Name lookup] Introduce a request for "extended nominal type decl"
2018-08-06 08:23:36 -07:00
Doug Gregor
3018becde7 [Type checker] Eliminate type checking from early extension binding.
Use ExtensionDecl::getExtendedNominal() to wire up extensions to their
nominal types early in type checking (the bindExtensions()) operation,
rather than going through type validation to do so.
2018-08-03 11:26:49 -07:00
Pavel Yaskevich
c2bf3d5ba9 [TypeChecker] NFC: Fix all of the diagnostics improved by conformance tracking 2018-08-02 21:55:16 -07:00
Doug Gregor
d07fa5ab69 Switch many callers of ClassDecl::getSuperclass() to ClassDecl::getSuperclassDecl().
ClassDecl::getSuperclass() produces a complete interface type describing the
superclass of a class, including any generic arguments (for a generic type).
Most callers only need the referenced ClassDecl, which is (now) cheaper
to compute: switch those callers over to ClassDecl::getSuperclassDecl().

Fixes an existing test for SR-5993.
2018-07-31 10:14:44 -07:00
Mark Lacey
78d83e5703 Use %target-typecheck-verify-swift where possible. 2018-07-26 23:13:43 -07:00
Slava Pestov
f4df20b92e Sema: Iterative worklist algorithm for extension binding
Note that you can still run into problems because of over-eager
declaration validation triggered by resolving nested types.
However, that's a separate problem.

Fixes <https://bugs.swift.org/browse/SR-631> and
<rdar://problem/20535574>.
2018-07-23 18:19:56 -07:00
Slava Pestov
3937732b9b Add more tests for protocols with superclass 2018-07-09 23:56:25 -07:00
Anthony Latsis
1146ddce8c Merge branch 'master' into remove-replacement-of-invalid-Self 2018-06-22 03:36:47 +03:00
fischertony
6f186c4b16 [Sema] Don't replace invalid Self req with the context type
Replacing invalid Self requirements with the context type only produces
an unnecessary and somewhat misleading error, stating that the replaced
type isn't a valid parameter or associated type. We don't want that when
the 'Self can't be used...' error is already being shown.
2018-06-22 03:33:22 +03:00
Matt Diephouse
87aaf10e6b [Diagnostics] Improve error when type parameters aren't equal 2018-06-15 15:08:05 -04:00
Robert Widmann
659e7ceb2d Merge pull request #16059 from AnthonyLatsis/unused-result-closure-warning
[DiagnosticsSema] SR-5983
2018-04-23 14:40:06 -04:00
fischertony
ba1bbd028d updated missed tests 2018-04-22 20:48:24 +03:00
Joe Groff
7cf6b75eb7 Sema: Relax restriction on @objc conformances for lightweight generics.
It's valid to extend an ObjC lightweight generic class to conform to an ObjC protocol. Fixes rdar://problem/39550290.
2018-04-19 20:32:19 -07:00
Huon Wilson
d33a5c16f2 [Sema] Explicit error for "extension GenericClass : ObjcProtocol".
If there was any requirements in the @objc protocol, the user got an error in
some form (a complaint about those requirements), but giving the direct error is
better, and handles @objc protocol with no requirements.

Also, fix a hole in that existing @objc method check: in `class Outer<T> { class
Inner {} }`, Inner should be considered generic, but the loop that was checking
for this didn't consider it.

Fixes https://bugs.swift.org/browse/SR-7370 and rdar://problem/39239512.
2018-04-13 08:09:02 +10: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
Slava Pestov
29f785f09b Sema: Don't emit duplicate diagnostics for members named 'Type' or 'Protocol'
IsFirstPass is going away soon, but in the meantime, the next
patch regresses matters slightly and causes us to emit
duplicate diagnostics in more cases, so let's fix it.
2018-04-02 23:17:59 -07:00
Pavel Yaskevich
5f222ec36d [Diagnostics] Improve diagnostics for type parameter requirements
Detect and diagnose failures in constraints representing
type parameter requirements generated by `openGeneric`.

Resolves: rdar://problem/35890334
2018-03-27 00:11:57 -07:00
Doug Gregor
3883ff0b29 [Type checker] Suppress near-miss warnings within the type definition.
There are a number of things that can only be written within the main
type definition (e.g., required initializers, stored properties),
making it far more likely that we'll get false positives from the
newly-introduced "near-miss" warnings for protocol
conformances. Moreover, sometimes a number of protocol conformances
are placed on the main type definition, increasing the potential for
false positives.

Suppress near-miss warnings for potential candidates when the protocol
conformance is stated on the main type definition itself. Therefore,
we only perform near-miss checking of non-@objc requirements when the
conformance itself was declared on an extension, which is roughly the
development style that near-miss warnings favor.

Fixes rdar://problem/37283860.
2018-02-13 11:46:43 -08:00
Doug Gregor
5c831a71ee Revert "[SE-0143] Put conditional conformances behind an "experimental" flag."
This reverts commit b59c30c1af.
2017-12-18 22:54:31 -08:00
Slava Pestov
336a97bc1a Sema: Diagnose invalid 'self.init' delegation where 'self' is an archetype
This is a corner case but would previously lead to a compiler crash
or miscompile.

Fixes <rdar://problem/21991470>, <https://bugs.swift.org/browse/SR-5022>.
2017-12-08 17:46:16 -08:00
Doug Gregor
b59c30c1af [SE-0143] Put conditional conformances behind an "experimental" flag.
Conditional conformances aren't quite ready yet for Swift 4.1, so
introduce the flag `-enable-experimental-conditional-conformances` to
enable conditional conformaces, and an error when one declares a
conditional conformance without specifying the flag.

Add this flag when building the standard library (which will vend
conditional conformances) and to all of the tests that need it.

Fixes rdar://problem/35728337.
2017-11-28 16:01:51 -08:00
Slava Pestov
3f51dbc3b1 Sema: Don't validate types when binding extensions 2017-11-15 21:55:55 -08:00
Doug Gregor
318a455314 [Type checker] Suppress near-miss warnings for unlabeled inits and subscripts.
Suggested by @brentdax, thanks!
2017-11-07 17:02:36 -08:00
Doug Gregor
493d4da667 Warn on “near-misses” when defaults are used for protocol witnesses.
When a particular nominal type or extension thereof declares conformance
to a protocol, check whether that type or extension contains any members
that *nearly* match a defaulted requirement (i.e., a requirement that 
is satisfied by something in a protocol extension), but didn’t match
for some reason and weren’t used to satisfy any other requirement of
that protocol. It’s intended to catch subtle mistakes where a default
gets picked instead of the intended member.

This is a generalization of the code we’ve had for @objc optional
requirements for a long time.

Fixes rdar://problem/24714887.
2017-10-26 17:04:46 -07:00
Huon Wilson
041b14175c [Sema] Remove restriction on conditional conformances. 2017-10-10 19:17:31 -07:00
Graydon Hoare
367a1500a3 [Diagnostics] s/may/must/ in 'may not be named' 2017-09-29 16:09:23 -04:00
Slava Pestov
edfaf7f724 Sema: Fix name lookup of nominal types nested inside a class from a protocol 2017-08-06 00:21:49 -07:00
Slava Pestov
fdd0259f7f Sema: Lift obsolete restriction on protocol extensions via typealias 2017-06-21 00:44:33 -07:00
Slava Pestov
151f38630b AST: Fix DeclContext::getAsTypeOrTypeExtensionContext() for extensions of typealiases 2017-06-21 00:44:32 -07:00
Slava Pestov
b5721e8d8e AST: Remove AnyObject protocol 2017-05-02 19:45:00 -07:00