Commit Graph

9116 Commits

Author SHA1 Message Date
Slava Pestov
06bd16c071 Sema: Fix another SE-0110 issue
The formal type of methods should be (Type) -> (Args...) -> (),
not Type -> (Args...) -> ().

This only matters in Swift 4 mode, where it was preventing the
newly-added test case from type checking.

Fixes <rdar://problem/31725325>.
2017-05-09 22:27:52 -07:00
Brent Royal-Gordon
1795471746 Use type map call instead of setting type directly 2017-05-09 20:18:05 -07:00
Brent Royal-Gordon
3084d09a01 Ignore missing types on for statement expressions
A malformed initializer or increment expression would come out of TypeChecker::typeCheckExpression() with no type, which `checkIgnoredExpr()` was not expecting.

Fixes:

* test/Parse/foreach.swift
* test/Parse/recovery.swift
* test/Parse/toplevel_library_invalid.swift
* test/IDE/complete_stmt_controlling_expr.swift
* validation-test/compiler_crashers_fixed/01736-void.swift
* validation-test/compiler_crashers_fixed/00587-swift-pattern-foreachvariable.swift
2017-05-09 20:18:05 -07:00
Brent Royal-Gordon
a3bda7567e Mark failed initializers as errors
When the initialization expression on a variable declaration fails, give it an ErrorType so Swift does not redundantly diagnose its un-inferrable type.

Fixes:

* test/NameBinding/stdlib.swift
* test/decl/typealias/protocol.swift

Would also have redundantly fixed the same tests as 2e875e7e, but the targeted solution there is cleaner.
2017-05-09 20:18:04 -07:00
Brent Royal-Gordon
252fa1b1fd Make ErrorType DeclRefExprs for ErrorType Decls
This prevents one invalid declaration from causing cascades of subsequent errors.

Fixes:
* test/NameBinding/scope_map_lookup.swift
* test/decl/var/variables.swift
2017-05-09 20:18:04 -07:00
Brent Royal-Gordon
9e50c5f63d Allow null types in switch typechecking
As well as anywhere else we `coerceToMaterializable`.

Fixes:

* test/IDE/complete_crashes.swift
* test/IDE/complete_enum_elements.swift
* test/IDE/complete_pattern.swift
* test/IDE/complete_unresolved_members.swift
* test/IDE/complete_value_expr.swift
* test/Parse/implicit_getter_incomplete.swift
* test/Parse/invalid.swift
* test/Parse/switch.swift
* test/SourceKit/Indexing/index_enum_case.swift
2017-05-09 20:18:04 -07:00
Brent Royal-Gordon
5f75ac4e28 Adjust capture typechecking to allow null type
Fixes crash in test/ClangImporter/objc_bridging_generics.swift.
2017-05-09 20:14:50 -07:00
Slava Pestov
a08c1a0655 Sema: Don't construct naked ErrorTypes in valid code
It is useful to set a breakpoint on ErrorType::get(ASTContext)
to see what is going wrong immediately instead of after the
fact when an ErrorType pops up where you don't expect it.

Unfortunately associated type inference, domain shrinking and
the type cleanup RAII utility would all build ErrorTypes even
with valid code.

Refactor things a bit so that this is no longer the case; at
least now the standard library and overlays build with
'assert(false)' inserted into ErrorType::get(ASTContext).

ErrorType::get(Type) is still expected to come up in associated
type inference since it is used as a signal while sorting
through potential type witness candidates.
2017-05-09 20:14:50 -07:00
swift-ci
c352f98ae9 Merge pull request #9428 from practicalswift/gardening-20170509b 2017-05-09 15:01:08 -07:00
practicalswift
8c40c65c80 [gardening] Fix typos. 2017-05-09 21:50:04 +02:00
Mark Lacey
08c1c988a7 Better fix for handling Equal constraints in matchTypes.
The fix committed in 15fb957f09 still
allows for the possibility that we can bind one type as part of
processing the Equal constraint and then later come along and attempt to
bind the LValue version of that type as part of processing another
constraint.

I don't have a test case for this as it was discovered by thought
process, not testing, and constructing a test case isn't really feasible
because it relies on a lot of specifics about the order in which things
happen to be processed the constraint solver.
2017-05-09 12:49:31 -07:00
practicalswift
089ea58fcb [gardening] Fix dupe word typo. 2017-05-09 20:45:10 +02:00
practicalswift
437a186032 [gardening] Remove redundant repetition of type names (DRY): RepeatedTypeName foo = dyn_cast<RepeatedTypeName>(bar) 2017-05-09 11:26:07 +02:00
swift-ci
6f0ddedfaf Merge pull request #9219 from xedin/closure-arg-diag 2017-05-08 23:58:36 -07:00
Pavel Yaskevich
ddaccf88bb [QoI] Properly diagnose closure parameter distructuring after SE-0110
Swift 3 supported limited argument destructuring when it comes to
declaring (trailing) closures. Such behavior has been changed by
SE-0110. This patch aims to provide better error message as well
as fix-it (if structure of the expected and actual arguments matches)
to make the migration easier and disambiguate some of the common
mistakes.

Resolves: SR-4738, SR-4745, rdar://problem/31892961.
2017-05-08 23:04:37 -07:00
Mark Lacey
15fb957f09 Fix some LValue type checking issues exposed by key paths.
We had an inconsistency in the handling of ConstraintKind::Equal in that
we would take
  $T1 Equal $T2
where $T2 was previously bound to a type, and bind the RValue type of
$T2's type to $T1.  That does not allow for us to later attempt to bind
the LValue type of that type to $T1 (as might happen in simplifying an
OptionalObject constraint).

Instead, if $T1 can be bound to an LValue and $T2 is not an LValue,
we'll defer simplifying the Equal constraint until after $T1 is bound
through some other type variable binding or constraint simplification.

Fixes rdar://problem/31724272.
2017-05-08 18:07:49 -07:00
Jordan Rose
03b38534a6 Make sure to inherit 'dynamic' when inheriting initializers. (#9400)
Uncovered by Slava's bcbd1d2, which infers 'dynamic' in more places,
but this was always a problem when an initializer was /explicitly/
marked 'dynamic'.

rdar://problem/32026930
2017-05-08 17:38:14 -07:00
Jordan Rose
01cb554387 Re-apply "Make all CF types Equatable and Hashable." (#4568)
Like NSObject, CFType has primitive operations CFEqual and CFHash,
so Swift should allow those types to show up in Hashable positions
(like dictionaries). The most general way to do this was to
introduce a new protocol, _CFObject, and then have the importer
automatically make all CF types conform to it.

This did require one additional change: the == implementation that
calls through to CFEqual is in a new CoreFoundation overlay, but the
conformance is in the underlying Clang module. Therefore, operator
lookup for conformances has been changed to look in the overlay for
an imported declaration (if there is one).

This re-applies 361ab62454, reverted in
f50b1e73dc, after a /very/ long interval
where we decided if it was worth breaking people who've added these
conformances on their own. Since the workaround isn't too difficult---
use `#if swift(>=3.2)` to guard the extension introducing the
conformance---it was deemed acceptable.

https://bugs.swift.org/browse/SR-2388
2017-05-08 14:05:11 -07:00
Pavel Yaskevich
c72b402afa [QoI] Improve diagnostics for generic subscripts
While diagnosing index expression associated with subscript call
`validateContextualType` didn't look through TupleType to identify
potential nullability of the contextual type related to generic
parameters.

Resolves: rdar://problem/31724211
2017-05-08 02:41:02 -07:00
Slava Pestov
8a1062dedd Sema: Try harder to preserve ParenType sugar when when performing upcasts
This fixes a case where we would allow a function conversion
in Swift 3 mode, but not in Swift 4. We were incorrectly stripping
off ParenTypes, which was OK in Swift 3, but with the implementation
of SE-0110 this resulted in a type mismatch.

Fixes <rdar://problem/31969605>.
2017-05-07 22:03:17 -07:00
Pavel Yaskevich
3117e50f68 Merge pull request #9376 from xedin/sr-4692
[QoI] Improve diagnostics for calling instance methods on type or in static context
2017-05-07 16:42:05 -07:00
Slava Pestov
d0105f5526 Sema: Fix incorrect 'static' operator check
We were not enforcing that operators were static if
the operator was defined in a final class, or if it
was defined in a non-final class but the operator was
itself final.

Fixes <rdar://problem/31469036>.
2017-05-07 03:10:45 -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
Pavel Yaskevich
5adeff065c [QoI] Improve diagnostics for calling instance methods on type or in static context.
Previously situations like `self.foo(...)` wouldn't be considered as viable
for diagnosing the instance method on type calls, because the base wasn't
TypeExpr, which only accounts for e.g. `X.foo`, instead of validating base
expression itself this patch checks if the _type_ of base expression is
Metatype which is less restrictive.

Resolves: SR-4692.
2017-05-07 01:29:50 -07:00
Robin Kunde
6d63d90e0e SR-331: Diagnostic notes and fixits for unicode confusables (#9070) 2017-05-06 17:40:35 -04:00
Slava Pestov
427bb321a4 Sema: Fix crash when diagnosing invalid assignment expression
CSGen can see an AssignExpr where the destination has an
unresolved type, even in a case where no diagnostic has been
emitted yet.

It is then wrong to return an empty type from here, because
this stops us from attempting to solve the expression, which
results in no diagnostic being emitted in the end.

The test case I added produces a decent diagnostic now,
but the original one in the radar now just says 'ambiguous
without more context'. Still, better than crashing.

Fixes <rdar://problem/30685195>.
2017-05-05 23:54:23 -07:00
Slava Pestov
dd7b62c0d8 Sema: Fix crash with ambiguous overload inside a trailing closure
Any ambiguity inside of a call with a trailing closure
call was going down the code path that would tell you to
add an argument label... but the ambiguity might not be
with the call that has the trailing closure itself, and
instead something inside.
2017-05-05 23:54:22 -07:00
Michael Gottesman
86620aaa7e Merge pull request #9318 from practicalswift/redundant-types-in-casts
[gardening] Remove redundant repetition of type names (DRY): RepeatedTypeName foo = dyn_cast<RepeatedTypeName>(bar)
2017-05-05 13:37:12 -07:00
practicalswift
fff77b01c7 [gardening] Remove unused variables 2017-05-05 17:03:02 +02:00
practicalswift
492f5cd35a [gardening] Remove redundant repetition of type names (DRY): RepeatedTypeName foo = dyn_cast<RepeatedTypeName>(bar)
Replace `NameOfType foo = dyn_cast<NameOfType>(bar)` with DRY version `auto foo = dyn_cast<NameOfType>(bar)`.

The DRY auto version is by far the dominant form already used in the repo, so this PR merely brings the exceptional cases (redundant repetition form) in line with the dominant form (auto form).

See the [C++ Core Guidelines](https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es11-use-auto-to-avoid-redundant-repetition-of-type-names) for a general discussion on why to use `auto` to avoid redundant repetition of type names.
2017-05-05 09:45:53 +02:00
Slava Pestov
bcbd1d28fe Sema: Infer 'dynamic' for overrides of imported methods
Fixes <rdar://problem/31104529>.
2017-05-04 23:54:40 -07:00
Jordan Rose
6de9b1e35d Merge pull request #9309 from jrose-apple/ast-vtables
Lift the decision of whether a method needs a vtable slot up to AST.
2017-05-04 20:12:07 -07:00
Robert Widmann
0a54806d30 Merge pull request #9147 from CodaFi/mutatis-mutandis
Refine parameter type 'inout' diagnostics
2017-05-04 22:20:33 -04:00
Jordan Rose
6c098033de Lift the decision of whether a method needs a vtable slot up to AST.
This lets us serialize that decision, which means we can conceivably
/change/ the decision in later versions of the compiler without
breaking existing code. More immediately, it's groundwork that will
eventually allow us to drop decls from the AST without affecting
vtable layout.

This isn't actually a great answer; what we really want is for SIL
vtables to be serialized consistently and treated as the point of
truth. But that would be more change than we're comfortable taking in
the Swift 4 timeframe.

First part of rdar://problem/31878396.
2017-05-04 17:49:47 -07:00
Jordan Rose
c333b0b071 [AST] Generalize TypeBase::canOverride to TypeBase::matches.
This allows fuzzy matching of top-level optionals in types, using the
same logic we use to fuzzy-match potential overrides.
2017-05-04 17:48:48 -07:00
David Farler
be86bcac1d [Migrator] Suggest String <-> Substring conversions
Some APIs that expected a String now expect a Substring and vice
versa. To ease the transition, emit fix-its on conversion errors
between these types that the migrator can pick up.

When converting from Substring -> String, suggest wrapping in
`String.init`.

When converting from String -> Substring, suggest appending the
void subscript `[]`. (This isn't implemented yet so this is
hidden behind a flag).

This can possibly be generalized later when converting between
some sequence and its subsequence, such as Array and ArraySlice,
for example.

rdar://problem/31665649
rdar://problem/31666638
2017-05-04 16:40:56 -07:00
Robert Widmann
102095433e Refine parameter type attribute diagnostics
- Subscripts parameter lists may not contain inout arguments, but we
were rejecting this at the call site.  Teach the type checker to reject
them during type resolution instead.

- We assumed a syntactic check for inout/var parameters would suffice
given that a parameter unified to an InoutType.  However, closures
passed to function parameters with inout parameters in their parameter
lists can also cause this case to appear, and we would emit a
SourceLoc-less diagnostic.  Instead, do not attempt this recovery path
if the user did not actually write ‘var’ or ‘inout’ on the parameter
type.
2017-05-04 14:24:32 -04:00
swift-ci
476d84e313 Merge pull request #9286 from DougGregor/swift-3-anyobject-compat 2017-05-04 11:05:13 -07:00
Doug Gregor
ef7b8039ec [Type checker] Warn about classes conforming to AnyObject in Swift 3 mode.
Swift 3 allowed a class to explicitly conform to AnyObject, although
it was meaningless. Recent AnyObject-related changes started rejecting
such conformances as ill-formed; allow them with a warning + Fix-It in
Swift 3 compatibility mode.
2017-05-04 10:28:27 -07:00
Doug Gregor
6c8dd3959f [AST] Dodge an annoying libc++ std::function quirk requiring complete result types 2017-05-04 08:51:47 -07:00
Doug Gregor
3dbfa9789f [Associated type inference] Check solutions against full requirement signature
When we have a potential assignment of associated types to type
witnesses during associated type inference, check that set of type
witnesses against the requirements in the requirement signature, so
that we can reject any solutions that fail some of the protocol's
requirements.

This is most of rdar://problem/31830524 --- but gets thwarted by the
inability of associated type inference to work across multiple
protocols.
2017-05-04 00:21:52 -07:00
Doug Gregor
204f6f2317 Generalize TypeChecker::checkGenericArguments().
Make this routine suppress diagnostics when there is no
source-location information (which allows it to be used as a query)
and to distinguish the "we couldn't substitute into a requirement"
failure mode from the "a requirement actually failed" failure mode.
2017-05-03 22:35:58 -07:00
Doug Gregor
66f38da9a3 Merge pull request #9206 from DougGregor/NSKeyedArchiveLegacy
Warn about NSCoding classes with unstable mangled names
2017-05-03 08:20:23 -07:00
Doug Gregor
c805da7291 Improve/clarify the inference of @_staticInitializeObjCMetadata.
Infer @_staticInitializeObjCMetadata in those cases where we need a
static initializer to make an NSCoding-conforming class visible to the
Objective-C runtime. This does *not* include classes with one of the
@NSKeyedArchive attributes:

* @NSKeyedArchiveLegacy implies that we'll register the class
  directly, with the necessary side effect of initialize Objective-C
  metadata.
* @NSKeyedArchiveSubclassesOnly promises not to archive the class
  directly anyway.
2017-05-02 23:30:28 -07:00
Doug Gregor
bf7a8612e6 Add @NSKeyedArchiveSubclassesOnly to suppress NSCoding unstable name diags.
Introduce the @NSKeyedArchiveSubclassesOnly attribute, which can be
placed on a class that conforms to NSCoding to suppress the
unstable-name diagnostics by promising to only archive
subclasses---not this class directly.
2017-05-02 23:30:28 -07:00
Doug Gregor
9b65c85b44 Make diagnostics for NSCoding classes with unstable names more discouraging.
The diagnostic regarding NSCoding classes with unstable names can be
suppressed by adding @objc (the preferred solution for new code) or
@NSKeyedArchiveLegacy (for existing archives). Provide those as
Fix-Its, in that order.

(Thanks, Jordan!)
2017-05-02 23:30:28 -07:00
Doug Gregor
e6c6470f1e Properly warn about local classes that conform to NSCoding. 2017-05-02 23:30:28 -07:00
Doug Gregor
bafa99cd6e Add the @_staticInitializeObjCMetadata attribute.
Currently inactive, this attribute indicates that a static initializer should be emitted to register the Objective-C metadata when the image is loaded, rather than on first use of the Objective-C metadata. Infer this attribute for NSCoding classes that won’t have static Objective-C metadata or have an @NSKeyedArchiveLegacy attributed.
2017-05-02 23:30:27 -07:00
Doug Gregor
b912c7f7e1 Diagnose attempts to apply @NSKeyedArchiveLegacy to a generic class.
Generic classes don't have a single name to register, so disallow
@NSKeyedArchiveLegacy.
2017-05-02 22:38:32 -07:00