Commit Graph

1177 Commits

Author SHA1 Message Date
John McCall
35ba809fd0 Don't diagnose failures to call symmetrically-typed binary operators
as a failure to convert the individual operand, since the operator
is likely conceptually generic in some way and the choice of any
specific overload is probably arbitrary.

Since we now fall back to a better-informed diagnostics point, take
advantage of this to generate a specialized diagnostic when trying to
compare values of function type with ===.

Fixes rdar://25666129.
2016-05-11 15:10:25 -07:00
Ted Kremenek
942e524285 Revert "SE-0036: Requiring Leading Dot Prefixes for Enum Instance Member Implementations" (#2477) 2016-05-11 11:02:37 -07:00
Alex Hoppen
0e8c69c56f [SE-0036]: Requiring Leading Dot Prefixes for Enum Instance Member Implementations 2016-05-07 22:39:26 +02:00
Jordan Rose
797260939e Handle argument labels in @available(renamed:"...")
Fixes SR-1008 / rdar://problem/25276961
2016-04-28 20:21:30 -07:00
Jordan Rose
ec59bf9f18 Move helper function 'diagnoseArgumentLabelError' to MiscDiagnostics.
Groundwork for SR-1008. No functionality change.
2016-04-28 20:21:30 -07:00
Ted Kremenek
b8bbed8c13 [WIP] Implement SE-0039 (Modernizing Playground Literals) (#2215)
* Implement the majority of parsing support for SE-0039.

* Parse old object literals names using new syntax and provide FixIt.

For example, parse "#Image(imageLiteral:...)" and provide a FixIt to
change it to "#imageLiteral(resourceName:...)".  Now we see something like:

test.swift:4:9: error: '#Image' has been renamed to '#imageLiteral
var y = #Image(imageLiteral: "image.jpg")
        ^~~~~~ ~~~~~~~~~~~~
        #imageLiteral resourceName

Handling the old syntax, and providing a FixIt for that, will be handled in a separate
commit.

Needs tests.  Will be provided in later commit once full parsing support is done.

* Add back pieces of syntax map for object literals.

* Add parsing support for old object literal syntax.

... and provide fixits to new syntax.

Full tests to come in later commit.

* Improve parsing of invalid object literals with old syntax.

* Do not include bracket in code completion results.

* Remove defunct code in SyntaxModel.

* Add tests for migration fixits.

* Add literals to code completion overload tests.

@akyrtzi told me this should be fine.

* Clean up response tests not to include full paths.

* Further adjust offsets.

* Mark initializer for _ColorLiteralConvertible in UIKit as @nonobjc.

* Put attribute in the correct place.
2016-04-25 07:19:26 -07:00
Jordan Rose
405bfdc33b [Sema] Add a fix-it for non-optional bindings initialized with nil.
<stdin>:1:16: error: nil cannot initialize specified type 'Int'
var foo: Int = nil
               ^
<stdin>:1:10: note: add '?' to form the optional type 'Int?'
var foo: Int = nil
         ^
            ?
2016-04-21 13:36:54 -07:00
Jordan Rose
16f857c78c [Sema] Use a TypeLoc for conversions with contextual types.
With a TypeLoc, we have a chance to offer diagnostics or even fix-its
to the contextual type, even though it's not represented by an
expression in the constraint system. This commit mostly just passes it
through, without attempting to use it anywhere or even pass a real
TypeLoc (with a valid TypeRepr).

(It does drop the contextual type parameter from
typeCheckExpressionShallow, since there were zero callers using it.)

No functionality change...yet.
2016-04-21 13:36:53 -07:00
Chris Lattner
8e12008d2b Mark tuple splat and ++/-- as errors instead of warnings. This
wraps up SE-0004 and SE-0029.

I consider the diagnostic changes in Constraints/lvalues.swift to be
indicative of a QoI regression, but I'll deal with that separately.
2016-04-16 23:44:22 -07:00
Chris Lattner
228bee7689 Rearrange how CalleeCandidateInfo processes candidates in its list, to
make sure that properties and subscripts have a self type in their tracked
type.  This makes SubscriptExpr processing more logical (given that it applies
both self and an index list), by putting hacks in more principled places.

It would be great to someday eliminate SubscriptExpr entirely, but this isn't
going to happen in the short term.  NFC.
2016-04-13 22:55:55 -07:00
Jordan Rose
bc83940301 Make pointer nullability explicit using Optional.
Implements SE-0055: https://github.com/apple/swift-evolution/blob/master/proposals/0055-optional-unsafe-pointers.md

- Add NULL as an extra inhabitant of Builtin.RawPointer (currently
  hardcoded to 0 rather than being target-dependent).
- Import non-object pointers as Optional/IUO when nullable/null_unspecified
  (like everything else).
- Change the type checker's *-to-pointer conversions to handle a layer of
  optional.
- Use 'AutoreleasingUnsafeMutablePointer<NSError?>?' as the type of error
  parameters exported to Objective-C.
- Drop NilLiteralConvertible conformance for all pointer types.
- Update the standard library and then all the tests.

I've decided to leave this commit only updating existing tests; any new
tests will come in the following commits. (That may mean some additional
implementation work to follow.)

The other major piece that's missing here is migration. I'm hoping we get
a lot of that with Swift 1.1's work for optional object references, but
I still need to investigate.
2016-04-11 20:06:38 -07:00
Josef Willsher
de18967992 [Sema] Improved error message for static functions on existential metatypes (#2127)
Type level lookups can fail because the lookup is on an existential
metatype, like `MyProtocol.staticMethod(_:)` is invalid; however the
error message is unclear: “static member 'staticMethod(_:)' cannot be
used on instance of type ‘MyProtocol.Protocol’”.

This fix checks the base of member lookups that failed with the reason
UR_TypeMemberOnInstance for being existential metatypes. It produces
the clearer message “static member ‘staticMethod(_:)’ cannot be used on
protocol metatype ‘MyProtocol.Protocol’”. This change makes it clear
that the use of a static member on the *existential* metatype is the
problem.
2016-04-10 19:03:20 -07:00
practicalswift
abfecfde17 [gardening] if ([space]…[space]) → if (…), for(…) → for (…), while(…) → while (…), [[space]x, y[space]] → [x, y] 2016-04-04 16:22:11 +02:00
Chris Lattner
514ec31114 Progress towards SR-1069:
- Fix ExprTypeSaverAndEraser to save & restore the invalid bit on closure parameter decls.
 - Teach CalleeCandidateInfo::evaluateCloseness to not try to find generic subs on types that
   contain a unresolved type within them.

With these changes, the compiler doesn't segfault on the testcase when assertions are
enabled.  It still doesn't produce a great diagnostic though.
2016-03-29 22:33:07 -07:00
Chris Lattner
59425c086d fix <rdar://problem/21523291> compiler error message for mutating immutable field is incorrect 2016-03-22 16:57:09 -07:00
Chris Lattner
a12674a571 When reporting a type error relating to the result type of an overload
set where all members of the set produce the same type, produce a more
specific error.

Before:

t.swift:4:17: error: no '&&' candidates produce the expected contextual result type 'Int'
  return a == b && 1 == 2
                ^
t.swift:4:17: note: produces result of type 'Bool'
  return a == b && 1 == 2
                ^

after:

t.swift:4:17: error: '&&' produces 'Bool', not the expected contextual result type 'Int'
  return a == b && 1 == 2
                ^

This improves the situation reported in https://twitter.com/_jlfischer/status/712337382175952896
2016-03-22 15:45:52 -07:00
Chris Lattner
e1ad6d80ee fix 28270-swift-constraints-constraintsystem-diagnosefailureforexpr.swift 2016-03-14 11:56:32 -07:00
Max Moiseev
1fae0d1325 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-08 12:48:48 -08:00
Chris Lattner
aacdf62e8b fix some whitespace issues noticed by Slava. 2016-03-07 23:11:45 -08:00
Chris Lattner
14b5b0a73f Resolve the QoI fixme's from the generic typealiases testcase, by specifying the
correct location for generic parameters that cannot be inferred.  The cases that
I thought were incorrectly rejected on first glance were actually correct.
2016-03-07 23:04:38 -08:00
Max Moiseev
7fe6916bf6 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-07 12:10:47 -08:00
Greg Titus
f6c59815ca Merge pull request #1504 from gregomni/sr-839
[SR-839][Sema] Better fixits for optional expressions
2016-03-04 18:16:36 -08:00
Max Moiseev
cf4bafe9e3 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-03 13:22:03 -08:00
Janek Spaderna
15f128bf4f [Sema] Don't crash on mutating getters on constants
With this change the base expression of the subscript (beeing already
revalidated earlier in the function) is passed to diagnoseUnviableLookupResults
to emit a fitting diagnostic.
2016-03-03 00:09:32 +01:00
gregomni
098f8e0ebf [SR-839][Sema] Better fixits for optional expressions
In member ref expressions, if the base is optional, and the expected
expression result is either optional or unknown, suggest a fixit that
makes it into an optional chain expr rather than force unwrapping.

Since in many cases the actual fixit is emitted during diagnosis, and
thus, while type checking sub exprs with no contextual type specified
(so nothing to check for preferring optionality), we also need an
additional flag to pass down from FailureDiagnosis for whether we
prefer to fix as force unwrapping or optional chaining.

I attempted to do this same job via providing a convert type but
setting the ConvertTypeIsOnlyAHint flag on the type checker, but
unfortunately there are a lot of other moving parts that look at that
type, even if it is only supposed to be a hint, so an additional flag
to the CS ended up being cleaner.
2016-03-01 22:57:24 -08:00
Max Moiseev
a49dab6bf8 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-29 12:08:52 -08:00
Chris Lattner
19b6ab8408 fix <rdar://problem/24251022> Swift 2: Bad Diagnostic Message When Adding Different Integer Types
Previously we would produce:

t.swift:3:3: error: binary operator '+=' cannot be applied to operands of type 'Int' and '_'
a += a + b
~ ^  ~~~~~

with a candidate set to follow.  Now we properly match up the inout/lvalue type and produce
the following more specific diagnostic:

t.swift:3:10: error: cannot convert value of type 'UInt32' to expected argument type 'Int'
a += a + b
         ^

pointing the the "b".
2016-02-28 20:52:33 -08:00
Dmitri Gribenko
dd75aed67a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-17 14:40:05 -08:00
gregomni
f8dbb20a19 [Sema] Extend callee diagnosis to multiple generics
Straightforward extension of the previous work here to handle callees
with multiple generic parameters. Same type requirements are already
handled by the ArchetypeBuilder, and protocol conformance is handled
explicitly. This is still bailing on nested archetypes.

Pre-existing tests updated that now give better diagnoses.
2016-02-15 15:52:24 -08:00
Max Moiseev
3a3984877a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-15 15:43:34 -08:00
gregomni
10370a5c0f Extend callee diagnoses to non-conforming complex args including generics.
Previously, type checking arguments worked fine if the entire arg was
UnresolvedType, but if the type just contained UnresolvedType, the
constraint system always failed via explicitly constraining to
unresolved.

Now in TypeCheckConstraints, if the solution allows for free variables
that are UnresolvedType, then also convert any incoming UnresolvedTypes
into variables. At worst, in the solution these just get converted back
into the same Unresolved that they started with.

This change allows for incorrect tuple/function type possibilities to
make it back out to CSDiag, where they can be more precisely diagnosed
with callee info. The rest of the changes are to correctly figure
out the failure info when evaluating more types of Types.

New diagnosis for a partial part of an arg type not confroming. Tests
added for that. Expected errors changed in several places where we
now get real types in the diagnosis instead of '(_)' unresolved.
2016-02-14 11:32:14 -08:00
Chris Lattner
b2fabdadcc move diagnoseArgumentLabelError to CSDiags.cpp now that it is the only client. 2016-02-12 17:33:22 -08:00
Chris Lattner
cf2e7f6f45 Fix SR-718: Type mismatch reported as extraneous parameter
The issue here is that the constraint solver was deciding on
FixKind::RelabelCallTuple as the fix for the problem and emitting the
diagnostic, even though there were two different fixes possible.

CSDiags has the infrastructure to support doing doing the right thing
here, but is only being used for ApplyExprs, not SubscriptExprs.

The solution is to fix both problems: remove FixKind::RelabelCallTuple,
to let CSDiags handle the problem, and enhance CSDiags to treat
SubscriptExpr more commonly with ApplyExpr.  This improves several cases
where the solver was picking one solution randomly and suggesting that
as a fix, instead of listing that there are multiple different solutions.
2016-02-12 17:19:54 -08:00
Robin Kunde
90b6f0e2a8 added improved diagnostic notes in case of missing binary operator overload for enums with associated values 2016-02-10 09:03:10 -05:00
Chris Lattner
836b88e320 refactor some code into a helper method, NFC. 2016-02-08 10:06:03 -08:00
Doug Gregor
ff8e91399b Merge pull request #1200 from gregomni/generic-func-args
Replace use of getArchetype() in diagnoseGenericParameterErrors()
2016-02-05 13:58:57 -08:00
Max Moiseev
61c837209b Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-04 16:13:39 -08:00
gregomni
5ba28eda3d Replace use of getArchetype() in diagnoseGenericParameterErrors()
This is a quick follow-up to
<https://github.com/apple/swift/pull/1160>, to replace the becoming
deprecated getArchetype() with doing the same thing via calling through
to ArchetypeBuilder with a declContext.

Uses findGenericSubstitutions() to do so. So this site could take
advantage of destructuring of more complex params containing generics.
Right now, though, that never happens. For complex params the
(unfortunately, worse) diagnosis happens in diagnoseFailureForExpr() on
the argument expression before reaching here. I’d like to improve that
in future work.
2016-02-04 11:56:13 -08:00
Slava Pestov
99b0e77d35 Merge pull request #1160 from gregomni/generic-func-args
[Sema] Extend callee diagnosis to complex args including generics, e.g. (Void) -> T
2016-02-03 22:55:15 -08:00
gregomni
6b30695869 Extend callee diagnosis to complex args including generics, e.g. (Void) -> T
Correctly determine callee closeness for func/ops that include generics
as part of more complicated parameters, i.e. tuple or closure args
containing generics as elements or args/results. Still only handling
single archetypes.

Also added code to check generic substitutions already made in the callee
parameters, which further helps diagnosis.
2016-02-03 22:54:19 -08:00
practicalswift
10c31294d4 [gardening] Substitute "substitions" with "substitutions". 2016-02-02 10:19:33 +01:00
Chris Lattner
8cc0ab170a fix a potential problem that Jordan noticed by inspection, NFC on the testsuite. 2016-02-01 21:02:00 -08:00
Chris Lattner
6173170c33 fix SR-650: REGRESSION: Assertion failed
When we have a contextual type of Optional<SomeNominal>, we get overload
lookup results indicating that the found member needs to look through the
optional.  Do so!
2016-02-01 13:22:45 -08:00
Chris Lattner
a63be90828 fix compiler_crashers/28219-swift-lvaluetype-get.swift
Not all types are l-valuable, notably InoutType's.  This seems like a
weird restriction to put in the type checker, but it is the cleanest
solution to this.  The better solution would be to change how
inoutexpr/inouttype are represented completely... maybe someday.
2016-01-29 23:18:33 -08:00
Chris Lattner
ff34db7950 fix a few compiler crashers
These were due to CSDiags not reverting the semantic expr embedded in CollectionExprs
when retypechecking them.
2016-01-29 21:13:57 -08:00
saisi
535d400dc6 Fixed niggling typos 2016-01-29 23:16:25 -05:00
gregomni
fc3a264ea9 Improve diagnoses on args to generic members
In both figuring out candidate closeness and in diagnosing generic
parameter errors, if the parameter is a GenericTypeParamType, get its
decl’s archetype to perform archetype substitutability checking upon.
2016-01-28 10:23:14 -08:00
gregomni
e811a64af3 Further improvement of diagnoses on args to generic functions
If the mismatched argument is on an archetype param, check to see
whether the argument conforms to all of the protocols on the archetype,
using a specific does-not-conform diagnosis if one or more protocols
fail.

Also added another closeness class
`CC_GenericNonsubstitutableMismatch`, which happens when more than one
argument is a mismatch, but all the failing arguments are of the same
type and mismatch only because of substitutability. This closeness is
farther away than normal `CC_ArgumentMismatch` so that if we note
expected matches, we’ll prefer non-generic matches. But if this is the
result, we can still produce the specific conforms-to-protocol
diagnosis (since, in a sense, it’s only one type of argument that is
wrong even though it is multiple arguments).
2016-01-26 20:07:20 -08:00
Chris Lattner
b5694e941e fix 28241-swift-valuedecl-isaccessiblefrom.swift 2016-01-25 22:06:14 -08:00
Doug Gregor
8336419844 Include completion source location information compound DeclNames.
When one spells a compound declaration name in the source (e.g.,
insertSubview(_:aboveSubview:), keep track of the locations of the
base name, parentheses, and argument labels.
2016-01-25 14:13:13 -08:00