Commit Graph

408 Commits

Author SHA1 Message Date
Allan Shortlidge cea706a03f AST: Improve InFlightDiagnostic::fixItRemove() heuristics.
If the fix-it would remove a range that is followed by a newline and the
remaining text on the line is empty or all whitespace then remove the entire
line. This produces better results when a fix-it removes an attribute that is
written on a line by itself.
2026-05-06 11:03:30 -07:00
Aviva Ruben 1a032f3c51 Add comments that link to #88367 2026-04-08 14:29:47 -07:00
Hamish Knight 00d8774032 [Sema] Upgrade tuple shuffle warning to error in future lang mode
This has been deprecated for a while now, flip it to an error for a
future language mode.
2025-10-29 15:14:23 +00:00
Hamish Knight 24347812f5 [Diag] Reword the tuple shuffle diagnostic
"reorder" seems a bit less jargony than "shuffle", and include the 
labels that are being reordered.
2025-10-29 15:14:23 +00:00
Henrik G. Olsson cbc0ec3b88 Add -verify-ignore-unrelated where necessary (NFC)
These are tests that fail in the next commit without this flag. This
does not add -verify-ignore-unrelated to all tests with -verify, only
the ones that would fail without it. This is NFC since this flag is
currently a no-op.
2025-10-04 14:19:52 -07:00
Hamish Knight 9f5a754b77 [Sema] Ban placeholders in typed throws
This never worked correctly and would crash in SILGen, ban the use
of placeholder types. While here, ensure we replace any ErrorTypes
with holes when solving the closure in the constraint system.
2025-09-17 20:41:20 +01:00
Pavel Yaskevich ded6158cc3 [CSSimplify] Detect when generic argument mismatch applies to argument and produce a tailed fix
The problem detection logic currently expects `generic argument #<N>`
location to always be associated with two generic types, but that
is not always the case, this locator element is sometimes used for
i.e. optional object types and pointer `Pointee` type when types
appear in argument positions. This needs to be handled specifically.

Resolves: rdar://82971941
2025-05-30 09:52:57 -07:00
Anthony Latsis 2cd90bdd69 AST: Quote attributes more consistently in DiagnosticsSema.def 2025-04-22 18:23:36 +01:00
Anthony Latsis fffa8c2f51 Diag: Abstract away some calls to DeclAttribute::getAttrName 2025-03-28 02:01:27 +00:00
Slava Pestov e9266c25d3 Sema: Fix accepts-invalid with throwing function types
We can't unconditionally skip the conformance check if the type contains type
parameters; instead, we only want to skip it in the structural resolution
stage. In interface resolution stage, we proceed by mapping the type into
the generic environment first.
2025-02-10 09:17:40 -05:00
Slava Pestov 8400b43388 Sema: Don't diagnose thrown error type that contains errors
If we have error types here, type resolution already diagnosed an
error; don't diagnose again.
2025-02-10 09:17:40 -05:00
Allan Shortlidge cb578172ea Tests: Remove -disable-availability-checking in more tests that use concurrency.
Use the `%target-swift-5.1-abi-triple` substitution to compile the tests for
deployment to the minimum OS versions required for use of _Concurrency APIs,
instead of disabling availability checking.
2024-10-19 12:35:20 -07:00
Alex Hoppen 66104395d7 [Sema/SourceKit] Emit same diagnostics for missing protocol requirements on the command line and in SourceKit
Some editors use diagnostics from SourceKit to replace build issues. This causes issues if the diagnostics from SourceKit are formatted differently than the build issues. Make sure they are rendered the same way, removing most uses of `DiagnosticsEditorMode`.

To do so, always emit the `add stubs for conformance` note (which previously was only emitted in editor mode) and remove all `; add <something>` suffixes from notes that state which requirements are missing.

rdar://129283608
2024-08-07 14:01:30 -07:00
Doug Gregor 21e5066249 Diagnose deployment constraints on type metadata involving typed throws
Fixes the rest of rdar://121530587.
2024-07-03 21:08:54 -07:00
Slava Pestov feea84dc6c Sema: Ban uncallable protocol member operators
Member operators of concrete nominal types must declare at least
one parameter with that type, like

```
struct S {
  static func +(lhs: S, rhs: Int) -> S {}
}
```

For protocol member operators, we would look for a parameter of type
`Self`, or an existential type `any P`. While the latter was
consistent with the concrete nominal type case, it was actually
wrong because then the resulting interface type does not give the
type checker any way to bind the `Self` type parameter.

There were two existing test cases that now produce errors, which I
believe is now correct. While this is technically a source break,
because these bogus operators seemingly cannot be witnessed or called,
such a protocol probably had no conforming types.

Fixes https://github.com/apple/swift/issues/73201.
2024-04-23 15:28:16 -04:00
Slava Pestov e342a38b87 Sema: Convert TypeChecker::computeCaptures() into two requests
We now compute captures of functions and default arguments
lazily, instead of as a side effect of primary file checking.

Captures of closures are computed as part of the enclosing
context, not lazily, because the type checking of a single
closure body is not lazy.

This fixes a specific issue with the `-experimental-skip-*` flags,
where functions declared after a top-level `guard` statement are
considered to have local captures, but nothing was forcing these
captures to be computed.

Fixes rdar://problem/125981663.
2024-04-20 22:16:25 -04:00
Holly Borla 9ba481ad53 [Diagnostics] Clarify the wording of error_in_future_swift_version. 2024-03-01 12:05:51 -08:00
Doug Gregor e008a4f86b Allow a throwing function to shadow a typed-throws one
This addresses a source compatibility issue with the introduction of
typed throws into the standard library.
Fixes https://github.com/apple/swift/issues/70970 / rdar://121149479.
2024-02-14 14:00:47 -08:00
Rick van Voorden f8ae46b3f3 [inclusive-language] changed sanity to soundness 2024-01-25 18:18:02 -08:00
Ben Barham 8bb1ac950b [Parse] Update error if closure has unnamed parameter to warning
This reverts commit 4ba4da45b9 and updates
the warning to a warning until Swift 6.
2024-01-19 12:42:27 -08:00
Doug Gregor b7fd43949a Remove the TypedThrows experimental feature from the remaining tests 2024-01-03 22:08:56 -08:00
Alex Hoppen 4ba4da45b9 [Parse] Error if closure has an unnamed parameter
We accepted unnamed closure parameters if the type was an array literal, dictionary literal, tuple or function (because the `[` or `(` starting the type was sufficient to disambiguate the type from the parameter’s name). This was never an accepted syntax and we should disallow it.
2023-11-28 11:07:57 -08:00
Doug Gregor aeaa523698 Merge pull request #69869 from DougGregor/rethrows-parameter-packs
Fix effects checking for rethrowing functions involving parameter packs
2023-11-15 12:21:08 -08:00
Doug Gregor 46f3ba6968 [Effects] Ensure that we use the original parameter type when determining rethrows
Rethrows checking is based on the interface type of the parameter, with
substitutions applied only when we need to figure out the thrown error
type. Fixes a regression caused by my attempt at dealing with parameter
packs in rethrows functions.
2023-11-15 09:45:24 -08:00
Doug Gregor ae1c0232c7 Fix effects checking for rethrowing functions involving parameter packs
Effects checking for rethrowing functions was using unsubstituted
interface types for the parameter list. When a rethrows function has
parameter pack arguments, the interface type may have a different
number of parameters from the resulting argument list, causing the
effects checking to bail out early.

This has been wrong since the introduction of parameter packs, but
recent refactoring of effects checking ended up promoting this bug to
a compiler crash. Fix the bug, and make sure we don't crash if the
effects checker hits an issue here.

Fixes rdar://116740385.
2023-11-14 14:28:48 -08:00
Doug Gregor eb3d7c5200 Drop throws(any Error) and throws(Never) sugar after substitution
When substitution into a function type with `throws(E)` produces either
`throws(any Error)` or `throws(Never)`, adjust the resulting function
type to the equivalent `throws` or non-throwing.
2023-11-13 14:24:13 -08:00
Doug Gregor de16b53ab4 [Typed throws] Add compatibility carve-out within rethrows functions
Allow a rethrows function to call a typed-throws function that has a
specific form that looks like it only rethrows, i.e., it is generic
over its thrown error type and carries the thrown error type from its
closure parameters to itself. This is a compatibility carve-out to
allow existing rethrows functions to move to typed throws without
breaking their clients.

Note that it is possible to write a sneaky function that passes this
check but throws when it shouldn't, so this compatibility carve-out is
phased out with the upcoming feature FullTypedThrows.
2023-11-12 02:47:39 -08:00
Doug Gregor 4bbfba790a [Typed throws] Rethrows functions always throw any Error
Rethrows functions only throw when their closure arguments throw.
However, they are free to translate the errors thrown from the closure
arguments in any way they want, and are therefore untyped.

Ensure that calls to `rethrows` functions are always treated as
throwing `any Error` if their closure arguments throw anything.
2023-11-09 10:02:21 -08:00
Doug Gregor d3ede19150 Generalize inference of Error type requirements from typed throws 2023-10-27 12:52:07 -07:00
Doug Gregor ff45fec1d8 [Typed throws] Handle key-path literal used with a function that has typed throws
When providing a key-path literal for a parameter of function type
where that function type has a generic parameter for its thrown error
type, infer `Never` for the generic argument because key paths don't
throw.

Thanks to @xedin for realizing that this would be an issue.
2023-10-27 12:52:05 -07:00
Doug Gregor b362e3f408 Add a test case ensuring that we diagnose throws(any Codable & Error). 2023-10-26 14:23:36 -07:00
Doug Gregor e4793cba65 Revert "Thrown types use "convertible to any Error" instead of "conforms to Error""
This reverts commit 6e0aeab149.
2023-10-26 13:12:47 -07:00
Doug Gregor 6e0aeab149 Thrown types use "convertible to any Error" instead of "conforms to Error"
Allow one to throw a type that is convertible to `any Error` even if it
doesn't conform to the `Error` protocol, e.g., an existential type like
`any Codable & Error`.
2023-10-24 13:23:47 -07:00
Doug Gregor 2d7eafd155 Infer error conformance for type parameters used in typed throws
The type that occurs as the thrown error type must conform to the
`Error` protocol. Infer this conformance when the type is a type
parameter in the signature of a function.
2023-10-13 16:09:44 -07:00
Doug Gregor ccf8619453 [Typed throws] Refactor thrown error subtyping check for reuse.
Lift the subtyping check for thrown error types out of the constraint
solver, so we can re-use it elsewhere.

There is a minor diagnostic change, from one that is actively
misleading (it shows a legitimate conversion that's wrong) to one that
is correct, which comes from us not treating "dropping throws" as a
legitimate way to handle equality of function types.
2023-10-09 21:21:21 -07:00
Doug Gregor 4b3d2f47d9 [Typed throws] Handle function conversions involving different thrown errors
Teach the constraint solver about the subtyping rule that permits
converting one function type to another when the effective thrown error
type of one is a subtype of the effective thrown error type of the
other, using `any Error` for untyped throws and `Never` for
non-throwing.

With minor other fixes, this allows us to use typed throws for generic
functions that carry a typed error from their arguments through to
themselves, which is in effect a typed `rethrows`:

```swift
func mapArray<T, U, E: Error>(_ array: [T], body: (T) throws(E) -> U)
throws(E) -> [U] {
  var resultArray: [U] = .init()
  for value in array {
    resultArray.append(try body(value))
  }
  return resultArray
}
```
2023-10-05 11:55:05 -07:00
Doug Gregor 31b8811167 [Typed throws] Perform access control and availability checking on thrown errors 2023-09-29 10:51:54 -07:00
Doug Gregor 54589e12f5 [Typed throws] Type-check thrown expressions against the thrown error type
Rather than always type-check the expression in a `throw` statement for
conformance to `Error`, check that it converts to the thrown error type.
2023-09-29 10:51:54 -07:00
Doug Gregor 51eed19d4b [Typed throws] Type system support for typed throws.
Add the thrown type into the AST representation of function types,
mapping from function type representations and declarations into the
appropriate thrown type. Add tests for serialization, printing, and
basic equivalence of function types that have thrown errors.
2023-09-29 10:51:53 -07:00
Nishith Shah 8e2e625543 [Diagnostics] Use imperative msg for protocol conformance & switch-case fixits
This commit changes fixit messages from a question/suggestion to an
imperative message for protocol conformances and switch-case. Addresses
https://github.com/apple/swift/issues/67510.
2023-08-13 22:34:26 -07:00
Allan Shortlidge b9262e5297 Sema: Allow default arguments to access @usableFromInline decls.
The restriction that default arguments be disallowed from accessing
`@usableFromInline` decls is overbearing for library developers who need to
write non-trivial code to compute a default value, since it forces them to
either write a verbose closure inline in the function signature or expose a
`public` helper function which unnecessarily expands API surface. A
`@usableFromInline` function a more reasonable way to encapsulate a verbose
default value computation.

This reverses the semantic changes included in https://github.com/apple/swift/pull/15666.

Resolves rdar://112093794.
2023-07-12 12:47:06 -07:00
Anthony Latsis 14b70f306b DiagnosticVerifier: Default expected fix-it start line to the diagnostic's 2023-03-08 12:10:27 +03:00
Joe Groff 704a4363d7 C++ parser: support borrowing and consuming modifiers.
And adjust contextual parameter modifier parsing in general to be more
properly contextual, so we don't have to reserve `__shared` or `__owned`,
or their successor spellings, as argument labels anymore.
2023-02-28 09:16:45 -08:00
Holly Borla 6725f365ba [Variadic Generics] Update tests for syntax changes. 2023-01-07 09:50:14 -08:00
Holly Borla 06486f627c [NFC] Add a test case for missing 'try' diagnostics with existential opening. 2022-10-25 19:57:51 -07:00
Suyash Srijan d124b3581b [Sema] Don't ignore implicit AST nodes in diagnoseUnhandledThrowSite (#61392) 2022-10-06 09:47:50 +01:00
Slava Pestov 1e12d0a31c Sema: Reject PackExpansionType in unsupported positions 2022-09-07 12:35:54 -04:00
Slava Pestov 79ed990728 AST: Replace TupleTypeRepr's ellipsis with PackExpansionTypeRepr 2022-09-07 12:35:54 -04:00
Anthony Latsis edd4010664 Gardening: Migrate test suite to GH issues: decl/func 2022-08-26 03:26:32 +03:00
Hamish Knight cfd24354e1 [Sema] Fix missing operator diagnostic logic
Introduce a `getTopmostDeclarationDeclContext`
utility to ensure we ensure we don't visit a
`nullptr` DeclContext for an erroneous module
under `-experimental-allow-module-with-compiler-errors`.

Additionally, tweak the insertion location such
that we insert at the start of any attributes
present.

rdar://97267326
2022-07-28 12:55:53 +01:00