Commit Graph

43 Commits

Author SHA1 Message Date
Anthony Latsis
2d899d0e73 AST: Cut down on DescriptiveDeclKind usage in DiagnosticsCommon.def 2025-04-05 12:31:20 +01:00
Anthony Latsis
fe2408c0ab TypeCheckType: Unconditionally warn about missing existential any until Swift 7
https://github.com/swiftlang/swift/pull/72659 turned out to have some
source compatibility fallout that we need to fix. Instead of introducing
yet another brittle compatibility hack, stop emitting errors about a
missing `any` altogether until a future language mode.

Besides resolving the compatibility issue, this will encourage
developers to adopt any sooner and grant us ample time to gracefully
address any remaining bugs before the source compatibility burden
resurfaces.

A subsequent commit adds a diagnostic group that will allow users to
escalate these warnings to errors with `-Werror ExistentialAny`.
2025-02-12 21:20:44 +00:00
Alex Hoppen
66104395d7 [Sema/SourceKit] Emit same diagnostics for missing protocol requirements on the command line and in SourceKit
Some editors use diagnostics from SourceKit to replace build issues. This causes issues if the diagnostics from SourceKit are formatted differently than the build issues. Make sure they are rendered the same way, removing most uses of `DiagnosticsEditorMode`.

To do so, always emit the `add stubs for conformance` note (which previously was only emitted in editor mode) and remove all `; add <something>` suffixes from notes that state which requirements are missing.

rdar://129283608
2024-08-07 14:01:30 -07:00
Karl Wagner
6164ee5690 Fix name lookup in nested protocol inheritance clauses 2024-03-29 23:09:25 +01:00
Karl Wagner
853d3c8650 [SE-0404] Make test for protocol nested in actor conditionally-available 2023-10-06 21:04:03 +02:00
Karl Wagner
69c57045dc [SE-0404] Rebase fixes and better handling of generic superclasses with inferred params 2023-10-06 21:04:03 +02:00
Karl Wagner
ab4f80ed95 [SE-0404] Allow protocols to be nested in non-generic contexts 2023-10-06 21:04:03 +02:00
Slava Pestov
fbd1f1acbd Sema: Fix a couple of problems in checkContextualRequirements()
The original bug was a crash-on-invalid with a missing '}', but it
actually exposed a bug with nested protocols (SE-0404) and another
long-time bug.

- Whatever we do, we should skip this for protocols because their 'Self'
  parameter is not bound from context.

- getTrailingWhereClause() is not the right proxy for "has a generic
  signature different than its parent", in particular it doesn't
  round-trip through serialization. Instead, just compare generic
  signatures for pointer equality in the early return check.

The second change is source-breaking because it was possible to
write a nested type with a `where` clause and use it contradicting
its requirements across a module boundary.

Fixes rdar://113103854.
2023-08-08 15:06:08 -04:00
Holly Borla
0e7594a3f9 [Sema] Require any for SE-0309 existential types. 2022-04-21 18:22:27 -07:00
Cassie Jones
bc686e733e [Diagnostics] Change phrasing of the existential_requires_any warning
The previous warning "must be explicitly marked as 'any'" isn't clear if
you don't already know the feature, this new phrasing should make it
clearer by including the correct spelling of the type as an example.

Fixes rdar://90384448
2022-03-18 18:18:35 -07:00
Holly Borla
ee331a8c01 [Type System] Enable explicit existential types. 2022-01-13 19:31:35 -08:00
Slava Pestov
9a64e50e91 Sema: Fix type resolution with invalid nested protocol
If you have a class with an (invalid) protocol nested inside:

    class OuterGenericClass<T, U> {
      protocol InnerProtocol : OuterGenericClass {}
    }

The reference to 'OuterGenericClass' in the protocol's inheritance
clause is not "in context", and should not have inferred generic
arguments <T, U>, because the protocol does not inherit the generic
arguments of the outer scope.

Previously, this would trigger an assertion in the requirement
machine, because the generic parameter τ_0_1 is not a valid generic
parameter in the protocol's generic signature.
2021-09-02 23:02:52 -04:00
Suyash Srijan
396087d0d4 [Test] Update existing tests 2021-08-31 19:21:26 +03:00
Kavon Farvardin
c0607b345f Revert "Merge pull request #33767 from theblixguy/chore/remove-self-or-associated-type-diagnostic"
The following regression test added for this feature is not passing:

Swift(linux-x86_64) :: decl/protocol/protocols_with_self_or_assoc_reqs_executable.swift

with a compiler crash happening during SILFunctionTransform "Devirtualizer".

Reverting to unblock CI.

This reverts commit f96057e260, reversing
changes made to 3fc18f3603.
2021-08-26 16:46:42 -07:00
Suyash Srijan
7834120a87 [Test] Update existing tests 2021-08-16 18:30:26 +03:00
Slava Pestov
dcc27ea819 Sema: Fix failure to emit diagnostic with some invalid member type references
There were two problems here:

- isUnsupportedMemberTypeReference() checked if the immediate parent type was
  an unbound generic type, but did not check for the parent of the parent, etc.

- The caller of isUnsupportedMemberTypeReference() had to re-check the
  various invalid conditions to determine what diagnostic to emit, and if
  none of those conditions matched it would just return an ErrorType without
  emitting a diagnostic.

Fix both of these by having isUnsupportedMemberTypeReference() return an
enum indicating what went wrong, and handle more cases.

Fixes <rdar://problem/67292528>.
2020-08-30 00:12:33 -04:00
Varun Gandhi
a1716fe2a6 [Diagnostics] Update compiler diagnostics to use less jargon. (#31315)
Fixes rdar://problem/62375243.
2020-04-28 14:11:39 -07:00
Pavel Yaskevich
93c2321106 [TypeChecker] Add types nested in protocol to lookup results
Although such functionality is not yet supported we have to
mirror AST lookup and add such members into results, otherwise
there is a risk of inner/outer results mismatch.
2020-01-29 09:14:25 -08:00
Slava Pestov
aa62509d65 Sema: Remove an unnecessary call to isInvalid()
This forces the interface type, which we don't want to do here.
2019-11-13 23:42:08 -05:00
Robert Widmann
01d272073a Requestify Witness Resolution
Witness matching is a source of a lot of ad-hoc cycles, and mixes the
logic that performs resolution, caching, validation, and cycle detection into one
place.  To make matters worse, some checkers kick off other checks in
order to cache work for further declarations, and access an internal
cache on their subject conformance for many requirements at once, or
sometimes just one requirement.

None of this fits into the request evaluator's central view of the
caching.  This is further evidenced by the fact that if you attempt to
move the caching step into the evaluator, it overcaches the same
witness and trips asserts.

As a start, define requests for the resolution steps, and flush some
hacks around forcing witness resolution. The caching logic is mostly
untouched (the requests don't actually cache anything), but some cycle
breaking is now handled in the evaluator itself.  Once witness matching
has been refactored to cache with the evaluator, all of these hacks can
go away.

My urge to destroy the LazyResolver outweighs the compromises here.
2019-11-05 13:28:07 -08:00
Robert Widmann
233f8645d1 Move some more common enum diagnostics into the decl checker
In preparation for checkEnumRawValues being turned into a request, move the common diagnostics to the decl checker so they're always emitted at the right time.
2019-09-26 11:07:03 -07:00
Robert Widmann
e0a41b19cb Break some cycles
Computing the generic signature changes the way that cycles appear in the compiler.  For now, break these cycles.  We should investigate each place where hasComputedGenericSignature() is used in service of breaking cycles.  See rdar://55263708
2019-09-16 12:50:24 -07:00
Robert Widmann
d4bb9a5cfe Define GenericParamListRequest
GenericParamListRequest formalizes the lazy generic parameter list construction pattern we were performing before.
2019-09-06 17:22:30 -07:00
Doug Gregor
5757d14365 Don’t add RawRepresentation conformance for enums with erroneous raw types
This avoids cascading errors with very unfortunate Fix-Its.
rdar://problem/47891507
2019-02-12 23:12:17 -08:00
Jordan Rose
23df594749 Remove a too-strict assertion concerning nested protocols (#18241)
This isn't allowed in the language, but the compiler shouldn't crash!
2018-07-25 19:35:21 -07:00
Slava Pestov
3ae11e6536 Sema: Fix a recusive validation crasher 2018-07-10 17:01:12 -07:00
Slava Pestov
ef864094b8 Sema: Fix crash when accessing nominal member of protocol 2018-07-10 17:01:12 -07:00
Slava Pestov
9ec1926731 Sema: Allow classes in protocol inheritance clauses 2018-07-02 22:06:33 -07:00
Pavel Yaskevich
9f7d91323b [Sema] Don't allow member type lookup on existential types
Existential types don't support nested types so let's not
allow the lookup to make such access, with only exception
for typealias or associated types without type parameters,
they are currently allowed.

Resolves: rdar://problem/38505436
2018-03-16 13:53:09 -07:00
Slava Pestov
c49a1d77d2 AST: Stricter DeclContext::getParentForLookup() 2017-06-06 23:56:32 -07:00
Jordan Rose
63bc717963 Error when one associated type is constrained to another. (#10053)
(...is constrained to be a subtype of another)

Previously the compiler would just mark the entry in the inheritance
clause invalid and move on without emitting any errors; in certain
circumstances in no-asserts builds this could actually lead to
everything working "correctly" if all conforming types happened to
pick the same concrete type for both associated types. In Swift 4 this
can actually be enforced with a same-type requirement, which will
guarantee that the two associated types are the same even in generic
contexts.

This fix avoids assertions and crashes, but the diagnostic is still
incorrect, and in the simple case of the inheritance clause it's
redundant. Doing something better and possibly even downgrading it to
a warning in Swift 3 mode is tracked by rdar://problem/32409449.

Initial patch by Slava, fixed up by me.
2017-06-01 19:45:34 -07:00
Slava Pestov
6548ad709b Sema: Remove TR_NonEnumInheritanceClauseOuterLayer
This was added at some point with the associated type where
clause work, but it appears to be unnecessary, because all
the tests passed with this removed.

It also introduced a source compatibility issue where we
stopped accepting typealiases to protocol compositions in
protocol inheritance clauses.

Add a test for this case too, since it wasn't tested before.

Fixes <https://bugs.swift.org/browse/SR-4855>.
2017-05-10 22:12:25 -07:00
Slava Pestov
2fe863266c AST: Fix crashes when name lookup finds declarations outside of a protocol or extension that's nested in another type
When performing a name lookup from inside of a protocol
or extension, skip directly to the source file context
when we are done visiting the protocol or extension.

Otherwise, if we have invalid code where the protocol
or extension is nested inside another type, we might
find a member whose type contains generic parameters
of the outer type; these parameters will not resolve,
since we do not model protocols or extensions nested
inside generic contexts (yet?).

This supercedes an earlier workaround for a similar
issue; the new workaround fixes more crashes.

This is needed to avoid crasher regressions with an
upcoming patch.
2017-05-07 03:01:47 -07:00
Slava Pestov
e5cc508415 Sema: Update inheritance clause checking for primitive AnyObject 2017-05-01 17:25:02 -07:00
Slava Pestov
39b697898a Sema: Fix for unbound generic type handling
Previously we prohibited unbound generics in the underlying
type of a typealias, but due to an oversight the check was
not performed when resolving a nested type.

So this worked:

struct Outer { struct Inner<T> {} }
typealias OuterInner = Outer.Inner
let _: OuterInner<Int> = Outer.Inner<Int>()

However it was easy to cause a crash this way by stating an
unbound generic type where one was not expected. Also,
unqualified types in a typealias did not get this treatment,
so the following did not work:

typealias MyOptional = Optional

Formalize the old behavior by allowing unbound generic types
in the underlying type of a typealias, while otherwise
prohibiting unbound references to nested types.
2017-04-23 00:02:29 -07:00
Slava Pestov
43ce2e7e1c AST: Simplify DeclContext::getGeneric{Signature,Environment}OfContext()
There's no need to walk up from a function or type context -- if
no generic signature (or environment) is set, the parent won't have
one, either, and if we're in the middle of validating the child
context, using the parent's signature or environment to resolve
dependent types is just wrong.
2017-01-04 01:08:28 -08:00
Slava Pestov
978941f1d5 Sema: More consistent logic for non-requirement protocol members 2016-12-21 14:20:27 -05:00
Slava Pestov
3f7a72eb84 Sema: Clean up diagnostic logic when deriving RawRepresentable conformance on enums 2016-12-21 14:20:26 -05:00
Slava Pestov
867a956fc2 Sema: Name lookup fix for nominal type members of protocols 2016-12-19 18:49:58 -08:00
Slava Pestov
5640339439 Sema: Map types out of context when resolving inheritance clause in ITC 2016-12-19 18:49:57 -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
7814c47b71 AST: Slightly change meaning of NominalTypeDecl::getDeclaredType()
Consider this code:

struct A<T> {
  struct B {}
  struct C<U> {}
}

Previously:

- getDeclaredType() of 'A.B' would give 'A<T>.B'
- getDeclaredTypeInContext() of 'A.B' would give 'A<T>.B'

- getDeclaredType() of 'A.C' would give 'A<T>.C'
- getDeclaredTypeInContext() of 'A.C' would give 'A<T>.C<U>'

This was causing problems for nested generics. Now, with this change,

- getDeclaredType() of 'A.B' gives 'A.B' (*)
- getDeclaredTypeInContext() of 'A.B' gives 'A<T>.B'
- getDeclaredType() of 'A.C' gives 'A.C' (*)
- getDeclaredTypeInContext() of 'A.C' gives 'A<T>.C<U>'

(Differences marked with (*)).

Also, this change makes these accessors fully lazy. Previously,
only getDeclaredTypeInContext() and getDeclaredIterfaceType()
were lazy, whereas getDeclaredType() was built from validateDecl().

Fix a few spots where the return value wasn't being checked
properly.

These functions return ErrorType if a circularity was detected via
the generic parameter list, or if the extension did not resolve.
They return Type() if the extension cannot be resolved *yet*.

This is pretty subtle, and I'll need to do another pass over
callers of these functions at some point. Many of them should be
moved over to use getSelfInContext(), getSelfOfContext() and
getSelfInterfaceType() instead.

Finally, this patch consolidates logic for diagnosting invalid
nesting of types.

The parser had some code for protocols in bad places and bad things
inside protocols, and Sema had several different bail-outs for
bad things in protocols, nested generic types, and stuff nested
inside protocol extensions.

Combine all of these into a single set of checks in Sema. Note
that we no longer give up early if we find invalid nesting.
Leaving decls unvalidated and un-type-checked only leads to
further problems. Now that all the preliminary crap has been
fixed, we can go ahead and start validating these funny nested
decls, actually fixing some crashers in the process.
2016-06-18 17:15:24 -07:00
Slava Pestov
011e306fe8 Sema: Add -enable-experimental-nested-generic-types frontend flag
Also, split up test/decl/nested.swift into several files.
2016-06-13 16:46:41 -07:00