Skip to content

Attributes on templates are mishandled #37650

@Quuxplusone

Description

@Quuxplusone
Bugzilla Link 38302
Version trunk
OS All
CC @DougGregor

Extended Description

// https://godbolt.org/g/pqog4L
template<class T>
[[noreturn]] void one();

template<>
void one<int>() {}

template<class T>
void two();

template<>
[[noreturn]] void two<int>() {}

As far as I can tell, this program produces bogus diagnostics in both directions.
First, the explicit specialization one<int> seems to be inheriting [[noreturn]] from the primary template; I believe this is unwanted behavior.

    test.cc:2:18: warning: function declared 'noreturn' should not return [-Winvalid-noreturn]
    void one<int>() {}
                     ^

Second, Clang complains that the explicit specialization two<int> is marked [[noreturn]] when its "first declaration" is not so marked. Clang seems to be considering the primary template as the "first" declaration of every specialization (even though the accompanying note nonsensically points to the specialization's own declaration). This is problematic, because maybe the programmer wants to apply [[noreturn]] NOT to ALL possible instantiations of the primary template, but only to this one explicit specialization.

    test.cc:9:3: error: function declared '[[noreturn]]' after its first declaration
    [[noreturn]] void two<int>() {}
      ^
    test.cc:9:19: note: declaration missing '[[noreturn]]' attribute is here
    [[noreturn]] void two<int>() {}
                      ^

After some discussion on Slack, I think what ought to happen here is that the attribute on the primary template ought to be applied only to instantiations that come directly from that primary (unspecialized) template; and the attribute on the explicit specialization ought to be applied only to that explicit specialization. But we see that Clang doesn't do either of those things.

This is highly relevant to my interests because of P1144 [[trivially_relocatable]], and I'd love to put together a patch to fix it, but at least at the moment I'm completely lost as to where I would even start looking.

See also: #21132

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillac++clang:frontendLanguage frontend issues, e.g. anything involving "Sema"

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions