Commit Graph

71 Commits

Author SHA1 Message Date
Amritpan Kaur
15c219cd70 [Tests] Add tests. 2025-03-19 10:54:09 -07:00
Hamish Knight
b840730958 [IDE] Pass LangOptions to ide::isSourceInputComplete
Ensure we account for things like the enablement
of bare slash regex literals.
2024-10-17 14:04:34 +01:00
Hamish Knight
6a435960b7 Requestify regex pattern parsing
Instead of doing the pattern parsing in both the
C++ parser and ASTGen, factor out the parsing into
a request that returns the pattern to emit, regex
type, and version. This can then be lazily run
during type-checking.
2024-10-11 19:25:58 +01:00
Hamish Knight
7971056e21 [Sema] Fold SequenceExpr in pre-checking pre-walk
Doing it in the post-walk meant we ended up
walking the children twice, which lead to duplicate
diagnostics and incorrect inference of the
level of application for function references. Move
it to the pre-walk, ensuring that we resolve any
operator references before folding.
2024-07-29 00:07:45 +01:00
Rintaro Ishizaki
0e122544ca [CodeCompletion] Fix completion for 'catch' pattern bound values
Previously code completion for 'catch' pattern bound values didn't work
correctly because code completion type checker fails to type check the
value decl in the pattern.
That was because the body of the 'do' statement is not type checked, so
the thrown error is not determined, then falled backed to the default
'Never', which doesn't matches any patterns.
To resolve this, always type check the body when typechecking 'catch'
patterns. Also, pretends 'do {}' throws 'any Error' even without
any throwing expressions in the body.

rdar://126699879
2024-04-22 11:09:12 -07:00
Doug Gregor
ea836389f7 Suppress warnings about Never error types in catch clauses
Enabling typed throws has introduced some spurious warnings about error
values of `Never` type on top of the custom "unreachable" diagnostics
for catch clauses, so suppress the new warnings---they aren't helpful.
2024-01-04 09:37:35 -08:00
Rintaro Ishizaki
47f18d492e [ASTGen] Move regex literal parsing from SwiftCompilerSources to ASTGen
ASTGen always builds with the host Swift compiler, without requiring
bootstrapping, and is enabled in more places. Move the regex literal
parsing logic there so it is enabled in more host environments, and
makes use of CMake's Swift support. Enable all of the regex literal
tests when ASTGen is built, to ensure everything is working.

Remove the "AST" and "Parse" Swift modules from SwiftCompilerSources,
because they are no longer needed.
2023-11-16 10:59:23 -08:00
Pavel Yaskevich
a987861d1c Merge pull request #65806 from xedin/rdar-108977234
[CSSimplify] Detect and diagnose conformance failures related to AnyHashable conversion
2023-05-16 09:56:41 -07:00
Rintaro Ishizaki
9fc1521aeb [Macros/Parser] Attributes on MacroExpansionDecl
* Parse `#<identifier>` attribute list as a `MacroExpansionDecl`
  regardless of the position
* Diagnose whitespaces between `#` and the macro name.
* Correctly attach attributes to `MacroExpansionDecl`
* Fix `OrigDeclAttributes` to handle modifiers (use `getLocation()`
  instead of `AtLoc`.)

Type checking is a TODO

rdar://107386648
2023-05-11 12:04:35 -07:00
Pavel Yaskevich
58ffca8f74 [ConstraintSystem] Don't produce partially matching note if none of the overloads matched 2023-05-09 14:11:38 -07:00
Hamish Knight
68075696eb [CS] A couple of minor improvements to operator diagnostics
- Simplify the fix locator when looking for a
fix already present in a pattern match, this
avoids us emitting both a diagnostic for the
argument conversion, and for a conformance failure.
- Include tuples in the diagnostic logic where
we emit a generic "operator cannot be applied"
diagnostic, as a conformance diagnostic is
unlikely to be helpful in that case.
2023-05-04 14:53:57 +01:00
Hamish Knight
af7134b884 Fully enable ExperimentalStringProcessing
Previously we would only enable by default when
`parseArgs` was called. However this wouldn't
enable it for clients such as LLDB, who provide
their own invocation. Switch the default to `true`
in the `LangOptions`, and remove some redundant
uses of `-enable-experimental-string-processing`.
The frontend flag remains, as it may be useful to
disable.

rdar://107419385
rdar://101765556
2023-03-31 18:10:39 +01:00
Richard Wei
833338f9ce [Macros] Top-level freestanding macros (#63553)
Allow freestanding macros to be used at top-level.
- Parse top-level `#…` as `MacroExpansionDecl` when we are not in scripting mode.
- Add macro expansion decls to the source lookup cache with name-driven lazy expansion. Not supporting arbitrary name yet.
- Experimental support for script mode and brace-level declaration macro expansions: When type-checking a `MacroExpansionExpr`, assign it a substitute `MacroExpansionDecl` if the macro reference resolves to a declaration macro. This doesn’t work quite fully yet and will be enabled in a future fix.
2023-03-06 07:15:20 -08:00
Pavel Yaskevich
bb1da5e599 [CSFix] Diagnose extraneous force unwraps in ambiguous context 2023-02-24 23:48:03 -08:00
Alex Hoppen
17d47bb691 [Sema] Don’t fail constraint generation if a closure contains an ErrorExpr
It appears like this was missed in #60062.
2023-01-30 17:36:36 +01:00
Doug Gregor
0c5436211c Merge pull request #62768 from DougGregor/macro-omnibus 2023-01-03 07:13:16 -08:00
Doug Gregor
8cbae04918 One last little test fix 2023-01-02 21:22:05 -08:00
Doug Gregor
1b14b5d356 Fixup some test cases 2023-01-02 21:22:05 -08:00
Doug Gregor
6bb9cb8b5d [Macros] Always parse macro expansions, diagnose later
Always parse macro expansions, regardless of language mode, and
eliminate the fallback path for very, very, very old object literals
like `#Color`. Instead, check for the feature flag for macro
declaration and at macro expansion time, since this is a semantic
restriction.

While here, refactor things so the vast majority of the macro-handling
logic still applies even if the Swift Swift parser is disabled. Only
attempts to expand the macro will fail. This allows us to enable the
macro-diagnostics test everywhere.
2023-01-02 21:22:04 -08:00
Pavel Yaskevich
697dfbae96 [TypeChecker] Enable result builder AST transform by default 2022-12-21 10:31:30 -08:00
Hamish Knight
85c41841e5 [test] NFC: Add missing REQUIRES
This test uses regex literals, so needs the
regex parsing code to be available.

rdar://101486764
2022-10-24 17:45:01 +01:00
Doug Gregor
e263b0758a Ensure that there is at least one parse error in this test 2022-09-30 17:24:54 -07:00
Hamish Knight
cebcbb0767 [CS] Improve diagnostics when buildPartialBlock is unavailable
If `buildBlock` is also unavailable, or the
builder itself is unavailable, continue to solve
using `buildPartialBlock` to get better
diagnostics.

This behavior technically differs from what is
specified in SE-0348, but only affects the invalid
case where no builder methods are available to use.

In particular, this improves diagnostics for
RegexComponentBuilder when the deployment target
is too low. Previously we would try to solve using
`buildBlock` (as `buildPartialBlock` is unavailable),
but RegexComponentBuilder only defines `buildBlock`
for the empty body case, leading to unhelpful
diagnostics that ultimately preferred not to use
the result builder at all.

rdar://97533700
2022-08-26 19:45:03 +01:00
Alex Hoppen
e14fa7291f [CS] Don’t fail constraint generation for ErrorExpr or if type fails to resolve
Instead of failing constraint generation by returning `nullptr` for an `ErrorExpr` or returning a null type when a type fails to be resolved, return a fresh type variable. This allows the constraint solver to continue further and produce more meaningful diagnostics.

Most importantly, it allows us to produce a solution where previously constraint generation for a syntactic element had failed, which is required to type check multi-statement closures in result builders inside the constraint system.
2022-07-20 09:46:12 +02:00
Hamish Knight
c33f363956 [test] Update Regex type for flattened captures 2022-07-06 23:01:22 +01:00
Hamish Knight
af713bc7e9 Merge pull request #59776 from hamishknight/loc-step 2022-07-01 10:39:14 +01:00
Stephen Canon
9afbddf2c3 Remove use of ~= in bare-slash-regex tests. 2022-06-30 15:23:37 -04:00
Hamish Knight
3ead014478 [test] Update a couple of regex diagnostic locations
The parser recovery work gives these slightly
better locations.
2022-06-29 15:27:54 +01:00
Hamish Knight
50b01fa66f Merge pull request #59525 from hamishknight/watch-this-space 2022-06-28 22:41:34 +01:00
Hamish Knight
c56ea461b6 [Sema] Add fix-it to import RegexBuilder
For code such as the following:

```
let r = Regex {
  /abc/
}
```

If RegexBuilder has not been imported, emit a
specialized diagnostic and fix-it to add
`import RegexBuilder` to the file.

Unfortunately we're currently prevented from
emitting the specialized diagnostic in cases where
the builder contains references to RegexBuilder
types, such as:

```
let r = Regex {
  Capture {
    /abc/
  }
}
```

This is due to the fact that we bail from CSGen
due to the reference to `Capture` being turned
into an `ErrorExpr`. We ought to be able to
handle solving in the presence of such errors, but
for now I'm leaving it as future work.

rdar://93176036
2022-06-28 11:38:41 +01:00
Hamish Knight
6c091a5dc2 [test] Re-enable forward-slash-regex-skipping-allowed.swift
Adjust to account for output differences between
asserts and non-asserts builds.

rdar://95806819
2022-06-24 13:49:42 +01:00
Andrew Trick
7849345fe3 Temporarily disable test/StringProcessing/Parse/forward-slash-regex-skipping-allowed.swift
Test fails after:
Merge pull request #59641 from hamishknight/no-skip-slash
Commit e610a69dac by github
2022-06-23 14:28:43 -07:00
Hamish Knight
8d59eb08df [Parse] Ban trailing space for /.../ regex literals
Ban space and tab as the last character of a
`/.../` regex literal, unless escaped with a
backslash. This matches the banning of space and
tab as the first character, and helps avoid breaking
source in even more cases.
2022-06-23 20:38:28 +01:00
Hamish Knight
515945fc6d [Parse] Avoid skipping bodies with /.../ regex literals
While skipping, if we encounter a token that looks
like it could be the start of a `/.../` regex
literal, fall back to parsing the function or type
body normally, as such a token could become a
regex literal. As such, it could treat `{` and
`}` as literal, or otherwise have contents that
would be lexically invalid Swift.

To avoid falling back in too many cases, we apply
the existing regex literal heuristics. Cases that
pass the heuristic fall back to regular parsing.
Cases that fail the heuristic are further checked
to make sure they wouldn't contain an unbalanced
`{` or `}`, but otherwise are allowed to be
skipped. This allows us to continue skipping for
most occurrences of infix and prefix `/`.

This is meant as a lower risk workaround to fix the
the issue, we ought to go back to handling regex
literals in the lexer.

Resolves rdar://95354010
2022-06-22 20:05:21 +01:00
Hamish Knight
4ee6a84383 Merge pull request #58835 from hamishknight/allow-prefix-slash 2022-05-12 17:56:32 +01:00
Hamish Knight
350a01aeb0 [Parse] Expand unbalanced ) regex literal heuristic
Previously we would only check for a starting
character of `)` when performing a tentative
lex of a regex literal. Expand this to cover the
entire range of the regex literal, ensuring to
take escapes and custom character classes into
account.
2022-05-12 11:49:11 +01:00
Hamish Knight
9c62319be1 [Parse] Lenient prefix / parsing
Treat a prefix operator containing `/` the same as
the unapplied infix operator case, where we
tentatively lex. This means that we bail if there
is no closing `/` or the starting character is
invalid. This leaves binary operator containing
`/` in expression position as the last place where
we know that we definitely have a regex literal.
2022-05-12 11:49:10 +01:00
Hamish Knight
105ab3149a [Parse] Re-allow prefix operators containing / 2022-05-12 11:49:09 +01:00
Hamish Knight
e234f80f14 Track regex literal source locations in SourceManager
This is unfortunately needed to ensure we correctly
re-lex regex literal tokens correctly, which is
needed for diagnostic logic to correctly compute
source ranges.

rdar://92469692
2022-05-11 10:45:39 +01:00
Hamish Knight
fb0ba6ab00 Merge pull request #58505 from hamishknight/regulation-grammar 2022-05-05 15:34:53 +01:00
Richard Wei
11154e5ff6 [Sema] Allow code to shadow definitions in implicit _StringProcessing module.
Treat _StringProcessing decls as if it were declared in the Swift module, just like how _Concurrency is treated (#34642).
2022-05-02 16:31:52 -07:00
Hamish Knight
ba28b6a19b [Parse] Split prefix operators from regex in the lexer
Teach the lexer not to consider `/` an operator
character when attempting to re-lex a regex
literal. This allows us to split off a prefix
operator.

Previously this was done after-the-fact in the
parser, but that didn't cover the unapplied infix
operator case, and didn't form a `tok::amp_prefix`
for `foo(&/.../)`, which led to a suboptimal
diagnostic.

This also now means we'll split an operator for
cases such as `foo(!/^/)` rather than treating it
as an unapplied infix operator.

rdar://92469917
2022-04-29 10:53:56 +01:00
Hamish Knight
f9a6a8b7e7 [test] Add some additional /.../ tests 2022-04-29 10:53:56 +01:00
Hamish Knight
5c3f6db997 [test] Update for new string processing changes
We now define a `~=` operator for `Regex`.
2022-04-28 18:45:52 +01:00
Rintaro Ishizaki
d292a95296 [SwiftCompiler/Regex] Use bridged DiagnosticEngine for error reporting
This fixes:
 * An issue where the diagnostic messages were leaked
 * Diagnose at correct position inside the regex literal

To do this:
 * Introduce 'Parse' SwiftCompiler module that is a bridging layer
   between '_CompilerRegexParser' and C++ libParse
 * Move libswiftParseRegexLiteral and libswiftLexRegexLiteral to 'Parse'

Also this change makes 'SwiftCompilerSources/Package.swift' be configured
by CMake so it can actually be built with 'swift-build'.

rdar://92187284
2022-04-22 22:53:46 -07:00
Hamish Knight
30fc57437e [Sema] Diagnose regex literals if Regex<Output> is unavailable
Because we don't form a type-checked call to the
Regex initializer in the AST, we need to explicitly
handle the availability checking for `Regex<Output>`
and the initializer we're implicitly calling.

rdar://92156542
2022-04-22 14:01:15 +01:00
Richard Wei
72ebcded98 Integrate newer string processing (a0ed7e1)
Friend PR: apple/swift-experimental-string-processing#282.

Also remove the `-enable-experimental-pairwise-build-block` flag when building regex modules as the feature is already on by default.
2022-04-15 12:01:45 -07:00
Hamish Knight
9aa315a416 Add -disable-experimental-string-processing
Introduce a frontend flag that disables
experimental string processing, including `/.../`
literals.
2022-04-13 13:26:53 +01:00
Hamish Knight
f1a799037e [Parse] Introduce /.../ regex literals
Start parsing regex literals with `/.../`
delimiters.

rdar://83253726
2022-04-12 16:03:49 +01:00
Hamish Knight
5a8dff0a76 [Parse] Emit error on prefix operator containing /
When forward slash regex is enabled, start emitting
an error on prefix operators containing the
`/` character.
2022-04-12 16:03:48 +01:00