Commit Graph

500 Commits

Author SHA1 Message Date
Suyash Srijan
f026c61dce [ConstraintSystem] Disallow use of enum case as a key path component (#31969) 2020-05-22 20:41:45 +01:00
Hamish Knight
cc062ee2bb Fix precedencegroup and operator decl lookup
Re-implement operator and precedencegroup decl
lookup to use `namelookup::getAllImports` and
existing decl shadowing logic. This allows us to
find operator decls through `@_exported` imports,
prefer operator decls defined in the same module
over imported decls, and fixes a couple of other
subtle issues.

Because this new implementation is technically
source breaking, as we can find multiple results
where we used to only find one result, it's placed
behind the new Frontend flag
`-enable-new-operator-lookup` (with the aim of
enabling it by default when we get a new language
mode).

However the new logic will always be used if the
result is unambiguous. This means that e.g
`@_exported` operators will be instantly available
as long as there's only one candidate. If multiple
candidates are found, we fall back to the old
logic.

Resolves SR-12132.
Resolves rdar://59198796.
2020-05-18 14:33:43 -07:00
Pavel Yaskevich
7e4eb9cee5 [Diagnostics] Diagnose inability to infer type of a closure parameter 2020-05-15 01:13:06 -07:00
Luciano Almeida
02c454c976 [Diagnostics] Diagnose that we cannot infer the key path type when binding to a hole 2020-05-11 18:08:40 -03:00
Artem Chikin
7aed042817 Add fix on Keypath -> Function with multiple arguments type mismatch.
When simplifying a keypath constraint with a function type binding, single-parameter functions have the parameter type and the return type matched against the keypath root and value; whereas multiple-parameter functions cause an ambiguous failure (in `simplifyKeyPathConstraint`).

Resolves rdar://problem/57930643
2020-05-06 19:15:08 -07:00
Pavel Yaskevich
5a7c70dcc4 [Diagnostics] NFC: Add isRawRepresentable/conformsToKnownProtocol helpers into FailureDiagnostic 2020-05-01 13:46:21 -07:00
Pavel Yaskevich
db6801b4b5 [Diagnostics] Add getRawType which allows to retrieve type associated with ASTNode
This is useful when diagnostic needs to check something about type
variables involved in a particular type e.g. whether type variable
has been defaulted.
2020-05-01 12:44:34 -07:00
Pavel Yaskevich
ee6c228214 [Diagnostics] NFC: Move is{Array, Collection}Type helpers into FailureDiagnostic 2020-05-01 12:21:15 -07:00
Pavel Yaskevich
99ef865aac [Diagnostics] Switch FailureDiagnostic to use ASTNode instead of TypedNode 2020-04-29 17:03:45 -07:00
Luciano Almeida
723a75343d [CSDiagnostics] Tailor diagnostic for define missing member when involving a collectin type 2020-04-28 17:07:12 -03:00
Luciano Almeida
041051e1ed [CSDiagnostics] Tailor diagnostic for define missing member when involves array literal and unresolved member 2020-04-28 12:35:28 -03:00
Joe Groff
b4c7a7e85d Merge pull request #31224 from AnthonyLatsis/rename-getfullname-2
AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl
2020-04-24 12:51:28 -07:00
Pavel Yaskevich
12a63ce907 [ConstraintSystem] Switch getContextualType and its variants to use TypedNode 2020-04-23 01:13:13 -07:00
Pavel Yaskevich
e5c94bfa45 [Diagnostics] Switch FailureDiagnostic constructor to accept TypedNode as an anchor 2020-04-23 01:13:13 -07:00
Pavel Yaskevich
398f37842a [ConstraintSystem] Elevate TypedNode access helpers up to the namespace
Originally such accessors were only useful for `FailureDiagnostic` but
now since `ConstraintLocator` is anchored with `TypedNode` it makes sense
to make them universally accessible.
2020-04-23 01:13:13 -07:00
Luciano Almeida
721dd7780d [NFC] Minor comment adjustments 2020-04-22 23:55:58 -03:00
Anthony Latsis
74252028ca AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl 2020-04-23 05:16:55 +03:00
Luciano Almeida
094ffd6b25 [CSDiagnostics] Adjusting KeyPathRootTypeMismatchFailure implementation and message 2020-04-22 20:30:54 -03:00
Luciano Almeida
f8cc9b52fe [tests] Adjusting SR-12425 diagnostics tests 2020-04-21 21:08:37 -03:00
Luciano Almeida
6e5be5b7d4 [CSDiagnostics] Create KeyPathRootTypeMismatchFailure to diagnose key path root fail on key path application 2020-04-21 17:38:06 -03:00
Pavel Yaskevich
967b3a3c8d [Diagnostics] Adjust getCallInfo to accept TypedNode instead of expression 2020-04-17 13:28:06 -07:00
Pavel Yaskevich
4ea7f3bbb1 [Diagnostics] Add helper functions to work with anchors - {castTo, getAs, is}Expr 2020-04-17 13:15:12 -07:00
Pavel Yaskevich
6335a4f2ef [Diagnostics] Resolve const-ness problems introduced by TypedNode
Since `TypedNode` elements are all marked as `const` diagnostics
need to get some of the APIs adjusted to support passing `const Expr *`.
2020-04-17 11:43:09 -07:00
Pavel Yaskevich
08e09fc685 [Diagnostics] Switch getType to use TypedNode 2020-04-17 11:43:09 -07:00
Pavel Yaskevich
66a07bab95 [Diagnostics] Switch getContextualType* to use TypedNode 2020-04-17 11:43:09 -07:00
Pavel Yaskevich
71ab19bdce [Diagnostics] Switch getConstraintLocator variants to accept TypedNode 2020-04-17 11:43:09 -07:00
Pavel Yaskevich
f5714bf50d [Diagnostics] Switch get{Raw}Anchor to return TypedNode
In preparation to anchor `ConstraintLocator` from `TypedNode`
let's refactor diagnostics (which is the biggest user of locators)
to support `TypedNode` instead of `Expr *`.
2020-04-17 11:43:09 -07:00
Pavel Yaskevich
4ed3665f86 [Diagnostics] Provide anchors on demand instead of storing in FailureDiagnostic
This decouples `FailureDiagnostic` from expression.
2020-04-17 11:43:09 -07:00
Pavel Yaskevich
1dc6a0ac55 [Diagnostics] Allow invalid initializer ref diagnostics point to .init 2020-04-14 12:18:55 -07:00
Pavel Yaskevich
cdee9a1ce3 [Diagnostics] Use new getLoc and getSourceRange methods
Split `emitDiagnostic` into `emitDiagnostic` and `emitDiagnosticAt`.

Refactor existing diagnostics to use new methods and avoid passing
location when possible.
2020-04-14 11:30:11 -07:00
Pavel Yaskevich
5dbced972f [Diagnostics] Allow failure diagnostic implementation to extend getAnchor
Some of the diagnostics have special rules about anchor location,
let's make `getAnchor` virtual and allow sub-class to override
default implementation.
2020-04-14 11:30:11 -07:00
Pavel Yaskevich
103630296d [Diagnostics] Add a getLoc method to FailureDiagnostic 2020-04-14 11:30:11 -07:00
Hamish Knight
a61223a255 [CS] Visit all fixed bindings for constraint re-activation (#30886)
[CS] Visit all fixed bindings for constraint re-activation
2020-04-10 12:27:47 -07:00
Hamish Knight
d69a42d656 [CS] Preserve compatibility for collection coercions
Previously we could allow some invalid coercions to
sneak past Sema. In most cases these would either
cause crashes later down the pipeline or
miscompiles. However, for coercions between
collections, we emitted somewhat reasonable code
that performed a force cast.

This commit aims to preserve compatibility with
those collection coercions that previously
compiled, and emits a warning telling the user to
use either 'as?' or 'as!' instead.
2020-04-10 10:16:07 -07:00
Pavel Yaskevich
42bdfc9bc6 [Diagnostics] Remove unsafe getRestrictionFor method 2020-04-09 15:35:14 -07:00
Pavel Yaskevich
6f2af1f433 [Diagnostics] Replace getChoiceFor with getCalleeOverloadChoiceIfAvailable
New name is more consistent with existing `getOverloadChoiceIfAvailable`
and allows us to clean up a couple of places where `getCalleeLocator` was
used directly before.
2020-04-09 12:44:16 -07:00
Pavel Yaskevich
6f83c08aae [Diagnostics] Remove obsolete getResolvedMemberRef 2020-04-08 16:09:08 -07:00
Pavel Yaskevich
a84e0b70fc [Diagnostics] Obsolete and remove HasComplexLocator field from diagnostic
This was useful when CSDiag was still in play but since everything
has been ported this is no longer useful.
2020-04-08 15:03:42 -07:00
Luciano Almeida
faba29a55f [Diagnostics] Diagnose key path reference to initializer methods. 2020-03-28 16:32:08 -03:00
Pavel Yaskevich
7800a04de3 [Diagnostics] Adjust "missing arguments" diagnostic to store parameter indices directly
This helps to avoid allocating new type variables (which shouldn't be done regardless)
to store parameter indices when `missing arguments` diagnostic is used by other diagnostics.
2020-03-13 16:55:37 -07:00
Pavel Yaskevich
3ab8710329 [Diagnostics] Cleanup use of constraint system by ConstraintFix/FailureDiagnostic 2020-03-13 15:02:10 -07:00
Pavel Yaskevich
0fc9d1b84a [Diagnostics] Move getFunctionArgApplyInfo to Solution
Instead of relying on constraint system having solution applied
let's pass a solution directly to `getFunctionArgApplyInfo`.
2020-03-13 12:46:25 -07:00
Pavel Yaskevich
af4afe3fbc [Diagnostics] Switch FailureDiagnostic to use a solution
Make diagnostics stateless by providing them with a solution
instead of applying a solution back to constraint system and
using constraint system. Latter doesn't work well when there
is an ambiguity and multiple solutions are available instead
of one.
2020-03-13 10:37:23 -07:00
Slava Pestov
019452f9af Sema: Diagnose unbound method references on 'super.'
This is something I noticed by inspection while working on
<https://bugs.swift.org/browse/SR-75>.

Inside a static method, 'self' is a metatype value, so
'self.instanceMethod' produces an unbound reference of type
(Self) -> (Args...) -> Results.

You might guess that 'super.instanceMethod' can similarly
be used to produce an unbound method reference that calls
the superclass method given any 'self' value, but unfortunately
it doesn't work.

Instead, 'super.instanceMethod' would produce the same
result as 'self.instanceMethod'. Maybe we can implement this
later, but for now, let's just diagnose the problem.

Note that partially-applied method references with 'super.'
-- namely, 'self.staticMethod' inside a static context, or
'self.instanceMethod' inside an instance context, continue
to work as before.

They have the type (Args...) -> Result; since the self value
has already been applied we don't hit the representational
issue.
2020-02-27 17:28:23 -05:00
Pavel Yaskevich
2dccdbfabf [ConstraintSystem] NFC: Remove workarounds related to (now deprecated) CSDiag 2020-02-18 10:13:09 -08:00
Pavel Yaskevich
2875aa8e2b [Diagnostics] Diagnose an attempt to init/use dictionary with array literal in CSGen
It's much easier to detect that contextual type is a dictionary
but expression is an array literal while generating constraints.
2020-02-06 09:32:07 -08:00
Pavel Yaskevich
217c343eb6 [Diagnostics] NFC: Fix name shadowing diagnostic comment to refer to max 2020-01-29 09:14:25 -08:00
Pavel Yaskevich
c9c20afe27 [Diagnostics] Port name shadowing diagnostics
Diagnose an attempt to reference a top-level name shadowed by
a local member e.g.

```swift
extension Sequence {
  func test() -> Int {
    return max(1, 2)
  }
}
```

Here `min` refers to a global function `min<T>(_: T, _: T)` in `Swift`
module and can only be accessed by adding `Swift.` to it, because `Sequence`
has a member named `min` which accepts a single argument.
2020-01-29 09:14:24 -08:00
Pavel Yaskevich
ec3b783380 [Diagnostics] Improve diagnostic for invalid conversion to AnyObject 2020-01-29 00:37:39 -08:00
Doug Gregor
133439dcbb [Constraint solver] Request contextual type information per expression.
When requesting information about the contextual type of a constraint
system, do so using a given expression rather than treating it like
the global state that it is.
2020-01-23 11:46:17 -08:00