Skip to content

Commit 694e659

Browse files
authored
Merge pull request swiftlang#78966 from tshortli/decl-context-is-in-swiftinterface
AST: Introduce and adopt `DeclContext::isInSwiftinterface()`
2 parents 459e592 + e019a32 commit 694e659

14 files changed

+46
-62
lines changed

include/swift/AST/DeclContext.h

+5
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,11 @@ class alignas(1 << DeclContextAlignInBits) DeclContext
576576
LLVM_READONLY
577577
SourceFile *getOutermostParentSourceFile() const;
578578

579+
/// Returns true if the source file that contains the context is a
580+
/// `.swiftinterface` file.
581+
LLVM_READONLY
582+
bool isInSwiftinterface() const;
583+
579584
/// Determine whether this declaration context is generic, meaning that it or
580585
/// any of its parents have generic parameters.
581586
bool isGenericContext() const;

lib/AST/Decl.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -8626,7 +8626,7 @@ static DefaultArgumentKind computeDefaultArgumentKind(DeclContext *dc,
86268626
// expected to include them explicitly in subclasses. A default argument of
86278627
// '= super' in a parameter of such initializer indicates that the default
86288628
// argument is inherited.
8629-
if (dc->getParentSourceFile()->Kind == SourceFileKind::Interface) {
8629+
if (dc->isInSwiftinterface()) {
86308630
return DefaultArgumentKind::Inherited;
86318631
} else {
86328632
return DefaultArgumentKind::Normal;

lib/AST/DeclContext.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,11 @@ SourceFile *DeclContext::getOutermostParentSourceFile() const {
413413
return sf;
414414
}
415415

416+
bool DeclContext::isInSwiftinterface() const {
417+
auto sf = getParentSourceFile();
418+
return sf && sf->Kind == SourceFileKind::Interface;
419+
}
420+
416421
DeclContext *DeclContext::getModuleScopeContext() const {
417422
// If the current context is PackageUnit, return the module
418423
// decl context pointing to the current context. This check

lib/AST/DiagnosticEngine.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,7 @@ InFlightDiagnostic::warnUntilSwiftVersion(unsigned majorVersion) {
457457

458458
InFlightDiagnostic &
459459
InFlightDiagnostic::warnInSwiftInterface(const DeclContext *context) {
460-
auto sourceFile = context->getParentSourceFile();
461-
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface) {
460+
if (context->isInSwiftinterface()) {
462461
return limitBehavior(DiagnosticBehavior::Warning);
463462
}
464463

lib/Sema/CodeSynthesis.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -1335,9 +1335,8 @@ static bool shouldAttemptInitializerSynthesis(const NominalTypeDecl *decl) {
13351335
return false;
13361336

13371337
// Don't add implicit constructors in module interfaces.
1338-
if (auto *SF = decl->getParentSourceFile())
1339-
if (SF->Kind == SourceFileKind::Interface)
1340-
return false;
1338+
if (decl->getDeclContext()->isInSwiftinterface())
1339+
return false;
13411340

13421341
// Don't attempt if we know the decl is invalid.
13431342
if (decl->isInvalid())

lib/Sema/TypeCheckAttr.cpp

+4-9
Original file line numberDiff line numberDiff line change
@@ -2396,8 +2396,7 @@ void AttributeChecker::visitAvailableAttr(AvailableAttr *parsedAttr) {
23962396

23972397
// Skip the remaining diagnostics in swiftinterfaces.
23982398
auto *DC = D->getDeclContext();
2399-
auto *SF = DC->getParentSourceFile();
2400-
if (SF && SF->Kind == SourceFileKind::Interface)
2399+
if (DC->isInSwiftinterface())
24012400
return;
24022401

24032402
// The remaining diagnostics are only for attributes that are active.
@@ -4549,8 +4548,7 @@ void AttributeChecker::visitCustomAttr(CustomAttr *attr) {
45494548
// Module interfaces don't print bodies for all getters, so allow getters
45504549
// that don't have a body if we're compiling a module interface.
45514550
// Within a protocol definition, there will never be a body.
4552-
SourceFile *parent = storage->getDeclContext()->getParentSourceFile();
4553-
bool isInInterface = parent && parent->Kind == SourceFileKind::Interface;
4551+
bool isInInterface = storage->getDeclContext()->isInSwiftinterface();
45544552
if (!isInInterface && !getter->hasBody() &&
45554553
!isa<ProtocolDecl>(storage->getDeclContext()))
45564554
return true;
@@ -5262,9 +5260,7 @@ TypeChecker::diagnosticIfDeclCannotBePotentiallyUnavailable(const Decl *D) {
52625260
// An enum element with an associated value cannot be potentially
52635261
// unavailable.
52645262
if (EED->hasAssociatedValues()) {
5265-
auto *SF = DC->getParentSourceFile();
5266-
5267-
if (SF->Kind == SourceFileKind::Interface) {
5263+
if (DC->isInSwiftinterface()) {
52685264
return diag::availability_enum_element_no_potential_warn;
52695265
} else {
52705266
return diag::availability_enum_element_no_potential;
@@ -7885,8 +7881,7 @@ void AttributeChecker::visitMacroRoleAttr(MacroRoleAttr *attr) {
78857881
break;
78867882
case MacroRole::Conformance: {
78877883
// Suppress the conformance macro error in swiftinterfaces.
7888-
SourceFile *file = D->getDeclContext()->getParentSourceFile();
7889-
if (file && file->Kind == SourceFileKind::Interface)
7884+
if (D->getDeclContext()->isInSwiftinterface())
78907885
break;
78917886

78927887
diagnoseAndRemoveAttr(attr, diag::conformance_macro)

lib/Sema/TypeCheckConcurrency.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -4821,9 +4821,7 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
48214821
bool isUnsafe = attr->isArgUnsafe();
48224822
if (attr->hasArgs()) {
48234823
if (isUnsafe) {
4824-
SourceFile *file = decl->getDeclContext()->getParentSourceFile();
4825-
bool inSwiftinterface =
4826-
file && file->Kind == SourceFileKind::Interface;
4824+
bool inSwiftinterface = decl->getDeclContext()->isInSwiftinterface();
48274825
ctx.Diags.diagnose(
48284826
attr->getLocation(),
48294827
diag::unsafe_global_actor)

lib/Sema/TypeCheckDecl.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -1201,8 +1201,7 @@ EnumRawValuesRequest::evaluate(Evaluator &eval, EnumDecl *ED,
12011201
// values are intentionally omitted from them (unless the enum is @objc).
12021202
// Without bailing here, incorrect raw values can be automatically generated
12031203
// and incorrect diagnostics may be omitted for some decls.
1204-
SourceFile *Parent = ED->getDeclContext()->getParentSourceFile();
1205-
if (Parent && Parent->Kind == SourceFileKind::Interface && !ED->isObjC())
1204+
if (ED->getDeclContext()->isInSwiftinterface() && !ED->isObjC())
12061205
return std::make_tuple<>();
12071206

12081207
if (!computeAutomaticEnumValueKind(ED)) {

lib/Sema/TypeCheckDeclPrimary.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -1152,8 +1152,7 @@ static void checkInheritedDefaultValueRestrictions(ParamDecl *PD) {
11521152
assert(PD->getDefaultArgumentKind() == DefaultArgumentKind::Inherited);
11531153

11541154
auto *DC = PD->getInnermostDeclContext();
1155-
const SourceFile *SF = DC->getParentSourceFile();
1156-
assert((SF && SF->Kind == SourceFileKind::Interface || PD->isImplicit()) &&
1155+
assert((DC->isInSwiftinterface() || PD->isImplicit()) &&
11571156
"explicit inherited default argument outside of a module interface?");
11581157

11591158
// The containing decl should be a designated initializer.
@@ -1807,7 +1806,7 @@ static void diagnoseRetroactiveConformances(
18071806
}
18081807

18091808
// Don't warn for this if we see it in module interfaces.
1810-
if (ext->getParentSourceFile()->Kind == SourceFileKind::Interface) {
1809+
if (ext->getDeclContext()->isInSwiftinterface()) {
18111810
return;
18121811
}
18131812

@@ -2557,9 +2556,8 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
25572556
// This allows the compiler to process existing .swiftinterface
25582557
// files that contain this issue.
25592558
if (resultType->isVoid()) {
2560-
if (auto sourceFile = MD->getParentSourceFile())
2561-
if (sourceFile->Kind == SourceFileKind::Interface)
2562-
diag.limitBehavior(DiagnosticBehavior::Warning);
2559+
if (MD->getDeclContext()->isInSwiftinterface())
2560+
diag.limitBehavior(DiagnosticBehavior::Warning);
25632561
}
25642562
}
25652563

@@ -3332,8 +3330,7 @@ class DeclChecker : public DeclVisitor<DeclChecker> {
33323330
void checkRequiredInClassInits(ClassDecl *cd) {
33333331
// Initializers may be omitted from property declarations in module
33343332
// interface files so don't diagnose in them.
3335-
SourceFile *sourceFile = cd->getDeclContext()->getParentSourceFile();
3336-
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface)
3333+
if (cd->getDeclContext()->isInSwiftinterface())
33373334
return;
33383335

33393336
ClassDecl *source = nullptr;

lib/Sema/TypeCheckDistributed.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -669,16 +669,16 @@ bool swift::checkDistributedActorProperty(VarDecl *var, bool diagnose) {
669669
void swift::checkDistributedActorProperties(const NominalTypeDecl *decl) {
670670
auto &C = decl->getASTContext();
671671

672-
if (auto sourceFile = decl->getDeclContext()->getParentSourceFile()) {
673-
if (sourceFile->Kind == SourceFileKind::Interface) {
674-
// Don't diagnose properties in swiftinterfaces.
675-
return;
676-
}
677-
} else {
672+
if (!decl->getDeclContext()->getParentSourceFile()) {
678673
// Don't diagnose when checking without source file (e.g. from module, importer etc).
679674
return;
680675
}
681676

677+
if (decl->getDeclContext()->isInSwiftinterface()) {
678+
// Don't diagnose properties in swiftinterfaces.
679+
return;
680+
}
681+
682682
if (isa<ProtocolDecl>(decl)) {
683683
// protocols don't matter for stored property checking
684684
return;

lib/Sema/TypeCheckProtocol.cpp

+9-12
Original file line numberDiff line numberDiff line change
@@ -1670,16 +1670,14 @@ bool WitnessChecker::findBestWitness(
16701670
// interface can be treated as opaque.
16711671
// FIXME: ...but we should do something better about types.
16721672
if (conformance && !conformance->isInvalid()) {
1673-
if (auto *SF = DC->getParentSourceFile()) {
1674-
if (SF->Kind == SourceFileKind::Interface) {
1675-
auto match = matchWitness(ReqEnvironmentCache, Proto,
1676-
conformance, DC, requirement, requirement);
1677-
if (match.isViable()) {
1678-
numViable = 1;
1679-
bestIdx = matches.size();
1680-
matches.push_back(std::move(match));
1681-
return true;
1682-
}
1673+
if (DC->isInSwiftinterface()) {
1674+
auto match = matchWitness(ReqEnvironmentCache, Proto, conformance, DC,
1675+
requirement, requirement);
1676+
if (match.isViable()) {
1677+
numViable = 1;
1678+
bestIdx = matches.size();
1679+
matches.push_back(std::move(match));
1680+
return true;
16831681
}
16841682
}
16851683
}
@@ -4651,8 +4649,7 @@ ResolveWitnessResult ConformanceChecker::resolveWitnessViaDerivation(
46514649
ValueDecl *requirement) {
46524650
assert(!isa<AssociatedTypeDecl>(requirement) && "Use resolveTypeWitnessVia*");
46534651

4654-
auto *SF = DC->getParentSourceFile();
4655-
if (SF != nullptr && SF->Kind == SourceFileKind::Interface)
4652+
if (DC->isInSwiftinterface())
46564653
return ResolveWitnessResult::Missing;
46574654

46584655
// Find the declaration that derives the protocol conformance.

lib/Sema/TypeCheckStorage.cpp

+2-7
Original file line numberDiff line numberDiff line change
@@ -478,11 +478,8 @@ const PatternBindingEntry *PatternBindingEntryRequest::evaluate(
478478
if (isReq) {
479479
continue;
480480
}
481-
auto varSourceFile = binding->getDeclContext()->getParentSourceFile();
482-
auto isVarInInterfaceFile =
483-
varSourceFile && varSourceFile->Kind == SourceFileKind::Interface;
484481
// Don't diagnose too strictly for textual interfaces.
485-
if (isVarInInterfaceFile) {
482+
if (binding->getDeclContext()->isInSwiftinterface()) {
486483
continue;
487484
}
488485
// var is only allowed in a protocol.
@@ -3625,14 +3622,12 @@ static void finishNSManagedImplInfo(VarDecl *var,
36253622
if (var->isLet())
36263623
diagnoseAttrWithRemovalFixIt(var, attr, diag::attr_NSManaged_let_property);
36273624

3628-
SourceFile *parentFile = var->getDeclContext()->getParentSourceFile();
3629-
36303625
auto diagnoseNotStored = [&](unsigned kind) {
36313626
// Skip diagnosing @NSManaged declarations in module interfaces. They are
36323627
// properties that are stored, but have specially synthesized observers
36333628
// and we should allow them to have getters and setters in a module
36343629
// interface.
3635-
if (parentFile && parentFile->Kind == SourceFileKind::Interface)
3630+
if (var->getDeclContext()->isInSwiftinterface())
36363631
return;
36373632

36383633
diagnoseAttrWithRemovalFixIt(var, attr, diag::attr_NSManaged_not_stored, kind);

lib/Sema/TypeCheckType.cpp

+3-6
Original file line numberDiff line numberDiff line change
@@ -2266,8 +2266,7 @@ namespace {
22662266
}
22672267

22682268
bool isInterfaceFile() const {
2269-
auto SF = getDeclContext()->getParentSourceFile();
2270-
return (SF && SF->Kind == SourceFileKind::Interface);
2269+
return getDeclContext()->isInSwiftinterface();
22712270
}
22722271

22732272
/// Short-hand to query the current stage of type resolution.
@@ -6464,8 +6463,7 @@ void TypeChecker::checkExistentialTypes(Decl *decl) {
64646463
return;
64656464

64666465
// Skip diagnosing existential `any` requirements in swiftinterfaces.
6467-
auto sourceFile = decl->getDeclContext()->getParentSourceFile();
6468-
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface)
6466+
if (decl->getDeclContext()->isInSwiftinterface())
64696467
return;
64706468

64716469
auto &ctx = decl->getASTContext();
@@ -6513,8 +6511,7 @@ void TypeChecker::checkExistentialTypes(ASTContext &ctx, Stmt *stmt,
65136511
return;
65146512

65156513
// Skip diagnosing existential `any` requirements in swiftinterfaces.
6516-
auto sourceFile = DC->getParentSourceFile();
6517-
if (sourceFile && sourceFile->Kind == SourceFileKind::Interface)
6514+
if (DC->isInSwiftinterface())
65186515
return;
65196516

65206517
// Previously we missed this diagnostic on 'catch' statements, downgrade

lib/Serialization/Serialization.cpp

+1-3
Original file line numberDiff line numberDiff line change
@@ -3599,9 +3599,7 @@ class Serializer::DeclSerializer : public DeclVisitor<DeclSerializer> {
35993599
// Everything should be safe in a swiftinterface. So, don't emit any safety
36003600
// record when building a swiftinterface in release builds. Debug builds
36013601
// instead print inconsistencies.
3602-
auto parentSF = DC->getParentSourceFile();
3603-
bool fromModuleInterface = parentSF &&
3604-
parentSF->Kind == SourceFileKind::Interface;
3602+
bool fromModuleInterface = DC->isInSwiftinterface();
36053603
#if NDEBUG
36063604
if (fromModuleInterface)
36073605
return;

0 commit comments

Comments
 (0)