Skip to content

Commit 5e500b9

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents dcb905d + b311c63 commit 5e500b9

32 files changed

+534
-290
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4993,6 +4993,8 @@ NOTE(iuo_to_any_coercion_note_func_result,none,
49934993
(const ValueDecl *))
49944994
NOTE(default_optional_to_any,none,
49954995
"provide a default value to avoid this warning", ())
4996+
NOTE(default_optional_parameter,none,
4997+
"use a default value parameter to avoid this warning", ())
49964998
NOTE(force_optional_to_any,none,
49974999
"force-unwrap the value to avoid this warning", ())
49985000
NOTE(silence_optional_to_any,none,

lib/ClangImporter/ClangImporter.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8812,8 +8812,7 @@ swift::importer::getCxxRefConventionWithAttrs(const clang::Decl *decl) {
88128812
if (const clang::RecordDecl *record =
88138813
ptrType->getPointeeType()->getAsRecordDecl()) {
88148814
return matchSwiftAttrConsideringInheritance<RC>(
8815-
record, {{"returned_as_unretained_by_default", RC::Unowned},
8816-
{"returned_as_retained_by_default", RC::Owned}});
8815+
record, {{"returned_as_unretained_by_default", RC::Unowned}});
88178816
}
88188817
}
88198818

lib/ClangImporter/ImportDecl.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3657,8 +3657,7 @@ namespace {
36573657
}
36583658

36593659
if (importer::matchSwiftAttrOnRecordPtr<bool>(
3660-
retType, {{"returned_as_retained_by_default", true},
3661-
{"returned_as_unretained_by_default", true}})) {
3660+
retType, {{"returned_as_unretained_by_default", true}})) {
36623661
unannotatedAPIWarningNeeded = false;
36633662
}
36643663

lib/ClangImporter/SwiftBridging/swift/bridging

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -191,23 +191,6 @@
191191
#define SWIFT_RETURNS_UNRETAINED \
192192
__attribute__((swift_attr("returns_unretained")))
193193

194-
/// Applied to a C++ foreign reference type annotated with
195-
/// SWIFT_SHARED_REFERENCE. Indicates that C++ APIs returning this type are
196-
/// assumed to return an owned (+1) value by default, unless explicitly annotated with
197-
/// SWIFT_RETURNS_UNRETAINED.
198-
///
199-
/// For example:
200-
/// ```c++
201-
/// struct SWIFT_SHARED_REFERENCE(retainFoo, releaseFoo)
202-
/// SWIFT_RETURNED_AS_RETAINED_BY_DEFAULT
203-
/// Foo { ... };
204-
/// ```
205-
///
206-
/// In Swift, C++ APIs returning `Foo*` will be assumed to return an owned
207-
/// value.
208-
#define SWIFT_RETURNED_AS_RETAINED_BY_DEFAULT \
209-
__attribute__((swift_attr("returned_as_retained_by_default")))
210-
211194
/// Applied to a C++ foreign reference type annotated with
212195
/// SWIFT_SHARED_REFERENCE. Indicates that C++ APIs returning this type are
213196
/// assumed to return an unowned (+0) value by default, unless explicitly annotated

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,8 +1520,14 @@ SILInstruction *SILCombiner::legacyVisitApplyInst(ApplyInst *AI) {
15201520
return nullptr;
15211521

15221522
SILValue callee = AI->getCallee();
1523-
if (auto *cee = dyn_cast<ConvertEscapeToNoEscapeInst>(callee)) {
1524-
callee = cee->getOperand();
1523+
for (;;) {
1524+
if (auto *cee = dyn_cast<ConvertEscapeToNoEscapeInst>(callee)) {
1525+
callee = cee->getOperand();
1526+
} else if (auto *mdi = dyn_cast<MarkDependenceInst>(callee)) {
1527+
callee = mdi->getValue();
1528+
} else {
1529+
break;
1530+
}
15251531
}
15261532
if (auto *CFI = dyn_cast<ConvertFunctionInst>(callee))
15271533
return optimizeApplyOfConvertFunctionInst(AI, CFI);

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,9 @@ static bool useHasTransitiveOwnership(const SILInstruction *inst) {
963963
if (isa<ConvertEscapeToNoEscapeInst>(inst))
964964
return true;
965965

966+
if (isa<MarkDependenceInst>(inst))
967+
return true;
968+
966969
// Look through copy_value, begin_borrow, move_value. They are inert for our
967970
// purposes, but we need to look through it.
968971
return isa<CopyValueInst>(inst) || isa<BeginBorrowInst>(inst) ||
@@ -1095,13 +1098,16 @@ void swift::getConsumedPartialApplyArgs(PartialApplyInst *pai,
10951098
}
10961099
}
10971100

1098-
bool swift::collectDestroys(SingleValueInstruction *inst,
1099-
SmallVectorImpl<Operand *> &destroys) {
1101+
static bool collectDestroysRecursively(SingleValueInstruction *inst,
1102+
SmallVectorImpl<Operand *> &destroys,
1103+
InstructionSet &visited) {
11001104
bool isDead = true;
11011105
for (Operand *use : inst->getUses()) {
11021106
SILInstruction *user = use->getUser();
1107+
if (!visited.insert(user))
1108+
continue;
11031109
if (useHasTransitiveOwnership(user)) {
1104-
if (!collectDestroys(cast<SingleValueInstruction>(user), destroys))
1110+
if (!collectDestroysRecursively(cast<SingleValueInstruction>(user), destroys, visited))
11051111
isDead = false;
11061112
destroys.push_back(use);
11071113
} else if (useDoesNotKeepValueAlive(user)) {
@@ -1113,6 +1119,12 @@ bool swift::collectDestroys(SingleValueInstruction *inst,
11131119
return isDead;
11141120
}
11151121

1122+
bool swift::collectDestroys(SingleValueInstruction *inst,
1123+
SmallVectorImpl<Operand *> &destroys) {
1124+
InstructionSet visited(inst->getFunction());
1125+
return collectDestroysRecursively(inst, destroys, visited);
1126+
}
1127+
11161128
/// Move the original arguments of the partial_apply into newly created
11171129
/// temporaries to extend the lifetime of the arguments until the partial_apply
11181130
/// is finally destroyed.

lib/Sema/CSFix.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,30 @@ getConcurrencyFixBehavior(ConstraintSystem &cs, ConstraintKind constraintKind,
282282
// We can only handle the downgrade for conversions.
283283
switch (constraintKind) {
284284
case ConstraintKind::Conversion:
285-
case ConstraintKind::ArgumentConversion:
286285
case ConstraintKind::Subtype:
287286
break;
288287

288+
case ConstraintKind::ArgumentConversion: {
289+
if (!forSendable)
290+
break;
291+
292+
// Passing a static member reference as an argument needs to be downgraded
293+
// to a warning until future major mode to maintain source compatibility for
294+
// code with non-Sendable metatypes.
295+
if (!cs.getASTContext().LangOpts.isSwiftVersionAtLeast(7)) {
296+
auto *argLoc = cs.getConstraintLocator(locator);
297+
if (auto *argument = getAsExpr(simplifyLocatorToAnchor(argLoc))) {
298+
if (auto overload = cs.findSelectedOverloadFor(
299+
argument->getSemanticsProvidingExpr())) {
300+
auto *decl = overload->choice.getDecl();
301+
if (decl && decl->isStatic())
302+
return FixBehavior::DowngradeToWarning;
303+
}
304+
}
305+
}
306+
break;
307+
}
308+
289309
default:
290310
if (!cs.shouldAttemptFixes())
291311
return std::nullopt;

lib/Sema/MiscDiagnostics.cpp

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5600,7 +5600,7 @@ static void diagnoseUnintendedOptionalBehavior(const Expr *E,
56005600
segment->getCalledValue(/*skipFunctionConversions=*/true), kind))
56015601
if (auto firstArg =
56025602
getFirstArgIfUnintendedInterpolation(segment->getArgs(), kind))
5603-
diagnoseUnintendedInterpolation(firstArg, kind);
5603+
diagnoseUnintendedInterpolation(segment, firstArg, kind);
56045604
}
56055605

56065606
bool interpolationWouldBeUnintended(ConcreteDeclRef appendMethod,
@@ -5666,13 +5666,40 @@ static void diagnoseUnintendedOptionalBehavior(const Expr *E,
56665666
return firstArg;
56675667
}
56685668

5669-
void diagnoseUnintendedInterpolation(Expr * arg, UnintendedInterpolationKind kind) {
5669+
std::string baseInterpolationTypeName(CallExpr *segment) {
5670+
if (auto selfApplyExpr = dyn_cast<SelfApplyExpr>(segment->getFn())) {
5671+
auto baseType = selfApplyExpr->getBase()->getType();
5672+
return baseType->getWithoutSpecifierType()->getString();
5673+
}
5674+
return "unknown";
5675+
}
5676+
5677+
void diagnoseUnintendedInterpolation(CallExpr *segment,
5678+
Expr * arg,
5679+
UnintendedInterpolationKind kind) {
56705680
Ctx.Diags
56715681
.diagnose(arg->getStartLoc(),
56725682
diag::debug_description_in_string_interpolation_segment,
56735683
(bool)kind)
56745684
.highlight(arg->getSourceRange());
56755685

5686+
if (kind == UnintendedInterpolationKind::Optional) {
5687+
auto wrappedArgType = arg->getType()->getRValueType()->getOptionalObjectType();
5688+
auto baseTypeName = baseInterpolationTypeName(segment);
5689+
5690+
// Suggest using a default value parameter, but only for non-string values
5691+
// when the base interpolation type is the default.
5692+
if (!wrappedArgType->isString() && baseTypeName == "DefaultStringInterpolation")
5693+
Ctx.Diags.diagnose(arg->getLoc(), diag::default_optional_parameter)
5694+
.highlight(arg->getSourceRange())
5695+
.fixItInsertAfter(arg->getEndLoc(), ", default: <#default value#>");
5696+
5697+
// Suggest providing a default value using the nil-coalescing operator.
5698+
Ctx.Diags.diagnose(arg->getLoc(), diag::default_optional_to_any)
5699+
.highlight(arg->getSourceRange())
5700+
.fixItInsertAfter(arg->getEndLoc(), " ?? <#default value#>");
5701+
}
5702+
56765703
// Suggest 'String(describing: <expr>)'.
56775704
auto argStart = arg->getStartLoc();
56785705
Ctx.Diags
@@ -5682,13 +5709,6 @@ static void diagnoseUnintendedOptionalBehavior(const Expr *E,
56825709
.highlight(arg->getSourceRange())
56835710
.fixItInsert(argStart, "String(describing: ")
56845711
.fixItInsertAfter(arg->getEndLoc(), ")");
5685-
5686-
if (kind == UnintendedInterpolationKind::Optional) {
5687-
// Suggest inserting a default value.
5688-
Ctx.Diags.diagnose(arg->getLoc(), diag::default_optional_to_any)
5689-
.highlight(arg->getSourceRange())
5690-
.fixItInsertAfter(arg->getEndLoc(), " ?? <#default value#>");
5691-
}
56925712
}
56935713

56945714
PreWalkResult<Expr *> walkToExprPre(Expr *E) override {

lib/Sema/TypeCheckMacros.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ MacroDefinition MacroDefinitionRequest::evaluate(
136136
SM.getEntireTextForBuffer(sourceFile->getBufferID());
137137
StringRef macroDeclText =
138138
SM.extractText(Lexer::getCharSourceRangeFromSourceRange(
139-
SM, macro->getSourceRangeIncludingAttrs()));
139+
SM, macro->getSourceRange()));
140140

141141
auto checkResult = swift_Macros_checkMacroDefinition(
142142
&ctx.Diags, sourceFileText, macroDeclText, &externalMacroName,

0 commit comments

Comments
 (0)