Commit Graph

178 Commits

Author SHA1 Message Date
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
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
Robert Widmann
9bd02e1d17 Allow eliding ownership conventions in a copyable context
Allow witnesses to protocols in a copyable context to elide explicit
ownership conventions.  This allows clients like the standard library to
standardize on one ownership convention without an ABI or API breaking
change in 3rd party code.

In the future, moveonly contexts must disallow this default behavior
else the witness thunks could silently transfer ownership.

rdar://40774922

protocol P {
  __consuming func implicit(x: __shared String)
  __consuming func explicit(x: __owned String)
  __consuming func mismatch(x: __shared String)
}

class C : P {
  // C.implicit(x:) takes self and x '@guaranteed' thru the witness thunk
  func implicit(x: String) {}
  // C.explicit(x:) takes self and x @owned with no convention changes
  __consuming func explicit(x: __owned String) {}
  // Would inherit __consuming, but x has been spelled __owned so the requirement match fails.
  func mismatch(x: __owned String) {}
}
2018-06-19 12:25:50 -07:00
Jordan Rose
6bd7e5e5b4 Make sure protocol witness errors don't leave the conformance context
That is, if there's a problem with a witness, and the witness comes
from a different extension from the conformance (or the original type,
when the conformance is on an extension), put the main diagnostic on
the conformance, with a note on the witness. This involves some
shuffling and rephrasing of existing diagnostics too.

There's a few reasons for this change:

- More context. It may not be obvious why a declaration in file
  A.swift needs to be marked 'public' if you can't see the conformance
  in B.swift.

- Better locations for imported declarations. If you're checking a
  conformance in a source file but the witness came from an imported
  module, it's better to put the diagnostic on the part you have
  control over. (This is especially true in Xcode, which can't display
  diagnostics on imported declarations in the source editor.)

- Plays better with batch mode. Without this change, you can have
  diagnostics being reported in file A.swift that are tied to a
  conformance declared in file B.swift. Of course the contents of
  A.swift also affect the diagnostic, but compiling A.swift on its
  own wouldn't produce the diagnostic, and so putting it there is
  problematic.

The change does in some cases make for a worse user experience,
though; if you just want to apply the changes and move on, the main
diagnostic isn't in the "right place". It's the note that has the info
and possible fix-it. It's also a slightly more complicated
implementation.
2018-05-10 19:31:12 -07:00
Jordan Rose
545b12fa9f Convert backticks to single-quotes in Sema diagnostics
Let's be consistent!
2018-05-09 21:59:39 -07:00
David Zarzycki
995dec5d82 [Sema] Error if ObjC interop is needed when disabled 2018-05-07 14:43:04 -04:00
Slava Pestov
b87944d52c Sema: DeclChecker::visitEnumDecl() does everything in the 'first pass' 2018-04-02 23:17:59 -07:00
Slava Pestov
24ac4f5e85 Sema: DeclChecker::visitStructDecl() does everything in the 'first pass'
The diagnostic change is harmless. The diagnostic that is no longer being
emitted was intended for multi-file conformance checking only, and we still
emit a diagnostic for the overall conformance failure with missing witnesses.
2018-03-22 17:47:58 -07:00
Doug Gregor
a29d5d3957 [Associated type inference] Add now-fixed test for rdar://problem/16316115. 2018-01-24 23:01:35 -08:00
Doug Gregor
e471e402da [Associated type inference] Don’t get blocked by typealiases in protocol extensions.
Typealiases in protocol extensions can be used to satisfy 
associated type requirements. However, when they don’t meet all
of the requirements placed on the associated type, fall back to
the normal inference path rather than failing outright.

Fixes SR-6609 / rdar://problem/36038033.
2018-01-24 22:56:17 -08:00
Doug Gregor
5d0076e1e7 [Associated type inference] Allow magic typealiases to inform inference.
Introduce an ugly hack where a typealias in a protocol extension that
has the name `_Default_Foo` can be found by associated type inference for
an associated type named `Foo`, respecting the constrains of the protocol
extension in which that typealias resides.
2017-12-20 23:11:51 -08:00
Doug Gregor
f26d794495 [Conformance checking] Allow same-type constraints to infer associated types.
When a protocol closes off an associated type by tying it to a concrete
type (e.g., via a same-type constraint), allow associated type inference
to use that information to infer the associated type rather than
requiring the user to restate the obvious.

Fixes SR-6640.
2017-12-19 14:28:20 -08:00
Doug Gregor
82a8833ab0 [Associated type inference] Use defaults from overridden associated types.
During associated type inference, also look for default associated type
witnesses from overridden associated types, so that one need not
redeclare default associated types at every level in a protocol hierarchy.
2017-12-19 13:03:29 -08: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
398f76d12b Sema: Check AnyObject constraint on associated type witnesses
Fixes <https://bugs.swift.org/browse/SR-6109>, <rdar://problem/34911213>.
2017-10-31 19:35:31 -07:00
Doug Gregor
f51d5b6aec [Conformance checking] Allow generic parameter when inferring type witnesses.
As part of type witness inference, allow us to infer a generic parameter
as the type witness for an associated type, when the associated type has
the same name as the generic parameter.

We likely want the more-general rule that generic parameters are always
visible as members of their enclosing type, but I'll tackle that separately.
This works around an order dependency that affected the source
compatibility suite.
2017-10-09 10:20:22 -07:00
Doug Gregor
7f6a71c173 [Conformance checking] Check 'inout' when matching witnesses.
Associated type inference was inferring an associated type to be an inout
type, causing crashing surprisingly late in IRGen. Fixes SR-4486.
2017-10-04 17:22:45 -07:00
Doug Gregor
d12dbf99ea [Type checker] Diagnose failed attempts to resolve associated type witnesses.
If unqualified name lookup finds an associated type, but resolution to
the type witness fails, produce a diagnostic rather than silently
failing. Fixes the crash in SR-5825, SR-5881, and SR-5905.

It's conceivable that we could resolve this with suitably global
associated type inference... but that's far off and it's best not to
crash until then.
2017-10-03 21:59:14 -07:00
Doug Gregor
684a484d81 SR-5753: Don't warn about constraints redundant with inferred constraints. 2017-09-26 17:22:43 -07:00
Mohammed M. Ennabah
81ab8a302d changed diagnostics argument name to argument label SR-5857 (#11894)
* changed diagnostics argument name to argument label SR-5857
2017-09-13 19:07:19 -07:00
John McCall
2d3d6addc0 Delay the validaton of storage accessors until finalization.
The base mutability of storage is part of the signature, so be sure
to compute that during validation.  Also, serialize it as part of
the storage declaration, and fix some places that synthesize
declarations to set it correctly.
2017-09-10 04:56:02 -04:00
Slava Pestov
19fed22872 Parse: Remove redundant diagnostic
We had two slightly different codepaths to diagnose ': class'
in an inheritance clause where it is not supported.

For generic parameters, we would fix the 'class' to 'AnyObject',
but for associated types we didn't do this. Perform the fix in
all cases where it makes sense and remove one of the two
diagnostics.
2017-09-05 21:53:52 -07:00