diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 3640bf00b77a4..d2276479bc793 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -10065,7 +10065,11 @@ QualType Sema::getDecltypeForExpr(Expr *E) { // access to a corresponding data member of the closure type that // would have been declared if x were an odr-use of the denoted // entity. - if (getCurLambda() && isa(IDExpr)) { + + // decltype result for blocks should be the same as decltype result in + // lambdas because blocks capture variables the same as lambdas do + // https://github.com/llvm/llvm-project/issues/207355#issuecomment-4877181419 + if (isa(IDExpr)) { if (auto *DRE = dyn_cast(IDExpr->IgnoreParens())) { if (auto *Var = dyn_cast(DRE->getDecl())) { QualType T = getCapturedDeclRefType(Var, DRE->getLocation()); diff --git a/clang/test/SemaObjCXX/decltype-block-capture.mm b/clang/test/SemaObjCXX/decltype-block-capture.mm new file mode 100644 index 0000000000000..ee64f2669e27e --- /dev/null +++ b/clang/test/SemaObjCXX/decltype-block-capture.mm @@ -0,0 +1,18 @@ +// RUN: %clang_cc1 -fsyntax-only -std=c++17 -fblocks %s +// expected-no-diagnostics +template +inline constexpr bool is_same_as = false; + +template +inline constexpr bool is_same_as = true; + +struct X {}; + +void f() { + X x; + + void (^b)(void) = ^{ + decltype(auto) y = (x); + static_assert(is_same_as); + }; +} \ No newline at end of file