Commit Graph

80 Commits

Author SHA1 Message Date
Xi Ge
509a99b71d FixCode: for missing type witnesses with default definitions, avoid inserting stubs. (#8068) 2017-03-14 11:03:14 -07:00
Xi Ge
555e52206a Sema: fill-protocol-stub fixits should include those missing witnesses that are handled by delayed diagnostics. (#8011) 2017-03-10 11:54:25 -08:00
Xi Ge
b5256f53b2 Sema: introduce a language option flag, DiagnosticsEditorMode. (#7982)
SourceKit always sets it positively. This may lead to more aggressive fixits however
less informative messages. We currently use the flag only for filling protocol stubs.
2017-03-08 12:46:02 -08:00
Xi Ge
5d2cd20051 Sema/FixCode: Introducing a wrapper for ConformanceChecker to fix missing stubs for multiple protocols in a single fixit invocation. (#7937) 2017-03-06 16:37:43 -08:00
Xi Ge
d6cc431a8d [FixCode] Enhance existing fill-protocol-stub fixit so that all missing witnesses can fixed at once. 2017-03-02 14:48:20 -08:00
Joe Groff
6d9a40f455 Sema: Treat the implicit RawRepresentable conformance for enums as Synthesized again.
This reverts part of #4038 which made the compiler consider it to be an `Explicit` conformance, breaking source code that was accepted in Swift 3.0 which declared a raw type as well as explicit conformance to `RawRepresentable` (reported as rdar://problem/30386658). While I'm here, a couple of spot fixes:

- Ensure an enum's raw value exprs are type-checked before checking conformances of any of its extensions, since the RawRepresentable conformance derivation will blow up if the raw value exprs haven't been checked. Fixes an order dependency issue if `extension Foo: RawRepresentable {}` gets checked before `enum Foo: Int { ... }`.
- Don't display the custom `enum_declares_rawrep_with_raw_type` diagnostic if the source location for the enum's inheritance clause is invalid, so that we don't emit a dislocated diagnostic.
2017-02-08 15:31:17 -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
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Slava Pestov
1df6e51c14 AST: Only print DynamicSelfType as 'Self' for class types
This fixes a regression from the previous patch which got
rid of PrintOptions::StripDynamicSelf.

When printing protocol declarations with a BaseType set in
PrintOptions, we can end up with a DynamicSelfType wrapping
a non-class type, if the protocol requirement returned
Self.

Note that this changes the diagnostic for missing protocol
requirements slightly; we used to sometimes refer to 'Self'
even if the conforming type is not a class, which is not
accepted by the type checker anyway. I believe the new
diagnostics are more correct.
2016-11-19 22:53:54 -08:00
Doug Gregor
77942d37d5 Tighten up @objc optional near-miss detection heuristics.
The @objc optional requirement near-miss heuristics were too
permissive, and could occasionally produce ridiculous results that
were nowhere close to a "near" miss. Make the diagnostics more
conservative, and fix an issue with an errant sentinel value.

Fixes rdar://problem/26380688.
2016-10-19 10:55:47 -07:00
Doug Gregor
da2a7cef46 Don't complain about near misses for declarations that are overrides.
An overriding declaration doesn't have a choice about its signature,
because the signature is dictated by the overridden
declaration. Therefore, don't produce a warning for it.

Fixes rdar://problem/28524237.
2016-10-18 11:25:22 -07:00
Doug Gregor
5267c6a269 If a member is a witness to any @objc requirement, don't call it a near miss.
When @objc inference was extended to look at the conformances of
superclasses, the code that diagnosed near misses was not similarly
updated, so we could end up producing a near-miss diagnostic on a
declaration that was already a witness to another protocol. Use the
same witness-finding logic that we use for @objc inference so this
doesn't happen again.

Fixes the rest of rdar://problem/27348369
2016-10-18 11:25:00 -07:00
Doug Gregor
50341da32b Use "TypeBase::hasError()" rather than "is<ErrorType>()" where needed.
In most places where we were checking "is<ErrorType>()", we now mean
"any error occurred". The few exceptions are in associated type
inference, code completion, and expression diagnostics, where we might
still work with partial errors.
2016-10-07 10:58:23 -07:00
Slava Pestov
a1eef126ba AST: Don't print "aka <<desugared type>>" for generic function types
This was causing us to emit diagnostics talking about τ_m_n, which is
not helpful.

Now that generic function types print sanely, print them in a few
places where we were previously printing PolymorphicFunctionTypes.
2016-09-15 21:47:57 -07:00
Slava Pestov
1487d590a9 Merge pull request #4617 from slavapestov/kill-canonical-mangling-signature
Kill canonical mangling signature
2016-09-06 13:44:25 -07:00
Slava Pestov
9771ec84dd Sema: Improve checking of associated type superclass constraints
When checking a conformance of a concrete type to a protocol, we
effectively checked the associated types twice -- once when
deriving them, and another time at the end, where we performed
a substitution of the protocol 'Self' type to the concrete type.

The latter checked superclass constraints, while the former did not.

However, this trick no longer works with minimized generic
signatures, because <P : Self> no longer has redundant requirements
for the associated types of 'P'.

Instead, check superclass constraints at the same time as checking
conformances.
2016-09-06 11:51:14 -07:00
Rintaro Ishizaki
f585a270f9 [Sema] Improve fix-it for missing initializer requirement
* If current context is extension of non-final class, don't suggest stub
* If current context is extension of final class, insert 'convenience'
* If current context is direct definition of non-final class, insert 'required'
2016-09-05 18:13:05 +09:00
Dmitri Gribenko
55864d10cb Tests: use 'mkdir -p' 2016-09-02 21:36:45 -07:00
Rintaro Ishizaki
3be5e89708 [Sema] Emit only public access modifier in fix-it for missing witness 2016-09-01 18:10:32 +09:00
SpringsUp
9432a84270 [Diagnostics QOI]: More descriptive errors for synthesized RawRepresentable enums 2016-08-23 01:03:03 +02:00
SpringsUp
f9af1257ef Tighter type-checking of enums with synthesized RawRepresentable
conformance
Fixes SR-2134
2016-08-22 22:49:48 +02:00
Argyrios Kyrtzidis
69918a966d [ASTPrinter] Fix printing of nested typealias types and make it consistent with printing of nominal types.
This fixes several issues:
- By default parent types of alias types are not printed which results in
	- Erroneous fixits, for example when casting to 'Notification.Name' from a string, which ends up adding erroneous cast
	  as "Name(rawValue: ...)"
	- Hard to understand types in code-completion results and diagnostics
- When printing with 'fully-qualified' option typealias types are printed erroneously like this "<PARENT>.Type.<TYPEALIAS>"

The change make typealias printing same as nominal types and addresses the above.
2016-08-11 12:15:15 -07:00
John McCall
afdda3d107 Implement SE-0117.
One minor revision: this lifts the proposed restriction against
overriding a non-open method with an open one.  On reflection,
that was inconsistent with the existing rule permitting non-public
methods to be overridden with public ones.  The restriction on
subclassing a non-open class with an open class remains, and is
in fact consistent with the existing access rule.
2016-08-02 07:46:38 -07:00
Doug Gregor
b9363fe6bd [SE-0111] Enable SE-0111 by default. 2016-07-29 17:28:24 -07:00
Alex Hoppen
c3b5b305b2 [Diag] Fixits for missing protocol requirements
To each note with a protocol requirement that is not met, a fixit is
added that inserts a stub for the requirement at the start of the
adoptee's declaration.
2016-07-20 11:47:08 +02:00
Doug Gregor
80f0852504 [SE-0091] Allow 'static' operators to be declared within types and extensions thereof.
Allow 'static' (or, in classes, final 'class') operators to be
declared within types and extensions thereof. Within protocols,
require operators to be marked 'static'. Use a warning with a Fix-It
to stage this in, so we don't break the world's code.

Protocol conformance checking already seems to work, so add some tests
for that. Update a pile of tests and the standard library to include
the required 'static' keywords.

There is an amusing name-mangling change here. Global operators were
getting marked as 'static' (for silly reasons), so their mangled names
had the 'Z' modifier for static methods, even though this doesn't make
sense. Now, operators within types and extensions need to be 'static'
as written.
2016-07-18 23:18:57 -07:00
Doug Gregor
9b24c8391b [QoI] Don't emit @objc Fix-Its for cases where @objc will be inferred from a requirement.
This is a follow-up to the change that allowed one to omit @objc (or
the name in an @objc) when it can be inferred by matching a
requirement. There is no point in suggesting that one add @objc if it
will be inferred anyway, since it's just syntactic noise.
2016-05-26 23:19:11 -07:00
Doug Gregor
c808b00314 Merge pull request #2423 from koke/sr-1395
SE-0070: Make Optional Requirements Objective-C-only.
2016-05-07 20:27:32 -07:00
Jorge Bernal
0a3f913b20 SE-0070: Make Optional Requirements Objective-C-only.
Optional protocol requirements now require an explicit @objc attribute.

Fixes SR-1395
2016-05-06 11:05:52 +02:00
Chris Lattner
2c81c8a114 add some parens to the testsuite, NFC. 2016-05-05 23:19:08 -07:00
Doug Gregor
df02ddd422 [Conformance checking] Diagnose the actual problem with near-misses.
Use the diagnostics machinery of the protocol conformance checker to
say why each near-miss actually missed, e.g., a type conflict. This
gives better information regarding how to fix the actual problem. Yet
more QoI for rdar://problem/25159872.
2016-04-22 14:50:03 -07:00
Doug Gregor
4d59d71e12 [Conformance checking] Apply omit-needless-words for near-miss checking.
The differences between Swift 2 and Swift 3 names can be very
significant, when the Swift 2 names have a lot of restated type
information. These differences end up disabling the near-miss
heuristics because the magnitude of the change is so high. Therefore,
apply the omit-needless-words heuristics to the potential witness and
the requirement before scoring them.

Should finish up rdar://problem/25159872 for real.
2016-04-22 14:50:03 -07:00
Doug Gregor
d817f3c508 [Conformance checking] Consider base name + first argument label together for near-miss checking.
The Grand API Renaming tends to split long base names into a shorter
base name + first argument label. To account for this in near-miss
checking, consider the base name + first argument label as a unit.
2016-04-22 14:50:03 -07:00
Doug Gregor
58570fdf9a Introduce "near-miss" warnings for unsatisfied optional requirements.
It's a common mistake to mistype a declaration that is intended to
satisfy an optional requirement. In such "near misses", we want to
warn about the mistake and give the user options to either fix the
declaration or suppress the warning. Approach this problem be walking
over all of the members of each nominal type declaration or extension
therefore and looking to see if there are any members remaining that

(1) are similarly-named to an unfilfilled optional requirement of a
protocol whose conformance is attributed to that nominal type
declaration or extension,
(2) are not witnesses to another optional requirement,
(3) haven't explicitly suppressed the warning (e.g., by adding
explicit "private" or explicit "@nonobjc"), and
(4) have a useful suppression mechanism.

In addition to the suppression mechanisms described in (3), one can
suppress this warning by moving the declaration to an(other)
extension. This encourages a programming style where one breaks an
interface into extensions each implement conformance to one
protocol. Note that we encode the various cases where one cannot move
the declaration to another extension (e.g., one cannot move a
designated initializer or stored property out of a class declaration)
and suppress the warning when there's no way for the user to cope with
it.

Each warning produced by this diagnostic can have a bunch of notes on
it for various courses of action. For example:

t2.swift:7:14: warning: instance method 'doSomething(z:)' nearly
matches optional requirement 'doSomething(x:)' of protocol 'P1'
  @objc func doSomething(z: Double) { }
             ^
t2.swift:7:14: note: rename to 'doSomething(x:)' to satisfy this
requirement
  @objc func doSomething(z: Double) { }
             ^
                         x
t2.swift:7:14: note: move 'doSomething(z:)' to an extension to silence
this warning
  @objc func doSomething(z: Double) { }
             ^
t2.swift:7:14: note: make 'doSomething(z:)' private to silence this
warning
  @objc func doSomething(z: Double) { }
             ^
        private
t2.swift:2:17: note: requirement 'doSomething(x:)' declared here
  optional func doSomething(x: Int)
                ^

It's a *lot* of detail, but is intended to cover the various choices
available to the user: Fix-It to the names of the requirement (for
naming-related mistakes) or suppress via various mechanisms. Combining
notes means losing Fix-Its, while dropping notes can lead users to
non-optimal solutions.

This is more of rdar://problem/25159872.
2016-04-19 17:21:49 -07:00
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
Dmitri Gribenko
65d840c0ae stdlib: lowercase cases in Optional and ImplicitlyUnwrappedOptional 2016-02-18 00:40:33 -08:00
Dmitri Gribenko
dd75aed67a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-17 14:40:05 -08:00
Doug Gregor
b7a415bade [Sema] An initializer in a protocol extension is effectively 'required'.
Fixes rdar://problem/24575507.
2016-02-16 11:37:17 -08:00
Max Moiseev
61c837209b Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-04 16:13:39 -08:00
Slava Pestov
5841cb07a1 Sema: Fix type safety hole with inherited conformances
When checking for permitted uses of Self in the input type of a
protocol requirement's function type, if the parameter itself was
a function we would recurse into its input, and reject all uses
of Self in the parameter type's result. This was the wrong way
around, and in fact we should recurse into the result.

Here is a test case that used to compile successfully and crash;
now it is rejected by the type checker:

    protocol P {
      func f(a: Self -> ())
    }

    protocol Q : P {
      func g()
    }

    class C : P {
      func f(a: C -> ()) { // should not be allowed to witness P.f
        a(C())
      }
    }

    class B : C, Q {
      var x: Int = 17

      func g() {
        print(x)
      }
    }

    func f<T : Q>(t: T) {
      // T == B here
      // t.f has type <T : Q> (T -> ()) -> ()
      t.f({ $0.g() }) // but at runtime, $0 is a C not a B
    }

    f(B())
2016-01-29 12:18:39 -08:00
Chris Willmore
983a674e0c Make use of curried function declaration syntax an error.
<rdar://problem/23111018>
2016-01-20 21:57:38 -08:00
Doug Gregor
7d70b704e4 Merge commit '5e11e3f7287427d386636a169c4065c0373931a8' into swift-3-api-guidelines 2016-01-19 23:18:20 -08:00
Doug Gregor
38c1de69e4 Reinstate "[SR-511][Parse] Add 'associatedtype' keyword and fixit"
This reverts commit ce7b2bcf09, tweaking
a few validation tests appropriately (1 crasher fixed, two -verify
tests that needed updating).
2016-01-14 00:21:48 -08:00
Erik Eckstein
ce7b2bcf09 Revert "[SR-511][Parse] Add 'associatedtype' keyword and fixit"
This reverts commit 2b2e9dc80e.

It broke some compiler crasher tests
2016-01-13 20:42:58 -08:00
gregomni
2b2e9dc80e [SR-511][Parse] Add 'associatedtype' keyword and fixit
Adds an associatedtype keyword to the parser tokens, and accepts either
typealias or associatedtype to create an AssociatedTypeDecl, warning
that the former is deprecated. The ASTPrinter now emits associatedtype
for AssociatedTypeDecls.

Separated AssociatedType from TypeAlias as two different kinds of
CodeCompletionDeclKinds. This part probably doesn’t turn out to be
absolutely necessary currently, but it is nice cleanup from formerly
specifically glomming the two together.

And then many, many changes to tests. The actual new tests for the fixits
is at the end of Generics/associated_types.swift.
2016-01-13 17:54:31 -08:00
Maxim Moiseev
0e0191380a OptionSetType => OptionSet 2015-12-10 14:58:24 -08:00
Chris Willmore
30af42fda9 Add warning that curried function decl syntax is going away.
<rdar://problem/23111018>
2015-11-02 15:45:11 -08:00
Doug Gregor
4b82904371 Don't allow silent failures in protocol conformances.
As of r31224, we tried to suppress redundant protocol-conformance
diagnostics for witnesses that look like they could have matched
something in an unconstrained protocol extension. However, this was
allowing ill-formed conformances to be accepted, causing a crash in
the AST verifier. Fixes rdar://problem/23033862.

Swift SVN r32671
2015-10-13 21:30:20 +00:00
Doug Gregor
6af6048c0c AST printing: take more care to only escape 'Self' when needed.
Cleans up AST printing somewhat as well as providing slightly better
type-to-declaration mappings for annotated AST printing and indexing.

Swift SVN r32420
2015-10-03 05:13:52 +00:00
Jordan Rose
f1b04beaa3 Don't complain about missing witnesses if they're available in extensions.
(Specifically, unconstrained extensions.)

This removes the problem where trying to conform to a protocol spits out
a dozen errors about missing requirements in a base protocol, but eleven
of them have a sensible default implementation with no constraints. This
is "P6" in the test cases; the others all passed before.

There's still plenty more work in this space. In particular, if an
associated type is missing we just give up rather than encouraging people
to provide the members it would be inferred from.

Swift SVN r31224
2015-08-13 20:49:26 +00:00