Commit Graph

1378 Commits

Author SHA1 Message Date
Pavel Yaskevich
aa9b3d8474 [TypeChecker] SE-0213: Implement literal init via coercion
Implementation is as follows: In `preCheckExpression` try to
detect if there is `T(literal)` call in the AST, replace it with
implicit `literal as T`, while trying to form type-checked AST,
after constraint solving, restore source information and drop
unnecessary coercion expression.

Resolves: rdar://problem/17088188
Resolves: rdar://problem/39120081
Resolves: rdar://problem/23672697
Resolves: rdar://problem/40379985
2018-07-17 12:08:59 -07:00
Pavel Yaskevich
10ddb04613 [ConstraintSystem] Add SuppressDiagnostics/AllowUnresolvedTypeVariables flags
Instead of mixing flags between type-checker and constraint solver, let's
move the ones which are useful in constraint system there. Doing
so allows for `solveForExpression` to be moved from `TypeChecker` to
`ConstraintSystem` which consolidates solver logic.

It also allows to set these flags as part of constraint generation/solving,
which is sometimes important.
2018-07-15 23:23:45 -07:00
Mark Lacey
eb57e209e3 Merge pull request #17691 from rudkx/remove-swift-3-in-expr-typechecking
Remove support for -swift-version 3 from the expression type checker.
2018-07-14 19:03:56 -07:00
Doug Gregor
5db1901d57 [Type checker] Emit coalesce-or-force-unwrap Fix-Its for necessary unwraps more consistently.
Replace the last (and most obscure) use of the poor “use ‘?’ or ‘!’” diagnostic with the
new, more explanatory version that provides separate notes with Fix-Its for coalescing or
force-unwrapping the value.

Finishes rdar://problem/42081852.
2018-07-13 16:48:15 -07:00
Doug Gregor
e7eac0af22 [Type checker] Extend the diagnostics for unwrapping the base of a member access.
Introduce a new fix kind into the constraint solver to cover unwrapping the base
of a member access so we can refer to the a member of the unwrapped base.
Wire this fix kind to the just-added diagnostic that suggests either the
chaining ‘?’ or the force-unwrap ‘!’ via separate, descriptive Fix-Its.

Example:

error: value of optional type 'X?' must be unwrapped to refer to member 'f' of wrapped base type 'X'
  let _: Int = x.f()
               ^
note: chain the optional using '?' to access member 'f' only for non-'nil' base values
  let _: Int = x.f()
               ^
                ?
note: force-unwrap using '!' to abort execution if the optional value contains 'nil'
  let _: Int = x.f()
               ^
                !

Before this, we would sometimes get a Fix-It for just ‘?’ and sometimes get a Fix-It for the
coalescing ‘??’, neither of which is likely to be right.

More work on rdar://problem/42081852.
2018-07-13 16:26:03 -07:00
Doug Gregor
945c09b1cc [Type checker] Improve diagnostics when an optional value is not unwrapped.
When we determine that an optional value needs to be unwrapped to make
an expression type check, use notes to provide several different
Fix-It options (with descriptions) rather than always pushing users
toward '!'. Specifically, the errors + Fix-Its now looks like this:

    error: value of optional type 'X?' must be unwrapped to a value of
        type 'X'
      f(x)
        ^
    note: coalesce using '??' to provide a default when the optional
        value contains 'nil'
      f(x)
        ^
          ?? <#default value#>
    note: force-unwrap using '!' to abort execution if the optional
        value contains 'nil'
      f(x)
         ^
         !

Fixes rdar://problem/42081852.
2018-07-13 11:02:04 -07:00
Mark Lacey
a5a1a63ed6 Silence a couple release build warnings about unused variables. 2018-07-12 07:52:02 -07:00
Mark Lacey
efb173857a Merge branch 'master' into remove-swift-3-in-expr-typechecking 2018-07-11 23:49:44 -07:00
Mark Lacey
aa30cc5adc Merge branch 'master' into remove-swift-3-in-expr-typechecking 2018-07-11 20:55:32 -07:00
Pavel Yaskevich
88e5e1a004 Merge pull request #17811 from xedin/fix-load-with-paren-expr
[TypeChecker] Make sure that `LoadExpr` is always precedes parens/force-value
2018-07-11 17:08:37 -07:00
Slava Pestov
4fa4133134 Sema: Fix crash with UnresolvedType in coerceExistential()
Fixes <rdar://problem/34357545>.
2018-07-10 17:01:12 -07:00
Pavel Yaskevich
b1bd9b6d05 [TypeChecker] Make sure that load always emitted before parens/forcing
While trying to emit implicit load expression make sure that it's
done in a way where force/paren expression is always top level,
that leads to better fix-its and, in case of forcing, more compact
SIL by intermediate optional container.

Resolves: [SR-8150](https://bugs.swift.org/browse/SR-8150) / rdar://problem/41725207
2018-07-07 19:25:41 -07:00
Slava Pestov
cb4a248e6b Sema: Diagnose non-required class initializer calls on protocols with superclass contraint 2018-07-06 23:39:39 -07:00
Mark Lacey
28f759c8b0 Remove support for -swift-version 3 from the expression type checker.
This is the obvious stuff. There will probably be more fallout.
2018-07-06 16:10:08 -07:00
Slava Pestov
381483bd74 AST: Remove Expr's LValueAccessKind 2018-07-05 23:54:13 -07:00
Joe Groff
01dea286ee Merge pull request #17679 from jckarter/nested-collection-upcast-peephole
Sema: Clear the types of exprs changed by collection upcast peepholes.
2018-07-02 19:05:17 -07:00
Joe Groff
64c1737211 Sema: Clear the types of exprs changed by collection upcast peepholes.
`finish{Array,Dictionary}Expr` currently invoke `cs.cacheExprTypes` after building their semantic exprs, which in a nested collection expression, immediately undoes the type changes done by this peephole, leading to crashes due to inconsistencies in the AST later. rdar://problem/41040820
2018-07-02 15:00:30 -07:00
John McCall
9bee3cac5a Generalize storage implementations to support generalized accessors.
The storage kind has been replaced with three separate "impl kinds",
one for each of the basic access kinds (read, write, and read/write).
This makes it far easier to mix-and-match implementations of different
accessors, as well as subtleties like implementing both a setter
and an independent read/write operation.

AccessStrategy has become a bit more explicit about how exactly the
access should be implemented.  For example, the accessor-based kinds
now carry the exact accessor intended to be used.  Also, I've shifted
responsibilities slightly between AccessStrategy and AccessSemantics
so that AccessSemantics::Ordinary can be used except in the sorts of
semantic-bypasses that accessor synthesis wants.  This requires
knowing the correct DC of the access when computing the access strategy;
the upshot is that SILGenFunction now needs a DC.

Accessor synthesis has been reworked so that only the declarations are
built immediately; body synthesis can be safely delayed out of the main
decl-checking path.  This caused a large number of ramifications,
especially for lazy properties, and greatly inflated the size of this
patch.  That is... really regrettable.  The impetus for changing this
was necessity: I needed to rework accessor synthesis to end its reliance
on distinctions like Stored vs. StoredWithTrivialAccessors, and those
fixes were exposing serious re-entrancy problems, and fixing that... well.
Breaking the fixes apart at this point would be a serious endeavor.
2018-06-30 05:19:03 -04:00
John McCall
9e905125f8 Perform function conversions when converting between autoclosures.
This is correct because it's the apparent rule used by CSSimplify:
value-to-autoclosure conversion is considered if and only if the
input type is not an autoclosure function type.

The real solution to this problem is that @autoclosure should be tracked
as a bit on the parameter type instead of as a bit on function types.
Slava has a PR leading towards that (https://github.com/apple/swift/pull/10094),
but it causes a source-compatibility problem when forwarding autoclosures.
You really shouldn't be able to forward autoclosures, but that's an
issue for the core team to resolve, and in the meantime there's this bug.

Fixes rdar://41219750.
2018-06-19 18:57:50 -04:00
Doug Gregor
18569e5f77 Merge pull request #16963 from DougGregor/evaluator-type-checker
[Type checker] Use the request-evaluator in the type checker.
2018-06-15 22:41:31 -07:00
Joe Groff
6257ebe2d4 Merge pull request #17203 from jckarter/rvalue-array-elements
Sema: Force the 'getElements' array of a type-checked ArrayExpr to contain rvalues.
2018-06-14 21:10:20 -07:00
Doug Gregor
b7fbe57bf9 [AST] Remove “validated” bit from TypeLoc.
TypeLocs have been “validated” when they have a non-null Type, so eliminate
the separate “validated” bit that was out-of-sync with the Type field.
2018-06-14 15:29:57 -07:00
Joe Groff
194f1f808b Sema: Force the 'getElements' array of a type-checked ArrayExpr to contain rvalues.
There are other parts of CSApply that attempt to peephole transform ArrayExprs (particularly bridging, which tries to turn `[x, y, ...] as T` into `[x as T, y as T, ...]`) and expect the elements to have already been rvalue-d. Fixes rdar://problem/40859007.
2018-06-14 12:08:44 -07:00
Slava Pestov
296ce3f312 AST: Remove hack-around for getInterfaceType() on ParamDecl returning InOutType
Most callers did not want the InOutType here, and checked
the ParamDecl's flags instead.
2018-06-13 15:38:52 -07:00
Slava Pestov
10b1755ccc Sema: Propagate noescape bit into closure passed to withoutActuallyEscaping
Fixes <rdar://problem/40853562>, <https://bugs.swift.org/browse/SR-7886>.
2018-06-11 22:36:08 -07:00
Robert Widmann
24abf3ea09 [NFC] Refactor default argument info
Refactor the interface to return a bit vector. Along the way, fix up
the callers and remove some dead usages of the defaults information
that were copied and pasted around Sema.
2018-05-31 17:28:04 -07:00
Lily Vulcano
63348bce6f Merge pull request #16736 from millenomi/LinuxAsBridging_2
Turn on 'as' bridging for Linux (2)
2018-05-31 09:23:45 -07:00
Lily Vulcano
1377ab7d7e Turn on ‘as’ bridging on Darwin. 2018-05-30 15:07:22 -07:00
Slava Pestov
ccc23c988b Sema: Fix bug in 'invalid dynamic constructor reference' check
If the base value was 'self', we were allowing a reference to a
non-required initializer, because you're allowed to do this inside
another initializer.

But if you're in a static method, 'self.init' should obey the same
restrictions as 'foo.init' for any other metatype value 'foo'.
2018-05-29 22:51:25 -07:00
Slava Pestov
d8fc9decf9 AST: Remove GenericSignature::getSubstitutionMap() 2018-05-28 19:45:28 -07:00
Pavel Yaskevich
d501b58f8c Merge pull request #16785 from brentdax/explicitly-implicit
Fix implicit UnresolvedMemberExpr crash
2018-05-24 00:23:23 -07:00
Brent Royal-Gordon
769bc80e64 Fix implicit UnresolvedMemberExpr crash
The isImplicit flag on an UnresolvedMemberExpr was not carried over to the CallExpr node generated after type checking, which caused later stages of compilation to try to use invalid SourceLocs. This change correctly propagates the flag.
2018-05-22 21:37:03 -07:00
swift-ci
81ce77d510 Merge pull request #16644 from ravikandhadai/LargeIntOverflowPR 2018-05-21 20:37:47 -07:00
Ravi Kandhadai
f1ff8a61a5 [Sema] Add Sema checks for detecting large integer literals that
overflow Builtin.Int2048 and add tests for checking the
correctness of the diagnostics.
2018-05-21 18:41:44 -07:00
David Zarzycki
3843d39297 Merge pull request #16741 from davezarzycki/nfc_create_abstract_LookupExpr
[AST] NFC: Create abstract class for MemberRefExpr/SubscriptExpr
2018-05-20 08:13:23 -04:00
David Zarzycki
6d52f434af [AST] NFC: Create abstract class for MemberRefExpr/SubscriptExpr
This consolidation includes two examples of where this useful. There are
probably more.
2018-05-19 12:33:35 -04:00
Slava Pestov
bd6281c558 AST: Change SubstitutionMap conformance lookup callbacks to take ProtocolDecl and not ProtocolType 2018-05-19 01:09:17 -07:00
Ben Langmuir
628b6a1fc7 Revert "Turn on ‘as’ bridging on Linux." 2018-05-17 14:54:35 -07:00
Lily Vulcano
b9455930ee Turn on ‘as’ bridging on Darwin. 2018-05-17 09:59:39 -07:00
Mark Lacey
bcb98fa29b Merge pull request #16623 from rudkx/rdar31538995
Disallow some implicit pointer conversions in autoclosures.
2018-05-16 16:07:33 -07:00
Mark Lacey
43775e817d Disallow some implicit pointer conversions in autoclosures.
Disallow implicit conversion or arguments from Array, String, and
InOut (formed by &) to pointer types if the argument is for an
@autoclosure parameter.

These conversions were really only intended to be used for C/ObjC
interop, and in some contexts like autoclosures they are not safe.

Fixes: rdar://problem/31538995
2018-05-16 12:13:25 -07:00
Pavel Yaskevich
58fb0ac6a7 [CSApply] Make sure that indexing into an r-value tuple produces r-value
This is modeled in constraint system in a way
that when member type is resolved by `resolveOverload`
it would take r-value type of the element at
specified index, but if it's a type variable it
could still be bound to l-value later, so r-value
result has to be enforced by coercion if necessary.

Resolves: rdar://problem/39931475
2018-05-16 00:20:46 -07:00
Robert Widmann
6ed7e4af59 Merge pull request #16088 from orakaro/exp
More detail diagnostic when downcast to CoreFoundation Type
2018-05-08 21:46:06 -04:00
Doug Gregor
ebf9cdcc44 [AST] Collapse storage for ConcreteDeclRef.
Rather than ASTContext-allocating ConcreteDeclRef’s storage when there is a
non-empty substitution map, put the SubstitutionMap directly in the
ConcreteDeclRef. Simplify the various interfaces along the way.
2018-05-02 13:39:21 -07: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
Slava Pestov
7b8359279d Sema: Eliminate typeCheckExpression() calls from convertLiteralInPlace()
This code dates back to the dark ages when forming substitutions
was something only the constraint solver could do.

Now that findNamedWitnessImpl() returns a ConcreteDeclRef instead
of a ValueDecl, we don't have to build a whole constraint system
and type check it just to form substitutions for the call.
2018-05-01 17:40:52 -06:00
Slava Pestov
2db629368e Sema: Support for abstract conformance in findNamedWitnessImpl() 2018-05-01 17:40:51 -06:00
Slava Pestov
7c6fc5ab8b Sema: Remove some obsolete logic from adjustSelfTypeForMember() 2018-04-30 18:59:20 -06:00
Slava Pestov
1da4430ecb Sema: Refactor findNamedWitnessImpl() to return a ConcreteDeclRef 2018-04-30 18:57:16 -06:00
Slava Pestov
754e8e2716 Sema: Eradicate uses of typeCheckExpressionShallow() from IfExpr and IfStmt rewriting
Instead of spinning up a new ConstraintSystem and solving it, we can
just directly construct a call to getBuiltinLogicValue().
2018-04-30 18:57:16 -06:00