Commit Graph

2448 Commits

Author SHA1 Message Date
Chris Willmore
02a6be6d01 Allow parsing of function types in expr position (#2273)
Previously it was not possible to parse expressions of the form

    [Int -> Int]()

because no Expr could represent the '->' token and be converted later
into a FunctionTypeRepr. This commit introduces ArrowExpr which exists
solely to be converted to FunctionTypeRepr later by simplifyTypeExpr.

https://bugs.swift.org/browse/SR-502
2016-04-22 21:53:26 -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
Jordan Rose
9cdbec13ee Merge pull request #1878 from jrose-apple/optional-pointers
Implements SE-0055: "Make unsafe pointer nullability explicit using Optional"
2016-04-12 09:49:44 -07:00
practicalswift
ba6c0f8daf [gardening] Fix code formatting. 2016-04-12 10:35:00 +02:00
practicalswift
f02cf29742 [gardening] Fix recently introduced typo: "specifiy" → "specify" 2016-04-12 10:31:38 +02: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
Xi Ge
0b50648263 [sema] Add a fixit for label-mismatch tuple patterns in for-each statement. rdar://25671800
In the following code example, compiler emits an error of "cannot express tuple conversion...". However,
this is trivially fixable by adding multiple labels in the tuple pattern of the for-each statement. This
commit adds such fixit.

func foo(array : [(some: Int, (key: Int, value: String))]) {
  for (i, (k, v)) in array {
  }
}
2016-04-11 19:09:59 -07:00
John McCall
8b65fb1ed4 Substitution can fail in some invalid cases.
Arguably, this code should not be run in invalid cases,
but that seems difficult to enforce given the numnber of paths
through the type-checker.  It's better to simply be more
conservative here.
2016-04-11 15:25:36 -07:00
John McCall
c0021e1c62 Only check the minimal set of generic requirements when opening
a generic function type during constraint solving, as opposed to
checking a bunch of implicit things that we already know.  This
should significantly improve the efficiency of checking uses of
generic APIs by reducing the total number of type variables and
constraints.

It is becoming increasingly funny to refer to this minimized generic
signature as the "mangling" signature.

The test changes are kind of a wash: in one case, we've eliminated
a confusing extra error, but in another we've caused the confusing
extra error to refer to '<<error type>>'.  Not worth fighting right
now.  The reference-dependencies change is due to not needing to
pull in all of those associated types anymore, which seems correct.
2016-04-11 14:53:29 -07:00
practicalswift
66183cdbf7 [gardening] Fix unjustified spacing 2016-04-07 10:10:24 +02:00
Slava Pestov
86d4c456f9 Sema: Assert that we don't try to do direct-to-storage access of properties in resilient types
Right now, the rule is that any access of a stored property from a
constructor or destructor through 'self' must go directly to storage,
skipping observers or getter/setter overrides.

This means that in particular, constructors of resilient types cannot
be @_transparent, or defined in an extension in another module.

Previously, this was only caught in IRGen when the @_transparent
function was inlined into a function from another module, which made
debugging difficult.

Now, we hit an assert in Sema in this case. Of course it should be a
diagnostic; we'll get there eventually.

We could partially lift the restriction, allowing convenience
initializers to be defined @_transparent or be added in extensions,
by having accesses of stored properties from convenience inits go
through accessors. This would be safe, because at that point, we must
already have performed a self.init() delegation, however it would
potentially break existing code in subtle ways.

Also, this patch marks the RangeIterator and Range types @_fixed_layout,
since they define @_transparent initializers and this tripped the
new assert. Furthermore, note that the @_transparent initializer
must be versioned because it is inlined into another versioned
@_transparent function.
2016-04-04 21:08:02 -07:00
practicalswift
b01c615e0d Merge pull request #2053 from practicalswift/spacing
[gardening] if ([space]…[space]) → if (…), for(…) → for (…), while(…) → while (…), [[space]x, y[space]] → [x, y]
2016-04-05 00:01:14 +02:00
Joe Pamer
73a8d2c04e Fix SR-1116, where the compiler will crash when diagnosing a failed
constraint system with synthesized member constraints.

(rdar://problem/25476306)
2016-04-04 14:30:19 -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
Michael Gottesman
7361e35bb9 Revert "Putting white spaces in between if/while clauses and braces." 2016-04-01 22:00:25 -07:00
Ge Sen
7dd61bdfa9 [gardening] Put white spaces in between if/while clauses and braces where it is missing.
For instance:

'if (foo){' => 'if (foo) {'
2016-04-02 08:22:23 +08:00
Chris Willmore
3bcb169f0b Import lightweight Objective-C generics as Swift generic type
parameters.
2016-03-28 09:50:30 -07:00
Doug Gregor
892181158f [Sema/SILGen] Bridge Objective-C objects to Swift value types via _ObjectiveCBridgeable.
Extend the use of
_ObjectiveCBridgeable._unconditionallyBridgeFromObjectiveC to all
bridged types rather than using the custom entry points. Note that
there is a lot of hackery around ensuring that the conformance is
correct, because Sema needs to anticipate that SILGen (or later SIL
passes) might need those conformances. This primarily affects the
overlays, but with generalized bridging that means any mixed
Objective-C/Swift framework with bridged types.
2016-03-15 17:45:38 -07:00
Doug Gregor
1d90b044fb Really reinstate "[SILGen] Implement NSString -> String bridging through _ObjectiveCBridgeable."
This reverts commit 052d2d0a69.

The only actual issue with the original change was a missing change to
the UIApplicationMain SILGen test, which needs to build SILGen
overlays to execute properly; -enable-source-import doesn't suffice.
2016-03-15 15:44:07 -07:00
Doug Gregor
052d2d0a69 Revert "Reinstate [SILGen] Implement NSString -> String bridging through _ObjectiveCBridgeable."
This reverts commit b25019c259. The
builders are failing in ways that are clearly related to my changes
but I'm unable to replicate locally.
2016-03-15 15:00:31 -07:00
Doug Gregor
b25019c259 Reinstate [SILGen] Implement NSString -> String bridging through _ObjectiveCBridgeable.
This reverts commit 01fe7e4848.
2016-03-15 11:38:06 -07:00
Doug Gregor
01fe7e4848 Revert "[SILGen] Implement NSString -> String bridging through _ObjectiveCBridgeable."
This reverts commit aa9cc23743.
2016-03-15 06:52:27 -07:00
Doug Gregor
d52530970e Merge pull request #1550 from kballard/use-declname-for-scope
[Parse] Fix lookup of foo(_:bar:) expressions
2016-03-14 11:36:04 -07:00
Doug Gregor
aa9cc23743 [SILGen] Implement NSString -> String bridging through _ObjectiveCBridgeable.
Introduce a new entrypoint to _ObjectiveCBridgeable,
_unconditionallyBridgeFromObjectiveC, which handles unconditional
bridging from an optional Objective-C object (e.g., an NSString) to
its bridged Swift type. Use it in SILGen to perform NSString -> String
bridging rather than the custom entry point.

Another small step toward generalized bridging.
2016-03-14 11:17:46 -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
fe9fe47b7e Implement support for generic typealiases. 2016-03-07 22:20:16 -08:00
Max Moiseev
7fe6916bf6 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-03-07 12:10:47 -08:00
Kevin Ballard
59df50960e [Sema] Improve diagnostics for #selector() expressions
When the `#selector()` expression refers to a parameter or variable, we
don't want to call it a property.

Fixes the rest of SR-880.
2016-03-05 00:31:11 -08: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
Slava Pestov
6812b6926b Sema: Cleanups and minor fixes for protocol 'Self' types
We had four duplicated implementations of checking how a protocol
requirement uses 'Self', all slightly wrong or incomplete:

- When deciding if the protocol type can be used as an existential.
  This one would just ignore 'Self' in the return type of a method
  completely, which was incorrect for cases where 'Self' is
  contravariant but part of the return value, for example:

  func foo() -> (Self -> ())

- When deciding if a member access can be performed on an existential
  value. This is distinct from the former, because the member may
  have been defined in a protocol extension, in which case it cannot
  be used even if the protocol type can be used as an existential.
  Unfortunately, this implementation was overly conservative, and
  would reject uses of 'Self' where Sema could in fact erase the
  existential type, for example:

  func foo() -> Self??
  func foo() -> Self.Type
  func foo() -> (Self, Self)

  This function handled function return types correctly, effectively
  plugging the leak in the previous code. It did lead to inconsistent
  behavior with protocols that had contravariant Self in requirements
  though; sometimes we would diagnose uses of the existential type,
  other times we would only complain about specific members.

- When deciding if a method in a non-final class can model a protocol
  requirement. This one was the most elaborate one, but here
  contravariance and uses of associated types are actually okay, so
  it was written to pick up covariant 'Self' only. However, it also
  did not handle metatypes and tuples.

- When opening the type of member of an existential, we would check
  if the return value was 'Self' or an optional of 'Self', but again
  this check was too conservative, so after the previous three were
  fixed, we could reference members on existentials that did not
  have a correct opened type.

Now, these have been combined into one check. To fix some crashes,
Sema's implementation of erasing existentials now relies on
coerceToType() instead of hand-rolling a few coercions of its own,
and wrapping the rest in CovariantFunctionConversionExpr, which
didn't make much sense if the result was not a function type.

SILGen still does not support function type conversions where an
existential return value is being erased; these would silently
miscompile before, but crash with an assertion now, because they
are correctly modeled as a FunctionConversionExpr, and not
CovariantFunctionConversionExpr.
2016-02-28 23:52:35 -08:00
Chris Lattner
0a8db5daf6 Fix <rdar://problem/23899799> Simple closure expression crashed SourceKit
which is a straight-forward logic bug that the static analyzer would have caught.
2016-02-27 22:41:00 -08:00
Andrew Trick
445cdff829 Merge pull request #1468 from atrick/attr-kind
Move enums into AttrKind.h.
2016-02-27 21:20:11 -08:00
Dmitri Gribenko
a9f8d97d3e Replace 'unsigned int' with 'unsigned'
'unsigned' is more idiomatic in LLVM style.
2016-02-27 16:20:27 -08:00
Andrew Trick
ff02652108 Move enums into AttrKind.h.
This reorganization allows adding attributes that refer to types.
I need this for a @_specialize attribute with a type list.

PrintOptions.h and other headers depend on these enums. But Attr.h
defines a lot of classes that almost never need to be included.
2016-02-26 21:10:22 -08:00
Max Moiseev
488b464f10 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-25 12:21:56 -08:00
Han Sangjin
7f4f3d2c70 Changed uint to unsigned int
The uint is not a standard C/C++ type. Some compiler environment may fail with it.
2016-02-25 14:09:03 +09:00
Dmitri Gribenko
8c3fbc51a4 stdlib: rename Optional and IUO intrinsics 2016-02-22 18:16:37 -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
37d05ea0dc Improved handling of mixed lvalues & rvalues in tuple exprs
My previous commit here didn’t work correctly for nested tuples, both
because it didn’t recurse into them to propagate access kind correctly
and because an outer TupleIndex overload (when indexing into the nested
tuple) could still be expecting an lvalue type.

This fix is much better. ConstraintSystem::resolveOverload now
correctly always expects rvalue types from rvalue tuples. And during
applyMemberRefExpr, if the overload expects an rvalue but the tuple
contains lvalues, coerceToType() correctly does any recursive munging
of the tuple expr required.
2016-02-15 20:59:53 -08:00
Max Moiseev
3a3984877a Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-15 15:43:34 -08:00
Chris Lattner
6552361c20 Remove 4 constraint solver fixits that were not pulling their weight anymore:
- 3 related to OptionSet migration for Swift 1 -> Swift 2.
- 1 related to calling a non-function type, which is now handled by CSDiags.
2016-02-12 21:12:19 -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
accffe5a11 now that argument relabeling fixits are handled by CSDiags, remove a bunch of
code from Fix and ConstraintSystem.  NFC.
2016-02-12 17:28:52 -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
Daniel Duan
efe230774b [AST] rename some isXXX methods to getAsXXX
There's a group of methods in `DeclContext` with names that start with *is*,
such as `isClassOrClassExtensionContext()`. These names suggests a boolean
return value, while the methods actually return a type declaration. This
patch replaces the *is* prefix with *getAs* to better reflect their interface.
2016-02-11 16:23:40 -08:00
Michael Ilseman
88060d580a [_BridgedNSError] Emit dynamic cast witness tables
_BridgedNSError conformances can affect the runtime behavior of
dynamic casts (e.g. 'is'). Unfortunately, the conformance is not
always emitted, in an effort to save space when not used. This change
forces the conformance witness tables to be emitted when we can detect
a dynamic cast to an _BridgedNSError conforming enum.

Test cases included, as well as a note about a potentially erroneous
path that is not currently handled: when the dynamic cast occurs in a
generic function to a generic type (and thus we are unsure which
conformances we need to pull in).
2016-02-08 15:18:48 -08:00
Max Moiseev
61c837209b Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-04 16:13:39 -08:00
Chris Lattner
94dd92fcb8 Fix compiler_crashers 22725 & 28236 by reworking parameter parsing error
recovery a bit.
2016-02-01 20:50:32 -08:00
Chris Lattner
d0a9479d46 Fix compiler_crashers/28246-swift-expr-propagatelvalueaccesskind.swift
Teach CSApply that InOutExpr around an UnresolvedType shouldn't propagate
lvalue access kinds.
2016-01-31 21:08:38 -08:00