Commit Graph

65 Commits

Author SHA1 Message Date
Hamish Knight
2f25d8fa58 [AST] Compute SubscriptDecl EndLoc
Instead of having the parser set it, compute it
using the recorded brace location for the accessor
record.
2023-12-15 21:44:31 +00:00
Slava Pestov
290701cb4d Sema: Ban shadowing generic parameters from outer scopes
Code like that is usually indicative of programmer error, and does not
round-trip through module interface files since there is no source
syntax to refer to an outer generic parameter.

For source compatibility this is a warning, but becomes an error with
-swift-version 6.

Fixes rdar://problem/108385980 and https://github.com/apple/swift/issues/62767.
2023-04-25 17:41:23 -04:00
Anthony Latsis
85fae6b9ff Gardening: Migrate test suite to GH issues: attr 2022-08-11 18:00:04 +03:00
Holly Borla
12459cff80 [Diagnostics] Print 'any' in diagnostic arguments. 2022-03-05 14:26:45 -08:00
Slava Pestov
3a92d2fc53 AST: Remove legacy GSB-based GenericSignature query implementation 2022-01-12 12:33:34 -05:00
Slava Pestov
7c29aaf3e2 Sema: Fix generics invariant violations in override checking
Override checking checks if the derived declaration's generic
signature is compatible with the base, but it does this after
doing a bunch of other checks which feed potentially invalid
type parameters to generic signature queries.

Now that the requirement machine is stricter about this kind
of this, re-organize some code to get around this.

Unfortunately this regresses a diagnostic, because we reject
candidates with mismatched generic requirements earlier in
the process.
2021-08-23 21:52:55 -04:00
Slava Pestov
ce550f2464 AST: Try harder to preserve type sugar in AbstractGenericSignatureRequest
AbstractGenericSignatureRequest tries to minimize the number of GSBs that we
spin up by only creating a GSB if the generic parameter and requirement types
are canonical. If they're not canonical, it first canonicalizes them, then
kicks off a request to compute the canonical signature, and finally, re-applies
type sugar.

We would do this by building a mapping for re-sugaring generic parameters,
however this mapping was only populated for the newly-added generic parameters.

If some of the newly-added generic requirements mention the base signature's
generic parameters, they would remain canonicalized.

Fixes <rdar://problem/67579220>.
2020-08-24 19:16:36 -04:00
Suyash Srijan
cc49c6105c [Diagnostics] Update diagnostic message for invalid overrides (#33097)
* [Diagnostics] Update 'does not override' diagnostic message to include protocol context as well

* [Sema] Check whether the override context is a class or a protocol for diagnostic purposes

* [Test] Update tests with new diagnostic message for overrides in protocol context

* [Sema] Adjust diagnostic for overrides in structs and enums to use the existing 'override_nonclass_decl' diagnostic
2020-07-27 15:15:28 +01:00
Anthony Latsis
14d3687d3b [SILGen] Test vtable thunk emission for overrides with contextual where clauses 2020-03-21 05:46:47 +03:00
Anthony Latsis
db3f7081ec Sema: Diagnose invalid overrides involving contextual where clauses 2020-03-05 20:20:07 +03:00
Suyash Srijan
a0d3542d08 [Typechecker] Covariant subscript check must also take into consideration that there may be a type mismatch (#29828) 2020-02-15 12:52:53 +00:00
Slava Pestov
736343f068 Sema: Skip override checking for members of constrained extensions
When a method in an extension of a class looks like an
override of a method from the base class, we emit a
diagnostic.

However due to a bug we used to skip this diagnostic for
certain members of constrained extensions.

Indeed it feels like we should not be doing this check
at all for members of constrained extensions, so lets
explicitly skip it, fixing a source compatibility problem
that was introduced when the unrelated bug was fixed.

Fixes <rdar://problem/57029805>, <https://bugs.swift.org/browse/SR-11740>.
2019-11-11 23:08:47 -05:00
Suyash Srijan
31153984ff [AST] Fix crash in 'getOverrideGenericSignature()' when there are no generic params in the GSB 2019-08-08 20:11:48 +01:00
Suyash Srijan
b384060988 [Test] Add a test case 2019-08-07 19:06:09 +01:00
Suyash Srijan
5279f2038e [Typechecker] Add the new generic signature to the diagnostic message 2019-07-13 21:50:24 +01:00
Suyash Srijan
8fc1c21951 [Test] Remove diagnostic from a test case 2019-07-13 21:50:24 +01:00
Suyash Srijan
3b7b723e0f [Diagnostics] Fix a typo in diagnostic message 2019-07-13 21:50:24 +01:00
Suyash Srijan
d8f8eef56d [Test] Fix test class names 2019-07-13 21:50:24 +01:00
Suyash Srijan
05335fed93 [Test] Add some more tests 2019-07-13 21:50:24 +01:00
Suyash Srijan
923fdc705e [Typechecker] Fix a crash when the superclass type is null 2019-07-13 21:50:24 +01:00
Suyash Srijan
c175ccdcb2 [Test] Fix a typo - use expected-note and not expected-error 2019-07-13 21:50:23 +01:00
Suyash Srijan
80cd803b8d [Test] Adds a new test case that mimics a Codable class
This is basically  a sort of regression test that ensures we apply proper context substitutions before checking the generic signatures of the functions
2019-07-13 21:50:23 +01:00
Suyash Srijan
511e7532e4 [Test] Adds a test case where a concrete class inherits from a generic class 2019-07-13 21:50:23 +01:00
Suyash Srijan
f0eb093f7d [Test] Remove dead test code 2019-07-13 21:50:23 +01:00
Suyash Srijan
24659de400 [Test] Add tests 2019-07-13 21:50:23 +01:00
Phill Farrugia
9cba37f39c Apply Override Fixit to Variable Declarations 2019-06-10 09:58:21 -07:00
Brent Royal-Gordon
c37fee1719 Add parallel tests for static subscripts
This commit modifies various subscript-related test files to add static subscript equivalents of existing tests.
2019-04-10 23:17:04 -07:00
Brent Royal-Gordon
0fcb09d9ce Fix description of static subscripts in errors 2019-04-10 23:17:04 -07:00
Brent Royal-Gordon
d9732a050f Allow the declaration of static subscripts
In this commit, MyStruct.self[0] parses and typechecks but the solution doesn’t apply correctly. MyStruct[0] gets diagnosed as an error.
2019-04-10 23:09:44 -07:00
Pavel Yaskevich
63b802ca88 [AST/Printing] Don't omit empty labels in special names
This makes diagnostics more verbose and accurate, because
it's possible to distinguish how many parameters there are
based on the message itself.

Also there are multiple diagnostic messages in a format of
`<descriptive-kind> <decl-name> ...` that get printed as
e.g. `subscript 'subscript'` if empty labels are omitted.
2018-09-24 18:36:53 -07:00
Doug Gregor
3ede41cf8d Update tests now that ‘override’ is allowed in protocols. 2018-09-04 16:42:06 -07:00
Mark Lacey
f08823757a IUO: Generate Optional<T> rather than ImplicitlyUnwrappedOptional<T>.
Stop creating ImplicitlyUnwrappedOptional<T> so that we can remove it
from the type system.

Enable the code that generates disjunctions for Optional<T> and
rewrites expressions based on the original declared type being 'T!'.

Most of the changes supporting this were previously merged to master,
but some things were difficult to merge to master without actually
removing IUOs from the type system:
- Dynamic member lookup and dynamic subscripting
- Changes to ensure the bridging peephole still works

Past commits have attempted to retain as much fidelity with how we
were printing things as possible. There are some cases where we still
are not printing things the same way:
- In diagnostics we will print '?' rather than '!'
- Some SourceKit and Code Completion output where we print a Type
  rather than Decl.

Things like module printing via swift-ide-test attempt to print '!'
any place that we now have Optional types that were declared as IUOs.

There are some diagnostics regressions related to the fact that we can
no longer "look through" IUOs. For the same reason some output and
functionality changes in Code Completion. I have an idea of how we can
restore these, and have opened a bug to investigate doing so.

There are some small source compatibility breaks that result from
this change:
- Results of dynamic lookup that are themselves declared IUO can in
  rare circumstances be inferred differently. This shows up in
  test/ClangImporter/objc_parse.swift, where we have
    var optStr = obj.nsstringProperty
  Rather than inferring optStr to be 'String!?', we now infer this to
  be 'String??', which is in line with the expectations of SE-0054.
  The fact that we were only inferring the outermost IUO to be an
  Optional in Swift 4 was a result of the incomplete implementation of
  SE-0054 as opposed to a particular design. This should rarely cause
  problems since in the common-case of actually using the property rather
  than just assigning it to a value with inferred type, we will behave
  the same way.
- Overloading functions with inout parameters strictly by a difference
  in optionality (i.e. Optional<T> vs. ImplicitlyUnwrappedOptional<T>)
  will result in an error rather than the diagnostic that was added
  in Swift 4.1.
- Any place where '!' was being used where it wasn't supposed to be
  allowed by SE-0054 will now treat the '!' as if it were '?'.
  Swift 4.1 generates warnings for these saying that putting '!'
  in that location is deprecated. These locations include for example
  typealiases or any place where '!' is nested in another type like
  `Int!?` or `[Int!]`.

This commit effectively means ImplicitlyUnwrappedOptional<T> is no
longer part of the type system, although I haven't actually removed
all of the code dealing with it yet.

ImplicitlyUnwrappedOptional<T> is is dead, long live implicitly
unwrapped Optional<T>!

Resolves rdar://problem/33272674.
2018-01-31 12:15:58 -08:00
Doug Gregor
cabdf84179 Suggest @objc for overrides of declarations from/in extensions.
The Swift class model does not support overriding declarations where either
the overridden declaration or the overriding declaration are in an extension.
However, the Objective-C class model does, so marking the declaration as
@objc (when possible) will work around the limitation.

Customize the "cannot override declaration in extension" diagnostic to
suggest adding @objc to the overridden declaration in cases where
@objc is permitted. Fixes SR-6512 / rdar://problem/35787914.
2017-12-13 14:54:32 -08:00
Jordan Rose
7c707ce97c For 'lazy', make "cannot override with a stored property" a warning (#13304)
Previous versions of Swift accidentally treated lazy properties as
computed properties because of how they were implemented. Now that we
check this correctly, we've broken source compatibility. Downgrade the
error to a warning in this case.

(Arguably we could /allow/ overriding with a stored property. The
original concerns were that you could accidentally end up with extra
storage you didn't need, and that observing accessors would behave
differently based on whether or not the property was overriding. But
there's at least no ambiguity for 'lazy', which can't have observing
accessors today.)

rdar://problem/35870371
2017-12-06 13:41:07 -08:00
Mohammed M. Ennabah
81ab8a302d changed diagnostics argument name to argument label SR-5857 (#11894)
* changed diagnostics argument name to argument label SR-5857
2017-09-13 19:07:19 -07:00
Ewa Matejska
21b2073b9e Small update to diagnostics to avoid the word yet 2017-09-07 10:34:34 -07:00
Doug Gregor
de66b0c25c [GSB] Warn about redeclarations of associated types from inherited protocols.
Introduce a warning about redeclaring the associated types from an
inherited protocol in the protocol being checked:

* If the new declaration is an associated type, note that the
  declaration could be replaced by requirements in the protocol's
  where clause.
* If the new declaration is a typealias, note that it could be
  replaced by a same-type constraint in the protocol's where clause.
2017-04-24 07:55:42 -07:00
Slava Pestov
f4396739fd Sema: Fix up override matching in the presence of non-canonical type parameters
Fixes <https://bugs.swift.org/browse/SR-4321>.
2017-03-25 22:52:14 -07:00
Slava Pestov
bd6c70c9d2 AST: Fix TypeBase::adjustSuperclassMemberDeclType() to strip generic signatures
Type::subst() does stupid things if you give it a
GenericFunctionType, attempting to build a new "substituted"
GenericSignature by hand.

As the newly-added test case demonstrates, this can trigger
assertions, and the callers of adjustSuperclassMemberDeclType()
didn't care about the generic signature anyway, so strip it off
first.

Soon, I want to have Type::subst() assert if it encounters a
GenericFunctionType, and remove all the junk there altogether,
but there's more work to be done first.

Progress on <rdar://problem/30817732> (RxCocoa build failures on
master), but now it hits further regressions.
2017-03-14 19:10:44 -07:00
Rintaro Ishizaki
4250f2c598 [AST] CompositionTypeRepr is not 'isSimple()'
We can't simply append '.self' for this.
2016-11-29 15:28:48 +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
Slava Pestov
4b2bb8ff97 SIL: Simpler and more correct TypeLowering::getConstantOverrideInfo()
We can use the new adjustSuperclassMemberDeclType() method here
to eliminate some code duplication and fix some corner cases with
generic method overrides.

Fixes <rdar://problem/18560464>, <https://bugs.swift.org/browse/SR-2427>,
<https://bugs.swift.org/browse/SR-2721>.
2016-10-24 02:16:21 -07:00
Slava Pestov
79a1512576 Sema: Three fixes for the new @escaping attribute
- If a parameter type is a sugared function type, mark the type
  as non-escaping by default. Previously, we were only doing this
  if the parameter type was written as a function type, with no
  additional sugar.

  This means in the following cases, the function parameter type
  is now non-escaping:

  func foo(f: ((Int) -> Void))

  typealias Fn = (Int) -> Void
  func foo(f: Fn)

- Also, allow @escaping to be used in the above cases:

  func foo(f: @escaping ((Int) -> Void))

  typealias Fn = (Int) -> Void
  func foo(f: @escaping Fn)

- Diagnose usages of @escaping in inappropriate locations, instead
  of just ignoring them.

It is unfortunate that sometimes we end up desugaring the typealias,
but currently there are other cases where this occurs too, such as
qualified lookpu of protocol typealiases with a concrete base
type, and generic type aliases. A more general representation for
sugared types (such as an AttributedType sugared type) would allow
us to solve this in a more satisfactory manner in the future.

However at the very least this patch factors out the common code
paths and adds comments, so it shouldn't be too bad going forward.

Note that this is a source-breaking change, both because @escaping
might need to be added to parameters with a sugared function type,
and @escaping might be removed if it appears somewhere where we
do not mark function types as non-escaping by default.
2016-08-17 19:37:23 -07:00
Michael Ilseman
c37751ae96 [noescape by defaul] make noescape the default
This flips the switch to have @noescape be the default semantics for
function types in argument positions, for everything except property
setters. Property setters are naturally escaping, so they keep their
escaping-by-default behavior.

Adds contentual printing, and updates the test cases.

There is some further (non-source-breaking) work to be done for
SE-0103:

- We need the withoutActuallyEscaping function
- Improve diagnostics and QoI to at least @noescape's standards
- Deprecate / drop @noescape, right now we allow it
- Update internal code completion printing to be contextual
- Add more tests to explore tricky corner cases
- Small regressions in fixits in attr/attr_availability.swift
2016-07-29 13:49:08 -07:00
Jordan Rose
bd18590686 Override checking: believe full-name matches over types. (#2466)
This at least emits notes when someone overrides something but
gets the types a little wrong (more than just mismatched optionals,
as handled in d669d152).

Part of rdar://problem/26183575
2016-05-10 14:39:40 -07:00
Jordan Rose
e1a5bcfdfd Improve the diagnostic for mismatched optionals in subscripts. 2016-04-15 18:06:21 -07:00
Jordan Rose
d669d152d0 Add fix-its when an override has mismatched optionals. 2016-04-15 18:03:18 -07:00
Daniel Duan
e0cc095063 [Parser] updated tests for "expected declaration" companion note 2016-02-19 11:05:03 -08:00
Chris Lattner
59e0b2d347 Fix rdar://21311590 QoI: Inconsistent diagnostics when no constructor is available
In the case of an invalid override of an init, mark the override invalid, not the
init.  This way, code that uses the init can be parsed and analyzed correctly and
bogus downstream errors are reduced.

Before:

t.swift:2:12: error: initializer does not override a designated initializer from its superclass
  override init() {}
  ~~~~~~~~ ^
t.swift:5:1: error: cannot invoke initializer for type 'C' with no arguments
C()
^
t.swift:6:1: error: 'B' cannot be constructed because it has no accessible initializers
B()
^

After:

t.swift:2:12: error: initializer does not override a designated initializer from its superclass
  override init() {}
  ~~~~~~~~ ^
2016-01-22 17:42:42 -08:00
Slava Pestov
d7b5ea5344 Split off attr_objc_override from attr_override test
Swift SVN r31781
2015-09-08 22:12:18 +00:00