Skip to content

[clang] Make nullability-on-classes more robust to redeclarations #114778

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 2 commits into from
Nov 4, 2024
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
18 changes: 13 additions & 5 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/FoldingSet.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
Expand Down Expand Up @@ -4774,7 +4775,10 @@ bool Type::canHaveNullability(bool ResultIfUnknown) const {
->getTemplateName()
.getAsTemplateDecl())
if (auto *CTD = dyn_cast<ClassTemplateDecl>(templateDecl))
return CTD->getTemplatedDecl()->hasAttr<TypeNullableAttr>();
return llvm::any_of(
CTD->redecls(), [](const RedeclarableTemplateDecl *RTD) {
return RTD->getTemplatedDecl()->hasAttr<TypeNullableAttr>();
});
return ResultIfUnknown;

case Type::Builtin:
Expand Down Expand Up @@ -4841,10 +4845,14 @@ bool Type::canHaveNullability(bool ResultIfUnknown) const {
// For template specializations, look only at primary template attributes.
// This is a consistent regardless of whether the instantiation is known.
if (const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(RD))
return CTSD->getSpecializedTemplate()
->getTemplatedDecl()
->hasAttr<TypeNullableAttr>();
return RD->hasAttr<TypeNullableAttr>();
return llvm::any_of(
CTSD->getSpecializedTemplate()->redecls(),
[](const RedeclarableTemplateDecl *RTD) {
return RTD->getTemplatedDecl()->hasAttr<TypeNullableAttr>();
});
return llvm::any_of(RD->redecls(), [](const TagDecl *RD) {
return RD->hasAttr<TypeNullableAttr>();
});
}

// Non-pointer types.
Expand Down
27 changes: 27 additions & 0 deletions clang/test/SemaCXX/nullability_redecl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// RUN: %clang_cc1 -std=c++11 -fsyntax-only -Wno-nullability-declspec %s -verify -Wnullable-to-nonnull-conversion -I%S/Inputs

class Foo;
using Foo1 = Foo _Nonnull; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'Foo'}}
class _Nullable Foo;
using Foo2 = Foo _Nonnull;
class Foo;
using Foo3 = Foo _Nonnull;

template <class T>
class Bar;
using Bar1 = Bar<int> _Nonnull; // expected-error{{nullability specifier '_Nonnull' cannot be applied to non-pointer type 'Bar<int>'}}
template <class T>
class _Nullable Bar;
using Bar2 = Bar<int> _Nonnull;
template <class T>
class Bar;
using Bar3 = Bar<int> _Nonnull;

namespace std {
template<class T> class unique_ptr;
using UP1 = unique_ptr<int> _Nonnull;
class X { template<class T> friend class unique_ptr; };
using UP2 = unique_ptr<int> _Nonnull;
template<class T> class unique_ptr;
using UP3 = unique_ptr<int> _Nonnull;
}
Loading