Commit Graph

1575 Commits

Author SHA1 Message Date
Slava Pestov
6f6974654a Sema: Fix type safety hole with convenience initializers which delegate to protocol extension initializers
A protocol extension initializer creates a new instance of the
static type of Self at the call site.

However a convenience initializer in a class is expected to
initialize an instance of the dynamic type of the 'self' value,
because convenience initializers can be inherited by subclasses.

This means that when a convenience initializer delegates to a
protocol extension initializer, we have to substitute the
'Self' type in the protocol extension generic signature with
the DynamicSelfType, and not the static type.

Since the substitution is formed from the type of the 'self'
parameter in the class convenience initializer, the solution is
to change the type of 'self' in a class convenience initializer
to DynamicSelfType, just like we do for methods that return
'Self'.

This fixes cases where we allowed code to type check that
should not type check (if the protocol extension initializer
has 'Self' in contravariant position, and we pass in an
instance of the static type).

It also fixes a miscompile with valid code -- if the protocol
extension initializer was implemented by calling 'Self()',
it would again use the static type and not the dynamic type.

Note that the SILGen change is necessary because Sema now creates
CovariantReturnExprs that convert a static class type to
DynamicSelfType, but the latter lowers to the former at the
SIL level, so we have to peephole away unnecessary unchecked_ref_cast
instructions in this case.

Because this change breaks source compatibility, it is guarded
by a '-swift-version 5' check.
2017-08-26 01:18:35 -07:00
Slava Pestov
8ca08e8d48 SILGen: Clean up RValueEmitter::visitRebindSelfInConstructorExpr() a bit
NFC for now, but becomes important once a subsequent patch gives
'self' a DynamicSelfType in convenience initializers.
2017-08-25 23:50:52 -07:00
Slava Pestov
b3c2f1f79a SILGen: Remove unnecessary optional-to-optional conversion in CovariantReturnConversionExpr
The UncheckedRefCast instruction supports optional-to-optional
casts natively, so we can avoid the unnecessary conditionals
here.
2017-08-25 23:50:52 -07:00
Slava Pestov
e4d26a2eb2 SILGen: Use the ManagedValue form of createUncheckedRefCast() where possible 2017-08-25 23:50:52 -07:00
Michael Gottesman
52edcd18d9 [silgen] Eliminate an unnecessary static function emitArrayToPointer.
We already have a method on SILGenFunction with the exact signature that just
calls this private function. All other uses can be re-routed to the method on
SILGenFunction, allowing us to simplify the code.
2017-08-18 12:30:52 -07:00
Slava Pestov
27fa5d3e1a SILGen: Fix bug with local function default arguments
If a local function is in generic context but does not capture
any generic parameters, it has a non-generic lowered type.

However we use substitutions from the AST when forming calls to
generic argument generators. In the AST, such local functions
always have generic types regardless of their captures.

As a result we would crash in this case.

Fixes <rdar://problem/33003280>.
2017-08-16 03:45:31 -04:00
Michael Gottesman
afe402ff86 [silgen] Begin splitting emitRValue into two different APIs: SILGenFunction::emitPlus{One,Zero}RValue(...).
Today, SILGenFunction::emitRValue assumes the caller will create any cleanup
scopes that are needed to cleanup side-effects relating to the rvalue
evaluation.  The API also provides the ability for the caller to specify that a
+0 rvalues is an "ok" result. The API then tries to produce a +0 rvalue and
returns a +1 rvalue otherwise. These two properties create conflicting
requirements on the caller since the caller does not know whether or not it
should create a scope (if a +1 rvalue will be returned) or not (if a +0 rvalue
would be returned).

The key issue here is the optionality of returning a +0 rvalue. This change
begins to resolve this difference by creating two separate APIs that guarantee
to the caller whether or not a +0 or a +1 rvalue is returned and also creates
local scopes for the caller as appropriate. So by using these APIs, the caller
knows that the +0 or +1 rvalue that is returned has properly been put into the
caller scope. So the caller no longer needs to create its own scopes anymore.

emitPlusOneRValue is emitRValue except that it scopes the rvalue emission and
then *pushes* the produced rvalue through the scope. emitPlusZeroRValue is
currently a stub implementation that just calls emitPlusOneRValue and then
borrows the resulting +1 RValue in the outer scope, creating the +0 RValue that
was requested by the caller.

rdar://33358110
2017-08-14 13:24:43 -07:00
Michael Gottesman
d2c3dba450 [silgen][gardening] Standardize more parts of SILGen on using SGF instead of gen for the SILGenFunction.
This adds consistency and makes it easier to work in SILGen in the debugger
since using SGF or gen is not context dependent. You can just use gen.
2017-08-08 13:50:18 -07:00
Michael Gottesman
7cf4cdf9d9 [silgen] Verify that all ManagedValues in all RValues that are loadable are actual loaded (i.e. have object type).
This is already an RValue invariant that used to be enforced upon RValue
construction. We put in a hack to work around a bug where that was not occuring
and changed RValue constructors to instead load stored objects when they needed
to. But the problem is that since then we have added more constructors that
provide other manners to create such an invalid RValue.

I added verification to many parts of RValue and exposed an additional verify
method that we can invoke at the end of emitRValue() eventually to verify our
invariants. This will give me the comfort to make that assumption in other parts
of SILGen without worry.

I also performed a small amount of cleanup of RValue construction.

rdar://33358110
2017-08-08 00:43:33 -07:00
John McCall
c0b3bf1711 Suppress access enforcement when an l-value is converted to a pointer
just for pointer identity.

The current technique for deciding whether that's the case is *extremely*
hacky and need to be replaced with an attribute, but I'm reluctant to
take that on so late in the schedule.  The hack is terrible but not too
hard to back out in the future.  Anyone who names a method like this just
to get the magic behavior knows well that they are not on the side of
righteousness.

rdar://33265254
2017-07-21 23:40:04 -04:00
Joe Groff
7705393f06 SILGen: Ease off +0 peepholes for load exprs.
Now that we more tightly close formal accesses on lvalues, having LoadExpr and friends try to return a +0 loaded value is unsafe without deeper analysis, since the access will be closed immediately after the load and potentially free temporary memory that might be the only thing keeping the borrowed object alive. Fixes rdar://problem/32730865.
2017-07-18 15:13:06 -07:00
Andrew Trick
2df98a60c8 [sil-opaque-values] Add support for visitBindOptionalExpr. 2017-07-18 09:02:43 -07:00
Andrew Trick
1c85d0c0ff [sil-opaque-values] Add support for SILGen emitOpenExistential for the Error type. 2017-07-18 09:02:43 -07:00
Andrew Trick
1b21d31b96 [sil-opaque-values] Add support for SILGen emitBuiltinReinterpretCast. 2017-07-17 14:06:27 -07:00
Joe Groff
59b53c94c8 SILGen: Remove unused catch block for a try? expr using SGF.eraseBasicBlock().
Erasing the block directly from its parent using bb->eraseFromParent() doesn't update SILGenFunction's internal state tracking the postmatter section of the function. Fixes SR-1333 | rdar://problem/33351098.
2017-07-17 11:52:36 -07:00
John McCall
b4c10bda40 When looking for non-bridging conversions to peephole into a contextual
conversion, make sure we don't look into open-existentials.

rdar://33341584
2017-07-16 22:03:59 -04:00
John McCall
80b180a9a1 Implement a syntactic peephole to recognize explicit bridging
conversions that reverse an implicit conversion done to align
foreign declarations with their imported types.

For example, consider an Objective-C method that returns an NSString*:
  - (nonnull NSString*) foo;
This will be imported into Swift as a method returning a String:
  func foo() -> String
A call to this method will implicitly convert the result to String
behind the scenes.  If the user then casts the result back to NSString*,
that would normally be compiled as an additional conversion.  The
compiler cannot simply eliminate the conversion because that is not
necessarily semantically equivalent.

This peephole recognizes as-casts that immediately reverse a bridging
conversion as a special case and gives them special power to eliminate
both conversions.  For example, 'foo() as NSString' will simply return
the original return value.  In addition to call results, this also
applies to call arguments, property accesses, and subscript accesses.
2017-07-15 01:13:41 -04:00
John McCall
c005618219 Improve dumping facilities for various types in SILGen. 2017-07-15 01:12:55 -04:00
Joe Groff
624311db51 SILGen: Project the referent type of property components in key paths.
We want the semantic type of the storage, not its weak/unowned-qualified type. Fixes SR-5408 | rdar://problem/33215221.
2017-07-13 17:07:27 -07:00
John McCall
7f22faf968 Substantially rework how SILGen handles bridging as part of laying the
ground work for the syntactic bridging peephole.

- Pass source and dest formal types to the bridging routines in addition
  to the dest lowered type.  The dest lowered type is still necessary
  in order to handle non-standard abstraction patterns for the dest type.

- Change bridging abstraction patterns to store bridged formal types
  instead of the formal type.

- Improve how SIL type lowering deals with import-as-member patterns.

- Fix some AST bugs where inadequate information was being stored in
  various expressions.

- Introduce the idea of a converting SGFContext and use it to regularize
  the existing id-as-Any conversion peephole.

- Improve various places in SILGen to emit directly into contexts.
2017-07-11 12:45:13 -04:00
Robert Widmann
957d633185 Rename getInOutOrLValueObjectType to getWithoutSpecifierType
Prepares the AST for a future in which more than just inout and
@lvalue need to be stripped off of ephemeral types.
2017-07-06 09:35:04 -07:00
Michael Gottesman
fc4079ec5d [silgen] Add support for creating guaranteed phi arguments.
The main thing here is that we create a cleanup that inserts the
end_borrow_argument. Once I merge end_borrow with end_borrow_argument, there
will only be one cleanup.

rdar://31880847
2017-07-05 17:57:32 -07:00
Joe Groff
3c82e981f9 KeyPaths: Add support for optional chaining/forcing components.
rdar://problem/31768715
2017-06-26 09:40:31 -07:00
Joe Groff
141a0b0d06 SILGen: Remove assertion that rejected context-free nested functions in generic contexts when referenced as C function pointers.
A ConcreteDeclRef to a nested function will include substitutions for its enclosing generic parameters, even if they aren't captured by use locally within the function. The assertion here is probably unnecessary since inconsistencies here should be caught by assertions elsewhere. Fixes SR-5023 | rdar://problem/32426540.
2017-06-21 15:49:52 -07:00
Michael Gottesman
cd55f46626 Use a cast instead of trying to rely on higher level APIs.
I missed this problem due to a missing test case in initializers.swift. In a
subsequent commit, I am going to add the rest of the missing cases with some
FileCheck tests for some of them. This is to ensure that the commit is easy to
review.

rdar://32539006
2017-06-19 19:37:23 -07:00
Slava Pestov
577768d752 SILGen: Use public_external linkage for __dso_handle instead of hidden_external
This allows it to be referenced from inlinable functions.

Fixes <rdar://problem/32597278>.
2017-06-16 17:46:00 -07:00
Robert Widmann
abd5aa8e6d Rename some X-Value-related entities
* Rename coerceToMaterializableValue to coerceToRValue

* Rename isLValueType to hasLValueType to better match the
intended semantics of the member.
2017-06-14 13:18:45 -07:00
Robert Widmann
a4bf57f9d1 Move HasInOut bit out of recursive type properties
In anticipation of removing this bit, move it from the
recursive type property into TupleType - its only real
user.  This necessitates uglifying a bit of logic in the
short term that used to speak broadly of materializability
to instead speak about LValues and Tuples of InOut values
independently.
2017-06-14 09:54:19 -07:00
Slava Pestov
46bc2b160d SILGen: Remove 'overrideLocationForMagicIdentifiers' hack 2017-06-11 21:54:41 -07:00
John McCall
b542c75cb1 Use abstract implicit-conversion expressions for bridging cases instead of
expanding the conversion calls in Sema.
2017-06-11 01:39:51 -04:00
John McCall
44fda629aa Teach SILGen to handle more bridging cases and thread SGFContexts
through a few places.

This patch should be NFC for existing patterns, but it's preparing for
using SILGen's built-in bridging capabilities for more things.
2017-06-11 01:39:51 -04:00
John McCall
f4a181bc21 Various improvements to RValue. 2017-06-11 01:39:50 -04:00
Joe Groff
4fc0b7df96 SILGen: Handle existential keypath root types.
SR-4917|rdar://problem/32254554
2017-05-31 16:01:02 -07:00
Joe Groff
e025a0801e SILGen: Don't crash when a keypath references an inherited computed property.
rdar://problem/32414969
2017-05-26 11:47:46 -07:00
Joe Groff
cdc7a5c945 Support application of AnyKeyPath/PartialKeyPath.
rdar://problem/32237567
2017-05-25 15:51:22 -07:00
John McCall
48d60a4277 Merge pull request #9852 from rjmccall/open-existential-metatype-lvalue
Fix the emission of open-existential-metatype l-values
2017-05-23 01:26:58 -04:00
John McCall
bb5fe8046f Fix the emission of open-existential-metatype l-values.
rdar://32288618
2017-05-22 20:47:25 -04:00
Joe Groff
879397008c Sema: Don't crash when recovering type errors from malformed keypath expressions.
It's particularly likely someone will try to type `\(foo)`, which looks like a string interpolation segment, outside of a string literal, so give that case a special diagnostic. Fixes rdar://problem/32315365.
2017-05-22 10:42:40 -07:00
Slava Pestov
da80d4b791 Merge pull request #9637 from slavapestov/silgen-existential-lvalue-plumbing
SILGen plumbing for existential lvalue access fixes
2017-05-15 19:57:26 -07:00
Slava Pestov
31efc6a40a SILGen: Use emitOpenExistentialLValue() when lowering OpenExistentialExprs with lvalue base 2017-05-15 18:17:26 -07:00
Joe Groff
69a7ef6e2d Revert "SILGen: Tweak key path computed property lowering to produce a consistent runtime-callable ABI."
This reverts commit 4522cd09aa. This was a failed approach I shouldn't have committed. Fixes rdar://problem/32201589.
2017-05-15 16:10:55 -07:00
Joe Groff
9830f394c1 SILGen/IRGen/KeyPaths: Components for ObjC properties need to be identified by selector.
A property imported from Objective-C, or marked in Swift with the `dynamic` keyword, doesn't have a vtable slot, so can't be identified that way. Use the ObjC selector as the unique identifier to ascribe equality to such components. Fixes rdar://problem/31768669. (While we're here, throw some more execution tests and a changelog note in.)
2017-05-11 14:28:19 -07:00
Slava Pestov
775462bf52 SILGen: Don't model closure captures as an 'uncurry level' 2017-05-09 00:56:36 -07:00
Slava Pestov
edb1e97a35 SIL: Remove uncurryLevel from SILDeclRef
All we need to store is whether the SILDeclRef directly
references the declaration, or if it references a curry
thunk, and we already have an isCurried bit for that.
2017-05-09 00:56:35 -07:00
John McCall
338825e73d Fix the emission of r-value pointer conversions to delay the
conversions and extend lifetimes over the call.

Apply this logic to string-to-pointer conversions as well as
array-to-pointer conversions.

Fix the AST verifier to not blow up on optional pointer conversions,
and make sure we SILGen them correctly.  There's still an AST bug
here, but I'll fix that in a follow-up patch.
2017-04-26 14:15:44 -04:00
John McCall
f7e73ccc48 Delay the formal accesses associated with inout-to-pointer and
array-to-pointer argument conversions until just before the call.

rdar://31781386
2017-04-25 03:54:49 -04:00
John McCall
e6d52d8808 LValueToPointerExpr is dead code. RIP LValueToPointerExpr. 2017-04-25 03:01:16 -04:00
Joe Groff
d5cdf658da KeyPaths: Generate _kvcKeyPathString for ObjC-compatible keypaths. 2017-04-21 16:56:17 -07:00
Joe Groff
517c45aa3d Followups from merging master 2017-04-19 20:58:52 -07:00
Joe Groff
595e0e4ede Merge branch 'master' into keypaths 2017-04-19 18:38:24 -07:00