Skip to content

Commit e973ff9

Browse files
authored
Merge pull request #67225 from tshortli/allow-usable-from-inline-access-in-default-args
Sema: Allow default arguments to access `@usableFromInline` decls
2 parents b158328 + 496d29c commit e973ff9

File tree

7 files changed

+49
-83
lines changed

7 files changed

+49
-83
lines changed

include/swift/AST/DeclContext.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,9 @@ struct FragileFunctionKind {
217217
};
218218

219219
Kind kind = None;
220-
bool allowUsableFromInline = false;
221220

222221
friend bool operator==(FragileFunctionKind lhs, FragileFunctionKind rhs) {
223-
return (lhs.kind == rhs.kind &&
224-
lhs.allowUsableFromInline == rhs.allowUsableFromInline);
222+
return lhs.kind == rhs.kind;
225223
}
226224

227225
/// Casts to `unsigned` for diagnostic %selects.

include/swift/AST/DiagnosticsSema.def

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6542,10 +6542,6 @@ ERROR(availability_macro_in_inlinable, none,
65426542

65436543
#undef FRAGILE_FUNC_KIND
65446544

6545-
NOTE(resilience_decl_declared_here_public,
6546-
none, DECL_OR_ACCESSOR "2 %1 is not public",
6547-
(DescriptiveDeclKind, DeclName, bool))
6548-
65496545
NOTE(resilience_decl_declared_here,
65506546
none, DECL_OR_ACCESSOR "2 %1 is not '@usableFromInline' or public",
65516547
(DescriptiveDeclKind, DeclName, bool))

lib/AST/DeclContext.cpp

Lines changed: 20 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -445,9 +445,10 @@ ResilienceExpansion DeclContext::getResilienceExpansion() const {
445445

446446
FragileFunctionKind DeclContext::getFragileFunctionKind() const {
447447
auto &context = getASTContext();
448-
return evaluateOrDefault(context.evaluator,
449-
FragileFunctionKindRequest { const_cast<DeclContext *>(this) },
450-
{FragileFunctionKind::None, false});
448+
return evaluateOrDefault(
449+
context.evaluator,
450+
FragileFunctionKindRequest{const_cast<DeclContext *>(this)},
451+
{FragileFunctionKind::None});
451452
}
452453

453454
FragileFunctionKind
@@ -466,23 +467,17 @@ swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
466467
if (VD->getDeclContext()->isLocalContext()) {
467468
auto kind = VD->getDeclContext()->getFragileFunctionKind();
468469
if (kind.kind != FragileFunctionKind::None)
469-
return {FragileFunctionKind::DefaultArgument,
470-
kind.allowUsableFromInline};
470+
return {FragileFunctionKind::DefaultArgument};
471471
}
472472

473473
auto effectiveAccess =
474-
VD->getFormalAccessScope(/*useDC=*/nullptr,
475-
/*treatUsableFromInlineAsPublic=*/true);
476-
auto formalAccess =
477-
VD->getFormalAccessScope(/*useDC=*/nullptr,
478-
/*treatUsableFromInlineAsPublic=*/false);
474+
VD->getFormalAccessScope(/*useDC=*/nullptr,
475+
/*treatUsableFromInlineAsPublic=*/true);
479476
if (effectiveAccess.isPublic()) {
480-
return {FragileFunctionKind::DefaultArgument,
481-
!formalAccess.isPublic()};
477+
return {FragileFunctionKind::DefaultArgument};
482478
}
483479

484-
return {FragileFunctionKind::None,
485-
/*allowUsableFromInline=*/false};
480+
return {FragileFunctionKind::None};
486481
}
487482

488483
// Stored property initializer contexts use minimal resilience expansion
@@ -491,13 +486,11 @@ swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
491486
auto bindingIndex = init->getBindingIndex();
492487
if (auto *varDecl = init->getBinding()->getAnchoringVarDecl(bindingIndex)) {
493488
if (varDecl->isInitExposedToClients()) {
494-
return {FragileFunctionKind::PropertyInitializer,
495-
/*allowUsableFromInline=*/true};
489+
return {FragileFunctionKind::PropertyInitializer};
496490
}
497491
}
498492

499-
return {FragileFunctionKind::None,
500-
/*allowUsableFromInline=*/false};
493+
return {FragileFunctionKind::None};
501494
}
502495

503496
if (auto *AFD = dyn_cast<AbstractFunctionDecl>(dc)) {
@@ -513,53 +506,44 @@ swift::FragileFunctionKindRequest::evaluate(Evaluator &evaluator,
513506
// If the function is not externally visible, we will not be serializing
514507
// its body.
515508
if (!funcAccess.isPublic()) {
516-
return {FragileFunctionKind::None,
517-
/*allowUsableFromInline=*/false};
509+
return {FragileFunctionKind::None};
518510
}
519511

520512
// If the function is public, @_transparent implies @inlinable.
521513
if (AFD->isTransparent()) {
522-
return {FragileFunctionKind::Transparent,
523-
/*allowUsableFromInline=*/true};
514+
return {FragileFunctionKind::Transparent};
524515
}
525516

526517
if (AFD->getAttrs().hasAttribute<InlinableAttr>()) {
527-
return {FragileFunctionKind::Inlinable,
528-
/*allowUsableFromInline=*/true};
518+
return {FragileFunctionKind::Inlinable};
529519
}
530520

531521
if (AFD->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
532-
return {FragileFunctionKind::AlwaysEmitIntoClient,
533-
/*allowUsableFromInline=*/true};
522+
return {FragileFunctionKind::AlwaysEmitIntoClient};
534523
}
535524

536525
if (AFD->isBackDeployed(context->getASTContext())) {
537-
return {FragileFunctionKind::BackDeploy,
538-
/*allowUsableFromInline=*/true};
526+
return {FragileFunctionKind::BackDeploy};
539527
}
540528

541529
// Property and subscript accessors inherit @_alwaysEmitIntoClient,
542530
// @backDeployed, and @inlinable from their storage declarations.
543531
if (auto accessor = dyn_cast<AccessorDecl>(AFD)) {
544532
auto *storage = accessor->getStorage();
545533
if (storage->getAttrs().getAttribute<InlinableAttr>()) {
546-
return {FragileFunctionKind::Inlinable,
547-
/*allowUsableFromInline=*/true};
534+
return {FragileFunctionKind::Inlinable};
548535
}
549536
if (storage->getAttrs().hasAttribute<AlwaysEmitIntoClientAttr>()) {
550-
return {FragileFunctionKind::AlwaysEmitIntoClient,
551-
/*allowUsableFromInline=*/true};
537+
return {FragileFunctionKind::AlwaysEmitIntoClient};
552538
}
553539
if (storage->isBackDeployed(context->getASTContext())) {
554-
return {FragileFunctionKind::BackDeploy,
555-
/*allowUsableFromInline=*/true};
540+
return {FragileFunctionKind::BackDeploy};
556541
}
557542
}
558543
}
559544
}
560545

561-
return {FragileFunctionKind::None,
562-
/*allowUsableFromInline=*/false};
546+
return {FragileFunctionKind::None};
563547
}
564548

565549
/// Determine whether the innermost context is generic.

lib/AST/TypeCheckRequests.cpp

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -588,29 +588,28 @@ void swift::simple_display(llvm::raw_ostream &out,
588588
switch (value.kind) {
589589
case FragileFunctionKind::Transparent:
590590
out << "transparent";
591-
break;
591+
return;
592592
case FragileFunctionKind::Inlinable:
593593
out << "inlinable";
594-
break;
594+
return;
595595
case FragileFunctionKind::AlwaysEmitIntoClient:
596596
out << "alwaysEmitIntoClient";
597-
break;
597+
return;
598598
case FragileFunctionKind::DefaultArgument:
599599
out << "defaultArgument";
600-
break;
600+
return;
601601
case FragileFunctionKind::PropertyInitializer:
602602
out << "propertyInitializer";
603-
break;
603+
return;
604604
case FragileFunctionKind::BackDeploy:
605605
out << "backDeploy";
606-
break;
606+
return;
607607
case FragileFunctionKind::None:
608608
out << "none";
609-
break;
609+
return;
610610
}
611611

612-
out << ", allowUsableFromInline: "
613-
<< (value.allowUsableFromInline ? "true" : "false");
612+
llvm_unreachable("Bad FragileFunctionKind");
614613
}
615614

616615
//----------------------------------------------------------------------------//

lib/Sema/ResilienceDiagnostics.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,9 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
6161
return false;
6262

6363
// General check on access-level of the decl.
64-
auto declAccessScope = D->getFormalAccessScope(/*useDC=*/nullptr,
65-
fragileKind.allowUsableFromInline);
64+
auto declAccessScope =
65+
D->getFormalAccessScope(/*useDC=*/nullptr,
66+
/*allowUsableFromInline=*/true);
6667

6768
// If the decl is imported, check if the import lowers it's access level.
6869
auto importAccessLevel = AccessLevel::Public;
@@ -134,13 +135,8 @@ bool TypeChecker::diagnoseInlinableDeclRefAccess(SourceLoc loc,
134135
diagAccessLevel,
135136
fragileKind.getSelector(), isAccessor);
136137

137-
if (fragileKind.allowUsableFromInline) {
138-
Context.Diags.diagnose(D, diag::resilience_decl_declared_here,
139-
D->getDescriptiveKind(), diagName, isAccessor);
140-
} else {
141-
Context.Diags.diagnose(D, diag::resilience_decl_declared_here_public,
142-
D->getDescriptiveKind(), diagName, isAccessor);
143-
}
138+
Context.Diags.diagnose(D, diag::resilience_decl_declared_here,
139+
D->getDescriptiveKind(), diagName, isAccessor);
144140

145141
if (problematicImport.has_value() &&
146142
diagAccessLevel == importAccessLevel) {

test/Constraints/type_inference_from_default_exprs.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ protocol StorageType {
208208
var identifier: String { get }
209209
}
210210

211-
class Storage { // expected-note {{class 'Storage' is not public}}
211+
class Storage { // expected-note {{class 'Storage' is not '@usableFromInline' or public}}
212212
}
213213

214214
extension Storage {
@@ -256,7 +256,7 @@ struct S61061_3<T> where T:Hashable {
256256

257257
// https://github.com/apple/swift/issues/62025
258258
// Syntactic checks are not run on the default argument expressions
259-
public struct MyStruct {} // expected-note {{initializer 'init()' is not public}}
259+
public struct MyStruct {} // expected-note {{initializer 'init()' is not '@usableFromInline' or public}}
260260
public func issue62025_with_init<T>(_: T = MyStruct()) {}
261261
// expected-error@-1 {{initializer 'init()' is internal and cannot be referenced from a default argument value}}
262262
public func issue62025_with_type<T>(_: T = Storage.self) {}

test/decl/func/default-values-swift4.swift

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
// RUN: %target-typecheck-verify-swift -swift-version 4 -enable-testing
33

44
private func privateFunction() {}
5-
// expected-note@-1 2{{global function 'privateFunction()' is not public}}
5+
// expected-note@-1 2{{global function 'privateFunction()' is not '@usableFromInline' or public}}
66
fileprivate func fileprivateFunction() {}
7-
// expected-note@-1 2{{global function 'fileprivateFunction()' is not public}}
7+
// expected-note@-1 2{{global function 'fileprivateFunction()' is not '@usableFromInline' or public}}
88
func internalFunction() {}
9-
// expected-note@-1 2{{global function 'internalFunction()' is not public}}
10-
@usableFromInline func versionedFunction() {}
11-
// expected-note@-1 4{{global function 'versionedFunction()' is not public}}
9+
// expected-note@-1 2{{global function 'internalFunction()' is not '@usableFromInline' or public}}
10+
@usableFromInline func usableFromInlineFunction() {}
1211
public func publicFunction() {}
1312

1413
func internalIntFunction() -> Int {}
15-
// expected-note@-1 {{global function 'internalIntFunction()' is not public}}
14+
// expected-note@-1 {{global function 'internalIntFunction()' is not '@usableFromInline' or public}}
1615

1716
private func privateFunction2() {}
1817
// expected-note@-1 {{global function 'privateFunction2()' is not '@usableFromInline' or public}}
@@ -31,7 +30,7 @@ func internalFunctionWithDefaultValue(
3130

3231
publicFunction()
3332
// OK
34-
versionedFunction()
33+
usableFromInlineFunction()
3534
// OK
3635
internalFunction()
3736
// OK
@@ -44,16 +43,14 @@ func internalFunctionWithDefaultValue(
4443
}(),
4544
y: Int = internalIntFunction()) {}
4645

47-
@usableFromInline func versionedFunctionWithDefaultValue(
46+
@usableFromInline func usableFromInlineFunctionWithDefaultValue(
4847
x: Int = {
4948
struct Nested {}
5049
// expected-error@-1 {{type 'Nested' cannot be nested inside a default argument value}}
5150

52-
// FIXME: Some errors below are diagnosed twice
53-
5451
publicFunction()
5552
// OK
56-
versionedFunction()
53+
usableFromInlineFunction()
5754
// OK
5855
internalFunction2()
5956
// expected-error@-1 {{global function 'internalFunction2()' is internal and cannot be referenced from a default argument value}}
@@ -76,8 +73,7 @@ public func publicFunctionWithDefaultValue(
7673

7774
publicFunction()
7875

79-
versionedFunction()
80-
// expected-error@-1 {{global function 'versionedFunction()' is internal and cannot be referenced from a default argument value}}
76+
usableFromInlineFunction()
8177

8278
internalFunction()
8379
// expected-error@-1 {{global function 'internalFunction()' is internal and cannot be referenced from a default argument value}}
@@ -101,18 +97,16 @@ public class MyClass {
10197
public func evilCode(
10298
x: Int = {
10399
let _ = publicFunction()
104-
let _ = versionedFunction()
105-
// expected-error@-1 {{global function 'versionedFunction()' is internal and cannot be referenced from a default argument value}}
100+
let _ = usableFromInlineFunction()
106101

107102
func localFunction() {
108103
publicFunction()
109-
versionedFunction()
110-
// expected-error@-1 {{global function 'versionedFunction()' is internal and cannot be referenced from a default argument value}}
104+
usableFromInlineFunction()
111105
}
112106
return 0
113107
}()) {}
114108

115-
private func privateIntFunction() -> Int {} // expected-note {{global function 'privateIntFunction()' is not public}}
109+
private func privateIntFunction() -> Int {} // expected-note {{global function 'privateIntFunction()' is not '@usableFromInline' or public}}
116110

117111
public struct HasSubscript {
118112
public subscript(x: Int = {
@@ -121,8 +115,7 @@ public struct HasSubscript {
121115

122116
publicFunction()
123117

124-
versionedFunction()
125-
// expected-error@-1 {{global function 'versionedFunction()' is internal and cannot be referenced from a default argument value}}
118+
usableFromInlineFunction()
126119

127120
internalFunction()
128121
// expected-error@-1 {{global function 'internalFunction()' is internal and cannot be referenced from a default argument value}}

0 commit comments

Comments
 (0)