Commit Graph

295 Commits

Author SHA1 Message Date
Pavel Yaskevich
d912e12ba7 [ResultBuilders] Use new fix to detect and diagnose use of return statements in a result builder body 2020-11-19 17:21:00 -08:00
Pavel Yaskevich
057e836c13 [ResultBuilders] Split diagnostic about return statements into error and warning versions 2020-11-19 17:21:00 -08:00
Pavel Yaskevich
1d3f3204ca [ResultBuilders] Account of a fact that re-write of a statement can fail
If one of the statements in the result builder body fails to
apply solution, let's fail entire rewrite attempt, otherwise
type-checker would end up with AST that has null pointers for
some child nodes.

Resolves: rdar://problem/70256351
2020-11-05 16:29:58 -08:00
Slava Pestov
d5ee1da9bb Sema: Put the AvailabilityContext inside the ExportContext 2020-10-28 23:55:17 -04:00
Pavel Yaskevich
1683fb91b6 [ConstraintSystem] Remove RValueAdjustment locator element
It was used for unresolved member and `.dynamicType` references
as well as a couple of other places, but now unresolved member
references no longer need that due to new implicit "chain result"
AST node and other places could use more precise locators
e.g. new `.dynamicType` locator or `sequence element` for `for in`
loops.
2020-10-26 00:40:45 -07:00
Doug Gregor
6d41524fe6 [SE-0289] Finish renaming source code, tests to "result builders" 2020-10-20 22:18:51 -07:00
Doug Gregor
0d568a93d4 [SE-0289] Update diagnostics & many other strings to "result builders" 2020-10-20 21:44:09 -07:00
Pavel Yaskevich
461eafff54 [ConstraintSystem] NFC: Move ConstraintSystem.h to include/swift/Sema 2020-10-08 10:45:47 -07:00
Pavel Yaskevich
767c1a0ed6 [ConstraintSystem] NFC: Move SolutionResult.h to include/swift/Sema 2020-10-08 10:42:08 -07:00
Doug Gregor
f07c7d15fe [Type checker] Eliminate a use-after-free due to C++ temporaries.
Found by Brent using ASan, thank you!
2020-09-25 11:17:19 -07:00
Slava Pestov
51a4a91a13 Sema: Mark a PatternBindingDecl as implicit in the builder transform 2020-09-22 00:16:54 -04:00
Slava Pestov
d7f4b1a1bd AST: Capture list bindings now point back to their parent CaptureListExpr
We'll need this to get the right 'selfDC' when name lookup
finds a 'self' declaration in a capture list, eg

class C {
  func bar() {}
  func foo() {
    _ = { [self] in bar() }
  }
}
2020-09-18 02:59:15 -04:00
Doug Gregor
3b8656dc6f [Function builders] Add stubs for missing buildBlock and buildLimitedAvailability
The lack of these functions in a function builder is diagnosed in
different places in the type checker. Extend these diagnostics to
provide stubs in the function builder.

Thanks to Suyash for the suggestion!
2020-09-16 16:31:03 -07:00
Doug Gregor
23b6b409ce [Code completion] Fix incorrect addition of '{' in function builder results.
Noticed by Rintaro, thank you!
2020-09-16 14:18:22 -07:00
Doug Gregor
20ab640e56 [Function builders] Add code completion for build* functions.
When performing code completion inside the declaration of a type with
the function builder attribute, also include completions for all of
the possible "build" functions.
2020-09-16 11:55:09 -07:00
Frederick Kellison-Linn
b82c57a800 [Sema] Require function builders to have at least one buildBlock method
If the supplied method is not static, offer a fix-it to make it static
2020-09-05 01:46:42 -04:00
Pavel Yaskevich
dadf2b47cc [ConstraintSystem] Track invalid function builder bodies to avoid duplicate diagnostics 2020-09-02 11:15:40 -07:00
Pavel Yaskevich
c17bdaf715 [AST] Adjust Diagnostic transaction to only track pending errors 2020-09-01 13:57:03 -07:00
Pavel Yaskevich
1bfb0b0747 [ConstraintSystem] Extend invalid function body fix to cover constraint generation failures
Ignore function builder body if it emits at least one diagnostic
during constraint generation.

Resolves: rdar://problem/65983237
2020-09-01 12:23:50 -07:00
swift-ci
7daed3cf08 Merge pull request #33570 from DougGregor/function-builders-throw 2020-08-20 17:16:27 -07:00
swift-ci
316475c4ff Merge pull request #33564 from DougGregor/function-builders-remove-build-do 2020-08-20 14:13:54 -07:00
Doug Gregor
d46c4ec5c5 [Function builders] Add support for 'throw' statement. 2020-08-20 12:11:45 -07:00
Doug Gregor
9ffddc9ba6 [Function builders] Remove buildDo.
"do" blocks will always go through buildBlock().
2020-08-20 11:31:07 -07:00
Pavel Yaskevich
a12ac414a1 Merge pull request #33509 from xedin/rdar-65320500
[FunctionBuilders] Implement graceful handling of pre-check failures
2020-08-18 00:58:15 -07:00
Doug Gregor
9ef0fce7b0 [Function builders] Allow build functions to be declared elsewhere.
Make the lookup of the various `build` functions for function builders
normal, qualified name lookup. This allows (e.g.) the implementation
of many of the 'build' functions to come from a protocol extension,
making function builders more composable. Thank you to @anreitersimon
on the Swift forums for the example!
2020-08-17 15:41:33 -07:00
Pavel Yaskevich
56ef379fb8 [ConstraintSystem] Integrate IgnoreInvalidFunctionBuilderBody into the solver 2020-08-17 12:39:29 -07:00
Pavel Yaskevich
b2982fa0e5 [TypeChecker] Add a flag PreCheckFunctionBuilderRequest to suppress diagnostics 2020-08-17 12:39:29 -07:00
Pavel Yaskevich
bc319779d5 [TypeChecker] Replace invalid refs with ErrorExpr only when explicitly allowed
Add a flag to `ConstraintSystem::preCheckExpression` and subsequently
to `TypeChecker::resolveDeclRefExpr` to indicate whether it's allowed
to replace invalid member refs with `ErrorExpr`.

It is useful for diagnostics and code completion to preserve AST
in it's original state otherwise it's impossible to diagnose errors
post factum or extract `CodeCompletionExpr` when it's a child of an
invalid reference.
2020-08-17 12:39:29 -07:00
Pavel Yaskevich
fddfd6d782 [BuilderTransform] Suppress diagnostics produced by preCheckExpression 2020-08-17 12:37:17 -07:00
Pavel Yaskevich
9b33ffa828 Merge pull request #33272 from xedin/rdar-65254452
[Diagnostics] Do more checking before recording `force downcast` fix
2020-08-04 10:22:47 -07:00
Doug Gregor
f787e1bd3b [Statement checker] Simplify checkUnknownAttrRestrictions().
This routine can compute the fallthrough destination on its own. When
it does, we start correctly checking @unknown cases in function
builders, so update and expand the test accordingly.
2020-08-03 22:13:10 -07:00
Pavel Yaskevich
b47698bd54 [TypeChecker] NFC: Add debug logging before applying solution in applyFunctionBuilderBodyTransform 2020-08-03 16:46:28 -07:00
Pavel Yaskevich
6c906b0e0c [BuilderTransform] Explicitly disallow empty case statements in switch 2020-07-31 18:22:29 -07:00
Pavel Yaskevich
0cac079d00 [TypeChecker] Diagnose empty switch statements in function builder bodies
If there are no 'case' statements in the body let's try
to diagnose this situation via limited exhaustiveness check
before failing a builder transform, otherwise type-checker
might end up without any diagnostics which leads to crashes
in SILGen.

Resolves: rdar://problem/65983237
2020-07-31 16:47:40 -07:00
Doug Gregor
0c684f0192 Merge pull request #33188 from DougGregor/function-builder-comment-typo
[Function builders] Fix typo in comment.
2020-07-29 13:26:33 -07:00
Doug Gregor
28884ff180 [Function builders] Fix typo in comment. 2020-07-29 10:34:27 -07:00
Holly Borla
9baca13088 [Function Builders] Teach diagnostics about function builder body
result types in order to properly diagnose requirement failures that
aren't anchored at an expression.
2020-07-27 18:28:27 -07:00
Hamish Knight
da998bae6d Merge pull request #33037 from hamishknight/to-no-avail 2020-07-22 12:19:06 -07:00
Doug Gregor
9a8cb0570f [Function builder availability] Address review comments. 2020-07-22 09:05:58 -07:00
Hamish Knight
f40cf4cda1 [CS] Use performSyntacticDiagnosticsForTarget for function builder
Add a case for `SolutionApplicationTarget::Kind::function`
and move the logic over.
2020-07-21 15:51:07 -07:00
Doug Gregor
cde899d1d5 Merge pull request #32994 from DougGregor/function-builder-limited-availability
[Function builders] Use buildLimitedAvailability() for #available block
2020-07-20 23:30:39 -07:00
Rintaro Ishizaki
994edab26c Merge pull request #33013 from rintaro/sema-precheckfunctionbuilder-descriptor
[Sema] Descriptor for argument of PreCheckFunctionBuilderRequest
2020-07-20 19:22:40 -07:00
Rintaro Ishizaki
15796734cd [Sema] Descriptor for argument of PreCheckFunctionBuilderRequest 2020-07-20 16:44:11 -07:00
Hamish Knight
a2f9a6976b [CS] Remove outdated assertion
We used to apply builder transforms to regular
functions without using a constraint system,
which is what the assertion is primarily geared
towards. However we now use a constraint system
for such cases, with the lack of a constraint
system only being used to check if we can even
apply the transform in the first place.
2020-07-20 11:47:04 -07:00
Hamish Knight
e06ccad06d [CS] Factor out more logic into getOpenedFunctionBuilderTypeFor
Factor out the callee locator computation and
function builder opening logic.
2020-07-20 11:47:04 -07:00
Doug Gregor
9409e9de2d [Function builders] Use buildLimitedAvailability() for #available block
The use of "if #available" in function builders can subvert availability
checking if the function builder carries all type information for the
values within the "then" block outside of the "else" block. Tighten up
the model in two ways:

* Check whether the type coming out of an "if #available" references
any declarations that are not available in the outer context, to close
up the model.
* If the function builder provides a buildLimitedAvailability(_:)
operation, call that on the result of the "then" block in an "if
that it cannot leak out of the "if #available"; if it doesn't, the
check above will still fire.

Stage this in with a warning so function builders out there in the wild
can adapt. We'll upgrade the warning to an error later.

Fixes rdar://problem/65021017.
2020-07-19 21:07:15 -07:00
Rintaro Ishizaki
a6b9815d07 Merge pull request #32946 from rintaro/ide-completion-rdar65692922
[CodeCompletion] Fast completion inside function builder function
2020-07-17 11:27:08 -07:00
Rintaro Ishizaki
e953e523a0 [CodeCompletion] Fast completion inside function builder function
`PreCheckFunctionBuilderRequest` applies `PreCheckExpression` to the
expressions inside the function body. Previously it used to receive only
`AnyFunctionRef` (`FunctionDecl` or `ClosureExpr`) as the parameter.
However, when fast-completion kicks-in, it replaces the body of the
function, then tries to call `PreCheckFunctionBuilderRequest` again, with
the same function decl as before. It used to return cached "Success"
result, but it didn't actually apply `PreCheckExpression`. So any
`UnresolvedDeclRefExpr` remained unresolved.

In this patch, make `PreCheckFunctionBuilderRequest` receive "body" of the
function as well, so it doesn't return the cached result for the *previous*
body.

rdar://problem/65692922
2020-07-16 23:38:49 -07:00
Robert Widmann
53505c7ef4 Fix a Source Break With Function Builders
Before 09db2902d2, the function builder
transform used to try to detect when the builder type wasn't fully
resolved. In such a case, rather than fail the solution set as we do
currently, it would construct a null TypeLoc, then stash the type-
variable-laden builder type inside of a TypeExpr and rely on CSGen
to reinterpret that as a request to read the stashed type as it was
constructing the rest of the system. This used to rely on the ability to construct
an implicit TypeExpr node with a null type but now such a thing is
banned by the TypeExpr interface.

Instead, we have to detect this case and construct an *explicit*
TypeExpr node pointing to all the usual fake data. This node's type will
be used to resolve the build* member, but the final applied type of the
node will be the builder type we stashed earlier - hopefully with
outstanding type variables solved.

rdar://65116204
2020-07-16 18:44:54 -07:00
Doug Gregor
a2758b78dc Fix a comment. 2020-06-30 09:31:30 -07:00