|
| 1 | +//===--- TypeCheckEmbedded.cpp - Embedded ----------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file implements type checking support for Embedded Swift. |
| 14 | +// |
| 15 | +//===----------------------------------------------------------------------===// |
| 16 | + |
| 17 | +#include "TypeCheckEmbedded.h" |
| 18 | +#include "swift/AST/ASTContext.h" |
| 19 | +#include "swift/AST/Decl.h" |
| 20 | +#include "swift/AST/DiagnosticsSema.h" |
| 21 | +#include "swift/AST/Effects.h" |
| 22 | +#include "swift/AST/Types.h" |
| 23 | + |
| 24 | +using namespace swift; |
| 25 | + |
| 26 | +std::optional<DiagnosticBehavior> |
| 27 | +swift::shouldDiagnoseEmbeddedLimitations(const DeclContext *dc, SourceLoc loc, |
| 28 | + bool wasAlwaysEmbeddedError) { |
| 29 | + // In Embedded Swift, things that were always errors will still be emitted |
| 30 | + // as errors. Use "unspecified" so we don't change anything. |
| 31 | + if (dc->getASTContext().LangOpts.hasFeature(Feature::Embedded) && |
| 32 | + wasAlwaysEmbeddedError) { |
| 33 | + return DiagnosticBehavior::Unspecified; |
| 34 | + } |
| 35 | + |
| 36 | + // Check one of the Embedded restriction diagnostics that is ignored by |
| 37 | + // default. If it's still ignored, we won't diagnose anything. |
| 38 | + // limitations. |
| 39 | + auto &diags = dc->getASTContext().Diags; |
| 40 | + if (diags.isIgnoredDiagnostic(diag::untyped_throws_in_embedded_swift.ID)) |
| 41 | + return std::nullopt; |
| 42 | + |
| 43 | + // If this was always an error in Embedded Swift, we aren't in Embedded Swift |
| 44 | + // now, so downgrade to a warning. |
| 45 | + if (wasAlwaysEmbeddedError) |
| 46 | + return DiagnosticBehavior::Warning; |
| 47 | + |
| 48 | + // Leave it as-is. |
| 49 | + return DiagnosticBehavior::Unspecified; |
| 50 | +} |
| 51 | + |
| 52 | +/// Check embedded restrictions in the signature of the given function. |
| 53 | +void swift::checkEmbeddedRestrictionsInSignature( |
| 54 | + const AbstractFunctionDecl *func) { |
| 55 | + // If we are not supposed to diagnose Embedded Swift limitations, do nothing. |
| 56 | + auto behavior = shouldDiagnoseEmbeddedLimitations(func, func->getLoc()); |
| 57 | + if (!behavior) |
| 58 | + return; |
| 59 | + |
| 60 | + // Untyped throws is not permitted. |
| 61 | + SourceLoc throwsLoc = func->getThrowsLoc(); |
| 62 | + if (throwsLoc.isValid() && !func->getThrownTypeRepr() && |
| 63 | + !func->hasPolymorphicEffect(EffectKind::Throws)) { |
| 64 | + diagnoseUntypedThrowsInEmbedded(func, throwsLoc); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +void swift::diagnoseUntypedThrowsInEmbedded( |
| 69 | + const DeclContext *dc, SourceLoc throwsLoc) { |
| 70 | + // If we are not supposed to diagnose Embedded Swift limitations, do nothing. |
| 71 | + auto behavior = shouldDiagnoseEmbeddedLimitations(dc, throwsLoc); |
| 72 | + if (!behavior) |
| 73 | + return; |
| 74 | + |
| 75 | + dc->getASTContext().Diags.diagnose( |
| 76 | + throwsLoc, diag::untyped_throws_in_embedded_swift) |
| 77 | + .limitBehavior(*behavior) |
| 78 | + .fixItInsertAfter(throwsLoc, "(<#thrown error type#>)"); |
| 79 | +} |
0 commit comments