Commit Graph

1710 Commits

Author SHA1 Message Date
Ben Langmuir
ea848aeaae Rename C++ macro 'defer' -> 'SWIFT_DEFER'
In C++ we can't have nice things. The macro name 'defer' collided with
use of 'defer' in the Tokens.def file and we were already doing horrible
workarounds in a couple of places to allow them to be included into the
same file. So use a less awesome but more robust name (thanks to Joe for
suggesting SWIFT_DEFER).

Incidentally, sort a bunch of #inlcudes.
2016-06-29 14:57:58 -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
Slava Pestov
7814c47b71 AST: Slightly change meaning of NominalTypeDecl::getDeclaredType()
Consider this code:

struct A<T> {
  struct B {}
  struct C<U> {}
}

Previously:

- getDeclaredType() of 'A.B' would give 'A<T>.B'
- getDeclaredTypeInContext() of 'A.B' would give 'A<T>.B'

- getDeclaredType() of 'A.C' would give 'A<T>.C'
- getDeclaredTypeInContext() of 'A.C' would give 'A<T>.C<U>'

This was causing problems for nested generics. Now, with this change,

- getDeclaredType() of 'A.B' gives 'A.B' (*)
- getDeclaredTypeInContext() of 'A.B' gives 'A<T>.B'
- getDeclaredType() of 'A.C' gives 'A.C' (*)
- getDeclaredTypeInContext() of 'A.C' gives 'A<T>.C<U>'

(Differences marked with (*)).

Also, this change makes these accessors fully lazy. Previously,
only getDeclaredTypeInContext() and getDeclaredIterfaceType()
were lazy, whereas getDeclaredType() was built from validateDecl().

Fix a few spots where the return value wasn't being checked
properly.

These functions return ErrorType if a circularity was detected via
the generic parameter list, or if the extension did not resolve.
They return Type() if the extension cannot be resolved *yet*.

This is pretty subtle, and I'll need to do another pass over
callers of these functions at some point. Many of them should be
moved over to use getSelfInContext(), getSelfOfContext() and
getSelfInterfaceType() instead.

Finally, this patch consolidates logic for diagnosting invalid
nesting of types.

The parser had some code for protocols in bad places and bad things
inside protocols, and Sema had several different bail-outs for
bad things in protocols, nested generic types, and stuff nested
inside protocol extensions.

Combine all of these into a single set of checks in Sema. Note
that we no longer give up early if we find invalid nesting.
Leaving decls unvalidated and un-type-checked only leads to
further problems. Now that all the preliminary crap has been
fixed, we can go ahead and start validating these funny nested
decls, actually fixing some crashers in the process.
2016-06-18 17:15:24 -07:00
Joe Groff
dfcdbdc9e6 Make -enable-experimental-property-behaviors really gate the feature.
And add a note about the manglings for DI thunks.
2016-06-16 10:47:56 -07:00
Jordan Rose
7c998776ff Update the "'=' to ':' in attributes" fix-it to include a space.
...because it was bugging me, and because the migrator uses it.
2016-06-09 11:30:58 -07:00
Trent Nadeau
e6a7e0d2b2 Fix diagnostics when @warn_unused_result is used with other attrs 2016-06-09 09:03:56 -04:00
Trent Nadeau
78a420d850 Made use of @warn_unused_result a warning with a fixit (#2760)
https://bugs.swift.org/browse/SR-1052
2016-06-08 10:10:21 -07:00
swift-ci
e484845613 Merge pull request #2803 from gregomni/compile-flag 2016-06-01 15:48:40 -07:00
swift-ci
59e247c38a Merge pull request #2637 from JaSpa/inheritance-clause-diags 2016-06-01 15:01:23 -07:00
gregomni
2f9ac3444e Disable typealiases in protocols and add frontend flag to re-enable.
Goes back to Swift 2.2 behavior of treating the 'typealias' keyword inside a protocol as a deprecated form of an associatedtype. To get the newer (but still partly buggy) behavior of treating it as an actual typealias, add "-Xfrontend -enable-protocol-typealiases" to the compile invocation. 'decl/typealias/typealias.swift' now uses this flag to continue testing the enabled behavior.
2016-06-01 14:11:56 -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
Erik Eckstein
268ce83460 Revert "Turn off typealiases in protocols."
This reverts commit bda398306b.

It breaks the internal compiler build
2016-05-28 16:04:07 -07:00
gregomni
bda398306b Turn off typealiases in protocols.
Since there still are some holes in this feature, and I haven't had time to
fill them lately: Go back to the 2.2 behavior of treating 'typealias' keyword
in protocols as an associated type, and emit a deprecation warning.

Commented out tests specifically for typealiases in protocols for now, and
random validation tests that crash or not based on whether keyword is interpreted as associatedtype or typealias updated.
2016-05-27 11:38:17 -07:00
Dmitri Gribenko
855700dd22 Merge pull request #2689 from rintaro/parser-attr-requires-foundation-inactive
[Parser] Don't report AttrsRequiringFoundation in inactive config block
2016-05-25 13:39:42 -07:00
Rintaro Ishizaki
4d686a424f [Parser] Don't report AttrsRequiringFoundation in inactive config block
Fixes rdar://problem/23487944
2016-05-26 02:43:21 +09:00
Janek Spaderna
7819da76d1 [Parse] Improve diagnostics in inheritance clauses
Before, a keyword in an inheritance clause would lead to a long list of errors
not really showing what was wrong.
A special case is added to handle protocol composition; in inheritance clauses
the protocols don't have to be composed with 'protocol<>'.
2016-05-25 14:47:51 +02:00
Joe Groff
5e2b20d05f SILGen: Fix crashes when conditionally looking up generic subscripts and properties via AnyObject.
The fix for methods to lower the dynamic method type from the substituted AST type of the expression also needed to be applied to the optional chaining, subscript, and property paths.

This also exposed a problem in the Clang importer, where imported subscript accessors would get the unbound generic context type as their Self parameter type instead of the type with the correct generic parameters. Fix this by renaming the all-too-convenient ParamDecl::createSelf factory to `createUnboundSelf`, and introduce a new `createSelf` that uses the bound generic type.

Fixes rdar://problem/26447758.
2016-05-24 19:18:37 -07:00
Dmitri Gribenko
690d98a078 Merge pull request #2635 from practicalswift/typo-fixes-20160521
[gardening] Fix recently introduced typos.
2016-05-21 21:56:00 -07:00
Slava Pestov
170992c39f AST: Add Throws flag and ThrowsLoc to AbstractFunctionDecl
The verifier now asserts that Throws, ThrowsLoc and isBodyThrowing()
match up.

Also, add /*Label=*/ comments where necessary to make the long argument
lists easier to read, and cleaned up some inconsistent naming conventions.

I caught a case where ClangImporter where we were passing in a loc as
StaticLoc instead of FuncLoc, but probably this didn't affect anything.
2016-05-21 12:51:50 -07:00
practicalswift
ef4f925977 [gardening] Fix recently introduced typos. 2016-05-21 13:02:00 +02:00
David Farler
eb435cb787 Direct and camel-cased concatenation fixits for repeated identifiers in function declarations
Additional QoI to also provide a fix-it to concatenate in camel-case.

rdar://problem/25761940
2016-05-16 21:13:23 -07:00
David Farler
2118585640 QoI: Provide a fix-it for repeated identifiers in function declarations
A function declaration like:

func dog cow() {}

... yields a bunch of noisy diagnostics about expecting certain tokens, like
"expected '(' in argument list of function declaration", or the dreaded
"consecutive statements on a line must be separated by ';'". Instead,
look for a repeated identifier in this position and affirm that the
repeated identifier wasn't expected, suggesting that maybe this was a
single identifier with a break in it.

rdar://problem/25761940
2016-05-16 19:55:00 -07:00
David Farler
414cfe7487 Minor changes to keyword-as-identifier diagnostics
- Remove stray newline
- Adjust wording when recommending backticks for a keyword identifier
- Provide fix-it when encountering a keyword as an identifier

rdar://problem/25761380
2016-05-10 14:49:21 -07:00
David Farler
b4dbdc49c2 Provide a more specific diag when using keywords as identifiers
When declaring a function like func repeat(){}, the diagnostic is
"expected an identifier" but 'repeat' looks like a reasonable
identifier at first glance, so actually say why it isn't.

rdar://problem/25761380
2016-05-10 11:08:13 -07:00
Jordan Rose
2c25d85cdf Handle "setter:" in @available(renamed:).
Example:

    @available(*, unavailable, renamed: "setter:CGRect.diagonal(self:_:)")
    func scale(_ rect: inout CGRect, toDiagonalLength length: CGFloat)

(My examples are getting more and more contrived, but there you go.)

This is pretty much the same as the getter handling, except that we also
want to strip off the '&' at the call site.
2016-05-05 14:46:34 -07:00
Jordan Rose
272e938876 Handle "getter:" in @available(renamed:).
Example:

    @available(*, unavailable, renamed: "getter:UIColor.CIColor(self:)")
    func convertToCIColor(_ color: UIColor) -> CIColor

This syntax looks weird, but it's the same as what's used by
NS_SWIFT_NAME. I intend to improve the diagnostic text once I have
all the fix-its working.

Next up: setters!
2016-05-05 14:46:34 -07:00
Jordan Rose
894318cfe0 Allow instance member syntax in @available(renamed:).
Example:

    @available(*, unavailable, renamed: "Sequence.enumerated(self:)")
    func enumerate<Seq: SequenceType>(_ sequence: Seq) ->
        EnumerateSequence<Seq>

This will allow us to reuse this logic to suggest fixes for APIs
turned into members by NS_SWIFT_NAME.
2016-05-04 14:17:15 -07:00
Jordan Rose
d7b3b6a462 Validate the "renamed" argument to @available.
It should have the same form as the argument to NS_SWIFT_NAME
in Objective-C, except that it permits operators and (currently)
disallows instance members and properties. We do get to share the
same parsing code, at least.

This actually caught an error in the Foundation overlay!

Groundwork for SR-1008.
2016-04-28 20:21:30 -07:00
Chris Lattner
985f0bf73c [generic typealiases] Allow constraints to be spelled in type alias
decls, aligning with the final direction of SE-0048
2016-04-16 22:21:40 -07:00
Chris Lattner
b5828e6a61 Port over all the semantic checking for @noescape/@autoclosure from the
declattr path to the typeattr path.  The decl attr path will eventually
go away, but we need to keep it for now to support the legacy syntax
migration.
2016-04-15 14:16:43 -07:00
Chris Lattner
ab14e6706f Progress towards implementing SE-0049 - Allow autoclosure in parameter types
as well as on parameter decls.  Also, tighten up the type checker to look at
parameter types instead of decl attributes in some cases (exposing a type
checker bug).

Still TODO:
 - Reject autoclosure/noescape on non-parameter types.
 - Move stdlib and other code to use noescape and autoclosure in the right
   spot.
 - Warn about autoclosure/noescape on parameters decls, with a fixit to move it.
 - Upgrade the warning to an error.
2016-04-14 23:13:43 -07:00
Xi Ge
152eeaa1fe [Sema] Add a fixit for diagnosis "variable used within its own initial value".
This fixit checks if a decl with the identical name can be found in the parent type
context; if can, we add "self." to try to resolve the issue. rdar://25389852
2016-04-14 15:55:01 -07:00
practicalswift
798877ae77 [gardening] "if (foo)[SPACE][SPACE]{" → "if (foo)[SPACE]{" 2016-04-03 22:57:05 +02:00
Andrew Trick
482b264afc Reapply "Merge pull request #1725 from atrick/specialize"
This was mistakenly reverted in an attempt to fix buildbots.
Unfortunately it's now smashed into one commit.

---
Introduce @_specialize(<type list>) internal attribute.

This attribute can be attached to generic functions. The attribute's
arguments must be a list of concrete types to be substituted in the
function's generic signature. Any number of specializations may be
associated with a generic function.

This attribute provides a hint to the compiler. At -O, the compiler
will generate the specified specializations and emit calls to the
specialized code in the original generic function guarded by type
checks.

The current attribute is designed to be an internal tool for
performance experimentation. It does not affect the language or
API. This work may be extended in the future to add user-visible
attributes that do provide API guarantees and/or direct dispatch to
specialized code.

This attribute works on any generic function: a freestanding function
with generic type parameters, a nongeneric method declared in a
generic class, a generic method in a nongeneric class or a generic
method in a generic class. A function's generic signature is a
concatenation of the generic context and the function's own generic
type parameters.

e.g.

struct S<T> {
var x: T
@_specialize(Int, Float)
mutating func exchangeSecond<U>(u: U, _ t: T) -> (U, T) {
x = t
return (u, x)
}
}
// Substitutes: <T, U> with <Int, Float> producing:
// S<Int>::exchangeSecond<Float>(u: Float, t: Int) -> (Float, Int)

---
[SILOptimizer] Introduce an eager-specializer pass.

This pass finds generic functions with @_specialized attributes and
generates specialized code for the attribute's concrete types. It
inserts type checks and guarded dispatch at the beginning of the
generic function for each specialization. Since we don't currently
expose this attribute as API and don't specialize vtables and witness
tables yet, the only way to reach the specialized code is by calling
the generic function which performs the guarded dispatch.

In the future, we can build on this work in several ways:
- cross module dispatch directly to specialized code
- dynamic dispatch directly to specialized code
- automated specialization based on less specific hints
- partial specialization
- and so on...

I reorganized and refactored the optimizer's generic utilities to
support direct function specialization as opposed to apply
specialization.
2016-03-21 12:43:05 -07:00
Xi Ge
4bebeadce5 [Parser] Better error-recovery to preserve operator decl's structures. 2016-03-19 14:47:58 -07:00
Andrew Trick
5bda28e1cb Revert "Merge pull request #1725 from atrick/specialize"
Temporarily reverting @_specialize because stdlib unit tests are
failing on an internal branch during deserialization.

This reverts commit e2c43cfe14, reversing
changes made to 9078011f93.
2016-03-18 22:31:29 -07:00
Andrew Trick
4c052274e6 Introduce @_specialize(<type list>) internal attribute.
This attribute can be attached to generic functions. The attribute's
arguments must be a list of concrete types to be substituted in the
function's generic signature. Any number of specializations may be
associated with a generic function.

This attribute provides a hint to the compiler. At -O, the compiler
will generate the specified specializations and emit calls to the
specialized code in the original generic function guarded by type
checks.

The current attribute is designed to be an internal tool for
performance experimentation. It does not affect the language or
API. This work may be extended in the future to add user-visible
attributes that do provide API guarantees and/or direct dispatch to
specialized code.

This attribute works on any generic function: a freestanding function
with generic type parameters, a nongeneric method declared in a
generic class, a generic method in a nongeneric class or a generic
method in a generic class. A function's generic signature is a
concatenation of the generic context and the function's own generic
type parameters.

e.g.

  struct S<T> {
    var x: T
    @_specialize(Int, Float)
    mutating func exchangeSecond<U>(u: U, _ t: T) -> (U, T) {
      x = t
      return (u, x)
    }
  }
  // Substitutes: <T, U> with <Int, Float> producing:
  // S<Int>::exchangeSecond<Float>(u: Float, t: Int) -> (Float, Int)
2016-03-17 18:27:10 -07:00
Ben Langmuir
ff895d2f5e [CodeCompletion] Fix completion in string literal interpolation at top-level
Mostly this was just returning the ParserStatus bits that we got from
parseExprList from parseExprStringLiteral. The rest was just cleaning up
places that didn't handle EOF very well, which is important here because
the code completion token is buried in the string literal, so the
primary lexer will walk past it.

rdar://problem/17101944
2016-03-14 23:13:15 -07:00
Doug Gregor
d251c7a731 Merge pull request #1522 from kballard/conditional-compilation-overloads
[Parse] Don't error on overloads in a conditional compilation block
2016-03-14 11:40:04 -07:00
Greg Titus
a11e911f66 Merge pull request #1610 from gregomni/typealias
[Parse/AST/Sema] Split parsing for typealias & associatedtype, allow typealias in protocols and generic constraints
2016-03-13 21:50:01 -07:00
gregomni
1e3ed8bdd0 Split parsing for typealias & associatedtype, and allow typealias in protocols.
Split up parsing of typealias and associatedtype, including dropping a
now unneeded ParseDeclOptions flag.

Then made typealias in a protocol valid, and act like you would
hope for protocol conformance purposes (i.e. as an alias possibly
involved in the types of other func/var conformances, not as a hidden
generic param in itself).

Also added support for simple type aliases in generic constraints. Aliases
to simple (non-sugared) archetype types (and also - trivially - aliases to
concrete types) can now be part of same-type constraints.

The strategy here is to add type aliases to the tree of
PotentialArchetypes, and if they are an alias to an archetype, also to
immediately find the real associated type and set it as the
representative for the PA. Thus the typealias PA node becomes just a
shortcut farther down into the tree for purposes of lookup and
generating same type requirements.

Then the typealias PA nodes need to be explicitly skipped when walking
the tree for building archetype types and other types of requirements,
in order to keep from getting extra out-of-order archetypes/witness
markers of the real associated type inserted where the typealias is
defined.

Any constraint with a typealias more complex than pointing to a single
nested associated type (e.g. `typealias T = A.B.C.D`), will now get a
specialized diagnoses.
2016-03-13 21:44:23 -07:00
practicalswift
c75d3966d9 [gardening] Fix recently introduced typo: "specifiedso" → "specified so" 2016-03-12 21:15:16 +01:00
Ben Langmuir
5bbc06b35c [CodeCompletion] Fix override completion parser lookback issues
This allows us to get override completions correct when
* There are multiple decls on one line
* The preceding attributes/keywords span multiple lines

Resolving a longstanding FIXME.
2016-03-11 23:00:11 -08:00
Chris Lattner
4992474168 Add support for #sourceLocation in its ratified forms. Switch gyb to produce
the new form.  This keeps accepting #setline for now, but we should rip it out
at some point.
2016-03-11 22:21:42 -08:00
Daniel Duan
b52fc6d321 [Parser] Replace '=' with ':' for attribute argument.
Implements [SE-0040](https://github.com/apple/swift-evolution/blob/master/proposals/0040-attributecolons.md).

When specifying arguments for attributes, both '=' and ':' are now accepted.
But '=' will generate a deprecation warning.
2016-03-11 15:37:08 -08:00
Joe Groff
013aad13d4 Initial implementation of a @_cdecl attribute to export top-level functions to C.
There's an immediate need for this in the core libs, and we have most of the necessary pieces on hand to make it easy to implement. This is an unpolished initial implementation, with the following limitations, among others:

- It doesn't support bridging error conventions,
- It relies on ObjC interop,
- It doesn't check for symbol name collisions,
- It has an underscored name with required symbol name `@cdecl("symbol_name")`, awaiting official bikeshed painting.
2016-03-10 13:27:39 -08:00
practicalswift
5b028d0651 [gardening] Fix recently introduced typo: " " → " "
[gardening] Fix recently introduced typo: "a initializer" → "an initializer"
[gardening] Fix recently introduced typo: "charaters" → "characters"
[gardening] Fix recently introduced typo: "fullfilled" → "fulfilled"
[gardening] Fix recently introduced typo: "initalizer" → "initializer"
[gardening] Fix recently introduced typo: "peoperty" → "property"
[gardening] Fix recently introduced typo: "reparing" → "repairing"
[gardening] Fix recently introduced typo: "spilt" → "split"
2016-03-10 11:52:02 +01:00
Daniel Duan
9b00e35290 [Parser] refactor nested 'init' diagnostics. NFC
Add better comment. Replace a dyn_cast with cast per Jordan's observation.
2016-03-09 10:38:50 -08:00
Chris Lattner
fe9fe47b7e Implement support for generic typealiases. 2016-03-07 22:20:16 -08:00
Kevin Ballard
a734063bb4 [Parse] Don't error on overloads in a conditional compilation block
Fixes SR-826.
2016-03-03 19:45:00 -08:00