Commit Graph

199 Commits

Author SHA1 Message Date
Suyash Srijan
f724d1ff85 [SE-0280] Enum cases as protocol witnesses (#28916)
* [Typechecker] Allow enum cases without payload to witness a static get-only property with Self type protocol requirement

* [SIL] Add support for payload cases as well

* [SILGen] Clean up comment

* [Typechecker] Re-enable some previously disabled witness matching code

Also properly handle the matching in some cases

* [Test] Update typechecker tests with payload enum test cases

* [Test] Update SILGen test

* [SIL] Add two FIXME's to address soon

* [SIL] Emit the enum case constructor unconditionally when an enum case is used as a witness

Also, tweak SILDeclRef::getLinkage to update the 'limit' to 'OnDemand' if we have an enum declaration

* [SILGen] Properly handle a enum witness in addMethodImplementation

Also remove a FIXME and code added to workaround the original bug

* [TBDGen] Handle enum case witness

* [Typechecker] Fix conflicts

* [Test] Fix tests

* [AST] Fix indentation in diagnostics def file
2020-03-28 10:44:01 +00:00
Suyash Srijan
543d649278 [Diagnostics] Warn when the result of a Void-returning function is ignored (by assigning into '_') (#29576) 2020-02-04 20:19:37 +00:00
Slava Pestov
55e3797471 Revert "Sema: Look for generic parameters first when inferring an associated type"
This breaks source compatibility a little bit more than we'd like, so
reverting it for now.

Fixes <rdar://problem/57213598>.

This reverts commit 04fbcc0149.
2019-11-15 14:32:13 -05:00
Slava Pestov
04fbcc0149 Sema: Look for generic parameters first when inferring an associated type
Previously we did this as a last resort if inference fails. The new
behavior is technically source-breaking, but I suspect nobody
relied on the old behavior.

This can help avoid cycles by eliminating some unnecessary validation work.

Fixes <https://bugs.swift.org/browse/SR-11407>, <rdar://problem/54979757>.
2019-11-13 23:42:08 -05:00
Slava Pestov
686ddd7cf7 Sema: Tighten up getReferencedAssociatedTypes()
If a protocol requirement has a type that's a nested member
type of another member type, eg,

protocol P {
  associatedtype A : Q
  func f(_: A.B)
}

Then we don't actually want to use 'f()' to infer the witness
for 'A'. By avoiding doing so, we eliminate some cycles which
can allow some programs to type check that didn't before.
2019-11-13 23:42:08 -05:00
Robert Widmann
c75af38a7a Drop ConformanceContexts out of the TypeChecker 2019-11-06 11:41:03 -08:00
Luciano Almeida
1184492d25 [Diagnostics] SR-11419 Diagnose protocol stub note in editor mode only (#28101)
* [TypeChecker] Enclosing stubs protocol note within editor mode

* [test] Removing note from test where there is no -diagnostics-editor-mode flag

* Formatting modified code

* [tests] Fixing tests under validation-tests
2019-11-06 07:42:48 -08:00
Pavel Yaskevich
58329e0c27 Revert "[Diagnostics][Qol] SR-11295 Emit diagnostics for same type coercion. " 2019-10-25 01:05:07 -07:00
Luciano Almeida
86ca3454d6 Fixing warning UnnecessaryCoercion tests 2019-10-21 23:11:21 -03:00
Slava Pestov
3dbdc09a4e Sema: Move ParamDecl interface type computation to validateDecl()
This eliminates typeCheckParameterList() altogether.
2019-10-10 15:28:14 -04:00
Robert Widmann
4f6951c42f Change a cycle condition
Not having the generic signature is the real culprit here.
2019-09-18 11:24:03 -07:00
Pavel Yaskevich
a1643d94f7 [Diagnostics] NFC: Update all of the improved test-cases 2019-09-13 22:35:52 -07:00
Suyash Srijan
5077b0036c [Test] Update test and add trailing space to 'static' fix-its 2019-09-04 19:27:30 +01:00
Suyash Srijan
535364c030 [Typechecker] We could also have a subscript when diagnosing SettableConflict, so guard against that 2019-09-03 21:46:31 +01:00
Suyash Srijan
37ffd6ba1b [Test] Add a test case for static subscript 2019-09-03 20:15:27 +01:00
Suyash Srijan
eb045f5b0b [Test] Add a test case for computed property 2019-09-03 20:03:11 +01:00
Suyash Srijan
8c3cb7111a [Typechecker] Remove some questionable fix-its when there is a witness mismatch 2019-09-03 19:54:09 +01:00
Suyash Srijan
7f959fd7e8 [Typechecker] Emit fix-its for witness mismatches 2019-09-02 22:05:01 +01:00
Pavel Yaskevich
253abad789 [Diagnostics] Port remaining contextual failures (expect associated with nil)
Remove the rest of the obsolete code from `diagnoseContextualConversionError`
and port diagnostic about string indexing to new diagnostic framework.
2019-08-13 14:33:38 -07:00
Xi Ge
1535bea268 FixCode: issue a separate note for protocol-stub fixit when the fixit location is in another file
Under non-editor mode, the fixit for inserting protocol stubs is associated with a note
pointing to the missing protocol member declaration which could stay in a separate file from
the conforming type, leading to the behavior of rdar://51534405. This change checks if
the fixit is in a separate file and issues another note to carry the fixit if so.

rdar://51534405
2019-07-10 12:30:54 -07:00
Slava Pestov
8f22da205a Sema: Stop wrapping Self returns in protocols with DynamicSelfType 2019-06-26 01:10:11 -04:00
fischertony
00e6ae1bf3 consolidate func & subscript signature validation 2019-04-20 00:15:12 +03:00
Brent Royal-Gordon
c37fee1719 Add parallel tests for static subscripts
This commit modifies various subscript-related test files to add static subscript equivalents of existing tests.
2019-04-10 23:17:04 -07:00
Slava Pestov
7566f98a45 Sema: Diagnose enum inheritance clause containing subclass existential
Also, tidy up the code a bit and stop emitting redundant diagnostics for
associated types.

Fixes <https://bugs.swift.org/browse/SR-10232>.
2019-04-01 22:41:16 -04:00
Pavel Yaskevich
aeaa26d926 [CSDiagnostics] Add missing arguments failure
Currently only supports closures, but could be easily expanded
to other types of situations e.g. function/member calls.
2019-02-26 13:29:35 -08:00
Doug Gregor
e3729f444a Revert "[GSB] When adding same-type requirements pick representative based on canonical order"
This reverts commit c725660cc9. It
uncovered a use-after-free in the GenericSignatureBuilder. Apologies
for the lack of a test case here; I'm still looking for something
small enough to commit.

Fixes rdar://problem/46772328.
2018-12-21 09:44:48 -08:00
Pavel Yaskevich
c725660cc9 [GSB] When adding same-type requirements pick representative based on canonical order
Instead of trying to order based on the "nested depth", let's
always prefer canonical ordering of type parameters when it comes
to picking representative equivalence class.

Resolves: rdar://problem/45957015
2018-11-23 00:08:14 -08: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
b91e455064 Worked out my confusion with existentials, and pulled out constraint fix checking into a separate function and try it with multiple fixes, if they exist. 2018-10-20 09:42:55 -07:00
gregomni
939de4fb4a Extend candidate missing conformance checking to other types of requirements so that we check superclass and same type requirements in the same way. 2018-10-19 10:02:30 -07:00
gregomni
002c04ef96 Modified diagnostics a bit given feedback 2018-10-17 07:35:44 -07:00
gregomni
f2a80c4a9d Let CS::solveSingle() optionally allow fixes in the solution. Use this in protocol inference to give better candidate failure notes. 2018-10-16 15:27:09 -07: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
Slava Pestov
4ef1904e88 AST: Don't allow declarations to shadow generic parameters
Associated type inference can synthesize type aliases with the same name
as a generic parameter. This is all fine since the underlying type of
the alias is the generic parameter type, however it might have been
synthesized in a constrained extension, resulting in bogus diagnostics
that depend on the order in which declarations are type checked, which
can vary between WMO and non-WMO, different batch mode settings, etc.

Instead, let's just check the generic parameter list first.

Fixes <rdar://problem/22587551>, <rdar://problem/44777661>.
2018-10-15 19:24:27 -07:00
gregomni
2e23ee5257 Exclude non-matching name diagonses for all candidates. 2018-10-15 07:48:24 -07:00
Slava Pestov
6f97923aac Sema: Tweak TypeResolution::areSameType() for when both types are type parameters
We can use a cheaper check; instead of transforming both types and
replacing any type parameters with their anchors, instead just
check if both map to the same equivalence class.

This also fixes a bug, even though it shouldn't, but I'll take it.

Fixes <rdar://problem/35756206>.
2018-10-15 01:52:11 -07:00
Slava Pestov
8acc11f80c Sema: Fix ambiguity resolution when doing unqualified lookup of associated types and type aliases
In structural lookup mode, let's resolve protocol typealiases to
dependent member types also. This is because areSameType() has
no way to see if a type alias is going to be equal to another
associated type with the same name, and so it would return false,
which produced ambiguity errors when there should not be any.

This exposes a deficiency in how we diagnose same-type constraints
where both sides are concrete. Instead of performing the check on
the requirement types, which might be dependent member types that
later on resolve to concrete types, do the check on the actual
equivalence classes further down in the GSB instead.

However, this in turn produces bogus diagnostics in some recursive
cases where we add same-type constraints twice for some reason,
resulting in a warning the second time around. Refine the check by
adding a new predicate to FloatingRequirementSource for requirements
that are explicitly written in source... which is not what
isExplicit() currently means.
2018-10-15 01:52:11 -07:00
Slava Pestov
9d131fd5f1 Merge pull request #18999 from hamishknight/location-by-association
[GSB] Avoid emitting associated type diagnostics on the protocol decl
2018-10-13 15:16:40 -07:00
Slava Pestov
64f12ff936 Sema: Allow protocols with 'Self' constraints again
These two declarations are now equivalent:

  protocol P : SomeClass { ... }
  protocol P where Self : SomeClass { ... }

There's a long, complicated story here:

- Swift 4.2 rejected classes in the inheritance clause of a
  protocol, but it accepted the 'where' clause form, even
  though it didn't always work and would sometimes crash

- Recently we got the inheritance clause form working, and
  added a diagnostic to ban the 'where' clause form, because
  we thought it would simplify name lookup to not have to
  consider the 'where' clause

- However, we already had to support looking at the 'where'
  clause from name lookup anyway, because you could write

  extension P where Self : SomeClass { ... }

- It turns out that despite the crashes, protocols with
  'Self' constraints were already common enough that it was
  worth supporting the existing behavior, instead of banning
  it

Fixes <rdar://problem/43028442>.
2018-10-12 03:06:52 -07:00
Slava Pestov
2d4b25960d Sema: Type variables for opened generic parameters store the generic parameter type and not an archetype
There's no need to instantiate archetypes in the generic environment
of the declaration being opened.

A couple of diagnostics changed. They were already misleading, and the
new diagnostics, while different, are not any more misleading than
before.
2018-09-27 20:49:23 -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
Graydon Hoare
046c5f6808 [Sema] Skip non-ProtocolRequirement requirements when inferring type witnesses. 2018-09-21 14:34:26 -07:00
Matt Diephouse
5e9da14b5a Pare down operator candidates during protocol type checking 2018-08-28 08:41:15 -04:00
Hamish Knight
819a13e880 [GSB] Avoid emitting associated type diagnostics on the protocol decl
For explicit abstract protocol floating requirement sources, get the source location from the protocol requirement rather than delegating to the parent `RequirementSource`.
2018-08-27 18:39:04 +01:00
Doug Gregor
e67d78d919 Update test case for better recursion detection. 2018-08-20 23:59:30 -07:00
Doug Gregor
8d11ce1028 Minor test case update due to cycle detection. 2018-08-14 02:19:21 -07:00
Slava Pestov
3454a54ee3 Sema: Move checkProtocolSelfRequirements() and checkReferencedGenericParams() to typeCheckDecl()
Checking these in a code path called from validateDecl() is overkill;
it's sufficient to perform these checks in declarations in primary files
only.
2018-07-31 02:08:37 -07:00
Slava Pestov
eeb5c953aa Sema: Ban protocol where clauses that place constraints on 'Self'
These will never work properly because of phase ordering issues with
the current declaration checker design. Since we can always express
the same thing with the protocol inheritance clause instead, just
diagnose this as an error instead of trying to hack around it.

Fixes <rdar://problem/38077232>, <https://bugs.swift.org/browse/SR-5581>.
2018-07-10 00:34:02 -07:00
Slava Pestov
19a37c8a83 Sema: Fix source compatibility break from relaxed witness matching rules
This is fix for a source compat regression from:

commit 790625ab5b
Author: Doug Gregor <dgregor@apple.com>
Date:   Mon Mar 19 15:29:32 2018 -0700

    Allow a witness's noescape parameter to match a requirement's escaping parameter

The regression is not severe but its easy enough to fix.

With the above change, it was possible for an optional requirement that did
not have a witness in Swift 4.1 to pick up a witness in Swift 4.2, because
the escaping/noescape mismatch prevented it from being considered in Swift 4.1.

If the new witness was not sufficiently visible, this caused a source
compatibility regression.

Work around this by discarding the witness if its not sufficiently
visible. In -swift-version 5, the hack expires, and we revert to the
stricter, more consistent behavior.

Fixes <rdar://problem/39614880>.
2018-07-04 00:30:36 -07:00
Slava Pestov
3701f745c4 Migrate various Sema tests to Swift 4 2018-06-25 01:02:20 -07:00