Commit Graph

78 Commits

Author SHA1 Message Date
Slava Pestov
051af61e48 Sema: Make sure mutating setters defined in extensions cannot witness class protocol setter requirements
Otherwise, we hit a problem because the setter is called with self passed as a
(single retainable pointer) value, but the witness expects a self value passed
indirectly.

I hit this while testing other stuff related to resilient default implementations.
2016-03-05 02:25:46 -08:00
Daniel Duan
780b58a9a5 [Parser] update tests for 'inout' syntax adjustment 2016-02-26 01:33:22 -08:00
Jordan Rose
0ab43e125b Merge pull request #1282 from dduan/SR771_stmt_in_type_body_2
[Parser][SR-711] Couple "expected declaration" Diagnose With A Note
2016-02-22 13:39:55 -08:00
Joe Groff
26e55ce465 Sema: Initial parsing and synthesis for properties with behaviors.
Parse 'var [behavior] x: T', and when we see it, try to instantiate the property's
implementation in terms of the given behavior. To start out, behaviors are modeled
as protocols. If the protocol follows this pattern:

  ```
  protocol behavior {
    associatedtype Value
  }
  extension behavior {
    var value: Value { ... }
  }
  ```

then the property is instantiated by forming a conformance to `behavior` where
`Self` is bound to the enclosing type and `Value` is bound to the property's
declared type, and invoking the accessors of the `value` implementation:

  ```
  struct Foo {
    var [behavior] foo: Int
  }

  /* behaves like */

  extension Foo: private behavior {
    @implements(behavior.Value)
    private typealias `[behavior].Value` = Int

    var foo: Int {
      get { return value }
      set { value = newValue }
    }
  }
  ```

If the protocol requires a `storage` member, and provides an `initStorage` method
to provide an initial value to the storage:

  ```
  protocol storageBehavior {
    associatedtype Value

    var storage: Something<Value> { ... }
  }
  extension storageBehavior {
    var value: Value { ... }

    static func initStorage() -> Something<Value> { ... }
  }
  ```

then a stored property of the appropriate type is instantiated to witness the
requirement, using `initStorage` to initialize:

  ```
  struct Foo {
    var [storageBehavior] foo: Int
  }

  /* behaves like */

  extension Foo: private storageBehavior {
    @implements(storageBehavior.Value)
    private typealias `[storageBehavior].Value` = Int
    @implements(storageBehavior.storage)
    private var `[storageBehavior].storage`: Something<Int> = initStorage()

    var foo: Int {
      get { return value }
      set { value = newValue }
    }
  }
  ```

In either case, the `value` and `storage` properties should support any combination
of get-only/settable and mutating/nonmutating modifiers. The instantiated property
follows the settability and mutating-ness of the `value` implementation. The
protocol can also impose requirements on the `Self` and `Value` types.

Bells and whistles such as initializer expressions, accessors,
out-of-line initialization, etc. are not implemented. Additionally, behaviors
that instantiate storage are currently only supported on instance properties.
This also hasn't been tested past sema yet; SIL and IRGen will likely expose
additional issues.
2016-02-20 15:01:05 -08:00
Daniel Duan
e0cc095063 [Parser] updated tests for "expected declaration" companion note 2016-02-19 11:05:03 -08:00
Chris Lattner
94dd92fcb8 Fix compiler_crashers 22725 & 28236 by reworking parameter parsing error
recovery a bit.
2016-02-01 20:50:32 -08:00
David Farler
3f635d04c7 Reinstante var bindings in refutable patterns, except function parameters.
This reverts commits: b96e06da44,
                      8f2fbdc93a,
                      93b6962478,
                      64024118f4,
                      a759ca9141,
                      3434f9642b,
                      9f33429891,
                      47c043e8a6.

This commit leaves 'var' on function parameters as a warning to be
merged into Swift 2.2. For Swift 3, this will be an error, to be
converted in a follow-up.
2016-01-29 15:27:08 -08:00
Denis Vnukov
db6582c72b Fixed several AST nodes wrongly marked with implicit/explicit flag 2016-01-27 13:43:39 -08:00
David Farler
a6f2530780 Revert "REVERTME: Temporarily make vars in refutable patterns a warning"
This reverts commit b96e06da44, making
vars in refutable patterns an error for Swift 3.

rdar://problem/23172698
2016-01-14 20:52:24 -08:00
Dmitri Gribenko
9d5cbc4428 Revert "Removed the ++ and -- operators" 2015-12-26 12:48:02 +01:00
David Walter
23772b1a21 Merge remote-tracking branch 'apple/master'
# Conflicts:
#	stdlib/private/SwiftPrivate/ShardedAtomicCounter.swift
#	stdlib/public/core/ArrayCast.swift
#	stdlib/public/core/Collection.swift
#	stdlib/public/core/ContiguousArrayBuffer.swift
#	stdlib/public/core/StringBuffer.swift
#	stdlib/public/core/StringUnicodeScalarView.swift
#	test/1_stdlib/Builtins.swift
#	test/1_stdlib/Collection.swift
#	test/1_stdlib/ErrorType.swift
#	test/1_stdlib/ErrorTypeBridging.swift
#	test/1_stdlib/ExistentialCollection.swift
#	test/1_stdlib/Float.swift
#	test/1_stdlib/Map.swift
#	test/1_stdlib/Mirror.swift
#	test/1_stdlib/Optional.swift
#	test/DebugInfo/arg-debug_value.swift
#	test/DebugInfo/closure.swift
#	test/DebugInfo/for.swift
#	test/DebugInfo/return.swift
#	test/Interpreter/availability_weak_linking.swift
#	test/Interpreter/break_continue.swift
#	test/SILGen/sil_locations.swift
#	test/SILGen/statements.swift
#	test/SILGen/unreachable_code.swift
#	test/SILPasses/definite_init_diagnostics.swift
#	test/SILPasses/return.swift
#	test/SILPasses/switch.swift
#	test/SILPasses/unreachable_code.swift
2015-12-24 13:18:29 +01:00
ken0nek
3ac60b13f5 Add spaces before and after closure arrow in test 2015-12-23 04:38:46 +09:00
Chris Lattner
0224f2d858 Convert more tests off of ++ and -- 2015-12-21 18:07:37 -08:00
David Walter
fc8b73b679 Error fixes 2015-12-07 11:39:28 +01:00
David Walter
4d80d1001b Reverted unnecessary changes 2015-12-07 02:47:17 +01:00
David Walter
f71dc7f790 Reverted more whitespace 2015-12-07 02:34:45 +01:00
David Walter
ab4c05e47c Some more 2015-12-07 00:24:35 +01:00
David Farler
b96e06da44 REVERTME: Temporarily make vars in refutable patterns a warning
Revert "Make function parameters and refutable patterns always
immutable"

This reverts commit 8f2fbdc93a.

Once we have finally merged master into the Swift 2.2 branch to be, we
should revert this commit to turn the errors back on for Swift 3.0.
2015-12-05 23:13:04 -08:00
David Farler
8f2fbdc93a Make function parameters and refutable patterns always immutable
All refutable patterns and function parameters marked with 'var'
is now an error.

- Using explicit 'let' keyword on function parameters causes a warning.
- Don't suggest making function parameters mutable
- Remove uses in the standard library
- Update tests

rdar://problem/23378003
2015-11-09 16:56:13 -08:00
David Farler
93b6962478 Warn when using 'var' bindings in function parameters
These will no longer be allowed in a future Swift release.

rdar://problem/23172698
2015-11-03 17:24:20 -08:00
Chris Lattner
ada5487153 add fixit tests to random other tests.
Swift SVN r31006
2015-08-04 20:35:36 +00:00
Chris Lattner
50be7e4ecf reapply r30789, r30795, r30796, r30797, without r30787 which causes a compile time hit:
- Produce more specific diagnostics relating to different kinds of invalid
 - add a testcase, nfc
 - Reimplement FailureDiagnosis::diagnoseGeneralMemberFailure in terms of

Not including r30787 means that we still generate bogus diagnostics like:
[1, 2, 3].doesntExist(0)  // expected-error {{type 'Int2048' does not conform to protocol 'IntegerLiteralConvertible'}}

But it is an existing and separable problem from the issues addressed here.



Swift SVN r30819
2015-07-30 23:31:56 +00:00
Ben Langmuir
c1a2955ef6 Revert r30787, r30789, r30795, r30796, r30797
r30787 causes our tests to time out; the other commits depend on r30787.

Revert "revert part of my previous patch."
Revert "Produce more specific diagnostics relating to different kinds of invalid"
Revert "add a testcase, nfc"
Revert "- Reimplement FailureDiagnosis::diagnoseGeneralMemberFailure in terms of"
Revert "Fix places in the constraint solver where it would give up once a single "

Swift SVN r30805
2015-07-30 17:44:22 +00:00
Chris Lattner
98a445384a Produce more specific diagnostics relating to different kinds of invalid
member references:

- Use of instance members from types
- Use of type members from instances
- Use of mutating getters.

This surely resolves some radars, but I'll have to dig them out later.


Swift SVN r30796
2015-07-30 06:26:43 +00:00
Chris Lattner
1d9bd6907f add a testcase, nfc
Swift SVN r30795
2015-07-30 06:00:25 +00:00
Chris Lattner
ede0c50856 Revamp how value & type member constraint failures are diagnosed, eliminating the
"unavoidable failure" path, along with Failure::DoesNotHaveNonMutatingMember and
just doing some basic disambiguation in CSDiags.

This provides some benefits:
 - Allows us to plug in much more specific diagnostics for the existing "only has 
   mutating members" diagnostic, including producing notes for why the base expr
   isn't mutable (see e.g. test/Sema/immutability.swift diffs).
 - Corrects issues where we'd drop full decl name info for selector references.
 - Wordsmiths diagnostics to not complain about "values of type Foo.Type" instead 
   complaining about "type Foo"
 - Where before we would diagnose all failures with "has no member named", we now
   distinguish between when there is no member, and when you can't use it.  When you
   can't use it, you get a vauge "cannot use it" diagnostic, but...
 - This provides an infrastructure for diagnosing other kinds of problems (e.g. 
   trying to use a private member or a static member from an instance).
 - Improves a number of cases where failed type member constraints would produce uglier
   diagnostics than a different constraint failure would.
 - Resolves a number of rdars, e.g. (and probably others):
   <rdar://problem/20294245> QoI: Error message mentions value rather than key for subscript



Swift SVN r30715
2015-07-28 07:04:22 +00:00
John McCall
bc3b47b98a Infer the return type of a closure to be () if it contains no
return statements, or a return statement with no operand.

Also, fix a special-case diagnostic about converting a return
expression to (1) only apply to converting the actual return
expression, not an arbitrary sub-expression, and (2) use the
actual operand and return types, not the drilled-down types
that caused the failure.

Swift SVN r30420
2015-07-20 21:52:18 +00:00
Chris Lattner
cb90745826 fix the bounce-back of rdar://19343997, where a fixit isn't adding a space right.
Swift SVN r29775
2015-06-28 18:50:04 +00:00
Chris Lattner
7ca647002d fix <rdar://problem/21176945> mutation through ? not considered a mutation
Swift SVN r29194
2015-05-31 21:26:51 +00:00
Chris Lattner
d1b092ee9c change "cannot assign to variable" diagnostic to "cannot assign to value", since
in some cases the problem is that you're trying to assign to a constant, not a 
variable.


Swift SVN r28966
2015-05-23 16:12:37 +00:00
Chris Lattner
cf1cb14205 recommit 28958/28959, there was a testcase that didn't get updated correctly
but it was between the two revisions.


Swift SVN r28964
2015-05-23 15:50:16 +00:00
Ted Kremenek
b4b81ff657 Revert "merge assignments to UnresolvedDotExpr onto the common path,"
Backing out to fix the build.

Swift SVN r28959
2015-05-23 07:55:07 +00:00
Ted Kremenek
381eb52207 Revert "Further consolidate mutation code, this time for DeclRefExprs. With that"
Backing out to fix th build.

Swift SVN r28958
2015-05-23 07:55:04 +00:00
Chris Lattner
291d7eaad7 Further consolidate mutation code, this time for DeclRefExprs. With that
done, the rest of the infrastructure is all common and can be simplified.  This
leaves us with a quite small and maintainable subsystem for diagnosing these
kinds of problems.

 include/swift/AST/DiagnosticsSema.def |   28 ++-----
 lib/Sema/CSDiag.cpp                   |  132 ++++++++++------------------------
 2 files changed, 48 insertions(+), 112 deletions(-)




Swift SVN r28957
2015-05-23 05:49:19 +00:00
Chris Lattner
3b64718b89 merge assignments to UnresolvedDotExpr onto the common path,
this is neutral w.r.t. diagnostics quality, but deletes a ton
of code:

 include/swift/AST/DiagnosticsSema.def |   21 ++---------
 lib/Sema/CSDiag.cpp                   |   64 ++--------------------------------
 2 files changed, 9 insertions(+), 76 deletions(-)



Swift SVN r28956
2015-05-23 05:18:59 +00:00
Chris Lattner
d5f68b478f Teach the recursive part of the diagnostics to handle the various things
that make vardecls and subscripts immutable.  This makes the indirect cases
a lot more specific ("this is a get-only property" instead of "this is 
immutable") and allows us to consolidate a bunch of code:

 2 files changed, 45 insertions(+), 119 deletions(-)




Swift SVN r28954
2015-05-23 04:53:08 +00:00
Chris Lattner
7d881f8440 - Switch "&x", "++x" and "x+=1" onto the new style mutability diagnostics,
which tell you what the problem is, not just that you have one.
- Enhance diagnostics to be more specific about function calls producing 
  rvalues.



Swift SVN r28939
2015-05-23 00:17:12 +00:00
Chris Lattner
ef9d112ffc consolidate some diagnostics in a helper in prep for making them more
rich, consistent, and pervasive.


Swift SVN r28938
2015-05-22 23:03:26 +00:00
Chris Lattner
012cfa8657 Improve mutability diagnostics when the top level is a ForceValueExpr.
As far as I know, the compiler now only produces the useless "cannot 
assign to the result of this expression" diagnostic in cases that don't
make sense to be an lvalue, like "4 = x".



Swift SVN r28935
2015-05-22 21:19:27 +00:00
Chris Lattner
b0df3ef35e Improve mutability diagnostics for parameters to add a note inserting/changing
a 'var' modifier on the parameter, e.g.:

x.swift:44:5: error: cannot assign to 'let' value 'a'
  a = 1
  ~ ^
x.swift:43:8: note: change 'let' parameter to 'var' to make it mutable
func f(let a : Int) {
       ^~~
       var
x.swift:48:5: error: cannot assign to 'let' value 'b'
  b = 2
  ~ ^
x.swift:47:8: note: mark parameter with 'var' to make it mutable
func g(b : Int) {
       ^
       var

Also fix a bug where we'd incorrectly suggesting adding 'mutating' to a class
method when assigning to self in some cases.



Swift SVN r28926
2015-05-22 19:13:25 +00:00
Chris Lattner
8853d19c2b Dramatically improve the diagnostics when a store is invalid, by taking
into account accesibility, assignments to self in a non-mutating
method (consistently), recursive components of an lvalue that makes it 
non-settable, etc.  Now we tell you what the *problem* was, instead of
just whining.

This fixes:
<rdar://problem/19370429> QoI: fixit to add "mutating" when assigning to a member of self in a struct
<rdar://problem/17632908> QoI: Modifying struct member in non-mutating function produces difficult to understand error message

in their full generality.



Swift SVN r28867
2015-05-21 05:53:36 +00:00
Chris Lattner
df19d03eb7 When diagnosing invalid assignment errors, dig into the constraint system to see
if it has already resolved a member binding of an UnresolvedDotExpr.  This allows
us to give tailored diagnostics to indicate whether the destination is not an lvalue
because the property itself was immutable or when the base is immutable.

In addition to improved diagnostics, this allows us to fixit hint "let" to "var" on
property definitions, and we can even go so far as to fixit hint insert 'mutating' on 
the enclosing func decl when self is the problem.

This fixes the non-subscript cases of:
<rdar://problem/17632908> QoI: Modifying struct member in non-mutating function produces difficult to understand error message
<rdar://problem/19370429> QoI: fixit to add "mutating" when assigning to a member of self in a struct
<rdar://problem/20234955> QoI: Error message for assigning to 'let' fields should say that the error is due to a 'let' binding

Subscript cases to follow.



Swift SVN r28854
2015-05-20 22:18:52 +00:00
David Farler
80571cf916 Check base expr when determining mutability of a member
When in an initializer, we allow setting into immutable properties
provided that the type of base in `base.member` matches that of that
initializer's containing type. This was an approximation for allowing
full access into `self` during initialization but this doesn't work when
passing in a different struct of the same type because that struct
should be still be immutable.

Check whether the base of the member access is the implicit self
parameter of the initializer before allowing mutation.

rdar://problem/19814302

Swift SVN r28634
2015-05-15 18:48:54 +00:00
Chris Lattner
4366da9250 more testcase updates for upcoming diagnostics change.
Swift SVN r28409
2015-05-11 06:05:00 +00:00
Dmitri Hrybenko
6a15456cec Eliminate uses of println() where it is irrelevant
Swift SVN r28172
2015-05-05 18:21:39 +00:00
Chris Lattner
0b5c1125e4 improve and consistify diagnostics for non-lvalue inout arguments.
Swift SVN r27795
2015-04-27 01:05:29 +00:00
Chris Lattner
9f36074698 fix <rdar://problem/19711233> QoI: poor diagnostic for operator-assignment involving immutable operand
We now produce tailored diagnostics for assignment operators that are passed a non-mutable LHS,
e.g.:

t.swift:14:3: error: cannot pass 'let' value 'x' to mutating binary operator '/='
x /= 19
~ ^
t.swift:13:1: note: change 'let' to 'var' to make it mutable
let x = 42
^~~
var





Swift SVN r27780
2015-04-27 00:20:57 +00:00
Chris Lattner
43c7334abc fix two QoI issues:
- <rdar://problem/16306600> QoI: passing a 'let' value as an inout results in an unfriendly diagnostic
 - <rdar://problem/16927246> provide a fixit to change "let" to "var" if needing to mutate a variable

We now refer to an inout argument as such, e.g.:

t.swift:7:9: error: cannot pass 'let' value 'a' as inout argument
  swap(&a, &b)
        ^

we also produce a note with a fixit to rewrite let->var in trivial cases where mutation is
being assed for, e.g.:

t.swift:3:3: note: change 'let' to 'var' to make it mutable
  let a = 42
  ^~~
  var

The note is produced by both Sema and DI.



Swift SVN r27774
2015-04-26 21:51:50 +00:00
Chris Lattner
bebbdfe001 fix <rdar://problem/17691565> attempt to modify a 'let' variable with ++ results in typecheck error not being able to apply ++ to Float
We now produce diagnostics like:
 - cannot pass 'let' value 'a' to mutating unary operator '++'
 - cannot pass get-only property 'b' to mutating unary operator '++'
 - cannot pass immutable value of type 'Int64' to mutating unary operator '++'



Swift SVN r27772
2015-04-26 20:42:19 +00:00
Dmitri Hrybenko
3b04d1b013 tests: reorganize tests so that they actually use the target platform
Most tests were using %swift or similar substitutions, which did not
include the target triple and SDK.  The driver was defaulting to the
host OS.  Thus, we could not run the tests when the standard library was
not built for OS X.

Swift SVN r24504
2015-01-19 06:52:49 +00:00