Skip to content

Commit 0f25a3a

Browse files
authored
Merge pull request #16586 from slavapestov/usable-from-inline-type-alias
Check that signatures of @usableFromInline declarations only reference @usableFromInline or public types
2 parents 1c86abb + 9b43f26 commit 0f25a3a

12 files changed

+2260
-1198
lines changed

include/swift/AST/Decl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2162,9 +2162,6 @@ class ValueDecl : public Decl {
21622162
llvm::PointerIntPair<Type, 3, OptionalEnum<AccessLevel>> TypeAndAccess;
21632163
unsigned LocalDiscriminator = 0;
21642164

2165-
private:
2166-
bool isUsableFromInline() const;
2167-
21682165
protected:
21692166
ValueDecl(DeclKind K,
21702167
llvm::PointerUnion<DeclContext *, ASTContext *> context,
@@ -2238,6 +2235,8 @@ class ValueDecl : public Decl {
22382235
SourceLoc getNameLoc() const { return NameLoc; }
22392236
SourceLoc getLoc() const { return NameLoc; }
22402237

2238+
bool isUsableFromInline() const;
2239+
22412240
bool hasAccess() const {
22422241
return TypeAndAccess.getInt().hasValue();
22432242
}
@@ -2266,7 +2265,6 @@ class ValueDecl : public Decl {
22662265
if (treatUsableFromInlineAsPublic &&
22672266
result == AccessLevel::Internal &&
22682267
isUsableFromInline()) {
2269-
assert(!useDC);
22702268
return AccessLevel::Public;
22712269
}
22722270
if (useDC && (result == AccessLevel::Internal ||

include/swift/AST/DiagnosticsSema.def

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,28 @@ WARNING(pattern_type_access_inferred_warn,none,
12921292
"because its type %6 uses "
12931293
"%select{a private|a fileprivate|an internal|%error|%error}5 type",
12941294
(bool, bool, bool, AccessLevel, bool, AccessLevel, Type))
1295+
ERROR(pattern_type_not_usable_from_inline,none,
1296+
"type referenced from a '@usableFromInline' "
1297+
"%select{%select{variable|constant}0|property}1 "
1298+
"must be '@usableFromInline' or public",
1299+
(bool, bool))
1300+
WARNING(pattern_type_not_usable_from_inline_warn,none,
1301+
"type referenced from a '@usableFromInline' "
1302+
"%select{%select{variable|constant}0|property}1 "
1303+
"should be '@usableFromInline' or public",
1304+
(bool, bool))
1305+
ERROR(pattern_type_not_usable_from_inline_inferred,none,
1306+
"type referenced from a '@usableFromInline' "
1307+
"%select{%select{variable|constant}0|property}1 "
1308+
"with inferred type %2 "
1309+
"must be '@usableFromInline' or public",
1310+
(bool, bool, Type))
1311+
WARNING(pattern_type_not_usable_from_inline_inferred_warn,none,
1312+
"type referenced from a '@usableFromInline' "
1313+
"%select{%select{variable|constant}0|property}1 "
1314+
"with inferred type %2 "
1315+
"should be '@usableFromInline' or public",
1316+
(bool, bool, Type))
12951317
ERROR(pattern_binds_no_variables,none,
12961318
"%select{property|global variable}0 declaration does not bind any "
12971319
"variables",
@@ -1346,6 +1368,16 @@ WARNING(type_alias_underlying_type_access_warn,none,
13461368
"because its underlying type uses "
13471369
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
13481370
(bool, AccessLevel, AccessLevel, bool))
1371+
ERROR(type_alias_underlying_type_not_usable_from_inline,none,
1372+
"type referenced from the underlying type of a "
1373+
"'@usableFromInline' type alias "
1374+
"must be '@usableFromInline' or public",
1375+
())
1376+
WARNING(type_alias_underlying_type_not_usable_from_inline_warn,none,
1377+
"type referenced from the underlying type of a "
1378+
"'@usableFromInline' type alias "
1379+
"should be '@usableFromInline' or public",
1380+
())
13491381

13501382
// Subscripts
13511383
ERROR(subscript_type_access,none,
@@ -1363,6 +1395,14 @@ WARNING(subscript_type_access_warn,none,
13631395
"because its %select{index|element type}3 uses "
13641396
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
13651397
(bool, AccessLevel, AccessLevel, bool))
1398+
ERROR(subscript_type_usable_from_inline,none,
1399+
"%select{index type|element type}0 of a '@usableFromInline' subscript "
1400+
"must be '@usableFromInline' or public",
1401+
(bool))
1402+
WARNING(subscript_type_usable_from_inline_warn,none,
1403+
"%select{index type|element type}0 of a '@usableFromInline' subscript "
1404+
"should be '@usableFromInline' or public",
1405+
(bool))
13661406

13671407
// Functions
13681408
ERROR(function_type_access,none,
@@ -1381,6 +1421,16 @@ WARNING(function_type_access_warn,none,
13811421
"because its %select{parameter|result}5 uses "
13821422
"%select{a private|a fileprivate|an internal|%error|%error}3 type",
13831423
(bool, AccessLevel, bool, AccessLevel, unsigned, bool))
1424+
ERROR(function_type_usable_from_inline,none,
1425+
"the %select{parameter|result}1 of a "
1426+
"'@usableFromInline' %select{function|method|initializer}0 "
1427+
"must be '@usableFromInline' or public",
1428+
(unsigned, bool))
1429+
WARNING(function_type_usable_from_inline_warn,none,
1430+
"the %select{parameter|result}1 of a "
1431+
"'@usableFromInline' %select{function|method|initializer}0 "
1432+
"should be '@usableFromInline' or public",
1433+
(unsigned, bool))
13841434
ERROR(noreturn_not_supported,none,
13851435
"'@noreturn' has been removed; functions that never return should have a "
13861436
"return type of 'Never' instead", ())
@@ -1585,6 +1635,12 @@ WARNING(protocol_access_warn,none,
15851635
"%select{protocol should not refine|protocol's 'where' clause should not use}2}0 "
15861636
"%select{a private|a fileprivate|an internal|%error|%error}3 protocol",
15871637
(bool, AccessLevel, bool, AccessLevel, bool))
1638+
ERROR(protocol_usable_from_inline,none,
1639+
"protocol %select{refined|used}0 by '@usableFromInline' protocol "
1640+
"must be '@usableForInline' or public", (bool))
1641+
WARNING(protocol_usable_from_inline_warn,none,
1642+
"protocol %select{refined|used}0 by '@usableFromInline' protocol "
1643+
"should be '@usableForInline' or public", (bool))
15881644
ERROR(protocol_property_must_be_computed_var,none,
15891645
"immutable property requirement must be declared as 'var' with a "
15901646
"'{ get }' specifier", ())
@@ -1627,6 +1683,16 @@ WARNING(associated_type_access_warn,none,
16271683
"%select{a private|a fileprivate|an internal|%error|%error}1 type in its "
16281684
"%select{default definition|requirement}2 ",
16291685
(AccessLevel, AccessLevel, unsigned))
1686+
ERROR(associated_type_not_usable_from_inline,none,
1687+
"type referenced from a "
1688+
"%select{default definition|requirement}0 of an associated type in a "
1689+
"'@usableFromInline' protocol must be '@usableFromInline' or public",
1690+
(unsigned))
1691+
WARNING(associated_type_not_usable_from_inline_warn,none,
1692+
"type referenced from a "
1693+
"%select{default definition|requirement}0 of an associated type in a "
1694+
"'@usableFromInline' protocol should be '@usableFromInline' or public",
1695+
(unsigned))
16301696

16311697
NOTE(bad_associated_type_deduction,none,
16321698
"unable to infer associated type %0 for protocol %1",
@@ -1879,6 +1945,16 @@ WARNING(generic_param_access_warn,none,
18791945
"because its generic %select{parameter|requirement}5 uses "
18801946
"%select{a private|a fileprivate|an internal|%error|%error}3 type",
18811947
(DescriptiveDeclKind, bool, AccessLevel, AccessLevel, bool, bool))
1948+
ERROR(generic_param_usable_from_inline,none,
1949+
"type referenced from a "
1950+
"generic %select{parameter|requirement}1 of a '@usableFromInline' %0 "
1951+
"must be '@usableFromInline' or public",
1952+
(DescriptiveDeclKind, bool))
1953+
WARNING(generic_param_usable_from_inline_warn,none,
1954+
"type referenced from a "
1955+
"generic %select{parameter|requirement}1 of a '@usableFromInline' %0 "
1956+
"should be '@usableFromInline' or public",
1957+
(DescriptiveDeclKind, bool))
18821958

18831959
ERROR(override_multiple_decls_base,none,
18841960
"declaration %0 cannot override more than one superclass declaration",
@@ -2108,6 +2184,12 @@ WARNING(enum_case_access_warn,none,
21082184
"enum case in %select{a private|a fileprivate|an internal|a public|%error}0 enum "
21092185
"uses %select{a private|a fileprivate|an internal|%error|%error}1 type",
21102186
(AccessLevel, AccessLevel))
2187+
ERROR(enum_case_usable_from_inline,none,
2188+
"type of enum case in '@usableFromInline' enum "
2189+
"must be '@usableFromInline' or public", ())
2190+
WARNING(enum_case_usable_from_inline_warn,none,
2191+
"type of enum case in '@usableFromInline' enum "
2192+
"should be '@usableFromInline' or public", ())
21112193
ERROR(enum_stored_property,none,
21122194
"enums must not contain stored properties", ())
21132195

@@ -2145,6 +2227,17 @@ WARNING(enum_raw_type_access_warn,none,
21452227
"%select{a private|a fileprivate|an internal|%error|%error}2 type",
21462228
(bool, AccessLevel, AccessLevel, bool))
21472229

2230+
ERROR(enum_raw_type_not_usable_from_inline,none,
2231+
"type referenced from the raw type of a "
2232+
"'@usableFromInline' enum "
2233+
"must be '@usableFromInline' or public",
2234+
())
2235+
WARNING(enum_raw_type_not_usable_from_inline_warn,none,
2236+
"type referenced from the raw type of a "
2237+
"'@usableFromInline' enum "
2238+
"should be '@usableFromInline' or public",
2239+
())
2240+
21482241
ERROR(empty_enum_raw_type,none,
21492242
"an enum with no cases cannot declare a raw type", ())
21502243
ERROR(enum_raw_value_without_raw_type,none,
@@ -3263,6 +3356,17 @@ WARNING(class_super_access_warn,none,
32633356
"|uses %select{a private|a fileprivate|an internal|%error|%error}2 "
32643357
"type as a generic parameter}4",
32653358
(bool, AccessLevel, AccessLevel, bool, bool))
3359+
ERROR(class_super_not_usable_from_inline,none,
3360+
"%select{type referenced from |}0the superclass of "
3361+
"a '@usableFromInline' class "
3362+
"must be '@usableFromInline' or public",
3363+
(bool))
3364+
WARNING(class_super_not_usable_from_inline_warn,none,
3365+
"%select{type referenced from |}0the superclass of "
3366+
"a '@usableFromInline' class "
3367+
"should be '@usableFromInline' or public",
3368+
(bool))
3369+
32663370
ERROR(dot_protocol_on_non_existential,none,
32673371
"cannot use 'Protocol' with non-protocol type %0", (Type))
32683372
ERROR(tuple_single_element,none,

lib/AST/Decl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2148,6 +2148,10 @@ bool ValueDecl::isUsableFromInline() const {
21482148
if (EED->getParentEnum()->getAttrs().hasAttribute<UsableFromInlineAttr>())
21492149
return true;
21502150

2151+
if (auto *ATD = dyn_cast<AssociatedTypeDecl>(this))
2152+
if (ATD->getProtocol()->getAttrs().hasAttribute<UsableFromInlineAttr>())
2153+
return true;
2154+
21512155
return false;
21522156
}
21532157

lib/Sema/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ add_swift_library(swiftSema STATIC
3232
PlaygroundTransform.cpp
3333
ResilienceDiagnostics.cpp
3434
SourceLoader.cpp
35+
TypeCheckAccess.cpp
3536
TypeCheckAttr.cpp
3637
TypeCheckAvailability.cpp
3738
TypeCheckCaptures.cpp

0 commit comments

Comments
 (0)