Commit Graph

72 Commits

Author SHA1 Message Date
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
Slava Pestov
8cd89bd709 Sema: Fix archetype vs interface type mixup with type in generic function 2018-07-10 17:01:12 -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
d4c4f74116 Sema: Remove another bogus circularity check 2018-07-03 15:36:12 -07:00
Slava Pestov
9ec1926731 Sema: Allow classes in protocol inheritance clauses 2018-07-02 22:06:33 -07:00
Slava Pestov
ba30de0f08 Sema: Re-word circular inheritance diagnostics 2018-06-28 16:54:28 -07:00
Slava Pestov
efe2a74389 AST: Tweak getContextSubstitutions() for types inside generic functions 2018-06-20 22:56:44 -07:00
Slava Pestov
29f785f09b Sema: Don't emit duplicate diagnostics for members named 'Type' or 'Protocol'
IsFirstPass is going away soon, but in the meantime, the next
patch regresses matters slightly and causes us to emit
duplicate diagnostics in more cases, so let's fix it.
2018-04-02 23:17:59 -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
Doug Gregor
24aa030f0c [Associated type inference] Break recursive inference and fail predictably.
There was a path through associated type inference where we would end
up recording a type witness that contained an error, but for which we
had not reported that error, which would lead to downstream
crashes. Make sure that we reject such inferences.

And because it triggers once we fix this issue... make sure break
recursion when trying to resolve type witnesses lazily.

Fixes the crash in SR-6609 / rdar://problem/36038033, but we're still
failing to infer in those cases.
2018-01-24 15:58:26 -08:00
Graydon Hoare
367a1500a3 [Diagnostics] s/may/must/ in 'may not be named' 2017-09-29 16:09:23 -04:00
Slava Pestov
3dc4114513 Sema: Fix crash when resolving bound generic type with unbound parent
Eg, if both Foo and Bar are generic, Foo.Bar<Int> would crash.

This was introduced when I refactored the code a bit to combine
the code path for nominal types and generic typealiases. I was
a little too eager in merging them, because in fact the
substitution-based approach doesn't work when the parent type
is an unbound generic, which can occur in the nominal type case.

Note that we skip validation of generic parameters when the
parent type is an unbound generic. This is because there may
be requirements relating the generic arguments of the parent
type with the generic arguments of the nested type, and unless
all arguments are known, we cannot check these requirements.

For example:

struct Outer<T : Collection> {
  struct Inner<U : Collection> where T.Element == U.Element { }
}

When Sema opens the unbound generic type by creating new type
variables for each generic argument, it will also introduce
constraints corresponding to each generic requirement. Therefore
constraint system will eventually fail if the arguments are
wrong.

Fixes <https://bugs.swift.org/browse/SR-5600>,
<rdar://problem/33655028>.
2017-08-02 22:03:57 -07:00
Alex Hoppen
f35f29d9cf [Diag] Change function diagnostics to take a DeclName parameter
This provides richer error messages that include the function's
parameters
2017-07-01 13:37:08 +02: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
c3ac890a34 Sema: Fix findDeclContextForType() for types nested inside closures
Fixes <rdar://problem/31803589>.
2017-05-07 03:08:45 -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
07c189558c Sema: Improve the fixit for 'Self' in invalid places
In an extension of a nested type, the extended type must be
fully qualified.

Also clean up the diagnostic logic a little bit and centralize
it in diagnoseUnknownType().

Fixes <https://bugs.swift.org/browse/SR-4379>.
2017-04-23 01:10:40 -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
e8d58b4d4d Add test for SR-4672 which got fixed somehow 2017-04-22 20:43:26 -07:00
Slava Pestov
9984194e3d Sema: Fix for name lookup from the where clause of a nested type extension
Normally name lookup from a where clause walks up to the parent
DeclContext, so it does not need to handle outer generic parameters
specially. But with an extension of a nested type, the outer
DeclContext is the source file, and the extension itself has a
chain of GenericParamLists linked by the OuterParameters field.
2017-04-22 20:30:36 -07:00
Slava Pestov
b1177f0413 Sema: Fix bogus "always fails" warnings with casts between generic classes
We would misreport a cast from G<T> to G<Int> or vice versa
as always failing, because we were checking for an exact
subtype relationship instead of archetype binding.

Fixes <https://bugs.swift.org/browse/SR-3609>.
2017-04-20 00:37:39 -07:00
Doug Gregor
d9fa16bccb [GSB] Diagnose explicit constraints made redundant by inferred ones. 2017-03-22 17:35:11 -07:00
Slava Pestov
707083d22f Sema: Fix crash when defining an extension of a nested type with constraints
If the nested type itself has generic constraints, we would
hit an assertion in requirement inference. Refactor some code
so that we can make the assertion more accurate.

Fixes <rdar://problem/30353095>.
2017-02-08 21:51:19 -08:00
Jordan Rose
1c8caeb689 Handle implicit member references inside extensions of nested types. (#7241)
Within an extension, references to other members of the extended type
are permitted without qualification. This is intended to work even
when the extended type was a nested type, although members of the
enclosing type are /not/ visible in this case. In order to implement
this, the type checker pre-checks to see if there are /any/ members
with this name and then rewrites the unqualified reference to a
qualified one, based on an unresolved TypeExpr with the name of the
enclosing type. Unfortunately, if the enclosing type is a nested type,
that isn't going to work very well---we find the correct declaration,
but fail to map it into context by virtue of not realizing where it
came from. Fix this by explicitly checking for this case.

https://bugs.swift.org/browse/SR-3847
2017-02-06 14:49:49 -08:00