-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathTypeChecker.cpp
732 lines (623 loc) · 25 KB
/
TypeChecker.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//===--- TypeChecker.cpp - Type Checking ----------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements the swift::performTypeChecking entry point for
// semantic analysis.
//
//===----------------------------------------------------------------------===//
#include "swift/Subsystems.h"
#include "TypeChecker.h"
#include "TypeCheckDecl.h"
#include "TypeCheckObjC.h"
#include "TypeCheckType.h"
#include "CodeSynthesis.h"
#include "MiscDiagnostics.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/ASTVisitor.h"
#include "swift/AST/Attr.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/AST/ExistentialLayout.h"
#include "swift/AST/Identifier.h"
#include "swift/AST/ImportCache.h"
#include "swift/AST/Initializer.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/Statistic.h"
#include "swift/Basic/STLExtras.h"
#include "swift/Parse/Lexer.h"
#include "swift/Sema/IDETypeChecking.h"
#include "swift/Strings.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/PointerUnion.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/ADT/Twine.h"
#include <algorithm>
using namespace swift;
ProtocolDecl *TypeChecker::getProtocol(ASTContext &Context, SourceLoc loc,
KnownProtocolKind kind) {
auto protocol = Context.getProtocol(kind);
if (!protocol && loc.isValid()) {
Context.Diags.diagnose(loc, diag::missing_protocol,
Context.getIdentifier(getProtocolName(kind)));
}
if (protocol && protocol->isInvalid()) {
return nullptr;
}
return protocol;
}
ProtocolDecl *TypeChecker::getLiteralProtocol(ASTContext &Context, Expr *expr) {
if (isa<ArrayExpr>(expr))
return TypeChecker::getProtocol(
Context, expr->getLoc(), KnownProtocolKind::ExpressibleByArrayLiteral);
if (isa<DictionaryExpr>(expr))
return TypeChecker::getProtocol(
Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByDictionaryLiteral);
if (!isa<LiteralExpr>(expr))
return nullptr;
if (isa<NilLiteralExpr>(expr))
return TypeChecker::getProtocol(Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByNilLiteral);
if (isa<IntegerLiteralExpr>(expr))
return TypeChecker::getProtocol(
Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByIntegerLiteral);
if (isa<FloatLiteralExpr>(expr))
return TypeChecker::getProtocol(
Context, expr->getLoc(), KnownProtocolKind::ExpressibleByFloatLiteral);
if (isa<BooleanLiteralExpr>(expr))
return TypeChecker::getProtocol(
Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByBooleanLiteral);
if (const auto *SLE = dyn_cast<StringLiteralExpr>(expr)) {
if (SLE->isSingleUnicodeScalar())
return TypeChecker::getProtocol(
Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByUnicodeScalarLiteral);
if (SLE->isSingleExtendedGraphemeCluster())
return getProtocol(
Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByExtendedGraphemeClusterLiteral);
return TypeChecker::getProtocol(
Context, expr->getLoc(), KnownProtocolKind::ExpressibleByStringLiteral);
}
if (isa<InterpolatedStringLiteralExpr>(expr))
return TypeChecker::getProtocol(
Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByStringInterpolation);
if (auto E = dyn_cast<MagicIdentifierLiteralExpr>(expr)) {
switch (E->getKind()) {
#define MAGIC_STRING_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case MagicIdentifierLiteralExpr::NAME: \
return TypeChecker::getProtocol( \
Context, expr->getLoc(), \
KnownProtocolKind::ExpressibleByStringLiteral);
#define MAGIC_INT_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case MagicIdentifierLiteralExpr::NAME: \
return TypeChecker::getProtocol( \
Context, expr->getLoc(), \
KnownProtocolKind::ExpressibleByIntegerLiteral);
#define MAGIC_POINTER_IDENTIFIER(NAME, STRING, SYNTAX_KIND) \
case MagicIdentifierLiteralExpr::NAME: \
return nullptr;
#include "swift/AST/MagicIdentifierKinds.def"
}
}
if (auto E = dyn_cast<ObjectLiteralExpr>(expr)) {
switch (E->getLiteralKind()) {
case ObjectLiteralExpr::colorLiteral:
return TypeChecker::getProtocol(
Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByColorLiteral);
case ObjectLiteralExpr::fileLiteral:
return TypeChecker::getProtocol(
Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByFileReferenceLiteral);
case ObjectLiteralExpr::imageLiteral:
return TypeChecker::getProtocol(
Context, expr->getLoc(),
KnownProtocolKind::ExpressibleByImageLiteral);
}
}
return nullptr;
}
DeclName TypeChecker::getObjectLiteralConstructorName(ASTContext &Context,
ObjectLiteralExpr *expr) {
switch (expr->getLiteralKind()) {
case ObjectLiteralExpr::colorLiteral: {
return DeclName(Context, DeclBaseName::createConstructor(),
{ Context.getIdentifier("_colorLiteralRed"),
Context.getIdentifier("green"),
Context.getIdentifier("blue"),
Context.getIdentifier("alpha") });
}
case ObjectLiteralExpr::imageLiteral: {
return DeclName(Context, DeclBaseName::createConstructor(),
{ Context.getIdentifier("imageLiteralResourceName") });
}
case ObjectLiteralExpr::fileLiteral: {
return DeclName(Context, DeclBaseName::createConstructor(),
{ Context.getIdentifier("fileReferenceLiteralResourceName") });
}
}
llvm_unreachable("unknown literal constructor");
}
ModuleDecl *TypeChecker::getStdlibModule(const DeclContext *dc) {
if (auto *stdlib = dc->getASTContext().getStdlibModule()) {
return stdlib;
}
return dc->getParentModule();
}
void swift::bindExtensions(ModuleDecl &mod) {
// Utility function to try and resolve the extended type without diagnosing.
// If we succeed, we go ahead and bind the extension. Otherwise, return false.
auto tryBindExtension = [&](ExtensionDecl *ext) -> bool {
assert(!ext->canNeverBeBound() &&
"Only extensions that can ever be bound get here.");
if (auto nominal = ext->computeExtendedNominal()) {
nominal->addExtension(ext);
return true;
}
return false;
};
// Phase 1 - try to bind each extension, adding those whose type cannot be
// resolved to a worklist.
SmallVector<ExtensionDecl *, 8> worklist;
for (auto file : mod.getFiles()) {
auto *SF = dyn_cast<SourceFile>(file);
if (!SF)
continue;
auto visitTopLevelDecl = [&](Decl *D) {
if (auto ED = dyn_cast<ExtensionDecl>(D))
if (!tryBindExtension(ED))
worklist.push_back(ED);;
};
for (auto item : SF->getTopLevelItems()) {
if (auto D = item.dyn_cast<Decl *>())
visitTopLevelDecl(D);
}
for (auto *D : SF->getHoistedDecls())
visitTopLevelDecl(D);
}
// Phase 2 - repeatedly go through the worklist and attempt to bind each
// extension there, removing it from the worklist if we succeed.
bool changed;
do {
changed = false;
auto last = std::remove_if(worklist.begin(), worklist.end(),
tryBindExtension);
if (last != worklist.end()) {
worklist.erase(last, worklist.end());
changed = true;
}
} while(changed);
// Any remaining extensions are invalid. They will be diagnosed later by
// typeCheckDecl().
}
static void typeCheckDelayedFunctions(SourceFile &SF) {
unsigned currentFunctionIdx = 0;
while (currentFunctionIdx < SF.DelayedFunctions.size()) {
auto *AFD = SF.DelayedFunctions[currentFunctionIdx];
assert(!AFD->getDeclContext()->isLocalContext());
(void) AFD->getTypecheckedBody();
++currentFunctionIdx;
}
SF.DelayedFunctions.clear();
}
void swift::performTypeChecking(SourceFile &SF) {
return (void)evaluateOrDefault(SF.getASTContext().evaluator,
TypeCheckSourceFileRequest{&SF}, {});
}
/// If any of the imports in this source file was @preconcurrency but
/// there were no diagnostics downgraded or suppressed due to that
/// @preconcurrency, suggest that the attribute be removed.
static void diagnoseUnnecessaryPreconcurrencyImports(SourceFile &sf) {
switch (sf.Kind) {
case SourceFileKind::Interface:
case SourceFileKind::SIL:
return;
case SourceFileKind::Library:
case SourceFileKind::Main:
case SourceFileKind::MacroExpansion:
break;
}
ASTContext &ctx = sf.getASTContext();
if (ctx.TypeCheckerOpts.SkipFunctionBodies != FunctionBodySkipping::None)
return;
for (const auto &import : sf.getImports()) {
if (import.options.contains(ImportFlags::Preconcurrency) &&
import.importLoc.isValid() &&
!sf.hasImportUsedPreconcurrency(import)) {
ctx.Diags.diagnose(
import.importLoc, diag::remove_predates_concurrency_import,
import.module.importedModule->getName())
.fixItRemove(import.preconcurrencyRange);
}
}
}
evaluator::SideEffect
TypeCheckSourceFileRequest::evaluate(Evaluator &eval, SourceFile *SF) const {
assert(SF && "Source file cannot be null!");
assert(SF->ASTStage != SourceFile::TypeChecked &&
"Should not be re-typechecking this file!");
// Eagerly build the top-level scopes tree before type checking
// because type-checking expressions mutates the AST and that throws off the
// scope-based lookups. Only the top-level scopes because extensions have not
// been bound yet.
auto &Ctx = SF->getASTContext();
SF->getScope()
.buildEnoughOfTreeForTopLevelExpressionsButDontRequestGenericsOrExtendedNominals();
BufferIndirectlyCausingDiagnosticRAII cpr(*SF);
// Could build scope maps here because the AST is stable now.
{
FrontendStatsTracer tracer(Ctx.Stats,
"Type checking and Semantic analysis");
if (!Ctx.LangOpts.DisableAvailabilityChecking) {
// Build the type refinement hierarchy for the primary
// file before type checking.
TypeChecker::buildTypeRefinementContextHierarchy(*SF);
}
// Type check the top-level elements of the source file.
for (auto D : SF->getTopLevelDecls()) {
if (auto *TLCD = dyn_cast<TopLevelCodeDecl>(D)) {
TypeChecker::typeCheckTopLevelCodeDecl(TLCD);
TypeChecker::contextualizeTopLevelCode(TLCD);
} else {
TypeChecker::typeCheckDecl(D);
}
}
typeCheckDelayedFunctions(*SF);
}
diagnoseUnnecessaryPreconcurrencyImports(*SF);
// Check to see if there are any inconsistent imports.
evaluateOrDefault(
Ctx.evaluator,
CheckInconsistentImplementationOnlyImportsRequest{SF->getParentModule()},
{});
evaluateOrDefault(
Ctx.evaluator,
CheckInconsistentSPIOnlyImportsRequest{SF},
{});
evaluateOrDefault(
Ctx.evaluator,
CheckInconsistentWeakLinkedImportsRequest{SF->getParentModule()}, {});
// Perform various AST transforms we've been asked to perform.
if (!Ctx.hadError() && Ctx.LangOpts.DebuggerTestingTransform)
performDebuggerTestingTransform(*SF);
if (!Ctx.hadError() && Ctx.LangOpts.PCMacro)
performPCMacro(*SF);
// Playground transform knows to look out for PCMacro's changes and not
// to playground log them.
if (!Ctx.hadError() && Ctx.LangOpts.PlaygroundTransform)
performPlaygroundTransform(*SF, Ctx.LangOpts.PlaygroundHighPerformance);
return std::make_tuple<>();
}
void swift::performWholeModuleTypeChecking(SourceFile &SF) {
auto &Ctx = SF.getASTContext();
FrontendStatsTracer tracer(Ctx.Stats,
"perform-whole-module-type-checking");
switch (SF.Kind) {
case SourceFileKind::Library:
case SourceFileKind::Main:
case SourceFileKind::MacroExpansion:
diagnoseObjCMethodConflicts(SF);
diagnoseObjCUnsatisfiedOptReqConflicts(SF);
diagnoseUnintendedObjCMethodOverrides(SF);
diagnoseAttrsAddedByAccessNote(SF);
return;
case SourceFileKind::SIL:
case SourceFileKind::Interface:
// SIL modules and .swiftinterface files don't benefit from whole-module
// ObjC checking - skip it.
return;
}
}
void swift::loadDerivativeConfigurations(SourceFile &SF) {
if (!isDifferentiableProgrammingEnabled(SF))
return;
auto &Ctx = SF.getASTContext();
FrontendStatsTracer tracer(Ctx.Stats,
"load-derivative-configurations");
class DerivativeFinder : public ASTWalker {
public:
DerivativeFinder() {}
PreWalkAction walkToDeclPre(Decl *D) override {
if (auto *afd = dyn_cast<AbstractFunctionDecl>(D)) {
for (auto *derAttr : afd->getAttrs().getAttributes<DerivativeAttr>()) {
// Resolve derivative function configurations from `@derivative`
// attributes by type-checking them.
(void)derAttr->getOriginalFunction(D->getASTContext());
}
}
return Action::Continue();
}
};
switch (SF.Kind) {
case SourceFileKind::Library:
case SourceFileKind::MacroExpansion:
case SourceFileKind::Main: {
DerivativeFinder finder;
SF.walkContext(finder);
return;
}
case SourceFileKind::SIL:
case SourceFileKind::Interface:
return;
}
}
bool swift::isAdditiveArithmeticConformanceDerivationEnabled(SourceFile &SF) {
auto &ctx = SF.getASTContext();
// Return true if `AdditiveArithmetic` derived conformances are explicitly
// enabled.
if (ctx.LangOpts.hasFeature(Feature::AdditiveArithmeticDerivedConformances))
return true;
// Otherwise, return true iff differentiable programming is enabled.
// Differentiable programming depends on `AdditiveArithmetic` derived
// conformances.
return isDifferentiableProgrammingEnabled(SF);
}
Type swift::performTypeResolution(TypeRepr *TyR, ASTContext &Ctx,
bool isSILMode, bool isSILType,
GenericSignature GenericSig,
GenericParamList *GenericParams,
DeclContext *DC, bool ProduceDiagnostics) {
TypeResolutionOptions options = None;
if (isSILMode)
options |= TypeResolutionFlags::SILMode;
if (isSILType)
options |= TypeResolutionFlags::SILType;
Optional<DiagnosticSuppression> suppression;
if (!ProduceDiagnostics)
suppression.emplace(Ctx.Diags);
return TypeResolution::forInterface(
DC, GenericSig, options,
[](auto unboundTy) {
// FIXME: Don't let unbound generic types escape type resolution.
// For now, just return the unbound generic type.
return unboundTy;
},
// FIXME: Don't let placeholder types escape type resolution.
// For now, just return the placeholder type.
PlaceholderType::get)
.resolveType(TyR, GenericParams);
}
namespace {
class BindGenericParamsWalker : public ASTWalker {
DeclContext *dc;
GenericParamList *params;
public:
BindGenericParamsWalker(DeclContext *dc,
GenericParamList *params)
: dc(dc), params(params) {}
PreWalkAction walkToTypeReprPre(TypeRepr *T) override {
if (auto *ident = dyn_cast<IdentTypeRepr>(T)) {
auto firstComponent = ident->getComponentRange().front();
auto name = firstComponent->getNameRef().getBaseIdentifier();
if (auto *paramDecl = params->lookUpGenericParam(name))
firstComponent->setValue(paramDecl, dc);
}
return Action::Continue();
}
};
}
/// Expose TypeChecker's handling of GenericParamList to SIL parsing.
GenericSignature
swift::handleSILGenericParams(GenericParamList *genericParams,
DeclContext *DC) {
if (genericParams == nullptr)
return nullptr;
SmallVector<GenericParamList *, 2> nestedList;
for (auto *innerParams = genericParams;
innerParams != nullptr;
innerParams = innerParams->getOuterParameters()) {
nestedList.push_back(innerParams);
}
std::reverse(nestedList.begin(), nestedList.end());
BindGenericParamsWalker walker(DC, genericParams);
for (unsigned i = 0, e = nestedList.size(); i < e; ++i) {
auto genericParams = nestedList[i];
genericParams->setDepth(i);
genericParams->walk(walker);
}
auto request = InferredGenericSignatureRequest{
/*parentSig=*/nullptr,
nestedList.back(), WhereClauseOwner(),
{}, {}, /*allowConcreteGenericParams=*/true};
return evaluateOrDefault(DC->getASTContext().evaluator, request,
GenericSignatureWithError()).getPointer();
}
void swift::typeCheckPatternBinding(PatternBindingDecl *PBD,
unsigned bindingIndex,
bool leaveClosureBodiesUnchecked) {
assert(!PBD->isInitializerChecked(bindingIndex) &&
PBD->getInit(bindingIndex));
auto &Ctx = PBD->getASTContext();
DiagnosticSuppression suppression(Ctx.Diags);
TypeCheckExprOptions options;
if (leaveClosureBodiesUnchecked)
options |= TypeCheckExprFlags::LeaveClosureBodyUnchecked;
TypeChecker::typeCheckPatternBinding(PBD, bindingIndex,
/*patternType=*/Type(), options);
}
bool swift::typeCheckASTNodeAtLoc(TypeCheckASTNodeAtLocContext TypeCheckCtx,
SourceLoc TargetLoc) {
auto &Ctx = TypeCheckCtx.getDeclContext()->getASTContext();
DiagnosticSuppression suppression(Ctx.Diags);
return !evaluateOrDefault(
Ctx.evaluator, TypeCheckASTNodeAtLocRequest{TypeCheckCtx, TargetLoc},
true);
}
bool swift::typeCheckForCodeCompletion(
constraints::SolutionApplicationTarget &target, bool needsPrecheck,
llvm::function_ref<void(const constraints::Solution &)> callback) {
return TypeChecker::typeCheckForCodeCompletion(target, needsPrecheck,
callback);
}
Expr *swift::resolveDeclRefExpr(UnresolvedDeclRefExpr *UDRE, DeclContext *Context,
bool replaceInvalidRefsWithErrors) {
return TypeChecker::resolveDeclRefExpr(UDRE, Context, replaceInvalidRefsWithErrors);
}
void TypeChecker::checkForForbiddenPrefix(ASTContext &C, DeclBaseName Name) {
if (C.TypeCheckerOpts.DebugForbidTypecheckPrefix.empty())
return;
// Don't touch special names or empty names.
if (Name.isSpecial() || Name.empty())
return;
StringRef Str = Name.getIdentifier().str();
if (Str.startswith(C.TypeCheckerOpts.DebugForbidTypecheckPrefix)) {
llvm::report_fatal_error(Twine("forbidden typecheck occurred: ") + Str);
}
}
DeclTypeCheckingSemantics
TypeChecker::getDeclTypeCheckingSemantics(ValueDecl *decl) {
// Check for a @_semantics attribute.
if (auto semantics = decl->getAttrs().getAttribute<SemanticsAttr>()) {
if (semantics->Value.equals("typechecker.type(of:)"))
return DeclTypeCheckingSemantics::TypeOf;
if (semantics->Value.equals("typechecker.withoutActuallyEscaping(_:do:)"))
return DeclTypeCheckingSemantics::WithoutActuallyEscaping;
if (semantics->Value.equals("typechecker._openExistential(_:do:)"))
return DeclTypeCheckingSemantics::OpenExistential;
}
return DeclTypeCheckingSemantics::Normal;
}
bool TypeChecker::isDifferentiable(Type type, bool tangentVectorEqualsSelf,
DeclContext *dc,
Optional<TypeResolutionStage> stage) {
if (stage)
type = dc->mapTypeIntoContext(type);
auto tanSpace = type->getAutoDiffTangentSpace(
LookUpConformanceInModule(dc->getParentModule()));
if (!tanSpace)
return false;
// If no `Self == Self.TangentVector` requirement, return true.
if (!tangentVectorEqualsSelf)
return true;
// Otherwise, return true if `Self == Self.TangentVector`.
return type->getCanonicalType() == tanSpace->getCanonicalType();
}
bool TypeChecker::diagnoseInvalidFunctionType(FunctionType *fnTy, SourceLoc loc,
Optional<FunctionTypeRepr *>repr,
DeclContext *dc,
Optional<TypeResolutionStage> stage) {
// Some of the below checks trigger cycles if we don't have a generic
// signature yet; we'll run the checks again in
// TypeResolutionStage::Interface.
if (stage == TypeResolutionStage::Structural)
return false;
// If the type has a placeholder, don't try to diagnose anything now since
// we'll produce a better diagnostic when (if) the expression successfully
// typechecks.
if (fnTy->hasPlaceholder())
return false;
// If the type is a block or C function pointer, it must be representable in
// ObjC.
auto representation = fnTy->getRepresentation();
auto extInfo = fnTy->getExtInfo();
auto &ctx = dc->getASTContext();
bool hadAnyError = false;
switch (representation) {
case AnyFunctionType::Representation::Block:
case AnyFunctionType::Representation::CFunctionPointer:
if (!fnTy->isRepresentableIn(ForeignLanguage::ObjectiveC, dc)) {
StringRef strName =
(representation == AnyFunctionType::Representation::Block)
? "block"
: "c";
auto extInfo2 =
extInfo.withRepresentation(AnyFunctionType::Representation::Swift);
auto simpleFnTy = FunctionType::get(fnTy->getParams(), fnTy->getResult(),
extInfo2);
ctx.Diags.diagnose(loc, diag::objc_convention_invalid,
simpleFnTy, strName);
hadAnyError = true;
}
break;
case AnyFunctionType::Representation::Thin:
case AnyFunctionType::Representation::Swift:
break;
}
// `@differentiable` function types must return a differentiable type and have
// differentiable (or `@noDerivative`) parameters.
if (extInfo.isDifferentiable()) {
auto result = fnTy->getResult();
auto params = fnTy->getParams();
auto diffKind = extInfo.getDifferentiabilityKind();
bool isLinear = diffKind == DifferentiabilityKind::Linear;
// Check the params.
// Emit `@noDerivative` fixit only if there is at least one valid
// differentiability parameter. Otherwise, adding `@noDerivative` produces
// an ill-formed function type.
auto hasValidDifferentiabilityParam =
llvm::find_if(params, [&](AnyFunctionType::Param param) {
if (param.isNoDerivative())
return false;
return TypeChecker::isDifferentiable(param.getPlainType(),
/*tangentVectorEqualsSelf*/ isLinear,
dc, stage);
}) != params.end();
bool alreadyDiagnosedOneParam = false;
for (unsigned i = 0, end = fnTy->getNumParams(); i != end; ++i) {
auto param = params[i];
if (param.isNoDerivative())
continue;
auto paramType = param.getPlainType();
if (TypeChecker::isDifferentiable(paramType, isLinear, dc, stage))
continue;
auto diagLoc =
repr ? (*repr)->getArgsTypeRepr()->getElement(i).Type->getLoc() : loc;
auto paramTypeString = paramType->getString();
auto diagnostic = ctx.Diags.diagnose(
diagLoc, diag::differentiable_function_type_invalid_parameter,
paramTypeString, isLinear, hasValidDifferentiabilityParam);
alreadyDiagnosedOneParam = true;
hadAnyError = true;
if (hasValidDifferentiabilityParam)
diagnostic.fixItInsert(diagLoc, "@noDerivative ");
}
// Reject the case where all parameters have '@noDerivative'.
if (!alreadyDiagnosedOneParam && !hasValidDifferentiabilityParam) {
auto diagLoc = repr ? (*repr)->getArgsTypeRepr()->getLoc() : loc;
auto diag = ctx.Diags.diagnose(
diagLoc,
diag::differentiable_function_type_no_differentiability_parameters,
isLinear);
hadAnyError = true;
if (repr) {
diag.highlight((*repr)->getSourceRange());
}
}
// Check the result
bool differentiable = isDifferentiable(result,
/*tangentVectorEqualsSelf*/ isLinear,
dc, stage);
if (!differentiable) {
auto diagLoc = repr ? (*repr)->getResultTypeRepr()->getLoc() : loc;
auto resultStr = fnTy->getResult()->getString();
auto diag = ctx.Diags.diagnose(
diagLoc, diag::differentiable_function_type_invalid_result, resultStr,
isLinear);
hadAnyError = true;
if (repr) {
diag.highlight((*repr)->getResultTypeRepr()->getSourceRange());
}
}
}
return hadAnyError;
}