Revert "Internal change" since GitHub hasn't picked up this change yet.
We need GitHub and GOB to be on the same change for Copybara to mirror correctly.
This reverts commit b5e2aa4211e65f2a8bcea1243dece6e97441f031.
diff --git a/nullability/pointer_nullability_analysis.cc b/nullability/pointer_nullability_analysis.cc
index 6895221..e0dbb6c 100644
--- a/nullability/pointer_nullability_analysis.cc
+++ b/nullability/pointer_nullability_analysis.cc
@@ -198,37 +198,29 @@
T,
[&](const SubstTemplateTypeParmType *ST)
-> std::optional<TypeNullability> {
- auto *DRE = dyn_cast<DeclRefExpr>(CE->getCallee()->IgnoreImpCasts());
- if (DRE == nullptr) return std::nullopt;
-
// TODO: Handle calls that use template argument deduction.
-
- // Does this refer to a parameter of the function template?
- // If not (e.g. nested templates, template specialization types in the
- // return value), we handle the desugaring elsewhere.
- auto *ReferencedFunction = dyn_cast<FunctionDecl>(DRE->getDecl());
- if (!ReferencedFunction) return std::nullopt;
- if (ReferencedFunction->getPrimaryTemplate() != ST->getAssociatedDecl())
- return std::nullopt;
-
- // Some or all of the template arguments may be deduced, and we won't
- // see those on the `DeclRefExpr`. If the template argument was deduced,
- // we don't have any sugar for it.
- // TODO(b/268348533): Can we somehow obtain it from the function param
- // it was deduced from?
- // TODO(b/268345783): This check, as well as the index into
- // `template_arguments` below, may be incorrect in the presence of
- // parameters packs. In function templates, parameter packs may appear
- // anywhere in the parameter list. The index may therefore refer to one
- // of the pack arguments, but we might incorrectly interpret it as
- // referring to an argument that follows the pack.
- if (ST->getIndex() >= DRE->template_arguments().size())
- return std::nullopt;
-
- TypeSourceInfo *TSI =
- DRE->template_arguments()[ST->getIndex()].getTypeSourceInfo();
- if (TSI == nullptr) return std::nullopt;
- return getNullabilityAnnotationsFromType(TSI->getType());
+ // TODO: Handle nested templates (...->getDepth() > 0).
+ if (auto *DRE =
+ dyn_cast<DeclRefExpr>(CE->getCallee()->IgnoreImpCasts());
+ DRE != nullptr && ST->getReplacedParameter()->getDepth() == 0 &&
+ // Some or all of the template arguments may be deduced, and we
+ // won't see those on the `DeclRefExpr`. If the template argument
+ // was deduced, we don't have any sugar for it.
+ // TODO(b/268348533): Can we somehow obtain it from the function
+ // param it was deduced from?
+ // TODO(b/268345783): This check, as well as the index into
+ // `template_arguments` below, may be incorrect in the presence of
+ // parameters packs. In function templates, parameter packs may
+ // appear anywhere in the parameter list. The index may therefore
+ // refer to one of the pack arguments, but we might incorrectly
+ // interpret it as referring to an argument that follows the pack.
+ ST->getIndex() < DRE->template_arguments().size()) {
+ TypeSourceInfo *TSI =
+ DRE->template_arguments()[ST->getIndex()].getTypeSourceInfo();
+ if (TSI == nullptr) return std::nullopt;
+ return getNullabilityAnnotationsFromType(TSI->getType());
+ }
+ return std::nullopt;
});
}
diff --git a/nullability/test/templates.cc b/nullability/test/templates.cc
index f994eb3..53bc052 100644
--- a/nullability/test/templates.cc
+++ b/nullability/test/templates.cc
@@ -1223,17 +1223,5 @@
)cc"));
}
-TEST(PointerNullabilityTest, CallFunctionReturningTemplateSpecializationType) {
- EXPECT_TRUE(checkDiagnostics(R"cc(
- template <class T>
- struct AliasWrapper {
- using Type = T;
- };
- template <typename>
- AliasWrapper<int* _Nonnull>::Type f();
- void target() { __assert_nullability<NK_nonnull>(f<int>()); }
- )cc"));
-}
-
} // namespace
} // namespace clang::tidy::nullability