Commit Graph

188 Commits

Author SHA1 Message Date
Hamish Knight
cb0c9adc46 Use getXXXType over getXXXDecl in a bunch of places 2019-11-07 08:26:08 -08:00
Slava Pestov
09a9b72669 SILGen: Fix assertion when emitting tuple conversion into a refutable pattern initialization
Refutable pattern initializations require that the 'isInit' parameter
is set to true, which transfers ownership of the input value.

One way it was getting set to false was when the pattern was bound to
an expression containing OpaqueValueExprs.

The opaque value of an open existential is +0, but that can't show
up here; only the result of a tuple conversion can. In principle this
is always a +1 value, however it could also be trivial, and we were
incorrectly treating it as a +0 value. We need to check isPlusOne()
instead of hasCleanup(); the latter returns false for trivial values.

Doing this lets us avoid going through the initialization altogether.

Fixes <rdar://problem/56809385>.
2019-11-05 23:28:43 -05:00
Robert Widmann
b849e51768 Use operator bool to claw back some readability 2019-10-29 16:56:21 -07:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Robert Widmann
5a8d0744c3 [NFC] Adopt TypeBase-isms for GenericSignature
Structurally prevent a number of common anti-patterns involving generic
signatures by separating the interface into GenericSignature and the
implementation into GenericSignatureBase.  In particular, this allows
the comparison operators to be deleted which forces callers to
canonicalize the signature or ask to compare pointers explicitly.
2019-09-30 14:04:36 -07:00
Slava Pestov
d434188157 SIL: Refactor TypeConverter to not require a SILModule
This mostly requires changing various entry points to pass around a
TypeConverter instead of a SILModule. I've left behind entry points
that take a SILModule for a few methods like SILType::subst() to
avoid creating even more churn.
2019-09-06 21:50:15 -04:00
Slava Pestov
4c499fd4ac AST: Stop passing around LazyResolvers in various places 2019-07-06 00:43:22 -04:00
Saleem Abdulrasool
731c31f9a5 MSVC: litter the code with llvm_unreachable (NFC)
Add `llvm_unreachable` to mark covered switches which MSVC does not
analyze correctly and believes that there exists a path through the
function without a return value.
2019-06-01 19:02:46 -07:00
Joe Groff
60aa49d69c merge fixup 2019-04-17 14:46:22 -07:00
Joe Groff
c771a7e71b SILGen: Substitute away opaque types. 2019-04-17 14:43:32 -07:00
Slava Pestov
18e8feac8f SILGen: Kill OpaqueValueState and clean up code for opening existentials
OpaqueValueState used to store a SILValue, so back then the IsConsumable flag
was meaningful. But now we can just check if the ManagedValue has a cleanup
or not.

Also, we were passing around an opened ArchetypeType for no good reason.
2019-03-27 17:41:40 -04:00
Slava Pestov
c791c4a137 SIL: SILUndef must be aware of the resilience expansion
The ownership kind is Any for trivial types, or Owned otherwise, but
whether a type is trivial or not will soon depend on the resilience
expansion.

This means that a SILModule now uniques two SILUndefs per type instead
of one, and serialization uses two distinct sentinel IDs for this
purpose as well.

For now, the resilience expansion is not actually used here, so this
change is NFC, other than changing the module format.
2019-03-12 00:30:35 -04:00
Slava Pestov
6c012b2aec AST: Remove some unnecessary LazyResolver * parameters from ASTContext methods 2018-12-07 20:39:27 -05:00
John McCall
7da688d75a Always manage subobject projections with formal-access cleanups.
To make that work, enter appropriate scopes (ArgumentScopes and
FormalEvaluationScopes) at a bunch of places.  But note that l-value
emission generally can't enter such a scope, so in generic routines
like emitOpenExistentialExpr we have to just assert that we're
already in a scope.
2018-11-03 02:14:06 -04:00
Michael Gottesman
d57a88af0d [gardening] Rename references to SILPHIArgument => SILPhiArgument. 2018-09-25 22:23:34 -07:00
Jordan Rose
e3ac6a36fb [SILGen] Honor #sourceLocation for run-time error messages
We use this when an optional unwrap fails and when a Swift-3-style
@objc entry point is used.
2018-08-29 11:46:41 -07:00
Joe Groff
15e4094544 Merge pull request #18937 from jckarter/mutating-opened-existential-covariant-return
SILGen: Fix order of operations when a mutating existential method returns Self.
2018-08-28 18:21:45 -07:00
Joe Groff
7f14a3bf48 SILGen: Fix order of operations when invoking a mutating method on an existential that returns Self.
Delay allocating the result buffer for an opened Self return until right before it's needed. When a mutating method is invoked on an existential, the Self type won't be opened until late, when the formal access to the mutable value begins. Fixes rdar://problem/43507711.
2018-08-28 09:52:04 -07:00
John McCall
7eb703bd74 Switch subscript index emission to use SILGenApply. NFC.
As always, most of the work here went into working around the AST
representations of parameter and argument lists.
2018-08-27 02:14:21 -04:00
Brent Royal-Gordon
b6e35038b2 [SILGen] Output a different message for failed IUO force-unwraps
Modifies SILGen and the `Swift._diagnoseUnexpectedNilOptional` call to print a slightly different message for force unwraps which were implicitly inserted by the compiler for IUOs. The message is chosen based on the presence of certain flags in the `ForceValueExpr`, not on the type of the value being unwrapped.
2018-07-12 19:09:56 -07:00
Michael Gottesman
d2e1bcd056 [silgen] When transforming values into an existential box representation, do it at +1.
In general, SILGen assumes that only +1 values are "forwarded" into memory. This
is because we want any value that is stored in memory to not be dependent on
other values and for the forwarded object to be able to maintain its own
liveness. So this assert was correct to fire.

The specific test case that exposed this issue is:

What happened here is that we needed to reabstract a loadable value into an
existential and tried to maximally abstract it and thus store it into
memory. The That code was never updated to make sure the value was at +1. So we
would store a guaranteed value into memory and hope that whereever we stored it
doesn't escape the current function. In this case, I believe that we would be
safe... but past returns are not indicators of future results.

rdar://40773543
SR-7858
2018-06-09 17:44:20 -07:00
Doug Gregor
0bf0a1ee6a [SILGen] Eliminate more SubstitutionLists for accessor calls. 2018-05-11 17:37:26 -07:00
David Zarzycki
8c0c55539f [SIL] NFC: Rename misleading getSwiftRValueType() to getASTType()
Reference storage types are not RValues. Also, use more SILType helper
methods to avoid line wrap.
2018-05-04 08:14:38 -04:00
Doug Gregor
192234415d [AST] Store SubstitutionMaps in ConcreteDeclRef and Witness data structures.
Replace two prominent uses of SubstitutionList, in ConcreteDeclRef and
Witness, with SubstitutionMap. Deal with the myriad places where we
now have substitution maps and need substitution lists (or vice versa)
caused by this change.

Overall, removes ~50 explicit uses of SubstitutionList (of ~400).
2018-05-02 13:38:14 -07:00
Michael Gottesman
e567bc9028 [+0-all-args] Enable +0 normal arguments.
rdar://34222540
2018-03-19 20:25:31 -07:00
Slava Pestov
597bea3109 SILGen: Peephole Optional-to-Optional conversions where the input value is 'none'
Fixes <rdar://problem/38545956>.
2018-03-18 00:13:55 -07:00
Michael Gottesman
aa9b27802a [silgen] Add helper methods to SwitchEnumBuilder for creating .Some, .None cases.
I also used it to clean up a few uses of SwitchEnumBuilder.
2018-03-15 17:28:47 -07:00
Michael Gottesman
6b2ca79381 [silgen] Make NormalCaseHandler and DefaultCaseHandler take SwitchCaseFullExpr as movable rvalues.
This fits more with the notion of the API, that the user is being given a scope
that was already setup for them and that they must decide when to pop.
2018-03-15 17:28:47 -07:00
Michael Gottesman
e5f9b1f141 [+0-normal-args] When performing optional-to-optional conversions, be sure the passed in conversion is at +1 since all conversions happen at +1.
This assumption is already in the code here. My change just makes it so that
when we perform optional-to-optional on a +0 argument, we copy before the
transform.

Caught via the ownership verifier when updating test/SILGen/objc_blocks_bridging.swift for +0 arguments.

rdar://34222540
2018-02-28 13:58:24 -08:00
Michael Gottesman
76af5c5b16 [silgen] Centralize SwitchEnumBuilder and SwitchCaseFullExpr into the same file: SwitchEnumBuilder.{h,cpp}.
Previously SwitchEnumBuilder was in SILGenBuilder.{h,cpp} and
SwitchEnumCaseFullExpr was in its own file. This really made no sense since:

1. The two classes are inherently related to each other so really should be
together in the source base.
2. SwitchEnumBuilder uses a SILGenBuilder, but is really a separate independent
concept/entity, so there really is no reason to keep it in SILGenBuilder.cpp.

Just a quick fix to eliminate something that was bugging me.

NFC.
2018-02-17 13:09:23 -08:00
Michael Gottesman
3888b5b054 [+0-normal-args] Use SILGenBuilder in SILGenConvert when injecting an optional so we propagate through +0 vs +1.
This commit does two different things:

1. Changes some code paths in SILGenConvert to use SILGenBuilder APIs.
2. It eliminates a few bugs in this path exposed by running the SILGen tests
with +0 enabled.

The bug specifically is that we are assuming in this code that the input value
is always at +1, so we unconditionally forward the input value and then create
destroys on the output. This is not correct in general (since we have guaranteed
values) and occurs all the time with +0 arguments enabled.

This is tested by running the SILGen tests with +0 arguments enabled.

rdar://34222540
2018-02-12 11:17:55 -08:00
Mark Lacey
be8defb29e Rename lookThroughAllAnyOptionalTypes to lookThroughAllOptionalTypes. 2018-02-05 23:59:01 -08:00
Mark Lacey
b4b66bc8e8 Replace getAnyOptionalObjectType with getOptionalObjectType. 2018-02-05 23:59:00 -08:00
Mark Lacey
f08823757a IUO: Generate Optional<T> rather than ImplicitlyUnwrappedOptional<T>.
Stop creating ImplicitlyUnwrappedOptional<T> so that we can remove it
from the type system.

Enable the code that generates disjunctions for Optional<T> and
rewrites expressions based on the original declared type being 'T!'.

Most of the changes supporting this were previously merged to master,
but some things were difficult to merge to master without actually
removing IUOs from the type system:
- Dynamic member lookup and dynamic subscripting
- Changes to ensure the bridging peephole still works

Past commits have attempted to retain as much fidelity with how we
were printing things as possible. There are some cases where we still
are not printing things the same way:
- In diagnostics we will print '?' rather than '!'
- Some SourceKit and Code Completion output where we print a Type
  rather than Decl.

Things like module printing via swift-ide-test attempt to print '!'
any place that we now have Optional types that were declared as IUOs.

There are some diagnostics regressions related to the fact that we can
no longer "look through" IUOs. For the same reason some output and
functionality changes in Code Completion. I have an idea of how we can
restore these, and have opened a bug to investigate doing so.

There are some small source compatibility breaks that result from
this change:
- Results of dynamic lookup that are themselves declared IUO can in
  rare circumstances be inferred differently. This shows up in
  test/ClangImporter/objc_parse.swift, where we have
    var optStr = obj.nsstringProperty
  Rather than inferring optStr to be 'String!?', we now infer this to
  be 'String??', which is in line with the expectations of SE-0054.
  The fact that we were only inferring the outermost IUO to be an
  Optional in Swift 4 was a result of the incomplete implementation of
  SE-0054 as opposed to a particular design. This should rarely cause
  problems since in the common-case of actually using the property rather
  than just assigning it to a value with inferred type, we will behave
  the same way.
- Overloading functions with inout parameters strictly by a difference
  in optionality (i.e. Optional<T> vs. ImplicitlyUnwrappedOptional<T>)
  will result in an error rather than the diagnostic that was added
  in Swift 4.1.
- Any place where '!' was being used where it wasn't supposed to be
  allowed by SE-0054 will now treat the '!' as if it were '?'.
  Swift 4.1 generates warnings for these saying that putting '!'
  in that location is deprecated. These locations include for example
  typealiases or any place where '!' is nested in another type like
  `Int!?` or `[Int!]`.

This commit effectively means ImplicitlyUnwrappedOptional<T> is no
longer part of the type system, although I haven't actually removed
all of the code dealing with it yet.

ImplicitlyUnwrappedOptional<T> is is dead, long live implicitly
unwrapped Optional<T>!

Resolves rdar://problem/33272674.
2018-01-31 12:15:58 -08:00
Erik Eckstein
0661380516 Fix wrong missing-return error message for an initializer returning an implicitly unwrapped optional
The problem was that the unreachable instruction after the IUO unwrapping error call was interpreted as missing-return-unreachable.

rdar://problem/36611041
2018-01-30 09:07:57 -08:00
John McCall
ad0f8ab3b2 Weaken an assertion: conversions can go through Any but still end up
at AnyObject?.

Also, ensure that @convention(block) functions can be erased to
AnyObject, and teach SILGen how to do this with unchecked_ref_cast.

Fixes a source compatibility suite regression.
2017-11-29 02:58:04 -05:00
swift-ci
371e1b8f27 Merge pull request #13096 from gottesmm/pr-2b9d20d5f94e2bcc9fff00ab39293a8c091f285a 2017-11-27 17:36:25 -08:00
Michael Gottesman
1a5b7e1e8e [+0-all-args] Change SGF::emitUncheckedGetOptionalValueFrom to use SILGenBuilder APIs instead of handrolling said APIs.
It additionally removes some code from SILGenBuilder::createUncheckedEnumData
that introduced extra borrows. That is really the builder's caller's
responsibility. Plus, it isn't that hard to do anymore now that one can just
call ManagedValue::borrow.

rdar://34222540
2017-11-27 16:38:56 -08:00
John McCall
549efbc240 Don't naively apply the bridging peephole to AnyObject? -> Any? -> AnyObject.
Also, fix some logic in the peephole that was incorrectly recognizing
T? -> U? -> AnyObject as a force instead of a request to create NSNull.

In both cases, we ought to still be able to do much better than the
unpeepholed code by just applying the peephole and then merging in NSNull
in the nil case, but in the short term we need to fix this bug.

Fixes rdar://35402853
2017-11-27 19:33:38 -05:00
Michael Gottesman
798bff756a [silgen] Rename SavedInsertionPoint => SILGenSavedInsertionPoint.
This rename makes since since:

1. This is SILGen specific functionality.
2. In the next commit I am going to be adding a SIL SavedInsertionPoint class. I
want to make sure the two can not be confused.
2017-11-08 10:40:39 -08:00
Slava Pestov
b5cf13d2d7 SILGen: Fix bug with bridging peephole
When we peephole away a bridge from Objective-C followed by a
bridge to Objective-C where the destination type is AnyObject,
we need to force the source value if it was an optional.

Fixes <rdar://problem/33669575>.
2017-10-15 23:15:11 -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
Andrew Trick
18004f8729 [sil-opaque-values] Fix SILGen and ownership of open_existential_box_value. 2017-09-02 19:37:57 -07:00
Michael Gottesman
ae4485b9ce [silgen] init_existential_value always returns a +1 value, so it should always have a cleanup.
rdar://33358110
2017-08-25 19:50:04 -07:00
Andrew Trick
81b1bead51 Remove seemingly unnecessary logic for emitting open_exsitential_addr.
Why would we need to recreate the cleanup if we don't forward it in the first place?
2017-08-23 22:15:10 -07:00
Andrew Trick
0a36f9dee7 Fix SIL ownership for open_existential_value.
CoW existentials means that we can't forward ownership.
2017-08-23 22:15:10 -07:00
Michael Gottesman
e035c93ed9 [silgen][gardening] More gen => SGF. 2017-08-10 21:42:21 -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
f657ad2d3a Rename *ExistentialOpaque instructions to *ExistentialValue.
These instructions have the same semantics as the *ExistentialAddr instructions
but operate directly on the existential value, not its address.

This is in preparation for adding ExistentialBoxValue instructions.
The previous name would cause impossible confusion with "opaque existentials"
and "opaque existential boxes".
2017-07-17 23:46:41 -07:00
Andrew Trick
7b02edf2d1 [sil-opaque-values] Add support for SILGen emitPointerToPointer. 2017-07-17 14:06:27 -07:00