Commit Graph

1287 Commits

Author SHA1 Message Date
Joe Groff
6ccc96cf67 Code updates from review on #11953
- Remove dead `if !genericEnv` checks
- Do conformance checks for subscript indexes `InExpression`
- Use `GenericEnvironment::mapTypeOutOfContext` static method instead of null checking everywhere
2017-09-15 15:43:59 -07:00
Joe Groff
78d75428d6 SILGen: Lower key path subscript indexes.
And fill out SIL support for parsing, printing, and serializing key path
patterns with captured indexes.
2017-09-15 10:00:32 -07:00
John McCall
2d3d6addc0 Delay the validaton of storage accessors until finalization.
The base mutability of storage is part of the signature, so be sure
to compute that during validation.  Also, serialize it as part of
the storage declaration, and fix some places that synthesize
declarations to set it correctly.
2017-09-10 04:56:02 -04:00
John McCall
16122cef58 Cleanup correctness for upcasts. 2017-09-10 04:56:01 -04:00
Michael Gottesman
e0b0f5f06f [silgen] Once we have exclusively borrowed self, do not use the let +0 self peephole. Instead always access self via the lvalue code.
This eliminates a bunch of complexity from delegating init self since now we
have a clear bifurcation, before the begin of the super.init call, you use the
normal cleanup machinery, but once you have begun the super.init call, you use
the lvalue/formal evaluation machinery.

rdar://31521023
2017-09-06 16:31:20 -07:00
Michael Gottesman
3e730d4fa6 [silgen] Rename {was,}InWritebackScope => {was,}InFormalEvaluationScope. 2017-09-06 16:31:20 -07:00
Michael Gottesman
aa16226530 [silgen] Rename emitRValueForPropertyLoad parameter isGuaranteedValid => isBaseGuaranteed. (#11767)
This name makes it easier to infer the use of the parameter without reading the body of
the method.
2017-09-05 12:35:39 -07:00
Michael Gottesman
9bf1d0b887 [gardening] Copy edit a comment. 2017-09-04 09:39:53 -07:00
Michael Gottesman
daeddd0075 [silgen] Enable ownership on objc factory constructor test and fix exposed problems.
The specific exposed problem had to do with my using the same emission routine
for both lvalues using delegating init self (where we want formal accesses) and
for routines that wanted normal access to self. By splitting them the issue is
resolved.

As a benefit, I added a small peephole that I needed to add for my own purposes
(i.e. to maintain invariants), but that also incidentally improve codegen in
other places!

rdar://31521023
2017-08-31 16:39:12 -07:00
Michael Gottesman
df36e82b72 [silgen] Use SILGenBuilder APIs when creating value metatypes.
rdar://33358110
2017-08-30 17:31:42 -07:00
Michael Gottesman
10db94e31e [silgen] When checking we check if a failable initializer that we delegated to returned null, reload self using the normal semantics.
We have already finished the delegation sequence at this point, so we should go
through normal semantics.

rdar://33358110
2017-08-29 15:16:38 -07:00
Slava Pestov
0ee80f4f1e SILGen: More ownership refactoring
Use the ManagedValue forms of the SILInstruction constructors
where possible. This ensures that after emitting an instruction
such as an upcast, we rewrite the value's cleanup to destroy
the new value, and not the old value. This is important because
the ownership verifier asserts that instructions dominated by
a destroy cannot use the destroyed value.

Should be NFC, since the ownership verifier is off by default
for now.
2017-08-26 01:56:09 -07:00
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