Skip to content

Commit 558390e

Browse files
authored
Merge pull request #64115 from DougGregor/macro-discardable-result
2 parents 31d73b8 + 1c37ee5 commit 558390e

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,16 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
18281828
}
18291829
}
18301830

1831+
// Check for macro expressions whose macros are marked as
1832+
// @discardableResult.
1833+
if (auto expansion = dyn_cast<MacroExpansionExpr>(valueE)) {
1834+
if (auto macro = expansion->getMacroRef().getDecl()) {
1835+
if (macro->getAttrs().hasAttribute<DiscardableResultAttr>()) {
1836+
return;
1837+
}
1838+
}
1839+
}
1840+
18311841
// Complain about functions that aren't called.
18321842
// TODO: What about tuples which contain functions by-value that are
18331843
// dead?

test/Macros/macro_expand.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,16 @@ testStringify(a: 1, b: 1)
9292

9393
func maybeThrowing() throws -> Int { 5 }
9494

95+
#if TEST_DIAGNOSTICS
96+
@freestanding(expression) @discardableResult
97+
macro discardableStringify<T>(_ value: T) -> (T, String) = #externalMacro(module: "MacroDefinition", type: "StringifyMacro")
98+
99+
func testDiscardableStringify(x: Int) {
100+
#stringify(x + 1) // expected-warning{{expression of type '(Int, String)' is unused}}
101+
#discardableStringify(x + 1)
102+
}
103+
#endif
104+
95105
func testStringifyWithThrows() throws {
96106
// Okay, we can put the try inside or outside
97107
_ = try #stringify(maybeThrowing())

utils/gyb_syntax_support/AttributeKinds.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def __init__(self, name, swift_name=None):
378378
ABIStableToAdd, ABIStableToRemove, APIStableToAdd, APIStableToRemove, # noqa: E501
379379
code=64),
380380
SimpleDeclAttribute('discardableResult', 'DiscardableResult',
381-
OnFunc, OnAccessor, OnConstructor,
381+
OnFunc, OnAccessor, OnConstructor, OnMacro,
382382
LongAttribute,
383383
ABIStableToAdd, ABIStableToRemove, APIStableToAdd, APIStableToRemove, # noqa: E501
384384
code=65),

0 commit comments

Comments
 (0)