Skip to content

Commit a394742

Browse files
authored
Merge pull request #42069 from beccadax/i-care-about-results
Import swift_attr(“@sendable”) for result types
2 parents 53bdf86 + a57e71c commit a394742

File tree

8 files changed

+342
-158
lines changed

8 files changed

+342
-158
lines changed

include/swift/AST/DiagnosticsClangImporter.def

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@ WARNING(clang_error_code_must_be_sendable,none,
8989
"cannot make error code type '%0' non-sendable because Swift errors "
9090
"are always sendable", (StringRef))
9191

92-
WARNING(clang_param_ignored_sendable_attr,none,
93-
"cannot make parameter '%0' sendable type %1 cannot be made sendable "
94-
"by adding '@Sendable' or '& Sendable'",
95-
(StringRef, Type))
92+
WARNING(clang_ignored_sendable_attr,none,
93+
"cannot make type %0 sendable because '@Sendable' and '& Sendable' "
94+
"cannot be added to it",
95+
(Type))
9696
NOTE(clang_param_should_be_implicitly_sendable,none,
9797
"parameter should be implicitly 'Sendable' because it is a completion "
9898
"handler", ())

lib/ClangImporter/ImportDecl.cpp

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2846,8 +2846,10 @@ namespace {
28462846
// or the original C type.
28472847
clang::QualType ClangType = Decl->getUnderlyingType();
28482848
SwiftType = Impl.importTypeIgnoreIUO(
2849-
ClangType, ImportTypeKind::Typedef, isInSystemModule(DC),
2850-
getTypedefBridgeability(Decl), OTK_Optional);
2849+
ClangType, ImportTypeKind::Typedef,
2850+
ImportDiagnosticAdder(Impl, Decl, Decl->getLocation()),
2851+
isInSystemModule(DC), getTypedefBridgeability(Decl),
2852+
getImportTypeAttrs(Decl), OTK_Optional);
28512853
}
28522854

28532855
if (!SwiftType)
@@ -2861,14 +2863,6 @@ namespace {
28612863
Loc,
28622864
/*genericparams*/nullptr, DC);
28632865

2864-
// If the typedef is marked with @Sendable and not @_nonSendable, make
2865-
// any function type in it Sendable.
2866-
auto sendability = Result->getAttrs().getEffectiveSendableAttr();
2867-
if (isa_and_nonnull<SendableAttr>(sendability))
2868-
SwiftType = applyToFunctionType(SwiftType, [](ASTExtInfo info) {
2869-
return info.withConcurrent();
2870-
});
2871-
28722866
Result->setUnderlyingType(SwiftType);
28732867

28742868
// Make Objective-C's 'id' unavailable.
@@ -2950,6 +2944,7 @@ namespace {
29502944
auto name = importedName.getDeclName().getBaseIdentifier();
29512945

29522946
// Create the enum declaration and record it.
2947+
ImportDiagnosticAdder addDiag(Impl, decl, decl->getLocation());
29532948
StructDecl *errorWrapper = nullptr;
29542949
NominalTypeDecl *result;
29552950
auto enumInfo = Impl.getEnumInfo(decl);
@@ -2964,8 +2959,8 @@ namespace {
29642959
case EnumKind::Unknown: {
29652960
// Compute the underlying type of the enumeration.
29662961
auto underlyingType = Impl.importTypeIgnoreIUO(
2967-
decl->getIntegerType(), ImportTypeKind::Enum, isInSystemModule(dc),
2968-
Bridgeability::None);
2962+
decl->getIntegerType(), ImportTypeKind::Enum, addDiag,
2963+
isInSystemModule(dc), Bridgeability::None, ImportTypeAttrs());
29692964
if (!underlyingType)
29702965
return nullptr;
29712966

@@ -2997,8 +2992,8 @@ namespace {
29972992

29982993
// Compute the underlying type.
29992994
auto underlyingType = Impl.importTypeIgnoreIUO(
3000-
decl->getIntegerType(), ImportTypeKind::Enum, isInSystemModule(dc),
3001-
Bridgeability::None);
2995+
decl->getIntegerType(), ImportTypeKind::Enum, addDiag,
2996+
isInSystemModule(dc), Bridgeability::None, ImportTypeAttrs());
30022997
if (!underlyingType)
30032998
return nullptr;
30042999

@@ -3929,7 +3924,8 @@ namespace {
39293924
auto &clangContext = Impl.getClangASTContext();
39303925
auto type = Impl.importTypeIgnoreIUO(
39313926
clangContext.getTagDeclType(clangEnum), ImportTypeKind::Value,
3932-
isInSystemModule(dc), Bridgeability::None);
3927+
ImportDiagnosticAdder(Impl, clangEnum, clangEnum->getLocation()),
3928+
isInSystemModule(dc), Bridgeability::None, ImportTypeAttrs());
39333929
if (!type)
39343930
return nullptr;
39353931

@@ -3995,7 +3991,9 @@ namespace {
39953991

39963992
auto importedType =
39973993
Impl.importType(decl->getType(), ImportTypeKind::Variable,
3998-
isInSystemModule(dc), Bridgeability::None);
3994+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
3995+
isInSystemModule(dc), Bridgeability::None,
3996+
getImportTypeAttrs(decl));
39993997
if (!importedType)
40003998
return nullptr;
40013999

@@ -4522,7 +4520,9 @@ namespace {
45224520

45234521
auto importedType =
45244522
Impl.importType(decl->getType(), ImportTypeKind::RecordField,
4525-
isInSystemModule(dc), Bridgeability::None);
4523+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
4524+
isInSystemModule(dc), Bridgeability::None,
4525+
getImportTypeAttrs(decl));
45264526
if (!importedType) {
45274527
Impl.addImportDiagnostic(
45284528
decl, Diagnostic(diag::record_field_not_imported, decl),
@@ -4648,7 +4648,9 @@ namespace {
46484648
Impl.importType(declType,
46494649
(isAudited ? ImportTypeKind::AuditedVariable
46504650
: ImportTypeKind::Variable),
4651-
isInSystemModule(dc), Bridgeability::None);
4651+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
4652+
isInSystemModule(dc), Bridgeability::None,
4653+
getImportTypeAttrs(decl));
46524654

46534655
if (!importedType)
46544656
return nullptr;
@@ -5849,8 +5851,9 @@ namespace {
58495851
clangSuperclassType =
58505852
clangCtx.getObjCObjectPointerType(clangSuperclassType);
58515853
superclassType = Impl.importTypeIgnoreIUO(
5852-
clangSuperclassType, ImportTypeKind::Abstract, isInSystemModule(dc),
5853-
Bridgeability::None);
5854+
clangSuperclassType, ImportTypeKind::Abstract,
5855+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
5856+
isInSystemModule(dc), Bridgeability::None, ImportTypeAttrs());
58545857
if (superclassType) {
58555858
assert(superclassType->is<ClassType>() ||
58565859
superclassType->is<BoundGenericClassType>());
@@ -6444,9 +6447,10 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
64446447
decl, AccessLevel::Public, Loc, name, Loc, None, nullptr, dc);
64456448

64466449
// Import the type of the underlying storage
6450+
ImportDiagnosticAdder addImportDiag(Impl, decl, decl->getLocation());
64476451
auto storedUnderlyingType = Impl.importTypeIgnoreIUO(
6448-
decl->getUnderlyingType(), ImportTypeKind::Value, isInSystemModule(dc),
6449-
Bridgeability::None, OTK_None);
6452+
decl->getUnderlyingType(), ImportTypeKind::Value, addImportDiag,
6453+
isInSystemModule(dc), Bridgeability::None, ImportTypeAttrs(), OTK_None);
64506454

64516455
if (!storedUnderlyingType)
64526456
return nullptr;
@@ -6466,8 +6470,8 @@ SwiftDeclConverter::importSwiftNewtype(const clang::TypedefNameDecl *decl,
64666470

64676471
// Find a bridged type, which may be different
64686472
auto computedPropertyUnderlyingType = Impl.importTypeIgnoreIUO(
6469-
decl->getUnderlyingType(), ImportTypeKind::Property, isInSystemModule(dc),
6470-
Bridgeability::Full, OTK_None);
6473+
decl->getUnderlyingType(), ImportTypeKind::Property, addImportDiag,
6474+
isInSystemModule(dc), Bridgeability::Full, ImportTypeAttrs(), OTK_None);
64716475
if (auto objTy = computedPropertyUnderlyingType->getOptionalObjectType())
64726476
computedPropertyUnderlyingType = objTy;
64736477

@@ -6726,8 +6730,9 @@ SwiftDeclConverter::importAsOptionSetType(DeclContext *dc, Identifier name,
67266730

67276731
// Compute the underlying type.
67286732
auto underlyingType = Impl.importTypeIgnoreIUO(
6729-
decl->getIntegerType(), ImportTypeKind::Enum, isInSystemModule(dc),
6730-
Bridgeability::None);
6733+
decl->getIntegerType(), ImportTypeKind::Enum,
6734+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
6735+
isInSystemModule(dc), Bridgeability::None, ImportTypeAttrs());
67316736
if (!underlyingType)
67326737
return nullptr;
67336738

@@ -6946,8 +6951,10 @@ SwiftDeclConverter::getImplicitProperty(ImportedName importedName,
69466951
bool isFromSystemModule = isInSystemModule(dc);
69476952
auto importedType = Impl.importType(
69486953
propertyType, ImportTypeKind::Property,
6954+
ImportDiagnosticAdder(Impl, getter, getter->getLocation()),
69496955
Impl.shouldAllowNSUIntegerAsInt(isFromSystemModule, getter),
6950-
Bridgeability::Full, OTK_ImplicitlyUnwrappedOptional);
6956+
Bridgeability::Full, getImportTypeAttrs(accessor),
6957+
OTK_ImplicitlyUnwrappedOptional);
69516958
if (!importedType)
69526959
return nullptr;
69536960

@@ -8130,9 +8137,12 @@ Optional<GenericParamList *> SwiftDeclConverter::importObjCGenericParams(
81308137
if (clangBound->getInterfaceDecl()) {
81318138
auto unqualifiedClangBound =
81328139
clangBound->stripObjCKindOfTypeAndQuals(Impl.getClangASTContext());
8140+
assert(!objcGenericParam->hasAttrs()
8141+
&& "ObjC generics can have attributes now--we should use 'em");
81338142
Type superclassType = Impl.importTypeIgnoreIUO(
81348143
clang::QualType(unqualifiedClangBound, 0), ImportTypeKind::Abstract,
8135-
false, Bridgeability::None);
8144+
ImportDiagnosticAdder(Impl, decl, decl->getLocation()),
8145+
false, Bridgeability::None, ImportTypeAttrs());
81368146
if (!superclassType) {
81378147
return None;
81388148
}
@@ -8852,6 +8862,15 @@ ClangImporter::Implementation::importSwiftAttrAttributes(Decl *MappedDecl) {
88528862
if (isa<TypeAliasDecl>(MappedDecl))
88538863
return;
88548864

8865+
// `@Sendable` on non-types is treated as an `ImportTypeAttr` and shouldn't
8866+
// be treated as an attribute on the declaration. (Particularly, @Sendable on
8867+
// a function or method should be treated as making the return value Sendable,
8868+
// *not* as making the function/method itself Sendable, because
8869+
// `@Sendable func` is primarily meant for local functions.)
8870+
if (!isa<TypeDecl>(MappedDecl))
8871+
while (auto attr = MappedDecl->getAttrs().getEffectiveSendableAttr())
8872+
MappedDecl->getAttrs().removeAttribute(attr);
8873+
88558874
// Some types have an implicit '@Sendable' attribute.
88568875
if (ClangDecl->hasAttr<clang::SwiftNewTypeAttr>() ||
88578876
ClangDecl->hasAttr<clang::EnumExtensibilityAttr>() ||

lib/ClangImporter/ImportMacro.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
102102
if (const clang::Expr *parsed = parseNumericLiteral<>(Impl, tok)) {
103103
auto clangTy = parsed->getType();
104104
auto literalType = Impl.importTypeIgnoreIUO(
105-
clangTy, ImportTypeKind::Value, isInSystemModule(DC),
106-
Bridgeability::None);
105+
clangTy, ImportTypeKind::Value,
106+
ImportDiagnosticAdder(Impl, MI, tok.getLocation()),
107+
isInSystemModule(DC), Bridgeability::None, ImportTypeAttrs());
107108
if (!literalType)
108109
return nullptr;
109110

@@ -112,8 +113,9 @@ static ValueDecl *importNumericLiteral(ClangImporter::Implementation &Impl,
112113
constantType = literalType;
113114
} else {
114115
constantType = Impl.importTypeIgnoreIUO(
115-
castType, ImportTypeKind::Value, isInSystemModule(DC),
116-
Bridgeability::None);
116+
castType, ImportTypeKind::Value,
117+
ImportDiagnosticAdder(Impl, MI, MI->getDefinitionLoc()),
118+
isInSystemModule(DC), Bridgeability::None, ImportTypeAttrs());
117119
if (!constantType)
118120
return nullptr;
119121
}
@@ -308,6 +310,7 @@ static Optional<clang::QualType> builtinTypeForToken(const clang::Token &tok,
308310

309311
static Optional<std::pair<llvm::APSInt, Type>>
310312
getIntegerConstantForMacroToken(ClangImporter::Implementation &impl,
313+
const clang::MacroInfo *macro,
311314
DeclContext *DC,
312315
const clang::Token &token) {
313316

@@ -317,8 +320,9 @@ static Optional<std::pair<llvm::APSInt, Type>>
317320
auto value = llvm::APSInt { literal->getValue(),
318321
literal->getType()->isUnsignedIntegerType() };
319322
auto type = impl.importTypeIgnoreIUO(
320-
literal->getType(), ImportTypeKind::Value, isInSystemModule(DC),
321-
Bridgeability::None);
323+
literal->getType(), ImportTypeKind::Value,
324+
ImportDiagnosticAdder(impl, macro, token.getLocation()),
325+
isInSystemModule(DC), Bridgeability::None, ImportTypeAttrs());
322326
return {{ value, type }};
323327
}
324328

@@ -551,7 +555,8 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
551555
// Parse INT1.
552556
llvm::APSInt firstValue;
553557
Type firstSwiftType = nullptr;
554-
if (auto firstInt = getIntegerConstantForMacroToken(impl, DC, tokenI[0])) {
558+
if (auto firstInt = getIntegerConstantForMacroToken(impl, macro, DC,
559+
tokenI[0])) {
555560
firstValue = firstInt->first;
556561
firstSwiftType = firstInt->second;
557562
} else {
@@ -566,7 +571,8 @@ static ValueDecl *importMacro(ClangImporter::Implementation &impl,
566571
// Parse INT2.
567572
llvm::APSInt secondValue;
568573
Type secondSwiftType = nullptr;
569-
if (auto secondInt = getIntegerConstantForMacroToken(impl, DC, tokenI[2])) {
574+
if (auto secondInt = getIntegerConstantForMacroToken(impl, macro, DC,
575+
tokenI[2])) {
570576
secondValue = secondInt->first;
571577
secondSwiftType = secondInt->second;
572578
} else {

0 commit comments

Comments
 (0)