Skip to content

Commit d7bba7f

Browse files
authored
[Typechecker] Use getCalledValue() rather than using DeclRefExpr to get the function decl when checking ignored expressions (#30077)
1 parent 031f5a4 commit d7bba7f

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

lib/Sema/TypeCheckStmt.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,8 +1466,8 @@ void TypeChecker::checkIgnoredExpr(Expr *E) {
14661466
isa<DotSyntaxCallExpr>(E) ? cast<DotSyntaxCallExpr>(E)->getFn() : E;
14671467

14681468
if (auto *Fn = dyn_cast<ApplyExpr>(expr)) {
1469-
if (auto *declRef = dyn_cast<DeclRefExpr>(Fn->getFn())) {
1470-
if (auto *FD = dyn_cast<AbstractFunctionDecl>(declRef->getDecl())) {
1469+
if (auto *calledValue = Fn->getCalledValue()) {
1470+
if (auto *FD = dyn_cast<AbstractFunctionDecl>(calledValue)) {
14711471
if (FD->getAttrs().hasAttribute<DiscardableResultAttr>()) {
14721472
isDiscardable = true;
14731473
}

test/attr/attr_discardableResult.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,23 @@ class Discard {
222222
bar // expected-error {{expression resolves to an unused function}}
223223
}
224224
}
225+
226+
// SR-12271
227+
228+
struct SR_12271_S {
229+
@discardableResult
230+
func bar1() -> () -> Void {
231+
return {}
232+
}
233+
234+
@discardableResult
235+
static func bar2() -> () -> Void {
236+
return {}
237+
}
238+
}
239+
240+
SR_12271_S().bar1() // Okay
241+
SR_12271_S.bar2() // Okay
242+
243+
SR_12271_S().bar1 // expected-error {{expression resolves to an unused function}}
244+
SR_12271_S.bar2 // expected-error {{expression resolves to an unused function}}

0 commit comments

Comments
 (0)