Commit Graph

171 Commits

Author SHA1 Message Date
Rintaro Ishizaki
229f98462e [Parse] Slightly refactor TupleTypeRepr element parsing.
* Instead of consuming 'inout' unconditionally, use backtracking to
  determine it's obsolete usage of 'inout' position.
* parse '...' before '= <initializer>' because '(Int... = [1])' is more
  likely than '(Int = 1 ...)'. This improves diagnostics.
2017-12-12 21:46:05 +09:00
Mark Lacey
73a492dc4f For Swift 3/4 mode, do not treat IUOs in illegal positions as Optionals.
Instead, just emit a deprecation warning and suggest switching to Optionals.

The old behavior caused several projects to break, so before we change
the behavior here we need to investigate whether we can mitigate most
of those breaks.
2017-12-04 16:15:13 -08:00
Rintaro Ishizaki
e7be78b84d [Parse] Skip type specifiers in middle of composition types
Like `P & inout P2`.
Don't bother to construct wrapped TypeRepr for them.
2017-11-30 15:59:35 +09:00
Mark Lacey
8b55a0f61b SE-0054: Rework diagnostics for IUOs and revise Swift 3 /4 semantics.
For Swift 3 / 4:

Deprecate the spelling "ImplicitlyUnwrappedOptional", emitting a warning
and suggesting "!" in places where they are allowed according to
SE-0054.

In places where SE-0054 disallowed IUOs but we continued to accept them
in previous compilers, emit a warning suggesting "Optional" or "?"  as
an alternative depending on context and treat the IUO as an Optional,
noting this in the diagnostic.

For Swift 5:

Treat "ImplicitlyUnwrappedOptional" as an error, suggesting
"!" in places where they are allowed by SE-0054.

In places where SE-0054 disallowed IUOs, emit an error suggestion
"Optional" or "?" as an alternative depending on context.
2017-11-18 11:41:53 +09:00
gregomni
7073eb15a4 Disallow override of dynamic Self return type with non-dynamic in a subclass
unless that subclass is final.
2017-10-18 13:07:04 -07:00
Slava Pestov
9126d316d8 Parse: Remove parseTypeForInheritance()
We can just use parseType() everywhere instead. We already check
for non-identifier types in inheritance clauses elsewhere, and indeed
we have to anyway because an identifier type might resolve to a
type alias whose underlying type is a non-nominal type.

It doesn't look like this change made any diagnostics worse, but if
we find a case where it did, we could revert it.
2017-09-05 21:53:52 -07:00
Slava Pestov
3e2acb8ab0 Parse: Allow protocol compositions in all inheritance clauses
We allowed them for generic parameter inheritance clauses but
not anywhere else. While arguably this has stylistic benefits,
the restriction was not enforced consistently and was mostly a
result of implementation limitations.

Lift the restriction and fix things up where needed to make them
work. This brings us closer to allowing protocols to constrain
the 'Self' type to a subclass of a class by listing the class in
the protocol's inheritance clause, which was a feature from SE-0156,
but this doesn't quite work.

Fixes <https://bugs.swift.org/browse/SR-4678> and
<rdar://problem/31785092>.
2017-09-05 21:53:52 -07:00
Slava Pestov
2c61ed5c99 Sema: Fix pre-check expression folding nested types of generic parameters
This allows [Foo.Bar](), [Foo.Bar?]() etc to type check if Bar is
an associated type of a generic parameter Foo.

Fixes <rdar://problem/27631137>.
2017-08-06 20:27:47 -07:00
Slava Pestov
6209a3ec4d Parse: Fix dictionary with generic key type in expression context
Fixes <rdar://problem/33506631> and <https://bugs.swift.org/browse/SR-5538>.
2017-07-24 23:55:18 -07:00
Slava Pestov
a3cad02708 Sema: Stricter superclass constraints when opening a generic signature
A generic signature like <T : SomeClass> would create
a subtype constraint between the type variable for T and
SomeClass. This is too loose, because a subclass existential
like 'SomeClass & SomeProto' is a subtype of SomeClass, but
cannot bind to T, because it is not representationally a
single retainable pointer.

Fixes <rdar://problem/32617814>, <https://bugs.swift.org/browse/SR-5142>.
2017-07-24 22:58:04 -07:00
Robert Widmann
b77f2c147c Parse Shared
Add parser support for __shared and __owned as type attributes.  Also, extend parser diagnostics and tests to account for the new type attributes.
2017-07-23 21:47:25 -07:00
Pavel Yaskevich
c431d5bbc0 [QoI] Add test-case for SR-5505 (attempting to use function name as a type) 2017-07-19 15:05:44 -07:00
Huon Wilson
04fa7ce29d [Parse] Upgrade protocol<...> warnings to Swift 4 errors.
Part of rdar://problem/28961650 .
2017-07-12 13:17:40 -07:00
Slava Pestov
76a005587b Sema: Specialization of nested types in expression context
Teach preCheckExpression() about UnresolvedSpecializeExpr
where the base is a TypeExpr.

This allows us to type check expressions like
'[Outer<T>.Inner<U>]()' by folding them down to a TypeExpr
with an array type.
2017-05-21 18:16:16 -07:00
Slava Pestov
cde493de0d Sema: Support member types in preCheckExpression()
This folds member access on types to a TypeExpr if the member
resolves to a nested type.

This allows [Foo.Bar]() to become an ApplyExpr of a TypeExpr,
rather than an ApplyExpr of an array literal; previously,
only [Foo]() worked.

Two cases that are still unsupported:

1) If G is a generic type and T is a generic typealias, then
   G.T<X> cannot be folded to a TypeExpr, because we cannot
   represent a generic typealias with an unbound generic
   parent type. Such lookups remain member lookups, where
   Sema first opens the base type to produce a bound generic
   type G<$T0>, and then resolves the generic typealias
   member on that.

2) If T is a generic parameter and X is an associated type,
   T.X is not folded down to a TypeExpr either.

Fixes <rdar://problem/16849958>.
2017-05-21 18:12:12 -07:00
Slava Pestov
54d709a428 Parse: Support parsing subclass existentials involving generic classes in expression context
Generic type specialization is ambiguous with < and > operators.
Extend the disambiguation hack to also consider parsing a generic
parameter list if the > is followed by &.

This fixes parsing types such as 'Base<Int> & P' in expression
context.
2017-05-19 15:05:46 -07:00
Slava Pestov
49a191768f Sema: Fix bug with protocol compositions in expression context
If the left hand side was not a TypeExpr, we would just drop
it on the floor instead of giving up on forming a protocol
composition.
2017-05-19 13:31:16 -07:00
Ewa Matejska
09fea35a12 more test updates 2017-05-15 17:46:22 -07:00
Dave Abrahams
ddf7ad517f UnicodeScalar => Unicode.Scalar 2017-05-11 15:23:25 -07:00
Slava Pestov
6548ad709b Sema: Remove TR_NonEnumInheritanceClauseOuterLayer
This was added at some point with the associated type where
clause work, but it appears to be unnecessary, because all
the tests passed with this removed.

It also introduced a source compatibility issue where we
stopped accepting typealiases to protocol compositions in
protocol inheritance clauses.

Add a test for this case too, since it wasn't tested before.

Fixes <https://bugs.swift.org/browse/SR-4855>.
2017-05-10 22:12:25 -07:00
Slava Pestov
3f723e799a Sema: Allow nested typealiases to reference protocols with associated type or Self requirements
Previously we allowed this:

protocol HasSelf {
  func foo(_: Self)
}

typealias Alias = HasSelf

But not this:

struct Outer {
  typealias Alias = HasSelf
}

Lift this restriction since the new String implementation
wants to make use of it.
2017-05-10 17:21:11 -07:00
Slava Pestov
b5721e8d8e AST: Remove AnyObject protocol 2017-05-02 19:45:00 -07:00
Slava Pestov
82d2d3ee43 Sema: Tweak inheritance clause checks for primitive AnyObject 2017-05-02 19:24:03 -07:00
Slava Pestov
e3b68e7bdd Remove -enable-experimental-subclass-existentials staging flag 2017-04-27 20:46:37 -07:00
Slava Pestov
77b760d500 Add some more tests for subclass existentials
- Test member access on metatypes
- SILGen tests for properties and subscripts
- End-to-end executable tests
2017-04-27 16:08:33 -07:00
Slava Pestov
e0391ca52e SILGen: More subclass existential tests 2017-04-25 02:42:15 -07:00
Slava Pestov
743aaa7e3d Sema: Add another small test case for subclass existentials 2017-04-25 01:32:45 -07:00
Slava Pestov
c3391afbea Sema: A composition consisting of a single class type is equivalent to the class itself
Otherwise, you can run into confusing behavior.
2017-04-25 01:32:44 -07:00
Slava Pestov
07c189558c Sema: Improve the fixit for 'Self' in invalid places
In an extension of a nested type, the extended type must be
fully qualified.

Also clean up the diagnostic logic a little bit and centralize
it in diagnoseUnknownType().

Fixes <https://bugs.swift.org/browse/SR-4379>.
2017-04-23 01:10:40 -07:00
Slava Pestov
7f3c97bf8b AST: More consistent spelling of 'type alias' in diagnostics
Some messages said 'typealias' and others said 'type alias'.
Change everything to use 'type alias' consistently (except
when it's talking about the keyword itself).
2017-04-23 00:02:30 -07:00
Slava Pestov
191afb0e96 Fix a test on Linux 2017-04-20 00:37:40 -07:00
Slava Pestov
55df58d2a3 Sema: More accurate cast diagnostics for subclass existentials
Normally there is very little the type checker can conclude about
casts between existentials, because new conformances can be added
retroactively. However if the existentials are class-constrained,
we can rule out certain casts as always failing by looking at
superclass bounds.
2017-04-20 00:37:37 -07:00
Slava Pestov
b756f76a66 Sema: Update 'can be represented in @objc' logic for subclass existentials 2017-04-12 00:07:30 -07:00
Slava Pestov
e4647bf494 Sema: More tests for subclass existentials 2017-04-12 00:07:29 -07:00
Slava Pestov
de323b5bef Sema: Update resolveType() for subclass existentials
If the -enable-experimental-subclass-existentials staging flag
is on, resolveType() now allows protocol compositions to contain
class types. It also diagnoses if a composition has more than one
superclass requirement.

Also, change diagnostics that talked about 'protocol composition'
to 'protocol-constrained type'.

Since such types can now contain a superclass constraint, it's not
correct to call them protocol composition.

"Protocol-constrained type" isn't quite accurate either because
'Any' has no protocols, and 'AnyObject' will have no protocols but
a general class constraint; but those are edge cases which won't
come up in these diagnostics.
2017-04-10 17:11:07 -07:00
Robin Kunde
ab415e6b3e [Parse] Improve parser diagnostics for keyword-as-identifer errors (#6045)
Instead of the simple "expected identifier in declaration", the error will now read "keyword '%' cannot be used as an identifier here", and will be accompanied by a note suggesting escaping the keyword with backticks, as well as a fixit.

https://bugs.swift.org/browse/SR-3167
2016-12-13 16:12:53 -08:00
Rintaro Ishizaki
97b89adab3 [Parse] Remove redundant 'inout' parsing from parseParameterClause 2016-11-29 23:30:59 +09:00
Rintaro Ishizaki
670d9a166b [Parse] Parse 'inout' along with type attributes. 2016-11-29 20:56:16 +09:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Jacob Bandes-Storch
f02e4ac2bf [Lexer] Don’t suppress diagnostics when splitting a token 2016-11-21 22:32:26 -08:00
Rintaro Ishizaki
6bc1fc947b [Parse] Change the way to add parenthesis in fix-it for deprecated protocol<..>
Fixes: https://bugs.swift.org/browse/SR-3124

"If '>' had any split token" was not really a correct condition for adding
parenthesis.

For instance:
    let a:protocol<P1, P2>=someThing
We don't need parenthesis for this case:
    let a:P1 & P2=someThing

On the other hand:
    typealias PMetaType = protocol<P1, P2>.Type
This doesn't produce a split token, but we need the parenthesis.
    typealias PMetaType = (P1 & P2).Type

The new rule is: "If the token after '>' can be any postfix TypeRepr"
(i.e. '?', '!', '.Type' or '.Protocol')
2016-11-04 13:40:01 +09:00
Rintaro Ishizaki
3b70c905d6 Add test case for P1 & protocol<P2, P3> production 2016-10-19 02:22:23 +09:00
Rintaro Ishizaki
c138e4cd8f [Sema] Swift3 compatibility: Fix ambiguous protocol composition production
Fixes: https://bugs.swift.org/browse/SR-2843

'P1 & P2.Type' is mistakingly accepted and parsed as (meatatype (composition P1, P2))
in swift3. Now, we parse it as (composition P1, (metatype P2))

For source compatibility, reconstruct it as Swift3.

Also, this solves inconsistent behavior between type and type-expression in Swift3.

typealias T1 = P1 & P2?  // was accepted as '(P1 & P2)?'
let T2 = (P1 & P2?).self // was silently accepted as 'P1' in Swift3.0
2016-10-19 02:22:23 +09:00
Rintaro Ishizaki
2c539c71f0 [Parse] Emit specialized diagnostics if seeing non-IdentTypeRepr in inheritance clause
Current Swift grammer accept only IdentTypeRepr (and protocol
composition in generics) in inheritance clause.
2016-10-19 02:22:23 +09:00
Rintaro Ishizaki
202779dea1 [QoI] Don't enclose single type identifier with parens
in fixit for deprecated protocol composition with trailing characters.

protocol<>?       => Any?
protocol<P1>?     => P1?
protocol<P1, P2>? => (P1 & P2)?
2016-10-12 16:41:19 +09:00
Rintaro Ishizaki
5693003d94 [Parse] Fix parsing protocol<Any>
Fix crash while constructing diagnostic fix-it for deprecated syntax.
(because of accessing elements in empty SmallVector)

```
0  swift                    0x000000010a1682ed PrintStackTraceSignalHandler(void*) + 45
1  swift                    0x000000010a167d36 SignalHandler(int) + 470
2  libsystem_platform.dylib 0x00007fff9452852a _sigtramp + 26
3  libsystem_platform.dylib 000000000000000000 _sigtramp + 1806531312
4  swift                    0x0000000107c6904a swift::Lexer::getTokenAtLocation(swift::SourceManager const&, swift::SourceLoc) + 490
5  swift                    0x0000000107cde0f2 swift::Parser::parseTypeIdentifierOrTypeComposition() + 1698
6  swift                    0x0000000107cdf2ec swift::Parser::parseType(swift::Diag<>, bool) + 380
7  swift                    0x0000000107c8d9e0 swift::Parser::parseExprSequence(swift::Diag<>, bool, bool) + 1456
8  swift                    0x0000000107c8d3c4 swift::Parser::parseExprImpl(swift::Diag<>, bool) + 148
9  swift                    0x0000000107ccdfed swift::Parser::parseExprOrStmt(swift::ASTNode&) + 429
10 swift                    0x0000000107cd5212 swift::Parser::parseBraceItems(llvm::SmallVectorImpl<swift::ASTNode>&, swift::BraceItemListKind, swift::BraceItemListKind) + 2994
11 swift                    0x0000000107c69598 swift::Parser::parseTopLevel() + 184
12 swift                    0x0000000107ca3a4f swift::parseIntoSourceFile(swift::SourceFile&, unsigned int, bool*, swift::SILParserState*, swift::PersistentParserState*, swift::DelayedParsingCallbacks*) + 239
13 swift                    0x0000000107ac77a4 swift::CompilerInstance::performSema() + 5156
14 swift                    0x000000010756ef1d performCompile(swift::CompilerInstance&, swift::CompilerInvocation&, llvm::ArrayRef<char const*>, int&, swift::FrontendObserver*) + 2765
15 swift                    0x000000010756c175 swift::performFrontend(llvm::ArrayRef<char const*>, char const*, void*, swift::FrontendObserver*) + 17029
16 swift                    0x00000001075293bd main + 8685
17 libdyld.dylib            0x00007fff998ea5ad start + 1
Stack dump:
0.      Program arguments: /Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift -frontend -interpret test.swift -target x86_64-apple-macosx10.9 -enable-objc-interop -sdk /Applications/Xcode-beta.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -color-diagnostics -module-name test
1.      With parser at source location: test.swift:2:1
Segmentation fault: 11
```
2016-10-12 16:41:19 +09:00
Slava Pestov
90d6e8dce8 Sema: Variadic parameters are always @escaping and cannot be @autoclosure
A variadic parameter of function type must be @escaping -- we cannot
reason about an array of non-escaping closures, so this was a safety
hole.

Also, attempting to define an @autoclosure variadic did not produce a
diagnostic, but would fail later on if you actually tried to do
anything with it. Let's ban this completely.

Both changes are source breaking, but impact is limited to code that
was already only marginally valid.
2016-09-15 21:46:02 -07:00
Nathan Hosselton
c681487cfa Clarify function_type_argument_label error msg
[SR-2451]
https://bugs.swift.org/browse/SR-2451
2016-08-31 21:24:26 -05:00
Xi Ge
1f6670f113 [Parser] Correct the fixit for protocol composition's syntax change (#4483)
* [Parser] Correct a fixit to preserve the trailing content after replacing Protocol<A, B>. rdar://27992964

When migrating, we found our fixit to replace the old protocol composition syntax, namely "Protocol<A, B>",
to the new syntax, "A & B", does not preserve the trailing content after '>'. For instance, we replace "Protocol<A, B>?"
with "A & B". This patch fixes the issue by inserting whatever after '>' in the old syntax to the new
replacement string. I consider this as a hack; the root-cause fix should be in the lexer to smartly separate
'>' and '?' as two tokens instead of one.

* [test] Update existing test.
2016-08-24 16:16:11 -07:00
Doug Gregor
f9d109442a Remove -suppress-argument-labels-in-types; it's dead. 2016-08-19 14:27:29 -07:00