Skip to content

[clang] Fix missing diagnostic of declaration use when accessing TypeDecls through typename access #129681

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 1 commit into from
Mar 4, 2025
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
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Potentially Breaking Changes
============================

- The Objective-C ARC migrator (ARCMigrate) has been removed.
- Fix missing diagnostics for uses of declarations when performing typename access,
such as when performing member access on a '[[deprecated]]' type alias.
(#GH58547)

C/C++ Language Potentially Breaking Changes
-------------------------------------------
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Sema/Sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -3168,6 +3168,13 @@ class Sema final : public SemaBase {

DeclGroupPtrTy ConvertDeclToDeclGroup(Decl *Ptr, Decl *OwnedType = nullptr);

enum class DiagCtorKind { None, Implicit, Typename };
/// Returns the TypeDeclType for the given type declaration,
/// as ASTContext::getTypeDeclType would, but
/// performs the required semantic checks for name lookup of said entity.
QualType getTypeDeclType(DeclContext *LookupCtx, DiagCtorKind DCK,
TypeDecl *TD, SourceLocation NameLoc);

/// If the identifier refers to a type name within this scope,
/// return the declaration of that type.
///
Expand Down
47 changes: 29 additions & 18 deletions clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,26 @@ class TypeNameValidatorCCC final : public CorrectionCandidateCallback {

} // end anonymous namespace

QualType Sema::getTypeDeclType(DeclContext *LookupCtx, DiagCtorKind DCK,
TypeDecl *TD, SourceLocation NameLoc) {
auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx);
auto *FoundRD = dyn_cast<CXXRecordDecl>(TD);
if (DCK != DiagCtorKind::None && LookupRD && FoundRD &&
FoundRD->isInjectedClassName() &&
declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent()))) {
Diag(NameLoc,
DCK == DiagCtorKind::Typename
? diag::ext_out_of_line_qualified_id_type_names_constructor
: diag::err_out_of_line_qualified_id_type_names_constructor)
<< TD->getIdentifier() << /*Type=*/1
<< 0 /*if any keyword was present, it was 'typename'*/;
}

DiagnoseUseOfDecl(TD, NameLoc);
MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
return Context.getTypeDeclType(TD);
}

namespace {
enum class UnqualifiedTypeNameLookupResult {
NotFound,
Expand Down Expand Up @@ -293,10 +313,11 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
bool IsClassTemplateDeductionContext,
ImplicitTypenameContext AllowImplicitTypename,
IdentifierInfo **CorrectedII) {
bool IsImplicitTypename = !isClassName && !IsCtorOrDtorName;
// FIXME: Consider allowing this outside C++1z mode as an extension.
bool AllowDeducedTemplate = IsClassTemplateDeductionContext &&
getLangOpts().CPlusPlus17 && !IsCtorOrDtorName &&
!isClassName && !HasTrailingDot;
getLangOpts().CPlusPlus17 && IsImplicitTypename &&
!HasTrailingDot;

// Determine where we will perform name lookup.
DeclContext *LookupCtx = nullptr;
Expand All @@ -320,11 +341,9 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
// refer to a member of an unknown specialization.
// In C++2a, in several contexts a 'typename' is not required. Also
// allow this as an extension.
if (AllowImplicitTypename == ImplicitTypenameContext::No &&
!isClassName && !IsCtorOrDtorName)
return nullptr;
bool IsImplicitTypename = !isClassName && !IsCtorOrDtorName;
if (IsImplicitTypename) {
if (AllowImplicitTypename == ImplicitTypenameContext::No)
return nullptr;
SourceLocation QualifiedLoc = SS->getRange().getBegin();
if (getLangOpts().CPlusPlus20)
Diag(QualifiedLoc, diag::warn_cxx17_compat_implicit_typename);
Expand Down Expand Up @@ -513,18 +532,10 @@ ParsedType Sema::getTypeName(const IdentifierInfo &II, SourceLocation NameLoc,
// C++ [class.qual]p2: A lookup that would find the injected-class-name
// instead names the constructors of the class, except when naming a class.
// This is ill-formed when we're not actually forming a ctor or dtor name.
auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(LookupCtx);
auto *FoundRD = dyn_cast<CXXRecordDecl>(TD);
if (!isClassName && !IsCtorOrDtorName && LookupRD && FoundRD &&
FoundRD->isInjectedClassName() &&
declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent())))
Diag(NameLoc, diag::err_out_of_line_qualified_id_type_names_constructor)
<< &II << /*Type*/1;

DiagnoseUseOfDecl(IIDecl, NameLoc);

T = Context.getTypeDeclType(TD);
MarkAnyDeclReferenced(TD->getLocation(), TD, /*OdrUse=*/false);
T = getTypeDeclType(LookupCtx,
IsImplicitTypename ? DiagCtorKind::Implicit
: DiagCtorKind::None,
TD, NameLoc);
} else if (ObjCInterfaceDecl *IDecl = dyn_cast<ObjCInterfaceDecl>(IIDecl)) {
(void)DiagnoseUseOfDecl(IDecl, NameLoc);
if (!HasTrailingDot)
Expand Down
19 changes: 7 additions & 12 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10914,20 +10914,15 @@ Sema::CheckTypenameType(ElaboratedTypeKeyword Keyword,
//
// FIXME: That's not strictly true: mem-initializer-id lookup does not
// ignore functions, but that appears to be an oversight.
auto *LookupRD = dyn_cast_or_null<CXXRecordDecl>(Ctx);
auto *FoundRD = dyn_cast<CXXRecordDecl>(Type);
if (Keyword == ElaboratedTypeKeyword::Typename && LookupRD && FoundRD &&
FoundRD->isInjectedClassName() &&
declaresSameEntity(LookupRD, cast<Decl>(FoundRD->getParent())))
Diag(IILoc, diag::ext_out_of_line_qualified_id_type_names_constructor)
<< &II << 1 << 0 /*'typename' keyword used*/;

QualType T = getTypeDeclType(Ctx,
Keyword == ElaboratedTypeKeyword::Typename
? DiagCtorKind::Typename
: DiagCtorKind::None,
Type, IILoc);
// We found a type. Build an ElaboratedType, since the
// typename-specifier was just sugar.
MarkAnyDeclReferenced(Type->getLocation(), Type, /*OdrUse=*/false);
return Context.getElaboratedType(Keyword,
QualifierLoc.getNestedNameSpecifier(),
Context.getTypeDeclType(Type));
return Context.getElaboratedType(
Keyword, QualifierLoc.getNestedNameSpecifier(), T);
}

// C++ [dcl.type.simple]p2:
Expand Down
14 changes: 14 additions & 0 deletions clang/test/CXX/dcl.dcl/dcl.attr/dcl.attr.deprecated/p1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,17 @@ template <typename T>
FunS2 f;// No warning, entire function is deprecated, so usage here should be fine.

}

namespace GH58547 {
struct A {
using ta [[deprecated]] = int; // expected-note 2{{marked deprecated here}}
};

using t1 = typename A::ta; // expected-warning {{'ta' is deprecated}}

template <class B1> struct B {
using tb = typename B1::ta; // expected-warning {{'ta' is deprecated}}
};

template struct B<A>; // expected-note {{requested here}}
} // namespace GH58547