Commit Graph

72 Commits

Author SHA1 Message Date
Hamish Knight
41b47f48cb [Sema] Fix crash in CheckRedeclarationRequest for IUO mismatch check
Make sure we only check this if both declarations have parameter lists.
While here, clean up the logic a bit such that we just iterate over
the parameter lists.

rdar://156874925
2025-10-24 14:32:05 +01:00
Doug Gregor
236418dbf8 Make the "typechecked function body" request more central and resilient
The "typechecked function body" request was defined to type-check a
function body that is known to be present, and not skipped, and would
assert these conditions, requiring its users to check whether a body
was expected. Often, this means that callers would use `getBody()`
instead, which retrieves the underlying value in whatever form it
happens to be, and assume it has been mutated appropriately.

Make the "typechecked function body" request, triggered by
`getTypecheckedBody()`, more resilient and central. A `NULL` result is
now acceptable, signifying that there is no body. Clients will need to
tolerate NULL results.

* When there is no body but should be one, produce an appropriate
error.
* When there shouldn't be a body but is, produce an appropriate error
* Handle skipping of function bodies here, rather than elsewhere.

Over time, we should move clients off of `getBody` and `hasBody`
entirely, and toward `getTypecheckedBody` or some yet-to-be-introduced
forms like `getBodyAsWritten` for the pre-typechecked body.
2023-11-26 09:09:29 -08:00
Karl Wagner
ab4f80ed95 [SE-0404] Allow protocols to be nested in non-generic contexts 2023-10-06 21:04:03 +02: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
cdded2ba9f Gardening: Migrate test suite to GH issues: decl 2022-08-26 03:26:23 +03:00
Saidhon Orifov
cee0ba22f0 [DiagnosticsQol] Restore optionality in IUO redeclared functions (#34823)
* [DiagnosticsQol] Restore optionality in IUO redeclared functions

* [DiagnosticsQol] Rename redeclare err by optionality

* Add redecl note for optionals/IUOs

* Change redecl note wording: not different -> same

* Modify iuo redecl note

Co-authored-by: Saidhon Orifov <saidhon94@hotmail.com>
2021-02-01 11:29:36 -05:00
Slava Pestov
dcc27ea819 Sema: Fix failure to emit diagnostic with some invalid member type references
There were two problems here:

- isUnsupportedMemberTypeReference() checked if the immediate parent type was
  an unbound generic type, but did not check for the parent of the parent, etc.

- The caller of isUnsupportedMemberTypeReference() had to re-check the
  various invalid conditions to determine what diagnostic to emit, and if
  none of those conditions matched it would just return an ErrorType without
  emitting a diagnostic.

Fix both of these by having isUnsupportedMemberTypeReference() return an
enum indicating what went wrong, and handle more cases.

Fixes <rdar://problem/67292528>.
2020-08-30 00:12:33 -04:00
Holly Borla
7fb27b3c74 [ConstraintSystem] Find solutions for code that contains circular member
references in the new diagnostic infrastructure.
2020-01-22 10:57:52 -08:00
Slava Pestov
1df3d1a33c Frontend: Don't interleave parsing and typechecking for the main file
SIL files still require this behavior; if we cleaned that up we
could simplify a fair bit of code here.

Fixes <https://bugs.swift.org/browse/SR-284>,
<https://bugs.swift.org/browse/SR-4426>.
2019-12-05 08:45:55 -05:00
Hamish Knight
92c42ec791 [AST] Add @_nonEphemeral parameter attribute
This non-user-facing attribute is used to denote pointer parameters
which do not accept pointers produced from temporary pointer conversions
such as array-to-pointer, string-to-pointer, and in some cases
inout-to-pointer.
2019-11-03 08:40:59 -08:00
Robert Widmann
63e3a0125c Memnber Type Lookups Shouldn't Recurse
When member type lookup is rooted at a typealias, it should not be possible for that
lookup to see the typealias itself. This prevents recursively trying
to resolve the underlying type, which used to be silently ignored when
validateDecl bounced the caller back with an error type or a null type.

Addresses rdar://56411408
2019-10-18 17:08:52 -07:00
Robert Widmann
69bd40cafd Replace the cycle breaker for pattern bindings 2019-10-17 10:55:13 -07:00
Robert Widmann
f15544de0b Strike VarDecls in Pattern Binding Initializers From Overloads
We would previously consider the VarDecls bound by a particular pattern
binding initializer context when performing overload resolution.  This
would lead to circular validation.  Validation was tacitly breaking the
cycle by returning an error type (but, crucially, not setting that
interface type).  Instead, pre-reject circular candidates.

This greatly improves the diagnostic we emit here in truly circular
cases since we can ride on `UR_InstanceMemberOnType` to yield

struct PatternBindingWithTwoVars2 { var x = y, y = 3 }
// cannot use instance member 'y' within property initializer; property initializers run before 'self' is available
2019-10-14 11:54:24 -07:00
Robert Widmann
233f8645d1 Move some more common enum diagnostics into the decl checker
In preparation for checkEnumRawValues being turned into a request, move the common diagnostics to the decl checker so they're always emitted at the right time.
2019-09-26 11:07:03 -07:00
Robert Widmann
e0a41b19cb Break some cycles
Computing the generic signature changes the way that cycles appear in the compiler.  For now, break these cycles.  We should investigate each place where hasComputedGenericSignature() is used in service of breaking cycles.  See rdar://55263708
2019-09-16 12:50:24 -07:00
Frederick Kellison-Linn
36d1ee7ec8 Use mapSignatureFunctionType on SubscriptDecls 2019-03-27 20:27:33 -04:00
Suyash Srijan
25e0a9f749 [Typechecker] Fix an issue with redeclaration checking (#23227)
* [typechecker] fix an issue with redeclaration checking

* [typechecker] use mapSignatureFunctionType in getOverloadSignatureType() to compute the type for the enum element decl, etc

* [typechecker] allow matching enums to functions as well, not just functions to enums

* [typechecker] fix the check for two enums

* [typechecker] check for nominal types as well when comparing enum elements

* [test] add more tests

* [typechecker] check for typealias as well
2019-03-20 00:25:42 -04:00
Doug Gregor
5757d14365 Don’t add RawRepresentation conformance for enums with erroneous raw types
This avoids cascading errors with very unfortunate Fix-Its.
rdar://problem/47891507
2019-02-12 23:12:17 -08: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
Huon Wilson
b96aedaf64 [Sema] Match Swift 4.0/4.1 overloading behaviour for properties in extensions of generic types.
The patch that nailed down our semantics here missed an additional case that
required a compatibility hack: a property on a generic type and a same-named one
in an (unconstrained) extension:

    struct Foo<T> {
        var x: Int { return 0 }
    }
    extension Foo {
        var x: Bool { return false }
    }

Fixes rdar://problem/40685642.
2018-06-22 08:43:34 +10:00
Slava Pestov
3a85829a8d Sema: DeclChecker::visitFuncDecl() does everything in the 'first pass' 2018-04-04 00:07:50 -07:00
Hamish
d9401d64dc [Sema] Revise #15412 in response to feedback
- Reuse existing logic to curry the signature type with the 'self' of the context (in addition we no longer use a MetatypeType for the 'self' of a static member as they don't have conflicting signatures with instance members anyway)

- Limit the fix for SR-7251 to Swift 5 mode

- Add tests for generic subscripts
2018-03-23 01:09:14 +00:00
Hamish
dee1590ae2 [Sema] Fix several redeclaration checking bugs
Currently we only give subscripts and var decls custom overload types if they're in generic extensions. However, because we give them no custom overload type in any other case, we don't detect for example a conflict with a previous declaration in the body of the extended type.

This commit changes the overload type logic such that properties and subscripts are always given custom overload types, which is determined by:

- The interface type of the decl (for subscripts only; as variables cannot be overloaded by type)
- The 'self' type of the context, if any
- The generic signature of the context, if any

Additionally, this commit adds a new `swift::conflicting` overload to ensure that different declarations always conflict even if their overload types are different.

Resolves SR-7249, SR-7250 & SR-7251.
2018-03-22 11:42:29 +00: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
Mark Lacey
e5a0c968ab Fix inout optional overload warning for methods.
Also add a note saying that allowing these overloads is deprecated and
will be removed in a future release.
2018-01-03 09:38:47 -08:00
Mark Lacey
6347112a04 Downgrade from error to warning for overloading by kind of optional.
Rather than error for overloading by the kind of optional (T? vs. T!)
for inout parameters, just emit a warning for now and continue
compiling.

We'll need to turn this back into an error when we remove IUOs from
the type system.
2018-01-03 00:48:53 -08:00
Mark Lacey
314d705f41 Sema: Look through inout when mapping IUOs to Optionals.
We translate IUOs to Optionals when generating signatures, but were
failing to look through inout in the process, so we were allowing
functions to be overloaded by Optional vs. IUO when the parameter was
inout.

Fixes https://bugs.swift.org/browse/SR-6685 / rdar://problem/36255630.
2018-01-02 11:36:38 -08: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
Graydon Hoare
17aa7aa910 [Diagnostics] s/may/must/ in 'may not be declared mutating' 2017-09-29 16:09:23 -04:00
Graydon Hoare
367a1500a3 [Diagnostics] s/may/must/ in 'may not be named' 2017-09-29 16:09:23 -04: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
Michael Ilseman
ed2522b384 [AST] Create ParameterTypeFlags and put them on function params
Long term, we want to refactor the AST to reflect the current
programming model in Swift. This would include refactoring
FunctionType to take a list of ParameterTypeElt, or something with a
better name, that can contain both the type and flags/bits that are
only specific to types in parameter position, such as @autoclosure and
@escaping. At the same time, noescape-by-default has severely hurt our
ability to print types without significant context, as we either have
to choose to too aggressively print @escaping or not print it in every
situation it occurs, or both.

As a gentle step towards the final solution, without uprooting our
overall AST structure, and as a way towards fixing the @escaping
printing ails, put these bits on the TupleTypeElt and ParenType, which
will serve as a model for what ParameterTypeElt will be like in the
future. Re-use these flags on CallArgParam, to leverage shared
knowledge in the type system. It is a little painful to tack onto
these types, but it's minor and will be overhauled soon, which will
eventually result in size savings and less complexity overall.

This includes all the constraint system adjustments to make these
types work and influence type equality and overload resolution as
desired. They are encoded in the module format. Additional tests
added.
2016-09-22 12:24:02 -07:00
Michael Ilseman
8c3b582793 [noescape by default] Incorporate Slav's feedback 2016-08-05 11:16:48 -07:00
Michael Ilseman
f48471ebd4 [noescape by default] purge tests of needless @noescape 2016-08-04 16:14:27 -07:00
Slava Pestov
ddc51c5917 AST: Implement SE-0102, introducing new semantics for Never alongside @noreturn
No migrator support yet, and the code for @noreturn is still in
place.
2016-07-22 14:56:39 -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
Chris Lattner
a0d34852bb Revert "Fix the build."
This reverts commit 932fe9d6b9.
2016-07-17 12:35:42 -07:00
Michael Gottesman
932fe9d6b9 Fix the build.
This reverts commit 4242af7503.
This reverts commit 76cf339694.
This reverts commit e97ed133a8.
This reverts commit 66961fcda8.
2016-07-17 01:02:48 -07:00
Chris Lattner
e97ed133a8 reduce testsuite dependence on the Boolean protocol. 2016-07-16 23:23:45 -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
Chris Lattner
8746676616 Move @noescape and @autoclosure to their new places in various tests, NFC. 2016-04-15 16:05:35 -07:00
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
Max Moiseev
a49dab6bf8 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-29 12:08:52 -08:00
Daniel Duan
780b58a9a5 [Parser] update tests for 'inout' syntax adjustment 2016-02-26 01:33:22 -08:00
Max Moiseev
61c837209b Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-04 16:13:39 -08:00
Chris Lattner
e9d1857ca3 Fix rdar://21783216 QoI: Cannot convert {TYPE} to {SAME TYPE}
which was reported here: https://twitter.com/jadengeller/status/619989059046240256

The underlying problem here is that the user was defining an associated
type named "Type", and then trying to refer to it with stuff.Type.  The
problem is that stuff.Type is a reserved way to refer to the metatype.

Solve this sort of confusion by banning type members named Type (and
Protocol, while we're here) since forming a reference to them won't
work.  This produces a note that indicates that a backtick'd version
of the identifier will work, since "stuff.`Type`" will correctly form
the reference to it.

This only bans type members named Type or Protocol, but we could consider
banning all ValueDecls from being named Type or Protocol.  Module
qualification isn't widely used though, and metatypes of modules don't
really make sense at the moment.
2016-01-23 14:45:06 -08:00
Max Moiseev
f51e708a8f Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-04 12:25:25 -08:00
ken0nek
3ac60b13f5 Add spaces before and after closure arrow in test 2015-12-23 04:38:46 +09:00
Maxim Moiseev
2c95bb6d51 BooleanType => Boolean 2015-12-10 14:56:32 -08:00
Chris Lattner
ada5487153 add fixit tests to random other tests.
Swift SVN r31006
2015-08-04 20:35:36 +00:00