Commit Graph

99 Commits

Author SHA1 Message Date
Hamish Knight
2e0be5afd7 [CS] Open generic requirements for types with unbound + placeholder types
- Introduce a generic requirements opening function for type resolution,
  which is used by the constraint system in cases where we need to
  impose requirements on opened type variables.
- Refactor `replaceInferableTypesWithTypeVars` to use this function
  when opening generic types that contain either placeholder or unbound
  generic types.

Together these changes ensure that we don't drop generic requirements
when an unbound generic or placeholder type is used as a generic
argument.
2025-08-26 12:16:54 +01:00
Hamish Knight
52bb8b4495 [CS] Record opened types early in getTypeOfMemberReference
Requirement opening relies on being able to query the opened type to
allow requirement fix coalescing to work. Record the opened type before
we open requirements to ensure we don't duplicate requirement fixes on
the base type with requirement fixes for the member.
2025-08-26 11:07:35 +01:00
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
Kavon Farvardin
8f2ed59823 un-XFAIL type_in_function.swift
We don't support nested types in methods of generic types, so the new
error popping up about `A` being noncopyable is not too surprising,
but also not the point of this test anyway.
2024-03-14 23:10:44 -07:00
Kavon Farvardin
f296d8e158 NCGenerics: mass XFAIL tests
It's easier to get a handle on regressions while working through
failures if the tests that are known to not pass are XFAIL'd for
NoncopyableGenerics.
2024-02-20 18:26:05 -05: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
Slava Pestov
290701cb4d Sema: Ban shadowing generic parameters from outer scopes
Code like that is usually indicative of programmer error, and does not
round-trip through module interface files since there is no source
syntax to refer to an outer generic parameter.

For source compatibility this is a warning, but becomes an error with
-swift-version 6.

Fixes rdar://problem/108385980 and https://github.com/apple/swift/issues/62767.
2023-04-25 17:41:23 -04:00
Anthony Latsis
b5e6fb3f1a Gardening: Migrate test suite to GH issues: decl/nested 2022-08-26 03:26:32 +03: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
Anthony Latsis
c2e5d04669 Test: Find better locations for some updated tests 2021-08-31 19:21:26 +03: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
Anthony Latsis
749a0ae853 Test: Find better locations for some updated tests 2021-08-16 18:30:26 +03:00
Suyash Srijan
7834120a87 [Test] Update existing tests 2021-08-16 18:30:26 +03:00
Luciano Almeida
19727f2cc2 [Sema][test] Adjusting all missing downcast diagnostics failures 2020-11-28 18:17:18 -03:00
Robert Widmann
363b66a7ad Fully Qualify the Parent Type When Diagnosing Missing Member Types
Use the FullyQualified<Type> abstraction from the prior commit plus DescriptiveDeclKind to give a bit more information when issuing a missing member type diagnostic during type resolution.
2020-11-09 17:10:18 -08: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
Luciano Almeida
564676491c fixing test in test/decl/nested/type_in_type.swift 2019-10-12 18:35:40 -03: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
Pavel Yaskevich
a1643d94f7 [Diagnostics] NFC: Update all of the improved test-cases 2019-09-13 22:35:52 -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
Robert Widmann
12b157e2bc Restore two tests
Address review feedback on #26970
2019-09-06 09:57:18 -07:00
Harlan Haskins
2062ee837d Merge pull request #26999 from harlanhaskins/my-internal-conflict
[Sema] Point to declaration when looking for a member type
2019-09-03 16:51:04 -07:00
Harlan Haskins
fb546513da [Sema] Point to declaration when looking for a member type
When there's a module with the same name as a type in a
different module, lookup will look into the type, not the module, when
resolving members. Until that behavior is fixed, add a note showing what
lookup was trying to look into, to make the behavior more clear.

Helps rdar://54770139
2019-09-03 15:27:28 -07:00
Robert Widmann
59687518e2 Reject nominal extensions (with a bit of indirection)
If the canonical type is a nominal type then we can offer a diagnostic
that rewrites to it.

Resolves rdar://54799560
2019-09-01 01:52:41 -05:00
Pavel Yaskevich
cbadd7ffc2 [Diagnostics] Correctly identify location of requirement failure
Previously in situations like:

```swift
protocol P {}

struct S<T: P> {
  var value: T
}

_ = S(value: 42)
```

Diagnostic has reported a problem as related to "reference" to `init`
but the failing generic type requirement belongs to `S`, so a
better diagnostic in such case should mention `generic struct S`.
2019-08-16 22:02:37 -07:00
Sam Lazarus
de7851b0e9 Test: Update tests to reflect change to generic mismatch note locations 2019-06-14 12:35:32 -04:00
Sam Lazarus
81dc5460c9 Sema / Test: Fix tests broken by introduction of GenericArgumentsMismatchFailure
Additionally, fixed a crash caused by the change relating to opaque types.
2019-06-14 12:35:31 -04:00
Pavel Yaskevich
c30845fa74 [ConstraintSystem] Detect and diagnose missing generic arguments
Introduce a fix to detect and diagnose situations when omitted
generic arguments couldn't be deduced by the solver based on
the enclosing context.

Example:

```swift
struct S<T> {
}

_ = S() // There is not enough context to deduce `T`
```

Resolves: rdar://problem/51203824
2019-05-29 16:39:41 -07:00
Pavel Yaskevich
91dbcfdfcc [ConstraintSystem] Deplay opening generic requirements until after contextual self has been applied
While computing a type of member via `getTypeOfMemberReference`
let's delay opening generic requirements associated with function
type until after self constraint has been created, that would give
a chance for contextual types to get propagated and make mismatch
originated in generic requirements much easier to diagnose.

Consider following example:

```swift
struct S<T> {}

extension S where T == Int {
  func foo() {}
}

func test(_ s: S<String>) {
  s.foo()
}
```

`foo` would get opened as `(S<$T>) -> () -> Void` and contextual `self`
type is going to be `S<String>`, so applying that before generic requirement
`$T == Int` would make sure that `$T` gets bound to a contextual
type of `String` and later fails requirement constraint `$T == Int`.

This is much easier to diagnose comparing to `$T` being bound to
`Int` right away due to same-type generic requirement and then
failing an attempt to convert `S<String>` to `S<Int>` while simplifying
self constraint.

Resolves: rdar://problem/46427500
Resolves: rdar://problem/34770265
2019-05-24 11:33:30 -07:00
John Holdsworth
dbe99d771e Make Self available to member functions (SE-0068?) (#22863)
* Make Self available to instance member functions (SE-0068?)

* Works for value types and static functions.

* Further experiments with TypeExpr

* Move Self processing off diagnostic path

* diagnostic instead of assertion fail

* TypeExpr of DynamicSelfType now working.

* Update tests for availability of Self

* Cast to Self fixed!

* Self not available as type in classes except for return type

* Could it be this simple?

* Nearly there

* Fix function decls using Self inside methods.

* Fix validation-test/compiler_crashers_2_fixed/0164-sr7989.swift

* Fix of ./validation-test/compiler_crashers_2_fixed/0179-rdar44963974.swift

* "Un-fix" validation-test/compiler_crashers_2_fixed/0164-sr7989.swift

* CHANGELOG entry

* Update CHANGELOG.md

Co-Authored-By: johnno1962 <github@johnholdsworth.com>

* Update CHANGELOG.md
2019-03-15 23:23:19 -04: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
Pavel Yaskevich
74a8ee177e [Diagnostics] Diagnose missing members via fixes
Try to fix constraint system in a way where member
reference is going to be defined in terms of its use,
which makes it seem like parameters match arguments
exactly. Such helps to produce solutions and diagnose
failures related to missing members precisely.

These changes would be further extended to diagnose use
of unavailable members and other structural member failures.

Resolves: rdar://problem/34583132
Resolves: rdar://problem/36989788
Resolved: rdar://problem/39586166
Resolves: rdar://problem/40537782
Resolves: rdar://problem/46211109
2019-01-09 17:29:49 -08:00
Slava Pestov
bcfbcad802 Sema: Don't allow generic typealiases to have an unbound generic underlying type
We allow this sort of thing:

struct Generic<T> {}
typealias Alias = Generic
...
Alias<Int>

However we have to be sure to ban this if the typealias is itself generic, ie

typealias Alias<T> = Generic

Otherwise we will crash.
2018-12-14 20:39:00 -05: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
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