Skip to content

[Diagnostics] Suppress unused expression warning if result is discard… #65445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/swift/AST/Types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,10 @@ class TupleType final : public TypeBase, public llvm::FoldingSetNode,

bool containsPackExpansionType() const;

/// Check whether this tuple consists of a single unlabeled element
/// of \c PackExpansionType.
bool isSingleUnlabeledPackExpansion() const;

private:
TupleType(ArrayRef<TupleTypeElt> elements, const ASTContext *CanCtx,
RecursiveTypeProperties properties)
Expand Down
8 changes: 8 additions & 0 deletions lib/AST/ParameterPack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ bool CanTupleType::containsPackExpansionTypeImpl(CanTupleType tuple) {
return false;
}

bool TupleType::isSingleUnlabeledPackExpansion() const {
if (getNumElements() != 1)
return false;

const auto &elt = getElement(0);
return !elt.hasName() && elt.getType()->is<PackExpansionType>();
}

bool AnyFunctionType::containsPackExpansionType(ArrayRef<Param> params) {
for (auto param : params) {
if (param.getPlainType()->is<PackExpansionType>())
Expand Down
10 changes: 10 additions & 0 deletions lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1749,6 +1749,16 @@ Stmt *PreCheckReturnStmtRequest::evaluate(Evaluator &evaluator, ReturnStmt *RS,
}

static bool isDiscardableType(Type type) {
// If type is `(_: repeat ...)`, it can be discardable.
if (auto *tuple = type->getAs<TupleType>()) {
if (tuple->isSingleUnlabeledPackExpansion()) {
type = tuple->getElementType(0);
}
}

if (auto *expansion = type->getAs<PackExpansionType>())
return isDiscardableType(expansion->getPatternType());

return (type->hasError() ||
type->isUninhabited() ||
type->lookThroughAllOptionalTypes()->isVoid());
Expand Down
11 changes: 11 additions & 0 deletions test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,14 @@ do {
_ = Defaulted(t: "b", 0) // Ok
_ = Defaulted(t: "c", 1.0, u: "d", 0) // Ok
}

// rdar://108064941 - unused result diagnostic is unaware of Void packs
func test_no_unused_result_warning(arr: inout [Any]) {
func test1<each T>(_ value: (repeat each T)) {
repeat arr.append(each value.element) // no warning
}

func test2<each T>(_ value: repeat each T) {
((repeat arr.append(each value))) // no warning
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to do this with a tuple instead of just writing the underlying pack expansion expression? I'm wondering if we should still diagnose this for tuples

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did it that way because users don't actually know that this is a tuple, syntactically it's just parens which seems reasonable to make consistent with non-variadic cases.

}
}