Commit Graph

130 Commits

Author SHA1 Message Date
fischertony
e78a42b06f [SR-8239][Parse] Fix same-type constraint commutativity 2018-09-11 05:18:44 +03:00
Rintaro Ishizaki
2134f9f768 [Parse] Move SyntaxParsingContext to Parse (#14360)
This is only for the Parser.
Also, this resolves layering violation where libAST and libSyntax
depends on each other.
2018-02-03 10:43:52 +09:00
Rintaro Ishizaki
48eb60702e [Syntax] Stop using SyntaxBuilders for TypeIdentifier
Since we haven't handled error cases yet, Error type identifier (e.g.
'Array.+') used to case round trip problem.
2017-12-27 13:18:17 +09:00
Xi Ge
fec040d95e libSyntax: support generic parameter clause. (#13286)
This patch also performs minor refactoring to align syntax parsing
context with the right scope. We start to support the generic clauses
because they are necessary pieces to construct struct or
function syntax node.
2017-12-05 19:34:55 -08:00
Xi Ge
ad70fb07f9 libSyntax: parse generic where clause.
Because generic where clause doesn't coerce well to our existing syntax
context kinds, we add a new syntax context kind with this patch called
"Syntax". This context kind indicates that when error occurs, the
collection of syntax nodes falling into the context should be coerced
to UnknownSyntax.
2017-12-04 15:00:40 -08:00
Rintaro Ishizaki
640a11d11e [libSyntax] Return type-identifier as SyntaxParserResult 2017-11-29 09:58:05 +09:00
Nathan Hawes
7403e779ad [ASTWalker|Refactoring] Walk generic params and requirements in source order for subscript decls
Also handle requirements in non-trailing where clauses (for -swift-version 3)
Resolves rdar://problem/34859742
2017-11-16 16:02:12 -08:00
Slava Pestov
2c7246ec1a Parse: Fix a comment 2017-09-05 21:53:52 -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
19fed22872 Parse: Remove redundant diagnostic
We had two slightly different codepaths to diagnose ': class'
in an inheritance clause where it is not supported.

For generic parameters, we would fix the 'class' to 'AnyObject',
but for associated types we didn't do this. Perform the fix in
all cases where it makes sense and remove one of the two
diagnostics.
2017-09-05 21:53:52 -07:00
Ben Langmuir
534c0cc2cb [code-completion] Generic where clauses
Complete generic parameters and their members inside generic where
clauses on structs, classes, enums, extensions, typealiases, funcs,
subscripts and inits.

Still not handled correctly are associatedtypes.

rdar://problem/20582394
2017-07-31 09:57:00 -07:00
Ewa Matejska
fa6e207f62 Updating to have stronger working for warning and review feedback 2017-05-15 18:43:10 -07:00
Ewa Matejska
4553d5ea67 Making the where_inside_brackets warning into an Error for > Swift 3. 2017-05-15 15:12:14 -07:00
Huon Wilson
8c8f35ee78 [Parse] Don't tell CodeCompletion nonsense about protocol/AT where clauses.
This doesn't give particularly useful information yet (i.e. Self isn't
listed, see rdar://problem/31981641 ), but it does stop the completion
code from just directly crashing.

Fixes rdar://problem/31981486.
2017-05-12 16:00:00 +10:00
Doug Gregor
ab7cd7fbe4 Enable associated type where clauses in all language modes
We're ready to start experimenting with and rolling out this feature
in earnest.
2017-04-04 10:58:01 -07:00
Huon Wilson
54f247693c [AST]/[Parse] parse where clauses on protocol declarations. 2017-03-09 16:08:16 -08:00
Slava Pestov
928a74c47e Parse: Support for generic subscripts 2017-02-23 21:14:02 -08:00
Roman Levenstein
e5d5d7d5fd Some LayoutConstraint improvements.
This biggest change is:
- LayoutConstraintInfo is now a FoldingSetNode, which allows for proper canonicalization of LayoutConstraints. This is important for the correctness of type comparisons if types contain layout constraints.

No functionality changes from the client's point of view.
2017-02-07 17:03:11 -08:00
Doug Gregor
392324c252 Set a bogus depth (0xFFFF) on parsed generic type parameters.
Test for the bogus depth when canonicalizing a reference to the
generic parameters, so we don't canonicalize before the depth has been
set. This flushed out various issues where we weren't validating the
context eagerly enough.
2017-02-07 15:22:50 -08:00
Slava Pestov
4f4e6f6ebf Sema: Remove another workaround and fuzz harder
We used to drop the entire generic parameter list if one of the
entries failed to parse. This caused a problem where the generic
parameters were still available for name lookup, so they had
to be special-cased since there's no generic environment set up
in this case.

Now, keep the parts of the generic parameter list around that
parsed successfully.

When I first made the change, almost a hundred crashers regressed;
now all the underlying issues have been fixed.

The result is that in addition to removing a crappy hack we get
some more mileage out of the compiler_crashers, because stuff like
this now builds a generic environment:

class S<T{...}
2017-02-03 17:02:04 -08:00
Slava Pestov
64352a7532 Parser: Don't return valid location from parseGenericWhereClause() if there were no requirements 2017-02-03 17:02:03 -08:00
Roman Levenstein
ed6c0aa34b Parsing of @_specialize
The new `@_specialize` attribute has a syntax like this:

```swift
@_specialize(exported: true, kind: full, where K == Int, V == Int)
@_specialize(exported: false, kind: partial, where K: _Trivial64)
func dictFunction<K, V>(dict: Dictionary<K, V>) {
}
```

If `exported` is set, the corresponding specialization would have a public visibility and can be used by clients.
If `exported` is omitted, it's value is assumed to be `false`.

If `kind` is `full` it means a full specialization.
If `kind` is `partial` it means a partial specialization.
If `kind` is omitted, its value is assumed to be `full`.
2017-01-18 16:42:10 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
38be6125e5 [gardening] C++ gardening: Terminate namespaces, fix argument names, ...
Changes:
* Terminate all namespaces with the correct closing comment.
* Make sure argument names in comments match the corresponding parameter name.
* Remove redundant get() calls on smart pointers.
* Prefer using "override" or "final" instead of "virtual". Remove "virtual" where appropriate.
2016-12-17 00:32:42 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
David Farler
f450f0ccdf Revert "Preserve whitespace and comments during lexing as Trivia"
This reverts commit d6e2b58382.
2016-11-18 13:23:31 -08:00
David Farler
44f15558d6 Revert "Refactor: Rename Parser::consumeToken, consumeLoc. Add consumeToken API."
This reverts commit 39bfc123a3.
2016-11-18 13:23:31 -08:00
David Farler
39bfc123a3 Refactor: Rename Parser::consumeToken, consumeLoc. Add consumeToken API.
These APIs return SourceLocs, and eventually the Parser should consume
tokens, which now include source trivia such as whitespace and comments,
and package them into a purely syntactic tree.  Just a tiny step. NFC.
2016-11-15 16:11:57 -08:00
David Farler
d6e2b58382 Preserve whitespace and comments during lexing as Trivia
Store leading a trailing "trivia" around a token, such as whitespace,
comments, doc comments, and escaping backticks. These are syntactically
important for preserving formatting when printing ASTs but don't
semantically affect the program.

Tokens take all trailing trivia up to, but not including, the next
newline. This is important to maintain checks that statements without
semicolon separators start on a new line, among other things.

Trivia are now data attached to the ends of tokens, not tokens
themselves.

Create a new Syntax sublibrary for upcoming immutable, persistent,
thread-safe ASTs, which will contain only the syntactic information
about source structure, as well as for generating new source code, and
structural editing. Proactively move swift::Token into there.

Since this patch is getting a bit large, a token fuzzer which checks
for round-trip equivlence with the workflow:

fuzzer => token stream => file1
  => Lexer => token stream => file 2 => diff(file1, file2)

Will arrive in a subsequent commit.

This patch does not change the grammar.
2016-11-15 16:11:57 -08:00
swift-ci
5877db3b6b Merge pull request #5099 from rintaro/rangle-location 2016-10-24 00:16:42 -07: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
17ebbae628 [Parse] Reorder precedence of '&' operator in type parsing
Now 'P1 & P2.Type' is parsed as (composition P1, (metatype P2))
instead of (metatype (composition P1, P2)).

For now, parsing inheritance clause accepts any TypeRepr, that is not allowed
in current Swift grammer. Diagnostic logic will be added in later commits.

Also, in Swift3, (composition P1, (metatype P2)) should be fixed to
(metatype (composition P1, P2)) for source compatibility.
2016-10-19 02:22:23 +09:00
Rintaro Ishizaki
056aed8fd3 [Parser] Fix right angle location in erroneous parameter list
If '>' could not be found, the parser should return the location of the
last token parsed, instead of the current token.
Previously, it may causes ASTVerifier error "child source range not contained
within its parent" in some cases.
2016-10-03 18:54:46 +09:00
Slava Pestov
a9c68c0736 AST: Remove archetype from AbstractTypeParamDecl
There's a bit of a hack to deal with generic typealiases, but
overall this makes things more logical.

This is the last big refactoring before we can allow constrained
extensions to make generic parameters concrete. All that remains
is a small set of changes to SIL type lowering, and retooling
some diagnostics in Sema.
2016-09-22 19:48:30 -07:00
Jordan Rose
133c63c44b [Parse] Fix fix-it for old-style 'where' clauses. (#4198)
Previously it didn't handle parameters with protocol composition bounds
correctly.

rdar://problem/27648438
2016-08-11 14:38:06 -07:00
David Farler
7bfaeb57f1 [SE-0081] Warn on deprecated where clause inside angle brackets
and provide a fix-it to move it to the new location as referenced
in SE-0081.

Fix up a few stray places in the standard library that is still using
the old syntax.

Update any ./test files that aren't expecting the new warning/fix-it
in -verify mode.

While investigating what I thought was a new crash due to this new
diagnostic, I discovered two sources of quite a few compiler crashers
related to unterminated generic parameter lists, where the right
angle bracket source location was getting unconditionally set to
the current token, even though it wasn't actually a '>'.
2016-07-26 01:41:10 -07:00
Joe
67dccb283e [SE-0095] Code feedback changes; Any is parsed as a keyword
- Any is made into a keyword which is always resolved into a TypeExpr,
allowing the removal of the type system code to find TheAnyType before
an unconstrained lookup.
- Types called `Any` can be declared, they are looked up as any other
identifier is
- Renaming/redefining behaviour of source loc methods on
ProtocolCompositionTypeRepr. Added a createEmptyComposition static
method too.
- Code highlighting treats Any as a type
- simplifyTypeExpr also does not rely on source to get operator name.
- Any is now handled properly in canParseType() which was causing
generic param lists containing ‘Any’ to fail
- The import objc id as Any work has been relying on getting a decl for
the Any type. I fix up the clang importer to use Context.TheAnyType
(instead of getAnyDecl()->getDeclaredType()). When importing the id
typedef, we create a typealias to Any and declare it unavaliable.
2016-07-19 12:01:37 -07:00
Joe
a6dad0091b [SE-0095] Initial parsing implementation for '&' composition syntax
This commit defines the ‘Any’ keyword, implements parsing for composing
types with an infix ‘&’, and provides a fixit to convert ‘protocol<>’

- Updated tests & stdlib for new composition syntax
- Provide errors when compositions used in inheritance.
Any is treated as a contextual keyword. The name ‘Any’
is used emit the empty composition type. We have to
stop user declaring top level types spelled ‘Any’ too.
2016-07-19 12:01:02 -07:00
Slava Pestov
5b4ee41772 Parser: Diagnose if free-standing 'where' clause is attached to a non-generic declaration
Previously we would produce an empty GenericParamList, crashing Sema.
2016-06-18 17:15:25 -07:00
Chris Lattner
c5bf433490 Implement parser support for SE-0081 - Move 'where' clause to end of declaration
This patch includes testsuite changes to show each of the decls supported.

Next step is to migrate the stdlib + testsuite + corelibs: I'd would *greatly* appreciate help with this.

After that is done, deprecation + migration of the old form can happen.
2016-05-30 15:28:49 -07:00
David Farler
cafbff53af [QoI] Improve diagnostics when putting a class bound in a generic signature
When declaring a nominal type:

struct Weak<T> {
  weak var value: T
}

The diagnostic might mislead the developer to adding ': class' literally
to the 'T' in the generic parameters for `Weak`:

"'weak' may not be applied to non-class-bound protocol 'T'; consider
adding a class bound"

This is misleading in two ways: 1, 'T' isn't necessarily a protocol
(this patch generalizes that part of the message) and 2, you can't put
`: class` in the generic parameter list for `Weak`. In addition, the
stray class constraint causes diagnostic spew that also hides the issue.

Also provide a fix-it to constrain with 'AnyObject', which is probably
what the devloper means in this case.

rdar://problem/25481209
2016-05-17 15:52:22 -07:00
practicalswift
abfecfde17 [gardening] if ([space]…[space]) → if (…), for(…) → for (…), while(…) → while (…), [[space]x, y[space]] → [x, y] 2016-04-04 16:22:11 +02:00
Chris Lattner
868a795566 Introduce a new class between TypeDecl and NominalTypeDecl named GenericTypeDecl.
This factors the DeclContext and generic signature behavior out of NTD, allowing
it to be reused in the future.  NFC.
2016-03-04 23:09:15 -08:00
Xi Ge
0f4e7a5ea5 [CodeCompletion] Add code completion for where clauses. rdar://24245022
When completing at "extension A where #^HERE^#", we suggest the generic params of A to users.
2016-01-19 14:17:15 -08:00
Doug Gregor
cd62d52bc3 Parse attributes on generic type parameter declarations and check availability. 2016-01-15 12:58:52 -08:00
John McCall
1f3b3142b4 Distinguish conformance and superclass generic requirements.
As part of this, use a different enum for parsed generic requirements.

NFC except that I noticed that ASTWalker wasn't visiting the second
type in a conformance constraint; fixing this seems to have no effect
beyond producing better IDE annotations.
2016-01-11 16:07:37 -08:00
Xi Ge
f5f796b648 [CodeCompletion] Add code completion for generic parameters' inherited types. rdar://20699515 2016-01-08 12:51:58 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Doug Gregor
ccde6bb87d Allow protocol extensions to add further constraints via a trailing where clause.
Start parsing a "trailing" where clause for extension declarations, which follows the extended type name and (optional) inheritance clause. Such a where clause is only currently permitted for protocol extensions right now.

When used on a protocol extension, it allows one to create a more-constrained protocol extension, e.g.,

  extension CollectionType where Self.Generator.Element : Equatable { ... }

which appears to be working, at least in the obvious cases I've tried.

More cleanup, tests, and penance for the previous commit's "--crash" introductions still to come.

Swift SVN r26689
2015-03-29 05:42:37 +00:00
Doug Gregor
97ec4ac5f1 Allow the right-hand side of same-type constraints to be an arbitrary type.
Allows same-type constraints that map down to tuple types. This is a
minimal fix that allows the code in rdar://problem/18120419 to
type-check. However, the actual code in that radar triggers a SILGen
assertion (Archetype to be substituted must be valid in function), and
we're not recursively matching concrete types the way we should be, so
this is a baby step.

Among other things, this allows one to provide a same-type con

Swift SVN r24535
2015-01-19 23:14:46 +00:00